Ver 1
import eeml.*;
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
float myVariable;
DataOut dOut;
float lastUpdate;
int buttonState = 0;
DataIn dIn;
void setup()
{
size (180, 50);
dIn = new DataIn(this,"http://www.pachube.com/api/feeds/7339.xml",
"API_KEY", 5000);
arduino = new Arduino(this, Arduino.list()[0]);
arduino.pinMode(10, Arduino.SERVO);
dOut = new DataOut(this, "http://www.pachube.com/api/feeds/7584.xml",
"API_KEY");
dOut.addData(2,"button");
arduino = new Arduino (this, Arduino.list()[0],57600);
arduino.pinMode(2,arduino.INPUT);
}
void draw()
{
if (myVariable == 0.0)
{
arduino.analogWrite(10, 0);
println("off");
}
else
{
println("on");
arduino.analogWrite(10, 180);
}
if ((millis() - lastUpdate) > 7000)
{
buttonState=arduino.digitalRead(2);
println("ready to POST: ");
dOut.update(0, buttonState);
int response = dOut.updatePachube();
println(response);
lastUpdate = millis();
}
}
void onReceiveEEML(DataIn d)
{
myVariable = d.getValue(2);
println(myVariable);
}
Ver 2
import eeml.*;
import processing.serial.*;
import cc.arduino.*;
import pachuino.*;
Pachuino p;
float lastUpdate;
float jacquesBtn;
float jacquesLdr;
float tapaniBtn;
float tapaniLdr;
float seamusBtn;
float seamusLdr;
float juditBtn;
float juditLdr;
void setup()
{
p = new Pachuino(this, Arduino.list()[0], 57600);
p.manualUpdate("http://www.pachube.com/api/7344.xml");
p.setKey("API_KEY");
p.addRemoteSensor("http://www.pachube.com/api/7344.xml", 0); // jacques button
p.addRemoteSensor("http://www.pachube.com/api/7344.xml", 2); // jacques ldr
p.addRemoteSensor("http://www.pachube.com/api/7339.xml", 2); // tapani button
p.addRemoteSensor("http://www.pachube.com/api/7339.xml", 3); // tapani ldr
p.addRemoteSensor("http://www.pachube.com/api/7472.xml", 2); // seamus button
p.addRemoteSensor("http://www.pachube.com/api/7472.xml", 3); // seamus ldr
p.addRemoteSensor("http://www.pachube.com/api/7472.xml", 0); // judit button
p.addRemoteSensor("http://www.pachube.com/api/7472.xml", 1); // judit ldr
p.addLocalSensor("digital", 2, "button");
//p.addLocalSensor("digital", 9, "servo");
p.addLocalSensor("analog", 3, "ldr");
}
void draw()
{
if ((millis() - lastUpdate) > 10000)
{
float lastUpdate;
jacquesBtn = p.remoteSensor[0].value;
jacquesLdr = p.remoteSensor[1].value;
tapaniBtn = p.remoteSensor[2].value;
tapaniLdr = p.remoteSensor[3].value;
seamusBtn = p.remoteSensor[4].value;
seamusLdr = p.remoteSensor[5].value;
juditBtn = p.remoteSensor[6].value;
juditLdr = p.remoteSensor[7].value;
if (jacquesBtn == 1.0)
{
p.analogWrite(9, 1);
println("off");
delay(500);
p.analogWrite(9, 0);
}
else if (jacquesBtn == 0.0)
{
if (jacquesLdr <= juditLdr jacquesLdr <= tapaniLdr jacquesLdr <= seamusLdr) { p.analogWrite(9, 180); println("on"); delay(500); p.analogWrite(9, 0); } else { println ("Sorry mate. You've got too much light already. HAHA!!"); } } lastUpdate = millis(); } } void onReceiveEEML(DataIn d) { p.updateRemoteSensors(d); }
Ver 3 - Bringing in Serial
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
boolean firstContact = false; // Whether we've heard from the microcontroller
boolean controlServo = false;
//setup for pachube recieving
import eeml.*;
DataIn dIn;
float myVariable;
//setup for pachube sending
DataOut dOut;
float lastUpdate;
int buttonState =0;
void setup() {
// List all the available serial ports
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
dIn = new DataIn(this,"http://www.pachube.com/api/feeds/7472.xml",
"API_KEY", 3500);
//dIn = new DataIn(this,"http://www.pachube.com/api/feeds/7584.xml",
//"API_KEY", 3500);
//dIn = new DataIn(this,"http://www.pachube.com/api/feeds/7339.xml",
//"API_KEY", 3500);
dOut = new DataOut(this, "http://www.pachube.com/api/feeds/7344.xml",
"API_KEY");
dOut.addData(2,"LDR");
}
void draw() {
background(255);
}
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// if you haven't heard from the microncontroller yet, listen:
if (firstContact == false) {
if (myString.equals("hello")) {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
//println(firstContact);
}
}
// if you have heard from the microcontroller, proceed:
else {
float recievedData = new Float(myVariable);
float myData = new Float(myString);
//println(recievedData);
//println(myData);
if (myData > recievedData){
myPort.write("B");
println("Seamus = " + recievedData);
println("Jacques = " + myData);
} else {
myPort.write("C");
println("Seamus = " + recievedData);
println("Jacques = " + myData);
}
//send data to pachube
if ((millis() - lastUpdate) > 7000){
println("ready to POST: ");
dOut.update(0, myString); // 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();
}
}
}
// when you've parsed the data you have, ask for more:
myPort.write("A");
}
void onReceiveEEML(DataIn d){
myVariable = d.getValue(2); // get the value of the stream 1
}
.
Final
This final LightShare code is based on Pachuino. It runs on the new Standard_Firmata, ver 2.1, code in Arduino 18. You must import 'pachuino', 'serial' and 'arduino' libraries into your processing sketchbook. The reason that we are able to use this code on pachube.com is the fact that we got whitelisted for the duration of the assignment. This meant that the data cap was removed. It is set up to listen for 1 set of local sensor data and 3 remote sensor feeds;
import processing.serial.*;
import eeml.*;
import cc.arduino.*;
import pachuino.*;
Pachuino p;
DataOut dOut;
float timer, scaledLDR;
float button;
float lastUpdate;
float jacquesLDR;
float tapaniLDR;
float seamusLDR;
float juditLDR;
PImage img;
void setup()
{
p = new Pachuino(this, Arduino.list()[0], 57600);
p.manualUpdate("http://www.pachube.com/api/7472.xml");
p.setKey("APIKEY_GOES_HERE");
dOut = new DataOut(this, "http://www.pachube.com/api/feeds/7472.xml",
"APIKEY_GOES_HERE");
dOut.addData(2,"CombinedLDR");
p.addRemoteSensor("http://www.pachube.com/api/7339.xml", 2); // tapani ldr
p.addRemoteSensor("http://www.pachube.com/api/7344.xml", 2); // jacques ldr
p.addRemoteSensor("http://www.pachube.com/api/7584.xml", 2); // judit ldr
p.addLocalSensor("analog", 1, "ldr");
p.addLocalSensor("analog", 2, "button");
size(600,581);
background(255);
}
void draw()
{
seamusLDR = p.localSensor[0].value;
button = p.localSensor[1].value;
tapaniLDR = p.remoteSensor[0].value;
jacquesLDR = p.remoteSensor[1].value;
juditLDR = p.remoteSensor[2].value;
dOut.update(0, timer);
int response = dOut.updatePachube();
if ((millis() - lastUpdate) > 5000)
{
lastUpdate = millis();
if (button > 10)
{
timer = timer + 1;
scaledLDR = seamusLDR/100;
timer = timer + scaledLDR;
delay(1000);
if (timer > jacquesLDR && timer > tapaniLDR && timer > juditLDR)
{
img = loadImage("mus.jpg");
image (img,0,0);
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 1);
println("off");
delay(500);
p.analogWrite(9, 0);
}
else if (jacquesLDR > timer && jacquesLDR > tapaniLDR && jacquesLDR > juditLDR)
{
img = loadImage("jaak.jpg");
image (img,0,0);
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 250);
println("on");
delay(500);
p.analogWrite(9, 0);
}
else if (juditLDR > timer && juditLDR > tapaniLDR && juditLDR > jacquesLDR)
{
img = loadImage("judit.jpg");
image (img,0,0);
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 250);
println("on");
delay(500);
p.analogWrite(9, 0);
}
else if (tapaniLDR > timer && tapaniLDR > juditLDR && tapaniLDR > jacquesLDR)
{
img = loadImage("tap.jpg");
image (img,0,0);
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 250);
println("on");
delay(500);
p.analogWrite(9, 0);
}
else
{
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 250);
println("on");
delay(500);
p.analogWrite(9, 0);
}
}
else
{
println("seamus = " + 0);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 1);
println("off");
delay(500);
p.analogWrite(9, 0);
}
}
}
void onReceiveEEML(DataIn d)
{
p.updateRemoteSensors(d);
}
Using this code meant that we could separate the data coming from each persons feed, to deal with them separately in the if and else statements.
import eeml.*;
import processing.serial.*;
import cc.arduino.*;
Arduino arduino;
float myVariable;
DataOut dOut;
float lastUpdate;
int buttonState = 0;
DataIn dIn;
void setup()
{
size (180, 50);
dIn = new DataIn(this,"http://www.pachube.com/api/feeds/7339.xml",
"API_KEY", 5000);
arduino = new Arduino(this, Arduino.list()[0]);
arduino.pinMode(10, Arduino.SERVO);
dOut = new DataOut(this, "http://www.pachube.com/api/feeds/7584.xml",
"API_KEY");
dOut.addData(2,"button");
arduino = new Arduino (this, Arduino.list()[0],57600);
arduino.pinMode(2,arduino.INPUT);
}
void draw()
{
if (myVariable == 0.0)
{
arduino.analogWrite(10, 0);
println("off");
}
else
{
println("on");
arduino.analogWrite(10, 180);
}
if ((millis() - lastUpdate) > 7000)
{
buttonState=arduino.digitalRead(2);
println("ready to POST: ");
dOut.update(0, buttonState);
int response = dOut.updatePachube();
println(response);
lastUpdate = millis();
}
}
void onReceiveEEML(DataIn d)
{
myVariable = d.getValue(2);
println(myVariable);
}
Ver 2
import eeml.*;
import processing.serial.*;
import cc.arduino.*;
import pachuino.*;
Pachuino p;
float lastUpdate;
float jacquesBtn;
float jacquesLdr;
float tapaniBtn;
float tapaniLdr;
float seamusBtn;
float seamusLdr;
float juditBtn;
float juditLdr;
void setup()
{
p = new Pachuino(this, Arduino.list()[0], 57600);
p.manualUpdate("http://www.pachube.com/api/7344.xml");
p.setKey("API_KEY");
p.addRemoteSensor("http://www.pachube.com/api/7344.xml", 0); // jacques button
p.addRemoteSensor("http://www.pachube.com/api/7344.xml", 2); // jacques ldr
p.addRemoteSensor("http://www.pachube.com/api/7339.xml", 2); // tapani button
p.addRemoteSensor("http://www.pachube.com/api/7339.xml", 3); // tapani ldr
p.addRemoteSensor("http://www.pachube.com/api/7472.xml", 2); // seamus button
p.addRemoteSensor("http://www.pachube.com/api/7472.xml", 3); // seamus ldr
p.addRemoteSensor("http://www.pachube.com/api/7472.xml", 0); // judit button
p.addRemoteSensor("http://www.pachube.com/api/7472.xml", 1); // judit ldr
p.addLocalSensor("digital", 2, "button");
//p.addLocalSensor("digital", 9, "servo");
p.addLocalSensor("analog", 3, "ldr");
}
void draw()
{
if ((millis() - lastUpdate) > 10000)
{
float lastUpdate;
jacquesBtn = p.remoteSensor[0].value;
jacquesLdr = p.remoteSensor[1].value;
tapaniBtn = p.remoteSensor[2].value;
tapaniLdr = p.remoteSensor[3].value;
seamusBtn = p.remoteSensor[4].value;
seamusLdr = p.remoteSensor[5].value;
juditBtn = p.remoteSensor[6].value;
juditLdr = p.remoteSensor[7].value;
if (jacquesBtn == 1.0)
{
p.analogWrite(9, 1);
println("off");
delay(500);
p.analogWrite(9, 0);
}
else if (jacquesBtn == 0.0)
{
if (jacquesLdr <= juditLdr jacquesLdr <= tapaniLdr jacquesLdr <= seamusLdr) { p.analogWrite(9, 180); println("on"); delay(500); p.analogWrite(9, 0); } else { println ("Sorry mate. You've got too much light already. HAHA!!"); } } lastUpdate = millis(); } } void onReceiveEEML(DataIn d) { p.updateRemoteSensors(d); }
Ver 3 - Bringing in Serial
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
boolean firstContact = false; // Whether we've heard from the microcontroller
boolean controlServo = false;
//setup for pachube recieving
import eeml.*;
DataIn dIn;
float myVariable;
//setup for pachube sending
DataOut dOut;
float lastUpdate;
int buttonState =0;
void setup() {
// List all the available serial ports
println(Serial.list());
myPort = new Serial(this, Serial.list()[0], 9600);
myPort.bufferUntil('\n');
dIn = new DataIn(this,"http://www.pachube.com/api/feeds/7472.xml",
"API_KEY", 3500);
//dIn = new DataIn(this,"http://www.pachube.com/api/feeds/7584.xml",
//"API_KEY", 3500);
//dIn = new DataIn(this,"http://www.pachube.com/api/feeds/7339.xml",
//"API_KEY", 3500);
dOut = new DataOut(this, "http://www.pachube.com/api/feeds/7344.xml",
"API_KEY");
dOut.addData(2,"LDR");
}
void draw() {
background(255);
}
void serialEvent(Serial myPort) {
// read the serial buffer:
String myString = myPort.readStringUntil('\n');
// if you got any bytes other than the linefeed:
if (myString != null) {
myString = trim(myString);
// if you haven't heard from the microncontroller yet, listen:
if (firstContact == false) {
if (myString.equals("hello")) {
myPort.clear(); // clear the serial port buffer
firstContact = true; // you've had first contact from the microcontroller
myPort.write('A'); // ask for more
//println(firstContact);
}
}
// if you have heard from the microcontroller, proceed:
else {
float recievedData = new Float(myVariable);
float myData = new Float(myString);
//println(recievedData);
//println(myData);
if (myData > recievedData){
myPort.write("B");
println("Seamus = " + recievedData);
println("Jacques = " + myData);
} else {
myPort.write("C");
println("Seamus = " + recievedData);
println("Jacques = " + myData);
}
//send data to pachube
if ((millis() - lastUpdate) > 7000){
println("ready to POST: ");
dOut.update(0, myString); // 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();
}
}
}
// when you've parsed the data you have, ask for more:
myPort.write("A");
}
void onReceiveEEML(DataIn d){
myVariable = d.getValue(2); // get the value of the stream 1
}
.
Final
This final LightShare code is based on Pachuino. It runs on the new Standard_Firmata, ver 2.1, code in Arduino 18. You must import 'pachuino', 'serial' and 'arduino' libraries into your processing sketchbook. The reason that we are able to use this code on pachube.com is the fact that we got whitelisted for the duration of the assignment. This meant that the data cap was removed. It is set up to listen for 1 set of local sensor data and 3 remote sensor feeds;
import processing.serial.*;
import eeml.*;
import cc.arduino.*;
import pachuino.*;
Pachuino p;
DataOut dOut;
float timer, scaledLDR;
float button;
float lastUpdate;
float jacquesLDR;
float tapaniLDR;
float seamusLDR;
float juditLDR;
PImage img;
void setup()
{
p = new Pachuino(this, Arduino.list()[0], 57600);
p.manualUpdate("http://www.pachube.com/api/7472.xml");
p.setKey("APIKEY_GOES_HERE");
dOut = new DataOut(this, "http://www.pachube.com/api/feeds/7472.xml",
"APIKEY_GOES_HERE");
dOut.addData(2,"CombinedLDR");
p.addRemoteSensor("http://www.pachube.com/api/7339.xml", 2); // tapani ldr
p.addRemoteSensor("http://www.pachube.com/api/7344.xml", 2); // jacques ldr
p.addRemoteSensor("http://www.pachube.com/api/7584.xml", 2); // judit ldr
p.addLocalSensor("analog", 1, "ldr");
p.addLocalSensor("analog", 2, "button");
size(600,581);
background(255);
}
void draw()
{
seamusLDR = p.localSensor[0].value;
button = p.localSensor[1].value;
tapaniLDR = p.remoteSensor[0].value;
jacquesLDR = p.remoteSensor[1].value;
juditLDR = p.remoteSensor[2].value;
dOut.update(0, timer);
int response = dOut.updatePachube();
if ((millis() - lastUpdate) > 5000)
{
lastUpdate = millis();
if (button > 10)
{
timer = timer + 1;
scaledLDR = seamusLDR/100;
timer = timer + scaledLDR;
delay(1000);
if (timer > jacquesLDR && timer > tapaniLDR && timer > juditLDR)
{
img = loadImage("mus.jpg");
image (img,0,0);
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 1);
println("off");
delay(500);
p.analogWrite(9, 0);
}
else if (jacquesLDR > timer && jacquesLDR > tapaniLDR && jacquesLDR > juditLDR)
{
img = loadImage("jaak.jpg");
image (img,0,0);
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 250);
println("on");
delay(500);
p.analogWrite(9, 0);
}
else if (juditLDR > timer && juditLDR > tapaniLDR && juditLDR > jacquesLDR)
{
img = loadImage("judit.jpg");
image (img,0,0);
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 250);
println("on");
delay(500);
p.analogWrite(9, 0);
}
else if (tapaniLDR > timer && tapaniLDR > juditLDR && tapaniLDR > jacquesLDR)
{
img = loadImage("tap.jpg");
image (img,0,0);
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 250);
println("on");
delay(500);
p.analogWrite(9, 0);
}
else
{
println("seamus = " + timer);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 250);
println("on");
delay(500);
p.analogWrite(9, 0);
}
}
else
{
println("seamus = " + 0);
println("jacques = " + jacquesLDR);
println("tapani = " + tapaniLDR);
println("judit = " + juditLDR);
p.analogWrite(9, 1);
println("off");
delay(500);
p.analogWrite(9, 0);
}
}
}
void onReceiveEEML(DataIn d)
{
p.updateRemoteSensors(d);
}
Using this code meant that we could separate the data coming from each persons feed, to deal with them separately in the if and else statements.
Below is the fritzing diagram of the circuit and an image of the finished switch box. In the final we used a Potpin instead of a toggle switch just to save a bit of cash. Worked nicely.
This unit fit over the lightswitch in my room. The servo inside physically switched my light on if I had the greatest light value.
No comments:
Post a Comment