ArduinoPi 1.0 includes an API

UPDATE: New version 1.5 now supports wireless Bluetooth connections!

I updated the ArduinoPi to version v1.0, meaning I finally created a PHP class to handle everything. The basic concept remains the same and the commands aren’t changed at all, but I updated the PHP side, I plan on updating the Arduino side but for now it only has 105 lines of code, no reason for a library. The GitHub with changes is found here.

ArduinoPi 1.0: API overview

The ArduinoPi will now check for a valid port and supports the Arduino Mega 2560, ADK, UNO and Leonardo. In general if you issue the command PWM on a port that doesn’t support it, you will receive an error. Note that the Arduino code uses Serial1, if you use Software Serial library you need to change it yourself!

I’ve also changed the PHP serial class and added better error handling. The functions will now throw a phpSerialException instead of using trigger_error. I also made it a bit faster and fixed/changed some errors/code I found.

Since the ArduinoPi class extends the Serial class all the configuration options (like baud rate) are available using the ArduinoPi class.

Lastly, it’s now also possible to use the keywords “high” and “low” to indicate 0 or 255.

ArduinoPi 1.0: Using POST or GET

To switch ports the user has 2 options, using a POST or a GET. For example to switch port 2 with PWM high a user can go to the following URL (raspberrypi.local points to your Raspberry Pi IP address).

http://raspberrypi.local/api/pwm/2/high

But the user can also use a POST request, making a POST to the following URL:

http://raspberrypi.local/api/

With the variables mode set to “pwm” and data which is an array containing 2 and “high”. The result will be the same, GET is quite handy for debugging purposes.

ArduinoPi 1.0: Configuration

First we need a bit of configuration. For people using Apache you’re in luck, the .htaccess file will take care of URL rewriting. People using lighttpd (like me) add the following line in your lighttpd.conf:

Then we open the file index.php located in the api folder. Only one line is important:

Change this line to your device, using the keywords: UNO, LEONARDO or ADK. Default the baud rate will be 115200, this can be changed by using the following command. Note that changing the baud rate in PHP means that you also need to change it in your Arduino sketch.

Users more experienced with PHP can open up the arduino_pi.class.php and add their own commands to the API. The process function takes care of all the API handling.

ArduinoPi 1.0: Command Set

Calling commands using the ArduinoPi 1.0 is changed a lot so I will cover the eight default commands. Every command can be called using ether GET or POST. I use jQuery to dynamically call the ArduinoPi API when a user clicks a button.

PWM

The PWM option of the API will check for the right port and checks if the values are between 0-255. The code demonstrates the ArduinoPi 1.0 API request using an AJAX GET request when a user presses a button.

It’s also possible to use a POST request:

The “high” and “low” keywords can also be used in jQuery. Both commands are equal, don’t mix a GET and POST request this will result in unexpected behavior.

Digital

The analog and PWM ports can be used as digital outputs. The ArduinoPi 1.0 only accepts the following values: high, low, 0 or 255. Any other value will throw an exception. The code below will switch port 2 low using an AJAX POST request when clicking on the button.

Analog

The analog function is only used for reading an analog port. To write an analog port use the digital option of the ArduinoPi 1.0 API. The code below will read port A0 using an AJAX GET, the result is displayed next to the button. Notice that it’s allowed to use A0 for selecting an Analog port. An ArduinoPi 1.0 GET example:

Multiple PWM

The multiple PWM allows a user to switch multiple ports at once using PWM. The ports and values can be written consecutively, creating a long API request. Note that any invalid port/value will be omitted from the request, no error will be reported. An ArduinoPi 1.0 POST example.

Note: if a GET doesn’t work check that your URL didn’t excite the 2000 char limit, when in doubt just use the POST version.

Multiple digital

Multiple digital works the same way as multiple PWM. The digital ports only accept a value of 0, 255, “low” or “high”. If a port is switched with another value it will be omitted from the API request. An ArduinoPi 1.0 GET example:

Multiple Analog

Multiple Analog will read the different analog ports specified and returns the value as an array. Again its possible to use A0 and A1. Note that the Arduino makes up a value if nothing is attached to the port itself. An ArduinoPi 1.0 API POST example:

 Analog File

The API command will do exactly the same as Analog, but it writes the result back to a specified JSON file. The first parameter is the analog port, the second, the JSON file in the data folder. Note that this file must be created and readable/writable. The extension .json is not allowed in the API call.

 Delete File

The API command will just open the file and clear everything in it. This is useful for starting a new session of data logging on the Arduino. An ArduinoPi 1.0 POST example:

ArduinoPi 1.0: Error Reporting

All the errors are thrown and catch when using the ArduinoPi API. When using the API functions on their own always catch for Exception and phpSerialException and use your own error handeling to deal with possible exceptions. A little example:

When using the ArduinoPi 1.0 API, all errors are returned in JSON format and can be read using jQuery. When are request is successfully executed the state variable will be “success” if not then it will be “failed”. If we requested an Analog reading the result will be available in the result variable (this can be an array or int). A little example:

The error message will be available in the error variable. It’s now possible to give error and feedback to an user using the ArduinoPi API.

Conclusion

