ArduinoPi v1.6 and Release of ArduinoPi Python

Today a new release of ArduinoPi and the first release of the Python version. Lets start with the ArduinoPi v1.6.

ArduinoPi v1.6

Not much has changed, no new features, but if anyone has a request feel free to ask! To start, it’s now only possible to request an analog port using the right number. Using “A0″ will not work, instead use 0. A small change.

I’ve also remove the writeable option in the switch statement. It’s a shame you can’t check if a serial port is writeable or not (only readable) I implemented this myself in the core libs but forgot to remove it, mea culpa.

ArduinoPi Python release

The last few months I’ve taken lessons on Python using codecademy. So I decided to convert the ArduinoPi PHP class to Python (the mother language of the RaspberryPi). The code is available in another GitHub repo (to make it easier) located here https://github.com/JanStevens/ArduinoPi-Python

This is my first real Python project so if there are any no-goes Python wise please say so in the comments. The functionality is rather limited, it’s possible to send digital, pwm, analog, multiple-pwm and multiple-digital commands to the Arduino.

Later on I’ll include more options in the python interface. Before using the ArduinoPi Python make sure you installed pip (sudo apt-get install python-pip).

After that you must install Flask and PySerial using the pip command as follows:

Now you can just run the main.py script using python main.py. I didn’t include an html example yet but you can go in your browser and type the IP address of your PI followed by port number 5000 and then use the commands from API documentation post.

The commands work the same as the PHP version, the only difference is the port number. All the responses are in JSON so you can use AJAX to make a call to a specific URL with POST or GET and get the desired result.

Stay tuned for more updates and more features!

ArduinoPi v1.5 Bluetooth Dongle Support

A new version and the release of ArduinoPi Python!

Today I updated ArduinoPi to support a Bluetooth connection instead of the wired connection. Last time I talked about connecting the Raspberry Pi with a BlueSmirf dongle. I’ve also updated the repository on Github, officially hitting version 1.5.

Preparing your Raspberry Pi for ArduinoPi 1.5

Before trying out the Bluetooth connection make sure you followed my last blog post. We only change one option in the rfcomm.conf file.

On line six we change the bind option “no” to ”yes“. This will automatically bind the device without connecting it, resulting in a locked port you cannot use for anything useful. The fun part, when you disable the automatically binding, is that your Raspberry Pi now automatically will pair with your BlueSmirf.

Working of ArduinoPi 1.5

Not much is changed but I’ll go over it quick. We don’t need to change anything if we have an active connection with our Bluetooth device. The phpSerial class will work as expected. In the deviceSet function I’ve added the following code:

So if we can’t open the device (Bluetooth or UART) with stty -F then I’ll look if we have a rfcomm port. If we have a rfcomm port then we connect with it using the new private function _exec_bg. This function will run the rfcomm command in the background and request the PID. Next we sleep for 2.5 seconds, the communication can take a while to set up depending on your Bluetooth dongle. If you have issues or the first command gives back an error then increase the sleep time.

This also means that If no connection is present, the first executed command will take considerable longer than the following commands.

More about the _exec_bg function

This function is a bit special and took me a while to come up with it. The basic idea is that you assign a new worker with the command proc_open. We ignore all the output and I also don’t want any streams from the command to read or write. The proc_get_status allows us to return the PID of the started process. One note, I increase the PID with one, a know PHP bug and not really the best solution, but it works.

Normally you would read the streams, doing this will result in a hanging script, the same with other PHP commands like exec or shell_exec, they all hang. I also tried to get the right PID by using ps aux | grep and so one but I found out that PHP can’t find his own opened processes. More about this issue is on stackoverflow.

Conclusion

It’s possible to connect the Raspberry Pi and Arduino with a wireless connection using Bluetooth. The ArduinoPi library can be used to control the Arduino wireless from a webpage.

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 :) .