We used the Standard Firmata sketch in Arduino to communicate with Processing, which functions in this example, as the intermediary and facilitator between physical devices of my Arduino circuit and the web.
The beauty of this sort of set up is that you could control any number of devices remotely from anywhere in the world.
To do this you will need to download the latest version's of Processing (1.1) and Arduino (18). Then download and import the 'arduino' and 'eeml' libraries into your Processing sketchbook library.
Also, you will need to use a data share server on the net to host your sensor feeds. For this example we used http://www.pachube.com/, a free site designed for this sort of application (this is a useful site but can be limiting later down the line due to data transfer caps).
We started by sending a button state reading to pachube. 0 for 'off' and 1 for 'on'. This is based on the 'eeml' example in Processing;
Also, you will need to use a data share server on the net to host your sensor feeds. For this example we used http://www.pachube.com/, a free site designed for this sort of application (this is a useful site but can be limiting later down the line due to data transfer caps).
We started by sending a button state reading to pachube. 0 for 'off' and 1 for 'on'. This is based on the 'eeml' example in Processing;
import eeml.*;
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
DataOut dOut;
float lastUpdate;
int buttonState =0;
void setup(){
// set up DataOut object; requires URL of the EEML you are updating, and your Pachube API key
dOut = new DataOut(this, "http://www.pachube.com/api/feeds/7472.xml", "yourAPIyourAPI24bbc4622f0d5fb86c1fe75d61");
// and add and tag a datastream
dOut.addData(2,"button");
arduino = new Arduino (this, Arduino.list()[0],57600);
arduino.pinMode(2,arduino.INPUT);
}
void draw()
{
// update once every 5 seconds (could also be e.g. every mouseClick)
if ((millis() - lastUpdate) > 5000){
buttonState=arduino.digitalRead(2);
println("ready to POST: ");
dOut.update(0, buttonState); // update the datastream
int response = dOut.updatePachube(); // updatePachube() updates by an authenticated PUT HTTP request
println(response); // should be 200 if successful; 401 if unauthorized; 404 if feed doesn't exist
lastUpdate = millis();
}
}
The feed that dOut is sending to is in green.
The blue code is where you put your unique API code from pachube. This is issued to you when you create an account at pachube.com
-------------------------------------------------------------------
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
DataOut dOut;
float lastUpdate;
int buttonState =0;
void setup(){
// set up DataOut object; requires URL of the EEML you are updating, and your Pachube API key
dOut = new DataOut(this, "http://www.pachube.com/api/feeds/7472.xml", "yourAPIyourAPI24bbc4622f0d5fb86c1fe75d61");
// and add and tag a datastream
dOut.addData(2,"button");
arduino = new Arduino (this, Arduino.list()[0],57600);
arduino.pinMode(2,arduino.INPUT);
}
void draw()
{
// update once every 5 seconds (could also be e.g. every mouseClick)
if ((millis() - lastUpdate) > 5000){
buttonState=arduino.digitalRead(2);
println("ready to POST: ");
dOut.update(0, buttonState); // update the datastream
int response = dOut.updatePachube(); // updatePachube() updates by an authenticated PUT HTTP request
println(response); // should be 200 if successful; 401 if unauthorized; 404 if feed doesn't exist
lastUpdate = millis();
}
}
The feed that dOut is sending to is in green.
The blue code is where you put your unique API code from pachube. This is issued to you when you create an account at pachube.com
-------------------------------------------------------------------
Next we hooked up a servo to one of the PWM pins on the Arduino. We called data in dIn from the feed in the first example, then used the button state reading to determine whether the servo sat at 0 or 180 degrees. On or Off.
import eeml.*;
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
float myVariable;
//int servoPin = 10; // Control pin for servo motor
DataIn dIn;
void setup(){
size (180, 50);
// set up DataIn object; indicate the URL you want, your Pachube API key, and how often you want it to update
// e.g. every 15 seconds
dIn = new DataIn(this,"http://www.pachube.com/api/feeds/7472.xml",
"yourAPIyourAPI24bbc4622f0d5fb86c1fe75d61", 5000);
arduino = new Arduino(this, Arduino.list()[0]);
arduino.pinMode(10, Arduino.OUTPUT);
}
void draw()
{
if (myVariable == 0.0) {
arduino.analogWrite(10, 0);
println("off");
} else {
println("on");
arduino.analogWrite(10, 180);
}
}
// onReceiveEEML is run every time your app receives back EEML that it has requested from a Pachube feed.
void onReceiveEEML(DataIn d){
myVariable = d.getValue(2); // get the value of the stream 2
println(myVariable);
}
In this example analogWrite tells the servo which position to be in, writing the value pulled from our feed to the PWM pin 10 that the servo is connected to.
Pushing a button in one room, which controls a servo in another may seem like small stuff, but has many practical and fun applications.
No comments:
Post a Comment