I’ve been messing about with Arduino derived micro controllers, and have been looking for a way to get them to send simple messages to and from each other. Something simpler than TCP/IP (which requires special hardware and relatively large libraries to run it), but more generic and flexible than just connecting an output pin on one device to the input pin on another.
aProtocol (or LLAP as it’s also known) as defined on OpenKontrol seems to fill that gap just nicely. So, in 16 lines of code, I got a simple acknowledgement working;
String msg;
String reply;
void setup() {
Serial.begin(9600);
Serial.print("aDDSTARTED--");
}
void loop(){
if (Serial.available() >= 12){
if (Serial.read() == 'a') {
if (Serial.read() == 'D' && Serial.read() == 'D'){
msg = "";
for (byte i=0; i<9; i++) msg += Serial.read();
if (msg.compareTo("HELLO----")) reply = "aDDHELLO----";
// else if ...
Serial.print(reply);
} } } }
Great! I type a 'hello' message on the serial port, and I get one back! Very polite, but kind of limited. What I would like to do is query it for the temperature of a sensor. I've been playing about with Dallas 1-wire sensors recently as they're easy to use, cheap, and give pretty accurate results. Here's a bit of code that will respond to a query of temperature sensor A with the temperature in degrees C;
#include <DallasTemperature.h>
#include <OneWire.h>
OneWire oneWire(7);
DallasTemperature sensors(&oneWire);
String msg;
String reply;
void setup() {
Serial.begin(9600);
sensors.begin();
Serial.print("aDDSTARTED--");
}
void loop() {
if (Serial.available() >= 12) {
if (Serial.read() == 'a') {
if (Serial.read() == 'D' && Serial.read() == 'D') {
msg = "";
for (byte i=0; i<9; i++) msg += (char)Serial.read();
if (msg.compareTo("HELLO----") == 0) reply = "aDDHELLO----";
else if (msg.compareTo("TMPA-----") == 0) {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
reply = "aDDTMPA";
reply += (int)tempC;
reply += ".";
reply += (int)((tempC - (int) tempC) *10);
reply += "----";
reply = reply.substring(0,12);
}
Serial.print(reply);
} } } }
The wiring is pretty simple. A 4.7k resistor between +5v and Arduino digital pin 7, pin 7 to sensor pin 2, and Arduino ground to sensor pin1.
Now, in addition to sending the hello message and being acknowledged, I can send one saying "aDDTMPA-----" which gets a response with the temperature such as "aDDTMPA17.4-". The dashes are there to pad out the messages to 12 characters.
All good so far, but what about using more than one sensor. Simple. The second sensor is wired in parallel with the first, and I extended the loop() to allow for sensor B like this;
void loop() {
if (Serial.available() >= 12) {
if (Serial.read() == 'a') {
if (Serial.read() == 'D' && Serial.read() == 'D') {
msg = "";
for (byte i=0; i<9; i++) msg += (char)Serial.read();
if (msg.compareTo("HELLO----") == 0) reply = "aDDHELLO----";
else if (msg.compareTo("TMPA-----") == 0) {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(0);
reply = "aDDTMPA";
reply += (int)tempC;
reply += ".";
reply += (int)((tempC - (int) tempC) *10);
reply += "----";
reply = reply.substring(0,12);
}
else if (msg.compareTo("TMPB-----") == 0) {
sensors.requestTemperatures();
float tempC = sensors.getTempCByIndex(1);
reply = "aDDTMPB";
reply += (int)tempC;
reply += ".";
reply += (int)((tempC - (int) tempC) *10);
reply += "----";
reply = reply.substring(0,12);
}
Serial.println(reply);
} } } }
So I can now send a query for either temperature sensor with "aDDTMPA-----" or "aDDTMPB-----"
From here I went on to add the cheap wireless transmitter I bought from eBay, and got it to broadcast each of the two temperatures every 3 seconds. A second Arduino was connected up to the receiver and a LCD display that displayed both temperatures. The code needs a bit of tidying up, but here's a quick photo of the end result