This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
user:jr018429:portfolio:webserverandarduino.html [2012/04/07 18:00] – [Execution] jr018429 | user:jr018429:portfolio:webserverandarduino.html [2012/04/17 00:04] (current) – [References] jr018429 | ||
---|---|---|---|
Line 1: | Line 1: | ||
+ | <WRAP centeralign round graybg box 96%> | ||
+ | <WRAP muchbigger> | ||
+ | <WRAP bigger> | ||
+ | <WRAP bigger> | ||
+ | </ | ||
+ | ======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:// | ||
+ | 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: | ||
+ | |||
+ | <WRAP centeralign> | ||
+ | ^ Attribute | ||
+ | | maintenance | ||
+ | | configuration | ||
+ | | logging | ||
+ | | implementation | ||
+ | | enhancement | ||
+ | | virtualization | ||
+ | | redundancy | ||
+ | | administration | ||
+ | | exploration | ||
+ | | log analysis | ||
+ | | troubleshooting | ||
+ | | security | ||
+ | </ | ||
+ | |||
+ | 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**, | ||
+ | < | ||
+ | void setup() | ||
+ | { | ||
+ | Serial.begin(9600); | ||
+ | } | ||
+ | |||
+ | void loop() | ||
+ | { | ||
+ | Serial.println(analogRead(0)); | ||
+ | delay(1000); | ||
+ | } | ||
+ | </ | ||
+ | This is my PHP source file, **rdArd.php**, | ||
+ | * stty 9600 cs8 -parenb -cstopb < / | ||
+ | * stty -F / | ||
+ | < | ||
+ | <?php | ||
+ | $fp = fopen("/ | ||
+ | $data = intval(fgets($fp)); | ||
+ | print $data; | ||
+ | fclose($fp); | ||
+ | ?> | ||
+ | </ | ||
+ | This is my 2nd PHP source file, **rdArd2.php**, | ||
+ | < | ||
+ | < | ||
+ | < | ||
+ | <form action="<? | ||
+ | < | ||
+ | </ | ||
+ | <?php | ||
+ | | ||
+ | { | ||
+ | $fp = fopen("/ | ||
+ | $data = intval(fgets($fp)); | ||
+ | print $data; | ||
+ | | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | |||
+ | ======Execution====== | ||
+ | I logged in to the emachine remotely using pUTTY:\\ | ||
+ | <cli> | ||
+ | login as: jr018429 | ||
+ | jr018429@192.168.1.2' | ||
+ | Welcome to Ubuntu 11.10 (GNU/Linux 3.0.0-12-generic i686) | ||
+ | |||
+ | * Documentation: | ||
+ | |||
+ | 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:\\ | ||
+ | <cli> | ||
+ | jr018429@emachine: | ||
+ | Package: arduino | ||
+ | Priority: optional | ||
+ | Section: universe/ | ||
+ | Installed-Size: | ||
+ | Maintainer: Ubuntu Developers < | ||
+ | Original-Maintainer: | ||
+ | Architecture: | ||
+ | Version: 0022+dfsg-4 | ||
+ | Depends: default-jre | java6-runtime, | ||
+ | Recommends: extra-xdg-menus | ||
+ | Filename: pool/ | ||
+ | Size: 677650 | ||
+ | MD5sum: 44a5e8bf16cfa3ed4c5af32d4c8db5d4 | ||
+ | SHA1: 7e6d000d6895134e946a39d7a5ab35437c6e32b7 | ||
+ | SHA256: 48ee609e5be8e8b457fd9fddd25a093f92976d5b6b780f21bd9f40fbfd4e01c2 | ||
+ | Description-en: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | . | ||
+ | This package will install the integrated development environment that | ||
+ | | ||
+ | to the Arduino development board. Libraries and example code will also | ||
+ | be installed. | ||
+ | Homepage: http:// | ||
+ | Description-md5: | ||
+ | Bugs: https:// | ||
+ | Origin: Ubuntu | ||
+ | |||
+ | Package: arduino | ||
+ | Priority: optional | ||
+ | Section: universe/ | ||
+ | Installed-Size: | ||
+ | Maintainer: Ubuntu Developers < | ||
+ | Original-Maintainer: | ||
+ | Architecture: | ||
+ | Version: 0022+dfsg-4ubuntu0.1 | ||
+ | Depends: default-jre | java6-runtime, | ||
+ | Recommends: extra-xdg-menus | ||
+ | Filename: pool/ | ||
+ | Size: 678268 | ||
+ | MD5sum: 48561334bb2b1c19f3cb258000174d6f | ||
+ | SHA1: 771e275dc741b7ddabcb447733d6a0419913e52f | ||
+ | SHA256: aa15593680f87dffd45cffe2736ea97ebcedd390502bfc1ffbc7fd657bb7f61c | ||
+ | Description-en: | ||
+ | | ||
+ | | ||
+ | | ||
+ | | ||
+ | . | ||
+ | This package will install the integrated development environment that | ||
+ | | ||
+ | to the Arduino development board. Libraries and example code will also | ||
+ | be installed. | ||
+ | Homepage: http:// | ||
+ | Description-md5: | ||
+ | Bugs: https:// | ||
+ | Origin: Ubuntu | ||
+ | |||
+ | jr018429@emachine: | ||
+ | </ | ||
+ | Next, I installed the arduino package on the emachine, however, it failed:\\ | ||
+ | <cli> | ||
+ | root@emachine:/ | ||
+ | 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:// | ||
+ | Something wicked happened resolving ' | ||
+ | Err http:// | ||
+ | Something wicked happened resolving ' | ||
+ | Err http:// | ||
+ | Something wicked happened resolving ' | ||
+ | Err http:// | ||
+ | Something wicked happened resolving ' | ||
+ | Err http:// | ||
+ | Something wicked happened resolving ' | ||
+ | Err http:// | ||
+ | Something wicked happened resolving ' | ||
+ | Err http:// | ||
+ | Something wicked happened resolving ' | ||
+ | Err http:// | ||
+ | Something wicked happened resolving ' | ||
+ | Err http:// | ||
+ | Something wicked happened resolving ' | ||
+ | Failed to fetch http:// | ||
+ | Failed to fetch http:// | ||
+ | Failed to fetch http:// | ||
+ | Failed to fetch http:// | ||
+ | Failed to fetch http:// | ||
+ | Failed to fetch http:// | ||
+ | Failed to fetch http:// | ||
+ | Failed to fetch http:// | ||
+ | Failed to fetch http:// | ||
+ | E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing? | ||
+ | root@emachine:/ | ||
+ | </ | ||
+ | After making changes to the / | ||
+ | <cli> | ||
+ | root@emachine:/ | ||
+ | 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:// | ||
+ | Get:2 http:// | ||
+ | Get:3 http:// | ||
+ | Get:4 http:// | ||
+ | Get:5 http:// | ||
+ | Get:6 http:// | ||
+ | Get:7 http:// | ||
+ | Get:8 http:// | ||
+ | Get:9 http:// | ||
+ | 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 .../ | ||
+ | Selecting previously deselected package librxtx-java. | ||
+ | Unpacking librxtx-java (from .../ | ||
+ | Selecting previously deselected package binutils-avr. | ||
+ | Unpacking binutils-avr (from .../ | ||
+ | Selecting previously deselected package gcc-avr. | ||
+ | Unpacking gcc-avr (from .../ | ||
+ | Selecting previously deselected package avrdude. | ||
+ | Unpacking avrdude (from .../ | ||
+ | Selecting previously deselected package avr-libc. | ||
+ | Unpacking avr-libc (from .../ | ||
+ | Selecting previously deselected package arduino-core. | ||
+ | Unpacking arduino-core (from .../ | ||
+ | Selecting previously deselected package arduino. | ||
+ | Unpacking arduino (from .../ | ||
+ | Selecting previously deselected package extra-xdg-menus. | ||
+ | Unpacking extra-xdg-menus (from .../ | ||
+ | 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 / | ||
+ | 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:/ | ||
+ | </ | ||
+ | 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.\\ | ||
+ | <cli> | ||
+ | root@emachine:/ | ||
+ | </ | ||
+ | Lastly, I had to restart the Web server.\\ | ||
+ | <cli> | ||
+ | root@emachine:/ | ||
+ | </ | ||
+ | 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.\\ | ||
+ | {{: | ||
+ | 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' | ||
+ | I checked the port parameters for the Arduino in Linux:\\ | ||
+ | <cli> | ||
+ | root@emachine:/ | ||
+ | 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' | ||
+ | <cli> | ||
+ | <? | ||
+ | //Valid baud rates: | ||
+ | //110 | ||
+ | //150 | ||
+ | //300 | ||
+ | //600 | ||
+ | //1200 | ||
+ | //2400 | ||
+ | //4800 | ||
+ | //9600 | ||
+ | //19200 | ||
+ | //38400 | ||
+ | //57600 | ||
+ | //115200 | ||
+ | |||
+ | exec(" | ||
+ | ?> | ||
+ | </ | ||
+ | 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.\\ | ||
+ | < | ||
+ | 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:/ | ||
+ | </ | ||
+ | After changing the baud rate be execting my php file, setBaudPHP.php, | ||
+ | <cli> | ||
+ | < | ||
+ | < | ||
+ | <form action="<? | ||
+ | < | ||
+ | </ | ||
+ | <?php | ||
+ | | ||
+ | //Valid baud rates: | ||
+ | //110 | ||
+ | //150 | ||
+ | //300 | ||
+ | //600 | ||
+ | //1200 | ||
+ | //2400 | ||
+ | //4800 | ||
+ | //9600 | ||
+ | //19200 | ||
+ | //38400 | ||
+ | //57600 | ||
+ | //115200 | ||
+ | |||
+ | exec(" | ||
+ | $fp = fopen("/ | ||
+ | //sleep(2); | ||
+ | $data = intval(fgets($fp)); | ||
+ | print $data; | ||
+ | fclose($fp); | ||
+ | } | ||
+ | ?> | ||
+ | </ | ||
+ | </ | ||
+ | </ | ||
+ | To verify that the revised rdArd.php file actually works, I changed the serial port back to 115200 baud:\\ | ||
+ | <cli> | ||
+ | root@emachine:/ | ||
+ | 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:/ | ||
+ | root@emachine:/ | ||
+ | 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:/ | ||
+ | </ | ||
+ | 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:\\ | ||
+ | <cli> | ||
+ | root@emachine:/ | ||
+ | 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:/ | ||
+ | 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:/ | ||
+ | </ | ||
+ | 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:// | ||
+ | - http:// | ||
+ | - http:// | ||
+ | - http:// | ||
+ | - http:// | ||
+ | - http:// | ||
+ | - http:// | ||
+ | - http:// | ||
+ | - http:// | ||
+ | - http:// | ||
+ | - http:// | ||