Sunday 5 February 2012

Pachube, Firmata Solutions & Multiple feeds

I've been having some trouble this week getting processing and arduino to talk to eachother using the firmata library. The sketch I used to respond to existing pachube feeds was having no LED response as it was meant to, although it was working and was having a visual processing output which i manipulated. I assessed that this was a problem with the code although i could not work out what it was.

I firstly  ran the 'Serial avaliable' sample code in processing to determine with serial port to use. http://processing.org/reference/libraries/serial/Serial_available_.html I discovered this function through looking at the processing reference files. Port 0 COM3 was being used so i then ran the sample 'arduino_output' sketch after uploading the standard firmata code to the arudino board. Clicking on the corresponding square lit up the LED, which i videoed below. So, there is definately a problem with the example sketch code rather than the wiring.



It is a problem with the analogWrite command: as confirmed by this forum http://forum.processing.org/topic/sending-analogwrite-to-arduino-via-processing. I wrote some new code for testing the LED functionality. The LED on the arduino board now will light up with digitalWrite (which sets pins to HIGH and LOW)

I used this code: arduino.digitalWrite(9, Arduino.HIGH); I also had to add this line of code: arduino.pinMode(9, Arduino.OUTPUT); which wasnt in the pachube example code, but is necessary for it to work.

For some reason analogWrite doesnt. StandardFirmata doesnt handle analog commands well according to the comment on the processing forums below:

'I've found the StandardFirmata does not handle analogWrite very well for some reason. Try uploading the analog Firmata and see if that gets the motors turning. '


I uploaded the 'simple analog firmata' and this worked extremely well. The LED is lighting up to different levels of brightness. I firstly used this code:      arduino.analogWrite(9,30); for dull brightness, but then i used this code:      arduino.analogWrite(9,myValue/5); 

The LED lights up corresponding to the remote pachube sensor value. The LED is no longer burning out: this is unexplained, but i believe it was because the board was providing unlimited current to the LED without a limiting resistor.

This is the final code at this point: 

import processing.serial.*;
import cc.arduino.*;

import eeml.*;

Arduino arduino;
int myValue;

DataIn dIn;

void setup()
{
size(255,255);
println(Arduino.list());
arduino = new Arduino(this, Arduino.list()[0], 115200);
dIn = new DataIn(this, "http://pachube.com/api/1228.xml", "UNIQUE KEY REMOVED", 5000);
 arduino.pinMode(9, Arduino.OUTPUT);

}

void draw()
{
println(myValue);

arduino.digitalWrite(9, Arduino.HIGH);
     arduino.analogWrite(9,myValue/5);


ellipse(125, 125, myValue/2, myValue/2);

if(myValue>149){
  fill(204, 102, 0);
  ellipse(125, 125, 20, 20);
}
  
}

void onReceiveEEML(DataIn d){
float remoteValue = d.getValue(0);
myValue = int(remoteValue) / 300;
}

Now i can work on code to execute functions when the sensor value is above a certain level i.e.

if(myValue<151){
  fill(204, 102, 0);
  ellipse(125, 125, 20, 20);
  println("Sally is ready to interact");

So, if the remote sensor was a light sensor and the person sat on it (changing the value), it would send a message to a read out LCD screen which may say something like 'Sally is ready to interact'. 

I then tried to get multiple feeds working. The first problem was getting the two feeds to work. Just adding in an extra DataIn object and an extra line of code did not work as expected: it kept switching between the two feeds. I found this solution which allows processing to more stably handle both: http://community.pachube.com/node/376

By using this construct: 

void setup(){

dIn1 = new DataIn(...);
dIn2 = new DataIn(...);

}

void onReceiveEEML(DataIn d){

if (d == dIn1) {
//do something
}

else if (d == dIn2){
// do something else
}

}

processing can handle both. If they were put into an 'if' loop, processing could potentially handle many more. Processing, however, require a computer and in the final build i will need to connect to Pachube or the internet directly using an Ethernet shield and execute code directly from the board.

Testing so far has been extremely useful in helping me to understand remote sensing and connectivity over the internet.

I believe that the next step in the project will be to buy an ethernet shield and start experimenting with it, as well as looking into keys and interfacing with hardware.

No comments:

Post a Comment