Home  >  Support  >  Support Forum  >  Can Xbee wifi models be connected through hardware serial ports of the Arduino Mega

Can Xbee wifi models be connected through hardware serial ports of the Arduino Mega

0 votes
I am trying to connect the wifi model to the Serial1 port of the mega and its not responding. However it works with a software serial port. Is it normal?
asked Jul 2, 2014 in IEEE 802.11 by arif New to the Community (2 points)
I feel, the Xbee module has some sort of buffer size limit ( although data sheet says its 2083 bytes, and my total packet is 309 bytes). I am using UART.

Here is the data sent from the server ( got from wireshark)

Code:
HTTP/1.1 200 OK
Date: Wed, 02 Jul 2014 20:36:29 GMT
Server: Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11
X-Powered-By: PHP/5.5.11
Content-Length: 31
Connection: close
Content-Type: text/html
Arif, 0, 61, 0, 34, 0, 21, 0,

However, using a little delay after GET request, I consistently am receiving a part of it only :
Code:
HTTP/1.1 200 OK
Date: Thu, 03 Jul 2014 02:38:56 GMT
Server: A


This is consistent and I am getting this everytime (with change in timestamp).

Here is the code segment :
Code:
void loop()
{
  if (millis() > (lastUpdate + UPDATE_RATE))
  {
    xB.print(phant.get());
    delay(3000);
    lastUpdate=millis();
  
   while(xB.available())
         {
           c=char(xB.read());
           delay(1);
          Serial.print(c);
         }
     }
}

 Any input will help, again. Thanks.
Could you give a more complete code? Your 3 second delay may have something to do with the partial response. Arduino MEGA has 63 bytes of serial buffer. It implements the wine-rule. Any new content is discarded after the buffer is full. Count your characters, I got 61 but I don't think all the \r or \n are properly preserved on a forum post. I don't know about the software serial case or its buffer. You have to dig in arduino softwareserial port source code to answer that.

Please log in or register to answer this question.

4 Answers

0 votes
what are you trying to issue to the radio? what is the exact data you are sending out the serial port of the arduino? What is the radio connected to? is it connected direct or via a daughter board?
answered Jul 2, 2014 by mvut Veteran of the Digi Community (3,341 points)
0 votes
I am trying to receive data from a server with http GET.

this is exactly what the server is sending :

HTTP/1.1 200 OK
Date: Wed, 02 Jul 2014 20:36:29 GMT
Server: Apache/2.4.9 (Win32) OpenSSL/1.0.1g PHP/5.5.11
X-Powered-By: PHP/5.5.11
Content-Length: 31
Connection: close
Content-Type: text/html
Arif, 0, 61, 0, 34, 0, 21, 0,

The radio is Xb-wifi sb6. and I am connected directly to the mega without any daughter board.

My code works when I use softwareserial. however i can't make it work in the hardware serial ports.
answered Jul 2, 2014 by arif New to the Community (2 points)
0 votes
If you think removing the delay 3000 solves your problem, mark THIS as answer. Forgot how this forum works.

Could you give a more complete code? Your 3 second delay may have something to do with the partial response. Arduino MEGA has 63 bytes of serial buffer. It implements the wine-rule. Any new content is discarded after the buffer is full. Count your characters, I got 61 but I don't think all the \r or \n are properly preserved on a forum post. I don't know about the software serial case or its buffer. You have to dig in arduino softwareserial port source code to answer that.
answered Aug 22, 2014 by liudr New to the Community (41 points)
0 votes
I could connect it later using the hardware serial port. much better i would say. with software serial port, the buffer gets full really fast (as you mentioned). Thanks for the reply liudr.
answered Aug 22, 2014 by arif New to the Community (2 points)
Hello arif!
Can you please help me with the xctu configurations on xbee S6B?
I can ping the module but can't send a post message to the data.sparkfun data base :(

Thank you for you help!
Sure. Can you post the code ? Are you using the phant library ?
#include <SoftwareSerial.h>
#include <Phant.h>

#define COMMAND_TIMEOUT 10000         // ms
#define DEBUG FALSE

