Controlling Hardware Remotely via the Web (Ubuntu/Apache2/PHP5/Arduino Duelimenove) John T. Rine 02/10/2012 ======Objectives====== The objective of this project is to control electronic hardware via the Web. I chose to use an old emachine T1096 desktop PC and Sony Multiscan 100SF monitor as a platform to run Apache 2 Web Server. An Arduino microcontroller board will be connected to the emachine via USB. Hardware will then be controlled by the Arduino which takes commands sent from a user via a Web browser. ======Prerequisites====== - Knowledge of microprocessors - Knowledge of the Arduino Wiring language - Knowledge of Apache2 - Knowledge of PHP and/or C - Knowledge of Linux - Knowledge of basic networking ======Background====== ======Scope====== ======Attributes====== The course requirements for HPC Experience I projects are listed at:\\ http://lab46.corning-cc.edu/haas/hpc1/tasks This project has the following project attributes described on that page: ======Project Attributes====== The following are the programming attributes you must consider while undertaking a project for credit in this course: ^ Attribute ^ Qty needed ^ Description ^ | maintenance | 4 |upkeep of an existing resource is undertaken | | configuration | 4 |configuration of a (new) resource is achieved | | logging | 4 |status of a resource is recorded | | implementation | 4 |a new resource is implemented and brought on-line | | enhancement | 4 |an existing resource is improved upon | | virtualization | 4 |a resource is deployed within a virtualized environment | | redundancy | 4 |a resource's availability or contents are hardened due to backups or avoiding a single point of failure | | administration | 4 |a resource receives oversight maintenance to better direct its capabilities | | exploration | 4 |exploration of new concepts/resources is undertaken | | log analysis | 4 |collected log data is analyzed to accomplish some ends | | troubleshooting | 4 |a problem is explored/solved using troubleshooting techniques | | security | 4 |a resource is hardened due to security improvements | There are a total of 48 items needed; with a maximum of 8 attributes achievable per project, that makes for a __minimum__ of 6 projects you must perform during the semester. Note that where multiple quantities of attributes are concerned, only a single instance of an attribute can be achieved on any single project. ======Code====== ===Reading Analog from Arduino via PHP=== This is my Arduino source file, **rdAna.pde**, which reads the analog pin 0 of the arduino and then writes it to the serial port once per second.\\ void setup() { Serial.begin(9600); } void loop() { Serial.println(analogRead(0)); delay(1000); } This is my PHP source file, **rdArd.php**, which gets analog data from the Web server and returns it to a client's Web browser. For this, and the next PHP source file to work, the ttyUSB0 must be set to the proper serial port configuration of 9600 baud, 8 data bits, no parity and 1 stop bit using one of the following invocations of stty:\\ * stty 9600 cs8 -parenb -cstopb < /dev/ttyUSB0 * stty -F /dev/ttyUSB0 9600 cs8 -parenb -cstopb This is my 2nd PHP source file, **rdArd2.php**, which, after pressing the 'Get A/D 0 Value' button, returns analog data from the Web server and returns it to a client's Web browser.
======Execution====== I logged in to the emachine remotely using pUTTY:\\ login as: jr018429 jr018429@192.168.1.2's password: Welcome to Ubuntu 11.10 (GNU/Linux 3.0.0-12-generic i686) * Documentation: https://help.ubuntu.com/ Last login: Tue Mar 20 18:27:39 2012 from 192.168.1.111 jr018429@emachine:~$ I performed an apt-cache show arduino to get information regarding the Arduino package:\\ jr018429@emachine:~$ apt-cache show arduino Package: arduino Priority: optional Section: universe/electronics Installed-Size: 956 Maintainer: Ubuntu Developers Original-Maintainer: Scott Howard Architecture: all Version: 0022+dfsg-4 Depends: default-jre | java6-runtime, libjna-java, librxtx-java (>= 2.2pre2-3), arduino-core (= 0022+dfsg-4) Recommends: extra-xdg-menus Filename: pool/universe/a/arduino/arduino_0022+dfsg-4_all.deb Size: 677650 MD5sum: 44a5e8bf16cfa3ed4c5af32d4c8db5d4 SHA1: 7e6d000d6895134e946a39d7a5ab35437c6e32b7 SHA256: 48ee609e5be8e8b457fd9fddd25a093f92976d5b6b780f21bd9f40fbfd4e01c2 Description-en: AVR development board IDE and built-in libraries Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. . This package will install the integrated development environment that allows for program writing, code verfication, compiling, and uploading to the Arduino development board. Libraries and example code will also be installed. Homepage: http://www.arduino.cc Description-md5: 60f8f72e8783c6b5a72254120b680cdb Bugs: https://bugs.launchpad.net/ubuntu/+filebug Origin: Ubuntu Package: arduino Priority: optional Section: universe/electronics Installed-Size: 956 Maintainer: Ubuntu Developers Original-Maintainer: Scott Howard Architecture: all Version: 0022+dfsg-4ubuntu0.1 Depends: default-jre | java6-runtime, libjna-java, librxtx-java (>= 2.2pre2-3), arduino-core (= 0022+dfsg-4ubuntu0.1) Recommends: extra-xdg-menus Filename: pool/universe/a/arduino/arduino_0022+dfsg-4ubuntu0.1_all.deb Size: 678268 MD5sum: 48561334bb2b1c19f3cb258000174d6f SHA1: 771e275dc741b7ddabcb447733d6a0419913e52f SHA256: aa15593680f87dffd45cffe2736ea97ebcedd390502bfc1ffbc7fd657bb7f61c Description-en: AVR development board IDE and built-in libraries Arduino is an open-source electronics prototyping platform based on flexible, easy-to-use hardware and software. It's intended for artists, designers, hobbyists, and anyone interested in creating interactive objects or environments. . This package will install the integrated development environment that allows for program writing, code verfication, compiling, and uploading to the Arduino development board. Libraries and example code will also be installed. Homepage: http://www.arduino.cc Description-md5: 60f8f72e8783c6b5a72254120b680cdb Bugs: https://bugs.launchpad.net/ubuntu/+filebug Origin: Ubuntu jr018429@emachine:~$ Next, I installed the arduino package on the emachine, however, it failed:\\ root@emachine:/home/jr018429# apt-get install arduino Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: arduino-core avr-libc avrdude binutils-avr extra-xdg-menus gcc-avr libjna-java librxtx-java Suggested packages: avrdude-doc task-c-devel gcc-doc gcc-4.2 libjna-java-doc The following NEW packages will be installed: arduino arduino-core avr-libc avrdude binutils-avr extra-xdg-menus gcc-avr libjna-java librxtx-java 0 upgraded, 9 newly installed, 0 to remove and 18 not upgraded. Need to get 18.8 MB of archives. After this operation, 77.6 MB of additional disk space will be used. Do you want to continue [Y/n]? y Err http://us.archive.ubuntu.com/ubuntu/ oneiric/universe libjna-java i386 3.2.7-4 Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Err http://us.archive.ubuntu.com/ubuntu/ oneiric/universe librxtx-java i386 2.2pre2-8 Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Err http://us.archive.ubuntu.com/ubuntu/ oneiric/universe binutils-avr i386 2.20.1-2 Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Err http://us.archive.ubuntu.com/ubuntu/ oneiric/universe gcc-avr i386 1:4.5.3-2 Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Err http://us.archive.ubuntu.com/ubuntu/ oneiric/universe avrdude i386 5.10-3 Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Err http://us.archive.ubuntu.com/ubuntu/ oneiric/universe avr-libc all 1:1.7.1-2 Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Err http://us.archive.ubuntu.com/ubuntu/ oneiric-updates/universe arduino-core all 0022+dfsg-4ubuntu0.1 Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Err http://us.archive.ubuntu.com/ubuntu/ oneiric-updates/universe arduino all 0022+dfsg-4ubuntu0.1 Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Err http://us.archive.ubuntu.com/ubuntu/ oneiric/universe extra-xdg-menus all 1.0-4 Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/universe/libj/libjna-java/libjna-java_3.2.7-4_i386.deb Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/universe/r/rxtx/librxtx-java_2.2pre2-8_i386.deb Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/universe/b/binutils-avr/binutils-avr_2.20.1-2_i386.deb Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/universe/g/gcc-avr/gcc-avr_4.5.3-2_i386.deb Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/universe/a/avrdude/avrdude_5.10-3_i386.deb Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/universe/a/avr-libc/avr-libc_1.7.1-2_all.deb Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/universe/a/arduino/arduino-core_0022+dfsg-4ubuntu0.1_all.deb Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/universe/a/arduino/arduino_0022+dfsg-4ubuntu0.1_all.deb Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) Failed to fetch http://us.archive.ubuntu.com/ubuntu/pool/universe/e/extra-xdg-menus/extra-xdg-menus_1.0-4_all.deb Something wicked happened resolving 'us.archive.ubuntu.com:http' (-5 - No address associated with hostname) E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing? root@emachine:/home/jr018429# After making changes to the /etc/resolv.conf file which is describe on my Setting up and Administering my Home Network, the Arduino package installed flawlessly:\\ root@emachine:/home/jr018429# apt-get install arduino Reading package lists... Done Building dependency tree Reading state information... Done The following extra packages will be installed: arduino-core avr-libc avrdude binutils-avr extra-xdg-menus gcc-avr libjna-java librxtx-java Suggested packages: avrdude-doc task-c-devel gcc-doc gcc-4.2 libjna-java-doc The following NEW packages will be installed: arduino arduino-core avr-libc avrdude binutils-avr extra-xdg-menus gcc-avr libjna-java librxtx-java 0 upgraded, 9 newly installed, 0 to remove and 18 not upgraded. Need to get 18.8 MB of archives. After this operation, 77.6 MB of additional disk space will be used. Do you want to continue [Y/n]? y Get:1 http://us.archive.ubuntu.com/ubuntu/ oneiric/universe libjna-java i386 3.2.7-4 [414 kB] Get:2 http://us.archive.ubuntu.com/ubuntu/ oneiric/universe librxtx-java i386 2.2pre2-8 [175 kB] Get:3 http://us.archive.ubuntu.com/ubuntu/ oneiric/universe binutils-avr i386 2.20.1-2 [4,347 kB] Get:4 http://us.archive.ubuntu.com/ubuntu/ oneiric/universe gcc-avr i386 1:4.5.3-2 [7,699 kB] Get:5 http://us.archive.ubuntu.com/ubuntu/ oneiric/universe avrdude i386 5.10-3 [199 kB] Get:6 http://us.archive.ubuntu.com/ubuntu/ oneiric/universe avr-libc all 1:1.7.1-2 [4,665 kB] Get:7 http://us.archive.ubuntu.com/ubuntu/ oneiric-updates/universe arduino-core all 0022+dfsg-4ubuntu0.1 [602 kB] Get:8 http://us.archive.ubuntu.com/ubuntu/ oneiric-updates/universe arduino all 0022+dfsg-4ubuntu0.1 [678 kB] Get:9 http://us.archive.ubuntu.com/ubuntu/ oneiric/universe extra-xdg-menus all 1.0-4 [12.8 kB] Fetched 18.8 MB in 35s (525 kB/s) Selecting previously deselected package libjna-java. (Reading database ... 128091 files and directories currently installed.) Unpacking libjna-java (from .../libjna-java_3.2.7-4_i386.deb) ... Selecting previously deselected package librxtx-java. Unpacking librxtx-java (from .../librxtx-java_2.2pre2-8_i386.deb) ... Selecting previously deselected package binutils-avr. Unpacking binutils-avr (from .../binutils-avr_2.20.1-2_i386.deb) ... Selecting previously deselected package gcc-avr. Unpacking gcc-avr (from .../gcc-avr_1%3a4.5.3-2_i386.deb) ... Selecting previously deselected package avrdude. Unpacking avrdude (from .../avrdude_5.10-3_i386.deb) ... Selecting previously deselected package avr-libc. Unpacking avr-libc (from .../avr-libc_1%3a1.7.1-2_all.deb) ... Selecting previously deselected package arduino-core. Unpacking arduino-core (from .../arduino-core_0022+dfsg-4ubuntu0.1_all.deb) ... Selecting previously deselected package arduino. Unpacking arduino (from .../arduino_0022+dfsg-4ubuntu0.1_all.deb) ... Selecting previously deselected package extra-xdg-menus. Unpacking extra-xdg-menus (from .../extra-xdg-menus_1.0-4_all.deb) ... Processing triggers for doc-base ... Processing 2 added doc-base files... Registering documents with scrollkeeper... Processing triggers for man-db ... Processing triggers for desktop-file-utils ... Processing triggers for bamfdaemon ... Rebuilding /usr/share/applications/bamf.index... Processing triggers for gnome-menus ... Processing triggers for hicolor-icon-theme ... Setting up libjna-java (3.2.7-4) ... Setting up librxtx-java (2.2pre2-8) ... Setting up binutils-avr (2.20.1-2) ... Setting up gcc-avr (1:4.5.3-2) ... Setting up avrdude (5.10-3) ... Setting up avr-libc (1:1.7.1-2) ... Setting up arduino-core (0022+dfsg-4ubuntu0.1) ... Setting up arduino (0022+dfsg-4ubuntu0.1) ... Setting up extra-xdg-menus (1.0-4) ... Processing triggers for libc-bin ... ldconfig deferred processing now taking place root@emachine:/home/jr018429# Once the Web server was up and running, I was ready to develop the Web interface which will be used to both command the Arduino to control hardware, but also return data.\\ I found HTML and PHP code snippets on the Web which demonstrate the following:\\ * Open and read or write to a serial port. * Create buttons that can be used to call PHP code. I combined these code snippets to get the Arduino to return analog data to the Web interface. I then logged in as root and uploaded the php file to /var/www, the Web server directory.\\ Next, I compiled and uploaded some simple Arduino code to read analog input pin 0 and then write its value to the serial port.\\ Before, I could use the serial port, I had to add www-data to group dialout, the owner of ttyUSB0, the serial port.\\ root@emachine:/var/www# adduser www-data dialout Lastly, I had to restart the Web server.\\ root@emachine:/var/www# /etc/init.d/apache2 restart Here is the Web page with data returned from the Arduino. The Arduino has 10-bit A/D converters. This means that the value returned from the Arduino can range from 0 to 1023 dec.\\ {{:opus:fall2011:jr018429:rdard.jpg?200|}}\\ In order to give this range of values meaning, the values might need to be offset and scaled using what is known as zero-span. Zero-span or offset and scaling can be performed either by hardware, op-amps for example, or software. After scaling and offsetting (if required) units must be attached to the value.\\ \\ My web server machine had been off for a few days, and I wanted to get back to working with PHP, HTML, and my Arduino. I found however, that I couldn't get the Arduino to return data via PHP.\\ I checked the port parameters for the Arduino in Linux:\\ root@emachine:/var/www# stty -F /dev/ttyUSB0 speed 115200 baud; line = 0; eof = ^A; min = 1; time = 0; -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke My arduino's serial port is set to 9600 baud but the serial port on the Linux side is set to 115200 baud so I decided to write a test PHP file to set the serial port baud rate:\\ When I exectued this PHP file from my Web browser and then checked the serial port attached to the Arduino again, I found that it had chenged.\\ root@emachine:/var/www# stty -F /dev/ttyUSB0 speed 9600 baud; line = 0; eof = ^A; min = 1; time = 0; -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke root@emachine:/var/www# After changing the baud rate be execting my php file, setBaudPHP.php, via the Web browser, I was able to get data again! I added the contents of the setBaudPHP.php to the my original PHP file rdArd.php:\\
To verify that the revised rdArd.php file actually works, I changed the serial port back to 115200 baud:\\ root@emachine:/var/www# stty -F /dev/ttyUSB0 speed 9600 baud; line = 0; eof = ^A; min = 1; time = 0; -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke root@emachine:/var/www# stty -F /dev/ttyUSB0 115200 root@emachine:/var/www# stty -F /dev/ttyUSB0 speed 115200 baud; line = 0; eof = ^A; min = 1; time = 0; -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke root@emachine:/var/www# Next, I executed the rdArd.php file from my Web browser and found that analog data was returned and the baud rate of serial port connected to the Arduino was changed to match the baud rate set in the Arduino:\\ root@emachine:/var/www# stty -F /dev/ttyUSB0 speed 115200 baud; line = 0; eof = ^A; min = 1; time = 0; -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke root@emachine:/var/www# stty -F /dev/ttyUSB0 speed 9600 baud; line = 0; eof = ^A; min = 1; time = 0; -brkint -icrnl -imaxbel -opost -onlcr -isig -icanon -iexten -echo -echoe -echok -echoctl -echoke root@emachine:/var/www# I noticed that sometimes the analog value returned is 0, usually after it hasn't been queried for a while. Normally the data returned without anything input to analog pin 0 on the Arduino is between 500 and 700.\\ ======Reflection====== ======References====== - http://askubuntu.com/questions/107619/how-do-i-install-the-arduino-ide-on-ubuntu - http://newbiedoc.sourceforge.net/tutorials/apt-get-intro/info.html - http://lab46.corning-cc.edu/user/jr018429/portfolio/First_HPC_2_EoCE_0x1_Preparation - http://webhole.net/2010/05/07/arduino-and-php-serial-port-communication-example/ - http://www.arduinoprojects.com/node/10 - http://www.bushveldlab.com/Bushveld_Labs/Blog/Entries/2011/3/13_Controlling_Arduino_with_PHP_in_Ubuntu.html - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1263875972 - http://www.clshack.com/en/arduino-php-write-read-serial-port-d.html - http://www.arduino.cc/cgi-bin/yabb2/YaBB.pl?num=1232739501 - http://www.phpclasses.org/browse/file/17926.html - http://www.arduino.cc/playground/Main/DisablingAutoResetOnSerialConnection