Introduction: Touch Me! Edison Based Touch Weblog
This instructable will guide you step by step to:
- setup your Intel® Edison Arduino Board with the Grove Starter Kit Plus V2.0
- read basic sensor information (button / touch sensor)
- display messages on the Grove - LCD screen
- communicate from the Arduino IDE with the underlying linux OS
- setup a basic web server to display a log of the sensor activity
Although there are real world situations where a touch sensor weblog could be useful (eg lap time logging), this project is also a good way to gain some familiarity with the Edison Arduino Board using the Grove Starter Kit.
I am thankful to Instructables and Intel for providing me the hardware used in this project (I was one of the 250 lucky ones!)
Step 1: Hardware and Software Setup
The Intel® Edison platform is still young, but luckily Intel has provided enough information to have your Intel® Edison Arduino Board up and running in a couple of minutes (if you follow the instruction carefully, that is)
Let's organize all relevant info here:
- DO THIS FIRST AND FOREMOST! Flash your Edison Board with the latest Yocto Linux distro, before doing anything else. The latest image has all goodies (lighttpd, node.js etc) that your board needs to communicate with the world around it. If you install later, all your settings and data stored so far will be lost and you'll have to start from zero again.
Instructions here: - Setup the Arduino Edison IDE, wifi etc. IMPORTANT: In order to communicate via terminal with your board, remember to connect both USB ports to your computer. One USB connection is used for uploading your sketches from the Arduino Edison IDE (in my mac: /dev/cu.usbmodem1413) and the other for the terminal connection, eg with the screen program (in my mac: /dev/cu.usbserial-A402IXWX)
Instructions here: - Attach the Seeedstudio Grove Shield on the Edison Board. This should be straightforward - just be careful not to bend any of the contacts of the Grove Shield. You can see on the photo the Grove shield attached to the Edison board.
- Attach the Grove-LCD screen with one of the I2C ports using one of the provided cables. Likewise, connect the button or the touch sensor to the D7 port.
Step 2: Controlling the Touch Sensor and the Lcd Screen
It is a good idea that you first play around with the code samples provided by Seeedstudio for the Grove Starter Kit V2.0
You can download them from github at
https://github.com/Seeed-Studio/Sketchbook_Starter...
Download them as ZIP, extract them at a convenient location and start playing with them.
Before testing the LCD screen, you have to install the LCD RGB Backlight library. To do that, go to you Arduino Edison IDE, select choose Sketch->Import Library->Add Library and navigate to where you extracted the Grove Starter Kit V2.0 Sketchbook, folder ./libraries//Grove_LCD_RGB_Backlight/ After that, you can run the LCD screen demos.
Upload the following sketch to your Arduino IDE and you'll have the LCD screen light up with a "touch!" message every time you touch the touch sensor (or you press the button).
#include <Wire.h>
#include "rgb_lcd.h" rgb_lcd lcd;
const int colorR = 0;
const int colorG = 0;
const int colorB = 255;
const int pinButton = 7; // pin of button define here
boolean touchStarted = false; void setup() { pinMode(pinButton, INPUT); // set button INPUT // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setRGB(0,0,0); // Print a message to the LCD.
lcd.print("Status:"); delay(1000); } void loop() { lcd.setCursor(0, 1); if(digitalRead(pinButton)) // when button is pressed { if (!touchStarted) // act only on start of touch event { touchStarted = true;
lcd.print ("touch!"); lcd.setRGB(colorR, colorG, colorB); } } else { touchStarted = false; lcd.setRGB(0,0,0); lcd.print (" "); } delay(10); }
Step 3: Log Touch Events to File
So far so good. It's now time to interact with the linux that's hiding underneath the Arduino Edison process.
The simplest way to do that is by calling a system command or script with Arduino Edison's system() function.
So, the modified sketch below, every time the touch sensor is activated, appends a line with the date and time, along with the "touch!" message and the Edison board's hostname to the file /tmp/arduino.log
Establish a terminal connection to your board, and you'll see the line added every time you press the button: Just give the following in your terminal :
root@edison:~#tail /tmp/arduino.log
The modified sketch (only the line with the system call added):
#include
#include "rgb_lcd.h" rgb_lcd lcd; const int colorR = 0; const int colorG = 0; const int colorB = 255; const int pinButton = 7; // pin of button define here boolean touchStarted = false; void setup() { pinMode(pinButton, INPUT); // set button INPUT // set up the LCD's number of columns and rows: lcd.begin(16, 2); lcd.setRGB(0,0,0); // Print a message to the LCD. lcd.print("Status:"); delay(1000); } void loop() { lcd.setCursor(0, 1); if(digitalRead(pinButton)) // when button is pressed { if (!touchStarted) // act only on start of touch event { touchStarted = true; lcd.print ("touch!"); lcd.setRGB(colorR, colorG, colorB); system("echo $(date '+%Y %b %d %H:%M:%S') Touch! $(hostname) >> /tmp/arduino.log"); } } else { touchStarted = false; lcd.setRGB(0,0,0); lcd.print (" "); } delay(10); }
Attachments
Step 4: Post to the Web (or at Least Your Local Network)
Now that you have your sensor data logged, you will want to post them to your local network. After all, Edison runs a fully fledged linux distro and is WLAN connected!
We'll be using node.js to run an HTTP server at port 8000
That is, our server will be running at http://youredisonaddress:8000/
First, connect via terminal to your Edison board. I assume that you connect as root, so after logging-in, you'll be at /home/root
Create a file called ardulog.js
and type or paste the following:
// Load the http module to create an http server.<br>var http = require('http'); var exec = require('child_process').exec; function execute(command, callback){ exec(command, function(error, stdout, stderr){ callback(stdout); }); }; // Configure the HTTP server to respond with the last entries of the arduino.log file var server = http.createServer(function (request, response) { response.writeHead(200, {"Content-Type": "text/plain"}); execute("tail /tmp/arduino.log", function(lastTouches){ response.end(lastTouches); }); }); // Listen on port 8000 server.listen(8000); // Acknowledge server at terminal console.log("Server running at port 8000");<br>
Type it, paste it? How?
Unfortunately, it seems the Yocto linux image provided by Intel, only has vi preinstalled. This, however, should not prevent you from installing your own favorite editor, in my case nano.
Install it as follows:
root@edison:~# wget http://www.nano-editor.org/dist/v2.2/nano-2.2.6.t...
root@edison:~# tar xvf nano-2.2.6.tar.gz
root@edison:~# cd nano-2.2.6/
root@edison:~/nano-2.2.6# ./configure
root@edison:~/nano-2.2.6# make
root@edison:~/nano-2.2.6# make install
If nothing goes wrong, you should have now nano installed. To create and edit the ardulog.js script, just type the following:
root@edison:~# cd ..
root@edison:~#nano ardulog.js
After having typed or pasted the above script, type Ctr+X and Y to save and exit nano.
To run your script, now type
root@edison:~# node ardulog.js
Voilà! You can see your touch logs from your local network! Open your computer's or tablet's browser at http://youredisonaddress:8000/ and start having fun!