// Phant Stuff
//String destIP = "54.86.132.254";     // data.sparkfun.com's IP address
//String publicKey = "xxx";
//String privateKey = "yyy";

String destIP = "54.86.132.254";     // data.sparkfun.com's IP address
String publicKey = "6Jd3E9WmQ6hg22VGDy31";
String privateKey = "WwVmAg2J8XU4WWYZo9l6";

Phant phant("data.sparkfun.com", publicKey, privateKey);
const String FieldOne = "temp";

const byte XB_RX = 2;                // XBee's RX (Din) pin
const byte XB_TX = 3;                // XBee's TX (Dout) pin
const int  XBEE_BAUD = 9600;         // Your XBee's baud (9600 is default)
SoftwareSerial xB(XB_RX, XB_TX);

// Initializing variables used
float valueOne    = 0;

// Phant limits you to 10 seconds between posts. Use this variable
// to limit the update rate (in milliseconds):
const unsigned long UPDATE_RATE = 30000;
unsigned long lastUpdate        = 0;            // Keep track of last update time

bool sendError = false;

//Setup
void setup()
{
  delay(5000);
    
  // Set up serial ports:
  Serial.begin(9600);
  xB.begin(XBEE_BAUD);

  // Set up WiFi network
  Serial.println("Testing network");
  connectWiFi();
  Serial.println("Connected!");
  Serial.print("IP Address: "); printIP(); Serial.println("......");
  // setupHTTP() will set up the destination address, port, and
  // make sure we're in TCP mode:
  setupHTTP(destIP);
  
  delay(5000);
}

//MainLoop
void loop()
{
  // If current time is UPDATE_RATE milliseconds greater than
  // the last update rate, send new data.
  if (millis() > (lastUpdate + UPDATE_RATE)) {
    Serial.print("\nSending update...");
    if (sendData())  {
      sendError = false;
      Serial.println(" SUCCESS!");
    } else {
      sendError = true;
      Serial.println(" Failed :(");
    }
    lastUpdate = millis();
  }
  
  Serial.print(FieldOne); Serial.print(" - "); Serial.println(valueOne);
  Serial.print(".");
  
  delay(2000);
}
//Functions
int sendData()
{
  xB.flush();                           // Flush data so we get fresh stuff in
  readSensors();                        // Get updated values from sensors.
  
 phant.add(FieldOne, valueOne);        //Adiciona o valor(valueOne) obtido pela função de leitura "readSensors()"
  
  // After our PHANT.ADD's we need to PHANT.POST(). The post needs
  // to be sent out the XBee. A simple "print" of that post will
  // take care of it.
  //oque vai enviar
  Serial.print(phant.url());
  
   Serial.println(phant.post());
  xB.print(phant.post());

  char response[12];
  
  //Serial.println(response);
  
  if (waitForAvailable(12) > 0) {
    
    for (int i=0; i<12; i++) {
      response[i] = xB.read();
           Serial.println(response[i]);
    }
        if (memcmp(response, "HTTP/1.1 200", 12) == 0){
        Serial.println("Ok!");
          return 1;
          }
        else {
          Serial.print("Server response ---- ");
          Serial.println(response);
              Serial.println("Error1");
          return 0; // Non-200 response
        }
  }  
  else{ // Otherwise timeout, no response from server

     //It crashes here :(
     Serial.println("Error2");
    return -1;
    }
}

void readSensors()
{
  //valueTwo = random (0,50);
  //valueOne = random (0,50);
//valueTwo = 22;
valueOne = 11;
}

// XBee WiFi Setup Stuff
void setupHTTP(String address)
{
  // Enter command mode, wait till we get there.
  while (!commandMode(1))  ;

  // Set IP (1 - TCP)
  command("ATIP1", 2);                    // RESP: OK
  // Set DL (destination IP address)
  command("ATDL" + address, 2);           // RESP: OK
  // Set DE (0x50 - port 80)
  command("ATDE50", 2);                   // RESP: OK

  commandMode(0);                         // Exit command mode when done
}

