Problems Using Arduino Mega with sensors and XBee

Hello everyone,

I am working on a home automation project. I am using the following components
-Arduino Mega 2560
-Two series 2 XBee radios configured as Router API, One series 2 Radio configured as Coordinator API
-2 TMP36 sensors plugged into Router XBee radios, acting as wireless sensor nodes

So for this project, I am trying to collect sensor information from TMP36, send this information to the coordinator connected to the Mega, and store this information in a MySQL database configured on another machine.

The problem I am facing is that I am able to connect to the MySQL database. However, I am not able to write the TMP36 sensor values into the database.

This is the puzzling part of it all
-I can read the TMP36 values via XBee nodes and display it on the Arduino serial monitor when using Mega using a separate code.
-I can read values and write into the MySQL database if I switch out the Mega with a Arduino Uno that I have. For the same code (with Rx and Tx pins assigned differently to the Mega of course).

This is my code to write sensor values in tmp36. Keep in mind that it works when I use a Uno instead of a Mega.

Sensor Networks Example Arduino Data Aggregate Node
This project demonstrates how to receive sensor data from
multiple XBee sensor nodes saving the samples in a MySQL
It uses an Arduino with an XBee shield with an XBee
coordinator installed.

#include <XBee.h>
#include <SoftwareSerial.h>
#include <Ethernet.h>
#include <SPI.h>
#include <sha1.h>
#include <mysql.h>
// Setup pin definitions for XBee shield
uint8_t recv = 10; // 8 if using a Leonardo
uint8_t trans = 11; // 9 if using a Leonardo
SoftwareSerial soft_serial(recv, trans);

// assign a MAC address and IP address for the Arduino
byte mac[] = {
0xDE, 0xAE, 0xBC, 0xEF, 0xFE, 0xEF};
IPAddress ip(10, 0, 0, 70);
IPAddress server_addr(10, 0, 0, 42);

// Instantiate an instance of the XBee library
XBee xbee = XBee();

// Instantiate an instance of the IO sample class
ZBRxIoSampleResponse ioSample = ZBRxIoSampleResponse();

/* Setup for the Connector/Arduino */
Connector my_conn; // The Connector/Arduino reference
char user[] = "root";
char password[] = "secret";

// 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) {
Serial.print("Received data from address: ");
address = (ioSample->getRemoteAddress64().getMsb() << 8) +
Serial.print(ioSample->getRemoteAddress64().getMsb(), HEX);
Serial.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

// 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)");

void setup() {
// start the SPI library:
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
while (!Serial); // Wait until Serial is ready - Leonardo
Serial.println("Connecting to MySQL...");
if (my_conn.mysql_connect(server_addr, 3306, user, password))
Serial.println("Connection failed.");

void loop() {
//attempt to read a packet
if (xbee.getResponse().isAvailable()) {
// got something
if (xbee.getResponse().getApiId() == ZB_IO_SAMPLE_RESPONSE) {
// Get the packet
// Get and store the data locally (in memory or on card?)
else {
Serial.print("Expected I/O Sample, but got ");
Serial.print(xbee.getResponse().getApiId(), HEX);
} else if (xbee.getResponse().isError()) {
Serial.print("Error reading packet. Error code: ");

Arduino serial monitor when using Mega displays that it connected to server but does not get the TMP6 values.
However, the Arduino UNO serial monitor displays the information correctly and writes into the MySQL database correctly.

IS there anyone who can help me figure this problem out?