BeagleBone POST handler… Get access to digital inputs, outputs, analog inputs and SPI (via shiftOut( ) )

I have been working on interfacing the BeagleBone to a client run on a mobile phone. In doing this, I needed a POST request handler running on the BeagleBone. So after a little reading and learning, I developed this project. It allows a client to query the BeagleBone via HTML POST requests. Access is given to digital and analog inputs, setting digital outputs and send commands to devices attached to the BeagleBone via the Arduino like shiftOut() SPI interface in node.js / Cloud9.

The code has two parts… server code that runs on the BeagleBone in Cloud9 and client code that runs on a remote system in a compatible browser. The client code is just an HTML page that displays all of the analog inputs (updated every 5 seconds), displays BeagleBone GPIO input pins updated every second (currently configured for three inputs), HTML buttons to toggle USR3 LED on/off, and a series of buttons to send complex digital outputs and SPI commands to an off board MSP430 that I am using to control steppers, servos, PWM and misc.digital outputs (that project will be discussed later in a different entry). The code can be found on my GitHub site:  https://github.com/kkeller/BBone-POST-Server

Client HTML page

Client side…

I have written a series of javascript functions that can be used on the client HTML page to build the POST and return values. This makes the use of the POST functionality simple and easy to use. For example, if you want a button to set a GPIO output on pin P8_43, then this would be the HTML code needed in the <body> section of the page:

<BODY>
  <script type="text/javascript">
      init('P8_43','OUTPUT');
  </script>
  <button type="button" onclick="digout('P8_43','HIGH')">BeagleBone P8_43 ON</button>
</BODY>

As you can see, I choose to use the pin names in the functions. This should hopefully be easy to use. See the list at the end of this post. Here is the list of routines that I have made in the client test program:

  • value = analog ( analog# );
  • init ( pin, dir );
  • digout ( pin, value );
  • value = digin ( pin );
  • shiftout ( datapin, clockpin, bitorder, value );

POST handler side…

The POST requests are very simple and can interface into the node.js / cloud9 Bonescript functions.

analogRead() interface:
 - http://<BBone IP>:8888/analog?pin=[0-7]
   example:  http://192.168.1.119:8888/analog?pin=0

pinMode() interface:
 - http://<BBone IP>:8888/init?pin=[see below]&value=[INPUT,OUTPUT]
   example:  http://192.168.1.119:8888/init?pin=P8_43&value=OUTPUT

digitalRead() interface:
 - http://<BBone IP>:8888/input?pin=[see below]
   example:  http://192.168.1.119:8888/input?pin=P8_42

digitalWrite() interface:
 - http://<BBone IP>:8888/output?pin=[see below]&value=[HIGH,LOW,0,1]
   example:  http://192.168.1.119:8888/output?pin=USR3&value=HIGH

shiftOut() interface (SPI using digital outputs):
 - http://<BBone IP>:8888/shift?data=[see below]&clock=[see below]&order=[LSBFIRST,MSBFIRST]&value=[0-0xff]
   example:  http://192.168.1.119:8888/shift?data=P8_43&clock=P8_44&order=LSBFIRST&value=0x20

Source code contents…

Server (run on BeagleBone using cloud9)

  • index.js : main file – run this file to start the POST server
  • server.js : creates the actual server
  • router.js : routes request to handler or 404 page
  • requestHandler.js : file with all of the handlers

client (run on remote system using a web browser)

  • test.html : test page for remote browser
  • analog.html : iframe page to display analogs every 5 seconds
  • input.html : iframe page to display digital inputs every second

Boring video…

Here is a boring video of the LEDs, servo, steppers operating.

BeagleBone operating with POST commands (m4v)

Pin names used in the calls or POSTs…

Possible “pin” names for init, output, input, shiftOut POST requests and init, digin, digout, shiftout and analog client javascript commands.

NOTE: Depending on your Linux configuration, some of these pins maybe used by other drivers or system functions.

"pin"     SIGNAL
======  =========
 P8_3   "GPIO1_6"
 P8_4   "GPIO1_7"
 P8_5   "GPIO1_2"
 P8_6   "GPIO1_3"
 P8_7   "TIMER4"
 P8_8   "TIMER7"
 P8_9   "TIMER5"
 P8_10  "TIMER6"
 P8_11  "GPIO1_13"
 P8_12  "GPIO1_12"
 P8_13  "EHRPWM2B"
 P8_14  "GPIO0_26"
 P8_15  "GPIO1_15"
 P8_16  "GPIO1_14"
 P8_17  "GPIO0_27"
 P8_18  "GPIO2_1"
 P8_19  "EHRPWM2A"
 P8_20  "GPIO1_31"
 P8_21  "GPIO1_30"
 P8_22  "GPIO1_5"
 P8_23  "GPIO1_4"
 P8_24  "GPIO1_1"
 P8_25  "GPIO1_0"
 P8_26  "GPIO1_29"
 P8_27  "GPIO2_22"
 P8_28  "GPIO2_24"
 P8_29  "GPIO2_23"
 P8_30  "GPIO2_25"
 P8_31  "UART5_CTSN"
 P8_32  "UART5_RTSN"
 P8_33  "UART4_RTSN"
 P8_34  "UART3_RTSN"
 P8_35  "UART4_CTSN"
 P8_36  "UART3_CTSN"
 P8_37  "UART5_TXD"
 P8_38  "UART5_RXD"
 P8_39  "GPIO2_12"
 P8_40  "GPIO2_13"
 P8_41  "GPIO2_10"
 P8_42  "GPIO2_11"
 P8_43  "GPIO2_8"
 P8_44  "GPIO2_9"
 P8_45  "GPIO2_6"
 P8_46  "GPIO2_7"
 P9_11  "UART4_RXD"
 P9_12  "GPIO1_28"
 P9_13  "UART4_TXD"
 P9_14  "EHRPWM1A"
 P9_15  "GPIO1_16"
 P9_16  "EHRPWM1B"
 P9_17  "I2C1_SCL"
 P9_18  "I2C1_SDA"
 P9_19  "I2C2_SCL"
 P9_20  "I2C2_SDA"
 P9_21  "UART2_TXD"
 P9_22  "UART2_RXD"
 P9_23  "GPIO1_17"
 P9_24  "UART1_TXD"
 P9_25  "GPIO3_21"
 P9_26  "UART1_RXD"
 P9_27  "GPIO3_19"
 P9_28  "SPI1_CS0"
 P9_29  "SPI1_D0"
 P9_30  "SPI1_D1"
 P9_31  "SPI1_SCLK"
 P9_41  "CLKOUT2"
 P9_42  "GPIO0_7"
 USR0   "USR0"    
 USR1   "USR1"     
 USR2   "USR2"       
 USR3   "USR3"