Hey,
So i started using pins 18 and 19 for Tx and Rx. That seemed to solve the problem of not being able to get the data, however I can no longer view the output on the serial monitor. Also, the code sometimes works and sometimes doesn't. Which is bizarre. I am unable to understand why, I will post the full code here. Kindly let me know if you can help.
#include <SPI.h>
#include <Ethernet.h>
#include <sha1.h>
#include <mysql.h>
#include <DHT22.h>
#include <XBee.h>
#include <SoftwareSerial.h>
//// Setup pin definitions for XBee shield
uint8_t recv = 18; // 8 if using a Leonardo
uint8_t trans = 19; // 9 if using a Leonardo
////SoftwareSerial soft_serial(recv, trans);
//Define DHT22 pins
#define DHT22_PIN 7 // DHT22 data is on pin 7
#define read_delay 5000 // 5 seconds
DHT22 myDHT22(DHT22_PIN); // DHT22 instance
// assign a MAC address and IP address for the Arduino
byte mac[] = {
0xDE, 0xAD, 0xBC, 0xDE, 0xFE, 0xED};
IPAddress ip(10, 0, 0, 111);
IPAddress server_addr(10, 0, 0, 42);
//MySQL credentials
char user[] = "root";
char password[] = "secret";
Connector my_conn; // The Connector/Arduino reference
// Instantiate an instance of the XBee library
XBee xbee = XBee();
// Instantiate an instance of the IO sample class
ZBRxIoSampleResponse ioSample = ZBRxIoSampleResponse();
// Sample data values
unsigned int address; // Last 4 digits of XBee address
float temperature; // Raw temperature value
float voltage; // Reference voltage
// Get sample data
void get_sample_data(ZBRxIoSampleResponse *ioSample) {
Serial1.print("Received data from address: ");
address = (ioSample->getRemoteAddress64().getMsb() << 8) +
ioSample->getRemoteAddress64().getLsb();
Serial1.print(ioSample->getRemoteAddress64().getMsb(), HEX);
Serial1.println(ioSample->getRemoteAddress64().getLsb(), HEX);
temperature = ioSample->getAnalog(3);
int ref = xbee.getResponse().getFrameData()[17] << 8;
ref += xbee.getResponse().getFrameData()[18];
voltage = (float(ref) * float(1200.0 / 1024.0))/1000.0;
}
// Record a sample
void record_sample(ZBRxIoSampleResponse *ioSample) {
int saved_addr;
// Get sample data from XBee
get_sample_data(ioSample);
// Send data to MySQL
String query("INSERT INTO house.temperature VALUES(NULL, '");
String addr(address, HEX);
char buff[40];
query += addr;
query += ("', '");
query += dtostrf(temperature, 4, 4, buff);
query += ("', '");
query += dtostrf(voltage, 4, 4, buff);
query += ("', NULL, NULL)");
Serial1.println(&query[0]);
my_conn.cmd_query(&query[0]);
}
void read_data() {
DHT22_ERROR_t errorCode;
errorCode = myDHT22.readData();
switch(errorCode)
{
case DHT_ERROR_NONE:
char buf[128];
sprintf(buf, "INSERT INTO dht22_test.temp_humid VALUES (NULL, %hi.%01hi, %i.%01i)",
myDHT22.getTemperatureCInt()/10,
abs(myDHT22.getTemperatureCInt()%10),
myDHT22.getHumidityInt()/10,
myDHT22.getHumidityInt()%10);
my_conn.cmd_query(buf);
Serial1.println("Data read and recorded.");
break;
case DHT_ERROR_CHECKSUM:
Serial1.print("check sum error ");
Serial1.print(myDHT22.getTemperatureC());
Serial1.print("C ");
Serial1.print(myDHT22.getHumidity());
Serial1.println("%");
break;
case DHT_BUS_HUNG:
Serial1.println("BUS Hung ");
break;
case DHT_ERROR_NOT_PRESENT:
Serial1.println("Not Present ");
break;
case DHT_ERROR_ACK_TOO_LONG:
Serial1.println("ACK time out ");
break;
case DHT_ERROR_SYNC_TIMEOUT:
Serial1.println("Sync Timeout ");
break;
case DHT_ERROR_DATA_TIMEOUT:
Serial1.println("Data Timeout ");
break;
case DHT_ERROR_TOOQUICK:
Serial1.println("Polled too quick ");
break;
}
}
void setup() {
// start the SPI library:
SPI.begin();
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
Serial1.begin(9600);
while (!Serial1); // Wait until Serial is ready - Leonardo
//soft_serial.begin(9600);
xbee.setSerial(Serial1);
Serial1.println("Connecting to MySQL...");
if (my_conn.mysql_connect(server_addr, 3306, user, password))
{
delay(500);
Serial1.println("Success!");
}
else
{
Serial1.println("Connection failed.");
}
}
void loop()
{
//attempt to read a packet-Temp Sensors
xbee.readPacket();
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == ZB_IO_SAMPLE_RESPONSE) {
// Get the packet
xbee.getResponse().getZBRxIoSampleResponse(ioSample);
// Get and store the data locally (in memory or on card?)
record_sample(&ioSample);
}
else {
Serial1.print("Expected I/O Sample, but got ");
Serial1.print(xbee.getResponse().getApiId(), HEX);
}
} else if (xbee.getResponse().isError()) {
Serial1.print("Error reading packet. Error code: ");
Serial1.println(xbee.getResponse().getErrorCode());
}
//DHT22
delay(read_delay);
read_data();
}
So since pins 18 and 19 correspond to Seria1. I have initialized it and used it as such everywhere.