Arduino Noob : Problems using Mega and XBee

Hello Everyone,

I am in a huge pickle with this sensor network project I am working on. I wanted to use the Mega because my code and libraries require more memory. I have 3 XBee radios in my network, 2 of them configured as router API's and another configured as a coordinator API. I am trying to collect temperature data from the two routers and send them to Arduino via a router.

When I try reading the sensor values and displaying it on the serial monitor it seems to work fine. However, using the Mega I am unable to do the same.

So my question is, when using the Mega2560, the serial Rx and Tx pins I have setup is 10 and 11 when uploading the sketch. Is this a mistake I am doing? Because on the Uno the Rx and Tx pins I was using was 2 and 3..

So my question is, when using the Mega2560, the serial Rx and Tx pins I have setup is 10 and 11 when uploading the sketch. Is this a mistake I am doing? Because on the Uno the Rx and Tx pins I was using was 2 and 3..

If you look closely at the UNO PINOUT attached you will see Rx & Tx are pins 0 & 1, not 2 & 3.
If you look at the Mega , you will see they are the same. In addition the mega has 3 more serial ports on pins 19 through 14.

Hey,

I no that 0 and 1 are used in Uno and Same for Mega. However, I have read that these pins cannot be used while uploading the sketch and hence other pins can be configured to do the same task. This is what I found on the Arduino website.

The SoftwareSerial library has been developed to allow serial communication on other digital pins of the Arduino, using software to replicate the functionality (hence the name "SoftwareSerial"). It is possible to have multiple software serial ports with speeds up to 115200 bps. A parameter enables inverted signaling for devices which require that protocol.
The version of SoftwareSerial included in 1.0 and later is based on the NewSoftSerial library by Mikal Hart.
Limitations

The library has the following known limitations:
If using multiple software serial ports, only one can receive data at a time.
Not all pins on the Mega and Mega 2560 support change interrupts, so only the following can be used for RX: 10, 11, 12, 13, 14, 15, 50, 51, 52, 53, A8 (62), A9 (63), A10 (64), A11 (65), A12 (66), A13 (67), A14 (68), A15 (69).
Not all pins on the Leonardo support change interrupts, so only the following can be used for RX: 8, 9, 10, 11, 14 (MISO), 15 (SCK), 16 (MOSI).

This is my code where I define the Tx and RX pins for the Mega.

// Setup pin definitions for XBee shield
uint8_t recv = 10; // or 8 if using a Leonardo
uint8_t trans = 11; // or 9 if using a Leonardo
SoftwareSerial soft_serial(recv, trans);

Why in the world would you even think about using Software Serial when you have FOUR SERIAL PORTS ?!
Did you not read this ?

In addition the mega has 3 more serial ports on pins 19 through 14.

Do you understand what that means ? (THREE EXTRA SERIAL PORTS) (so why would you need Software Serial ?)

Thank you very much for pointing that out.

So instead of using softwareserial soft_serial(recv, trans) how do I alter the code to use these serial ports?

I really appreciate your help

Look ay Xbee Library to see if there is a Pinouts file.

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.