We began experimenting using serial communication as a means of bypassing using Firmata and the technical issues we'd been having with it. I adapted a code implementing the 'handshake method' which works by sending one set of values at a time, rather than repeatedly. The receiving program has to request new data every time it finishes reading what it's got. We didn't need to do much to the original code, it was a matter of making telling Processing what to send back to Arduino and then telling Arduino how to respond to that. We implemented this simply at first communication within one computer.
The modified code is as follows:
Arduino Code:
/*
Sensor Reader
Language: Wiring/Arduino
Reads two analog inputs and one digital input
and outputs their values.
Connections:
analog sensors on analog input pins 0 and 1
switch on digital I/O pin 2
*/
int analogOne = 0; // analog input
int analogTwo = 1; // analog input
int digitalOne = 2; // digital input
int sensorValue = 0; // reading from the sensor
int servoControl;
#include
Servo myservo;
//int pos = 0;
void setup() {
// configure the serial connection:
Serial.begin(9600);
// configure the digital input:
pinMode(digitalOne, INPUT);
myservo.attach(9);
establishContact();
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
int inByte = Serial.read();
// read the sensor:
sensorValue = analogRead(analogOne);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");
// read the sensor:
sensorValue = analogRead(analogTwo);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");
// read the sensor:
sensorValue = digitalRead(digitalOne);
// print the last sensor value with a println() so that
// each set of four readings prints on a line by itself:
Serial.println(sensorValue, DEC);
if(inByte == 66){
myservo.write(180);
}
if(inByte == 67){
myservo.write(0);
}
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("hello"); // send a starting message
delay(300);
}
}
Processing Code:
/*
Serial String Reader
Language: Processing
Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.
Adapted from original code by Tom Igoe
*/
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
//float bgcolor; // Background color
boolean firstContact = false; // Whether we've heard from the microcontroller
boolean controlServo = false;
void setup() {
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
}
void draw() {
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
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 {
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum <>
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensors.length > 1) {
if (sensors[2] == 1){
myPort.write("B");
} else {
myPort.write("C");
}
}
}
// when you've parsed the data you have, ask for more:
myPort.write("A");
}
}
Following this, we went back to our first set up with me at one computer and Seamus on another one. We modified his code to, rather send the button state back to the Arduino to send it to his Pachube feed. We modified my code to rather than take the button data from my Arduino, to read his feed and send that value back to the servo on my Arduino, something like this:
It was a bit fiddly to paste the right pieces of code in the right place but we were successful after a few minor syntax errors. From here, we just did the same thing again but setting up both codes to send and receive data, something like this:
This is where we were a bit concerned about encounter clashes as we had when we were using Firmata but it all worked as we had hoped; my button controlled his servo and visa versa. Celebrations all around. The final code for processing (Arduino code remains unchanged) is as follows:
Processing Code:
/*
Serial String Reader
Language: Processing
Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.
Adapted from original code by Tom Igoe
*/
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());
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
// 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,"FEED BEING READ",
"API KEY", 7000);
// set up DataOut object; requires URL of the EEML you are updating, and your Pachube API key
dOut = new DataOut(this, "FEED BEING POSTED TO",
"API KEY");
dOut.addData(2,"button");
}
void draw() {
background(255);
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
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 {
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum <>
// print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
//println();
if (sensors.length > 1) {
if (myVariable == 1){
myPort.write("B");
} else {
myPort.write("C");
}
//send data to pachube
if ((millis() - lastUpdate) > 7000){
println("ready to POST: ");
dOut.update(0, sensors[2]); // 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 2
println(myVariable);
}
From here, we finally had the base mechanisms of our Smart System operational. With about two weeks left until deadline, we were forced to start considering exactly was was feasibly achievable to build and code with this. We had to starting considering the very real possibility of having to scale it back and having to figure out such a way to do it that wouldn't compromise what we were trying to achieve and show.
The modified code is as follows:
Arduino Code:
/*
Sensor Reader
Language: Wiring/Arduino
Reads two analog inputs and one digital input
and outputs their values.
Connections:
analog sensors on analog input pins 0 and 1
switch on digital I/O pin 2
*/
int analogOne = 0; // analog input
int analogTwo = 1; // analog input
int digitalOne = 2; // digital input
int sensorValue = 0; // reading from the sensor
int servoControl;
#include
Servo myservo;
//int pos = 0;
void setup() {
// configure the serial connection:
Serial.begin(9600);
// configure the digital input:
pinMode(digitalOne, INPUT);
myservo.attach(9);
establishContact();
}
void loop() {
if (Serial.available() > 0) {
// read the incoming byte:
int inByte = Serial.read();
// read the sensor:
sensorValue = analogRead(analogOne);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");
// read the sensor:
sensorValue = analogRead(analogTwo);
// print the results:
Serial.print(sensorValue, DEC);
Serial.print(",");
// read the sensor:
sensorValue = digitalRead(digitalOne);
// print the last sensor value with a println() so that
// each set of four readings prints on a line by itself:
Serial.println(sensorValue, DEC);
if(inByte == 66){
myservo.write(180);
}
if(inByte == 67){
myservo.write(0);
}
}
}
void establishContact() {
while (Serial.available() <= 0) {
Serial.println("hello"); // send a starting message
delay(300);
}
}
Processing Code:
/*
Serial String Reader
Language: Processing
Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.
Adapted from original code by Tom Igoe
*/
import processing.serial.*; // import the Processing serial library
Serial myPort; // The serial port
//float bgcolor; // Background color
boolean firstContact = false; // Whether we've heard from the microcontroller
boolean controlServo = false;
void setup() {
// List all the available serial ports
println(Serial.list());
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
}
void draw() {
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
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 {
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum <>
print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
println();
if (sensors.length > 1) {
if (sensors[2] == 1){
myPort.write("B");
} else {
myPort.write("C");
}
}
}
// when you've parsed the data you have, ask for more:
myPort.write("A");
}
}
Following this, we went back to our first set up with me at one computer and Seamus on another one. We modified his code to, rather send the button state back to the Arduino to send it to his Pachube feed. We modified my code to rather than take the button data from my Arduino, to read his feed and send that value back to the servo on my Arduino, something like this:
It was a bit fiddly to paste the right pieces of code in the right place but we were successful after a few minor syntax errors. From here, we just did the same thing again but setting up both codes to send and receive data, something like this:
This is where we were a bit concerned about encounter clashes as we had when we were using Firmata but it all worked as we had hoped; my button controlled his servo and visa versa. Celebrations all around. The final code for processing (Arduino code remains unchanged) is as follows:
Processing Code:
/*
Serial String Reader
Language: Processing
Reads in a string of characters from a serial port until
it gets a linefeed (ASCII 10). Then splits the string into
sections separated by commas. Then converts the sections to ints,
and prints them out.
Adapted from original code by Tom Igoe
*/
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());
// I know that the first port in the serial list on my mac
// is always my Arduino module, so I open Serial.list()[0].
// Change the 0 to the appropriate number of the serial port
// that your microcontroller is attached to.
myPort = new Serial(this, Serial.list()[0], 9600);
// read bytes into a buffer until you get a linefeed (ASCII 10):
myPort.bufferUntil('\n');
// 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,"FEED BEING READ",
"API KEY", 7000);
// set up DataOut object; requires URL of the EEML you are updating, and your Pachube API key
dOut = new DataOut(this, "FEED BEING POSTED TO",
"API KEY");
dOut.addData(2,"button");
}
void draw() {
background(255);
}
// serialEvent method is run automatically by the Processing applet
// whenever the buffer reaches the byte value set in the bufferUntil()
// method in the setup():
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 {
// split the string at the commas
// and convert the sections into integers:
int sensors[] = int(split(myString, ','));
// print out the values you got:
for (int sensorNum = 0; sensorNum <>
// print("Sensor " + sensorNum + ": " + sensors[sensorNum] + "\t");
}
// add a linefeed after all the sensor values are printed:
//println();
if (sensors.length > 1) {
if (myVariable == 1){
myPort.write("B");
} else {
myPort.write("C");
}
//send data to pachube
if ((millis() - lastUpdate) > 7000){
println("ready to POST: ");
dOut.update(0, sensors[2]); // 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 2
println(myVariable);
}
From here, we finally had the base mechanisms of our Smart System operational. With about two weeks left until deadline, we were forced to start considering exactly was was feasibly achievable to build and code with this. We had to starting considering the very real possibility of having to scale it back and having to figure out such a way to do it that wouldn't compromise what we were trying to achieve and show.






