Arduino Mega and Xbee HELP

Good day, everyone! I have been doing a project using ARduino Mega (with WiFi module) and Xbee and ultrasonic sensor. My problem is my data from the ultrasonic sensor. What type of data am I getting in the xbee from my ultrasonic sensor? I am in AT Command in xbee and i can read the data from the ultrasonic (with arduino nano) to my serial monitor in Arduino Mega with xbee. I can get distance reading real time. But since i am trying to send this to my database using wifi module, I dont know what to type in my code to get the reading from the xbee that is connected to my Arduino Mega. The data is transferred between the 2 xbees, right? How can i get this data to be send to my database. Your help will be much appreciated. Thanks!


Post your code. Then we can talk.

BTW, can you restate your question and your project description? Your post is very unclear.

Post your code. Then we can talk.

BTW, can you restate your question and your project description? Your post is very unclear.

Hello thanks for your reply. My problem is that i dont know how to get the data from the xbee to be sent to my database using the esp8266. i have a device (ultrasonic, arduino nano, xbee) sending readings to another xbee that is connected to arduino mega and esp8266. I can see the readings in serial monitor of arduino mega. Now, I tried programing the arduino mega to get the xbee reading as input to be sent to my server. But when i run the program, it seems that it is getting no data. here is my code:

#include <SoftwareSerial.h>
String ssid = "___";
String password = "___";

SoftwareSerial esp(10, 11);// RX, TX
SoftwareSerial XBee(17, 16);//RX, TX

String data;
String server = "_____"; //
String uri = "arduinotry.php";// our example is /esppost.php
int sens1 , sens2;
int datasens1;

void setup() {



//reset esp8266 module

void reset() {

  if (esp.find("OK") ) Serial.println("Module Reset");


//connect to wifi network

void connectWifi() {

  String cmd = "AT+CWJAP=\"" + ssid + "\",\"" + password + "\"";
  if (esp.find("OK")) {


  else {


    Serial.println("Cannot connect to wifi");


void loop () {

  if (XBee.available()) {
  datasens1 =;
  sens1 = datasens1 ;

  data = "&waterlvl=" + sens1;// data sent must be under this form //name1=value1&name2=value2.




void httppost () {

  esp.println("AT+CIPSTART=\"TCP\",\"" + server + "\",80");//start a TCP connection.

  if ( esp.find("OK")) {

    Serial.println("TCP connection ready");

  } delay(1000);

  String postRequest =

    "POST " + uri + " HTTP/1.0\r\n" +

    "Host: " + server + "\r\n" +

    "Accept: *" + "/" + "*\r\n" +

    "Content-Length: " + data.length() + "\r\n" +

    "Content-Type: application/x-www-form-urlencoded\r\n" +

    "\r\n" + data;

  String sendCmd = "AT+CIPSEND=";//determine the number of characters to be sent.


  esp.println(postRequest.length() );


  if (esp.find(">")) {
    Serial.println("Sending.."); esp.print(postRequest);

    if ( esp.find("SEND OK")) {
      Serial.println("Packet sent");

      while (esp.available()) {

        String sensdat1 = esp.readString();



      // close the connection




It is not a good idea to use the String (capital S) class on an Arduino as it can cause memory corruption in the small memory on an Arduino. Just use cstrings - char arrays terminated with 0.

For receiving the data from the XBee have a look at the examples in Serial Input Basics - simple reliable ways to receive data.