Sending data from DHT11 to Xively using Xbees [SOLVED]
hello,
i trying send temperature , humidity data dht11 sensor between 2 arduino mega 2560's using xbees. the code transmitter follows
the code receiving arduino follows:-
i can send data xively directly arduino ok , can send , receive temperature , humidity data between 2 arduino's via xbees ok can't seem integrate serial communication between arduino's xively. whenever try cannot temperature , humidity readings. if have @ code , suggest going wrong appreciated.
kind regards
i trying send temperature , humidity data dht11 sensor between 2 arduino mega 2560's using xbees. the code transmitter follows
code: [select]
#include "dht.h"
#define dhtpin 2
#define dhttype dht11
dht dht(dhtpin, dhttype);
void setup()
{
serial.begin(9600);
}
void loop()
{
float h = dht.readhumidity();
float t = dht.readtemperature();
serial.print("<t:");
serial.print(t);
serial.print(">");
delay(2000);
serial.print("<h:");
serial.print(h);
serial.print(">");
delay(2000);
}
the code receiving arduino follows:-
code: [select]
#include <spi.h>
#include <ethernet.h>
#include <httpclient.h>
#include <xively.h>
// mac address ethernet shield
byte mac[] = { mac address };
int hum;
int temp;
char indata[24]; // reading serial tx
byte index; // reading serial tx
boolean started = false; // reading serial tx
boolean ended = false; // reading serial tx
// xively key let upload data
char xivelykey[] = "xivelykey";
// analog pin we're monitoring (0 , 1 used ethernet shield)
// define strings our datastream ids
char sensorid[] = "humidity";
char sensorid2[] = "temperature";
const int buffersize = 140;
char buffervalue[buffersize]; // enough space store string we're going send
xivelydatastream datastreams[] = {
xivelydatastream(sensorid, strlen(sensorid), datastream_float),
xivelydatastream(sensorid2, strlen(sensorid2), datastream_float),
};
// finally, wrap datastreams feed
xivelyfeed feed(xxxxxx, datastreams, 2);/* number of datastreams */
ethernetclient client;
xivelyclient xivelyclient(client);
void setup() {
// put setup code here, run once:
serial.begin(9600);
serial.println("starting multiple datastream upload xively...");
serial.println();
while (ethernet.begin(mac) != 1)
{
serial.println("error getting ip address via dhcp, trying again...");
delay(15000);
}
}
void loop() {
//start of code read transmitter
while(serial.available() > 0)
{
char achar = serial.read();
if(achar == '<')
{
started = true;
index = 0;
indata[index] = '\0';
}
else if(achar == '>')
{
ended = true;
}
else if(started)
{
indata[index] = achar;
index++;
indata[index] = ',';
}
}
if(started && ended)
{
// use value
if(indata[0] == 't')
{
indata[0] = ' ';
int temp = atoi(indata);
serial.println(" ");
serial.print("temp:");
serial.print(indata);
serial.print("c");
serial.println(" ");
}
else if(indata[0] == 'h')
{
indata[0] = ' ';
int hum = atoi(indata);
serial.println(" ");
serial.print("humidity:");
serial.print(indata);
serial.print("%");
serial.println(" ");
}
started = false;
ended = false;
index = 0;
indata[index] = '\0';
}
//end of code read transmitter
datastreams[0].setfloat(temp);
datastreams[1].setfloat(hum);
serial.print("read sensor value temp ");
serial.println(datastreams[0].getfloat());
serial.print("read sensor value hum ");
serial.println(datastreams[1].getfloat());
serial.println("uploading xively");
int ret = xivelyclient.put(feed, xivelykey);
serial.print("xivelyclient.put returned ");
serial.println(ret);
serial.println();
delay(15000);
}
i can send data xively directly arduino ok , can send , receive temperature , humidity data between 2 arduino's via xbees ok can't seem integrate serial communication between arduino's xively. whenever try cannot temperature , humidity readings. if have @ code , suggest going wrong appreciated.
kind regards
the network code should called when started==true && ended == true;
if see correctly every loop tries t send data xively.
you can improve code defining separate function separate steps.
code: (not tested) [select]
void handleincomingserial()
{
//start of code read transmitter
while(serial.available() > 0)
{
char achar = serial.read();
if(achar == '<')
{
started = true;
index = 0;
indata[index] = '\0';
}
else if(achar == '>')
{
ended = true;
}
else if(started)
{
indata[index] = achar;
index++;
indata[index] = ',';
}
}
}
void printvalues()
{
// use value
if(indata[0] == 't')
{
indata[0] = ' ';
int temp = atoi(indata);
serial.println(" ");
serial.print("temp:");
serial.print(indata);
serial.print("c");
serial.println(" ");
}
else if(indata[0] == 'h')
{
indata[0] = ' ';
int hum = atoi(indata);
serial.println(" ");
serial.print("humidity:");
serial.print(indata);
serial.print("%");
serial.println(" ");
}
}
void sendotoxively()
{
datastreams[0].setfloat(temp);
datastreams[1].setfloat(hum);
serial.print("read sensor value temp ");
serial.println(datastreams[0].getfloat());
serial.print("read sensor value hum ");
serial.println(datastreams[1].getfloat());
serial.println("uploading xively");
int ret = xivelyclient.put(feed, xivelykey);
serial.print("xivelyclient.put returned ");
serial.println(ret);
serial.println();
}
void loop()
{
handleincomingserial();
if(started && ended)
{
printvalues();
sendotoxively();
started = false;
ended = false;
index = 0;
indata[index] = '\0';
}
delay(15000);
}
Arduino Forum > Using Arduino > Networking, Protocols, and Devices (Moderator: fabioc84) > Sending data from DHT11 to Xively using Xbees [SOLVED]
arduino
Comments
Post a Comment