I’ve updated the github to include the new changes, almost everything changed. The examples from last time are also updated and use the new ArduinoPi API. For new features or issues please use github issue tracker to let me know. As always feel free to copy/change the code but leave a link back to my blog :) .

ArduinoPi Web based Controller for Arduino

UPDATE: New version found here: http://www.fritz-hut.com/arduinopi-v1-5-bluetooth-dongle-support/

The ArduinoPi is a web-based controller for the Arduino using the Raspberry Pi. The advantage of using a Raspberry Pi (€28) is that its cheaper than an Ethernet shield (€35) and actually more useful.

I talked about connecting the Raspberry Pi and Arduino over a serial connection and how to install and configure PHP serial class so PHP can talk with the Arduino.

The principal behind ArduinoPi

First a little clarification, the ArduinoPi isn’t really a library or a command set or an out of the box controller, its more a proof of concept using already know programming languages. If you want to use it be ready for some PHP, CSS, HTML, C++ and jQuery!

The principal is as follows: The Arduino is connected as a slave device, meaning it waits for a command, execute it and maybe return a value or something. Everything must be initiated from the web browser. A true slave device.

It’s also possible for the Arduino to execute its own program and the ArduinoPi can then be used as a controller to switch variables. For example I have an automatic light switching system but using the ArduinoPi controller and my web browser I’m able to manually override it.

The command set of the ArduinoPi

The Arduino will check for a valid command. Every command starts with @ and ends with :, variables can be separated with a comma. Of course other commands can be added yourself, I’ve found that these are the basics and cover enough to make some interesting interfaces.

Basic switch-port-high command

This command is sent to the Arduino, for example this will switch port 6 high (255 = HIGH). The first value in this command must be between 0-99, which corresponds to the port number. Note that there is no checking so always see the port is set in OUTPUT mode and you are addressing the right port.

RGB command

I’ve also added a RGB command for LEDs, this is a special command and is constructed as follows:

The 101 indicates the special command mode and the following 3 values are just the RGB values. In my example setup I’ve added 3 RGB LEDs and they all get turned on to the right value using the RGB command.

Read Sensor Command

The last command is the sensor command. It will read an analog sensor value and return it to the PHP script.

The 102 indicate a special command and the following value indicates the port that should be read using analogRead().

My test setup for the ArduinoPi

I’ve used the following components for testing various functions: 3 RGB LEDs + resistors and a light sensor. The example will work with the following configuration. The image does not contain the connection setup with level converter circuit from last time (to cut some space).

The connection schematic for the ArduinoPi test setup

The Arduino code for ArduinoPi

SerialEvent1 gets called every time data is available on the Serial1 interface, the function will check for a valid command, if it found a valid command (meaning it starts with @ and ends with : ) it will switch the Boolean value cmdRec to true, in the loop function we always call handleCmd(); now the cmdRec is set so we process the command. The while loop will split out the values and save them in the data array. Because we also need the last command we repeat the same stuff out of the while loop once more, I should fix that btw.

If the first value has a number lower than 100 we turn the port on with the second value as second parameter, else we process it like a special command. I’ve used a switch so you can add your own special commands as needed.

The web browser interface for the ArduinoPi

As interface I decided to use 4 pages each showcasing the commands. I made use of the following scripts/plugins/blogs/code and I would like to thank the original authors: Bootstrap, jQuery, Farbtastic Color Picker, jQuery color picker using Farbtastic and jQuery UI and flot. The complete source is on Github, I’ll give a short overview of the different pages.

Hover example

The hover example for the ArduinoPi

The Hover example works as follows, we add a jQuery hover handler over all the buttons. If the user moves over the button we request the value of the button, this value corresponds with the port we want to switch. We then send an ajax call to a PHP script that will handle the communication with Arduino and sending the right command. For a value we use 255. When the mouse leaves the button we do the same but use the value 0 to turn it off.

Color picker

The color picker for the ArduinoPi

The color picker works as follows. The user selects the desired color the RGB LEDs should produce and then click on the big square. An AJAX request is then made to a PHP page that will send the special command 101 with the right values for red, green and blue.

 Basic Sensor Display

Sensor display for the ArduinoPi

This has nothing to do with the Arduino, but remember my light measurements? I always had trouble displaying them and I’ve included an example how you can display graphs really easy using this tool. It’s really useful for offline measurements that are saved on a SD card and then need to be represented in a browser.

Live Sensor Data

Live sensor Data for the ArduinoPi

This will execute live measurements. A PHP cron job needs to be setup to run every minute as follows:

The PHP script will execute a measurement and save the value in a JSON file. On the client side we refresh the graph every minute with an Ajax call. I’ve also include two buttons to clear the log or request your own value.

Conclusion

I gave a bare bone, alpha release of my ArduinoPi controller. This example can be expanded all the way and check the Github source, I’ve added a lot of comments to get people started.

There is one big limitation, the server (Raspberry Pi) always has to request data from the Arduino. It’s not possible to send data from the Arduino to the Raspberry Pi and then magically update the browser. When using for example Processing, it is possible to have a more live connection and build an interface in JAVA. But this web interface is accessible for any smartphone or laptop (assuming you don’t run IE6).