// Simple function that enters command mode, reads the IP and
// prints it to a serial terminal. Then exits command mode.
void printIP()
{
  // Wait till we get into command Mode.
  while (!commandMode(1))
    ;
  // Get rid of any data that may have already been in the
  // serial receive buffer:
  xB.flush();
  // Send the ATMY command. Should at least respond with "0.0.0.0\r" (7 characters):
  command("ATMY", 7);
  // While there are characters to be read, read them and throw
  // them out to the serial monitor.
  while (xB.available() > 0)  {
    Serial.write(xB.read());
  }
  
  // Exit command mode:
  commandMode(0);
}

// Assumes you've already configured your XBee module's WiFi settings using X-CTU
// and simply blinks the LED while connecting or if there is a problem. We also
// turn on the LED once connected.
void connectWiFi()
{
  const String CMD_SSID = "ATID";
  const String CMD_ENC = "ATEE";
  const String CMD_PSK = "ATPK";
  // Check if we're connected. If so, sweet! We're done.
  // Otherwise, time to configure some settings, and print
  // some status messages:
  int status;
  while ((status = checkConnect()) != 0)
  {
    // Print a status message. If `status` isn't 0 (indicating
    // "connected"), then it'll be one of these
    //  (from XBee WiFI user's manual):

    // We added 0xFE to indicate connected but SSID doesn't match the provided id.
    Serial.print("Waiting to connect: ");
    Serial.println(status, HEX);
    delay(1000);
  }
}

// Check if the XBee is connected to a WiFi network.
// This function will send the ATAI command to the XBee.
// That command will return with either a 0 (meaning connected)
// or various values indicating different levels of no-connect.
byte checkConnect()
{
  byte i=0xFF;
  char temp[2];
  commandMode(0);
  while (!commandMode(1))
    ;
  command("ATAI", 2);
  temp[0] = hexToInt(xB.read());
  temp[1] = hexToInt(xB.read());
  xB.flush();
  
  #ifdef DEBUG
    Serial.print("temp[0] = ");   Serial.println(temp[0], HEX);
    Serial.print("temp[1] = ");   Serial.println(temp[1], HEX);
  #endif  
  if (temp[0] == 0) {
    return 0;
  }
  else {
    return (temp[0]<<4) | temp[1];
  }
}

// Low-level, ugly, XBee Functions //

void command(String atcmd, int rsplen)
{
  xB.flush();
  #ifdef DEBUG
    Serial.print("Entering command: ");
  #endif
  
  xB.print(atcmd);
  
  #ifdef DEBUG
    Serial.println(atcmd);
  #endif
  
  xB.print("\r");
  waitForAvailable(rsplen);
}
//
int commandMode(boolean enter)
{
  xB.flush();
  
  if (enter)  {
      #ifdef DEBUG
         Serial.println("Entering Command Mode");
      #endif
    char c;
    xB.print("+++");   // Send CMD mode string
    waitForAvailable(1);
    if (xB.available() > 0)  {
      c = xB.read();
      #ifdef DEBUG
        Serial.print("Command Mode Response: ");
        Serial.println(c);
      #endif
      
      if (c == 'O')       // That's the letter 'O', assume 'K' is next
        return 1;         // IF we see "OK" return success
    }
    return 0;             // If no (or incorrect) receive, return fail
  }
  else  {
   #ifdef DEBUG
    Serial.println("Exiting Command Mode");
   #endif
  
    command("ATCN", 2);
    return 1;
  }
}

int waitForAvailable(int qty)
{
  int timeout = COMMAND_TIMEOUT;

  while ((timeout-- > 0) && (xB.available() < qty))
    delay(1);

  return timeout;
}

byte hexToInt(char c)
{
  if (c >= 0x41) // If it's A-F
    return c - 0x37;
  else
    return c - 0x30;
}

//End

I'm using Phant and SoftwareSerial libraries, with the XBee wifi connected to pins 2 and 3 respectively.
I am also using the latest firmware(2024) on the module, configured with XCTU soft.
Is there any problem with the code?

Thank you for your help!
RC
Contact a Digi expert and get started today! CONTACT US
...