I am very pleased to say that I have changed the reciever sketch and can now send temperature and humidity readings to Xively via Xbees (Pro S1) .
The code below is my latest version of the reciever code and although very clumsy it works for the moment. Using Serial.parse() and making sure my coding syntax, particularly the placement of curly brackets was correct seemed to help. However I have learned much and hope to write a more efficient version soon.
Next step is to test the range of the XBees. Am hoping to get about 500 metres, line of sight, outdoors.
#include <SPI.h>
#include <Ethernet.h>
#include <HttpClient.h>
#include <Xively.h>
unsigned long lastConnectionTime = 0; // last time we connected to Xively
const unsigned long connectionInterval = 15000; // delay between connecting to Xively in milliseconds
// MAC address for your Ethernet shield
byte mac[] = {
XXXXXXXXXXX };
char inData[24]; // for reading serial from tx
byte index; // for reading serial from tx
boolean started = false; // for reading serial from tx
boolean ended = false; // for reading serial from tx
// Your Xively key to let you upload data
char xivelyKey[] = "XXXXXXXXXXXXXXXXXXX";
// Define the strings for our datastream IDs
char dht11temp[] = "temperature";
char dht11hum[] = "humidity";
const int bufferSize = 140;
char bufferValue[bufferSize]; // enough space to store the string we're going to send
XivelyDatastream datastreams[] = {
XivelyDatastream(dht11temp, strlen(dht11temp), DATASTREAM_FLOAT),
XivelyDatastream(dht11hum, strlen(dht11hum), DATASTREAM_FLOAT),
};
XivelyFeed feed(XXXXXXXX, datastreams, 2 /* number of datastreams */);
EthernetClient client;
XivelyClient xivelyclient(client);
void setup() {
// put your setup code here, to run once:
Serial.begin(9600);
Serial.println("Starting multiple datastream upload to Xively...");
Serial.println();
while (Ethernet.begin(mac) != 1)
{
Serial.println("Error getting IP address via DHCP, trying again...");
delay(15000);
}
}
void loop()
{
// main program loop
if (millis() - lastConnectionTime > connectionInterval)
{
Serial.flush();
while (Serial.available() == 0);
{
char ch = Serial.read();
if(ch == 'T') // Wait for the capital T.
{
long t = Serial.parseInt(); //Temperature
datastreams[0].setFloat(t); // The maths is there to convert mV into V.
Serial.print("Read Temperature ");
Serial.println(datastreams[0].getFloat());
Serial.println("Uploading it to Xively");
int ret = xivelyclient.put(feed, xivelyKey);
Serial.print("xivelyclient.put returned ");
Serial.println(ret);
Serial.println();
lastConnectionTime = millis();
}
else if (ch == 'H')
{
long h = Serial.parseInt(); //Humidity
datastreams[1].setFloat(h); // The maths is there to convert mV into V.
Serial.print("Read Humidity ");
Serial.println(datastreams[1].getFloat());
Serial.println("Uploading it to Xively");
int ret = xivelyclient.put(feed, xivelyKey);
Serial.print("xivelyclient.put returned ");
Serial.println(ret);
Serial.println();
lastConnectionTime = millis();
}
}
}
}