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
database.
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) +
ioSample->getRemoteAddress64().getLsb();
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
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)");
Serial.println(&query[0]);
my_conn.cmd_query(&query[0]);
}

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

void loop() {
//attempt to read a packet
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 {
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: ");
Serial.println(xbee.getResponse().getErrorCode());
}
}

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?