Arduino Pi

fritzhut

Created by: fritzhut

Published Mar. 14, 2013

The ArduinoPi controller is a web-based solution to control your Arduino using a RaspberryPi. The controller uses AJAX and PHP, this project demonstrates some of the possibilities. The communication between the RaspberryPi and the Arduino is possible thanks to the php_serial.class.php.

Also featured by Hack a Day check out their explanation!

It is also possible to use a bluetooth connection. Another version of the project uses Python instead of PHP.

PHP based GitHub

Python based GitHub

Related categories: Web

Step 1: Step 1: Connecting the Arduino and Raspberry Pi

This step assumes you are using the external UART for connecting the Raspberry Pi with an Arduino Mega 2560. It's also possible to use this with another Arduino.

The Arduino uses 5V logic but the Raspberry Pi uses 3.3V logic, so a conversion must be made. I decided to build a circuit but you can also use Sparkfun Logic Level Converter. Note that the schematic is a bit wrong R3 should be 10k instead of 10 ohm!

The next image shows how the logic level shifting circuit should be connected with the Raspberry Pi. In the next step the Raspberry Pi gets configured and the first data can be send.

Step 2: Step 2: Configuring the Raspberry Pi

The Raspberry Pi sends all terminal output over the external serial to disable this behaviour the following file needs to be edited: sudo nano /etc/inittab the following line needs to be commented out by placing a hashtag before it (#):

T0:23:respawn:/sbin/getty-LttyAMA0115200vt100

Now the terminal stops listening on the UART pins but the debug information is still being send, to change this the following file needs to be edited: sudo nano /boot/cmdline.txt and the following part should be removed:

console=ttyAMA0,115200kgdboc=ttyAMA0,115200

Now upload the following script on the Arduino (I call it a pass through script):

voidsetup()
{
  // Open serial communications to PC and wait for port to open:
  Serial.begin(115200);
  Serial.println("Connected to PC");
  // set the data rate for the Bluetooth port
  Serial1.begin(115200);
}
voidloop()// run over and over {   if(Serial1.available())     Serial.write(Serial1.read());   if(Serial.available())     Serial1.write(Serial.read()); }

Next we can test the connection by sending the following command, the images show the result:

echo-e"This is the text I want to display on my Arduino \r\n">/dev/ttyAMA0

Step 3 will setup PHP or Python

Step 3: Step 3: Configuring PHP serial class

I assume you have installd PHP and apache/nginx/lighttpd installed. First upload a simple php script to see what user php is run in. For lighttpd this is www-data:

<?php 
echoexec('whoami');
?>

Note down the user returned by this PHP script. Now we need to grand the user asses to the serial port using the following command:

usermod-a-Gdialout www-data

If we now run groups www-data we can check to see that the dialout group has been added to the user:

root@raspberrypi:/opt/www# groups www-data
www-data:www-data dialout

We now test the communication using PHP serial and test the following simple php script to see if the communication works:

If everything works head over to the following github, fetch the code, copy the PHP code and also install the Arduino code in the repository. Everything should work and now you can use a web browser to control your arduino.

<?php
error_reporting(E_ALL);
ini_set('display_errors','1');
include"php_serial.class.php";
$serial=newphpSerial;
$serial->deviceSet("/dev/ttyAMA0");
$serial->confBaudRate(115200);
$serial->confParity("none");
$serial->confCharacterLength(8);
$serial->confStopBits(1);
$serial->deviceOpen();
$serial->sendMessage("Hello from my PHP script, say hi back!");
$serial->deviceClose();
echo"I've sended a message! \n\r";
?>

PHP Github: https://github.com/JanStevens/ArduinoPi-Controller

If you want to use the Python version you can ignore the complete step 3 and just fetch the code on github and run it.

Python Github: https://github.com/JanStevens/ArduinoPi-Python

Finally some intressting links on how to use the API and some extra information:

ArduinoPi 1.0 API explenation

A small tutorial on how to use Bluetooth with the Arduino Pi

Comments

comments powered by Disqus