Post a new topicPost a reply Page 1 of 1   [ 9 posts ]
Author Message
 Post subject: connecting to GPRS failing
PostPosted: Wed May 09, 2012 4:23 pm 

Joined: Wed May 09, 2012 4:15 pm
Posts: 18
Hi,

I'm experiencing a weird problem when trying to use a sketch to connect GPRS.

If I run the example code WaspGPRS_2_sendingSMS it works and connects fine,
If I run a sketch I created with the same setup() code on the same Waspmote it won't connect.

Now, once I comment out enough code in the latter sketch it will connect.

Therefore, I'm concluding that perhaps I'm compiling with a small memory model, so that when the sketch becomes large it's not able to perform correct jumps to certain code.
How can I fix this?

I'm running the latest version (24) on Mac OS X Leopard.


Top
 Profile  
 
 Post subject: Re: connecting to GPRS failing
PostPosted: Thu May 10, 2012 8:23 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7818
marc de hoogh,

Could you tell us which GPRS module are you using?

besides that, Could you please post the code that is causing you these problems?

Best Regards


Top
 Profile  
 
 Post subject: Re: connecting to GPRS failing
PostPosted: Thu May 10, 2012 1:31 pm 

Joined: Wed May 09, 2012 4:15 pm
Posts: 18
Hi,

I'm using the standard GPRS module with the Sagem Communications MODULE HiLo plugged in.

Here's the sending SMS adjusted code that works fine:
Code:
/*
 *  ------Waspmote GPRS Sending an SMS Example--------
 *
 *  Explanation: This example shows how to send an SMS
 *
 *  Copyright (C) 2009 Libelium Comunicaciones Distribuidas S.L.
 *  http://www.libelium.com
 *
 *  This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 *  Version:                0.1
 *  Design:                 David Gascón
 *  Implementation:    Alberto Bielsa
 */

void reportGPRSStatus(char *statusPrefix) {
  int gprsFlag=GPRS.flag;
  USB.print("GPRS status after '");
  USB.print(statusPrefix);
  USB.print("': ");
  USB.print(gprsFlag);
  USB.println(".");
  if (gprsFlag&1) USB.println("  Error powering up the GRPS module.");
  if (gprsFlag&2) USB.println("  Error making a call.");
  if (gprsFlag&4) USB.println("  Error hanging up a call.");
  if (gprsFlag&8) USB.println("  Error while working with SMS.");
  if (gprsFlag&16) USB.println("  Error setting the PIN.");
  if (gprsFlag&32) USB.println("  Error receiving a call.");
  if (gprsFlag&64) USB.println("  Error receiving a SMS.");
  if (gprsFlag&128) USB.println("  Error configuring the GPRS connection.");
  if (gprsFlag&256) USB.println("  Error setting the pattern of the TCP connection.");
  if (gprsFlag&512) USB.println("  Error setting the timer for the TCP connection.");
  if (gprsFlag&1024) USB.println("  Error setting the profile for the TCP connection.");
  if (gprsFlag&2048) USB.println("  Error setting the socket.");
  if (gprsFlag&4096) USB.println("  Error checking the connection.");
  if (gprsFlag&8192) USB.println("  Error closing the socket.");
  if (gprsFlag&16384) USB.println("  Error while setting SMTP parameters.");
}

void setup(){
 
  // setup for Serial port over USB
  USB.begin();
  USB.println("USB port started...");
 
  ////////GPRS.OFF();
 
  delay(10000);
 
  GPRS.begin();
 
  delay(1000);
 
  GPRS.setMode(GPRS_ON);
 
  //////////GPRS.ON();
  reportGPRSStatus("GPRS module");
 
  delay(1000);
 
  ////GPRS.configureGPRS();
  ////reportGPRSStatus("GPRS configure");
 
  ////GPRS.setPIN("6523");
  ////reportGPRSStatus("GPRS PIN");
 
  delay(1000);
  GPRS.flag=0;
  // waiting while GPRS connects to the network
  int i=0;
  //  while(++i<=5&&GPRS.check()==0)reportGPRSStatus("Check");
  while(++i<=5) {
    USB.print("Checking GPRS connection #");
    USB.print(i);
    USB.println("...");
    if (GPRS.check())
      break;
  }
  reportGPRSStatus("Check");
   
  USB.println("GPRS connected to the network");
 
  // configure SMS and Incoming Calls
  if (GPRS.setInfoIncomingCall())
    USB.println("Info Incoming Call OK");
  else
    USB.println("Info Incoming Call Error");
  reportGPRSStatus("Info Incoming Call");
 
  if (GPRS.setInfoIncomingSMS())
    USB.println("Info Incoming SMS OK");
  else
    USB.println("Info Incoming SMS Error");
  reportGPRSStatus("Info Incoming SMS");
   
  if (GPRS.setTextModeSMS())
    USB.println("Text Mode SMS OK");
  else
    USB.println("Text Mode SMS Error");
  reportGPRSStatus("Text Mode SMS");

  if(GPRS.sendSMS("Ja, dit doet het wel, he!","+31618408210")) USB.println("SMS Sent OK"); // * should be replaced by the desired tlfn number
  else USB.println("Error sending sms");

}

long time=0;

void loop(){

  USB.println("Loop!");
  /*
  if(GPRS.sendSMS("Test Message!","+31618408210")) USB.println("SMS Sent OK"); // * should be replaced by the desired tlfn number
  else USB.println("Error sending sms");
  */
  delay(5000);
}



And here's the code that blocks in check():

Code:
// Coordinator that collects data from the different sensors
// using a predefined 64-bit PAN ID that all nodes use

// Coordinator only parameters
char* PRM_SMS_RECEIVER="+31618408210"; // MdH's mobile phone number

char* PRM_GPRS_PIN=NULL; // NOT using a GPRS PIN

// as soon as we manage to get the TCP client working we can send the data over to 'Thomas' platform!!!!
char* PRM_TCP_SERVER_ADDRESS="131.180.117.130";
char* PRM_TCP_SERVER_PORT="23456";
char* PRM_USER_ID="A.1"; // to be prefixed in front of any message to send... (see sendToTcpServer)

// A gateway node might also be active to send log data to
uint8_t PRM_GATEWAY[8]={0x00,0x13,0xA2,0x00,0x40,0x55,0x95,0xB1}; // the MAC address of the gateway

char* PRM_NETKEY="WaspmoteKey";
char* PRM_LINKKEY="WaspmoteKey";

// end Coordinator only parameters

char* PRM_ID="COORDINATOR_MHV_1"; // ID of this node

uint8_t PRM_COORDINATOR_PAN_ID[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};

void setLEDs(boolean led0, boolean led1) {
  Utils.setLED(LED0,led0?LED_ON:LED_OFF);
  Utils.setLED(LED1,led1?LED_ON:LED_OFF);
}

void setup(){
 
  // setup for Serial port over USB
  USB.begin();
  USB.println("USB port started...");

  delay(10000);
   
  GPRS.begin();
 
  delay(1000);
 
  GPRS.setMode(GPRS_ON);
  reportGPRSStatus("GPRS module");
 
  delay(1000);
  GPRS.flag=0;
  // waiting while GPRS connects to the network
  int i=0;
  while(++i<=5) {
    USB.print("Checking GPRS connection #");
    USB.print(i);
    USB.println("...");
    if (GPRS.check())
      break;
  }
  reportGPRSStatus("Check");
   
  USB.println("GPRS connected to the network...");
 
  // configure SMS and Incoming Calls
  if (GPRS.setInfoIncomingCall())
    USB.println("Info Incoming Call OK");
  else
    USB.println("Info Incoming Call Error");
  reportGPRSStatus("Info Incoming Call");
 
  if (GPRS.setInfoIncomingSMS())
    USB.println("Info Incoming SMS OK");
  else
    USB.println("Info Incoming SMS Error");
  reportGPRSStatus("Info Incoming SMS");
   
  if (GPRS.setTextModeSMS())
    USB.println("Text Mode SMS OK");
  else
    USB.println("Text Mode SMS Error");
  reportGPRSStatus("Text Mode SMS");

  // why does the following take so long???????
  if (GPRS.sendSMS("Een nieuwe lente, een nieuw geluid!!","+31618408210"))
    USB.println("SMS Sent OK."); // * should be replaced by the desired tlfn number
  else
    USB.println("Error sending sms.");

  // start XBee in order to be able to receive data from the routers
  delay(1000);
  xbeeInitializeCoordinator();
  USB.println("Coordinator initialized..");
 
 
  delay(1000);   
  xbeeStartCoordinator(); 
  USB.println("Coordinator started...");

}

// methods for initializing a router and a coordinator
void setLinkKey() {
  xbeeZB.setLinkKey(PRM_LINKKEY);
  if (!xbeeZB.error_AT)
    XBee.println("Link Key set OK");
  else
    XBee.println("Error while setting Key");
}

void xbeeInitializeRouter() {
 
  // Inits the XBee ZigBee library
  xbeeZB.init(ZIGBEE,FREQ2_4G,NORMAL);
 
  delay(1000);
 
  // turn it on, so we can get something done
  xbeeZB.ON();
 
  delay(5000);
 
  // Enabling security
  xbeeZB.encryptionMode(1);
  if (!xbeeZB.error_AT)
    XBee.println("Security enabled");
  else
    XBee.println("Error while enabling security"); 
 
  setLinkKey();
 
  xbeeZB.setAPSencryption(XBEE_ON);
  if (!xbeeZB.error_AT)
    XBee.println("APS Encryption turned on");
  else
    XBee.println("Error while setting APS Encryption");
 
}

void xbeeInitializeCoordinator() {
 
  // a coordinator is also a router
  xbeeInitializeRouter();

  // Setting Network Key (only in Coordinator)
  xbeeZB.setNetworkKey(PRM_NETKEY);
  if(!xbeeZB.error_AT)
    XBee.println("Network Key set OK");
  else
    XBee.println("Error while setting Key"); 
 
  // Configuring Trust Center (only in the coordinator)
  !xbeeZB.setEncryptionOptions(0x02);
  if (!xbeeZB.error_AT)
    XBee.println("Security options configured");
  else
    XBee.println("Error while configuring security"); 
 
  // store the coordinator values
  xbeeZB.writeValues();
  if(!xbeeZB.error_AT)
    XBee.println("Changes stored OK");
  else
    XBee.println("Error while storing values"); 
 
}

void xbeeStartCoordinator() {
  // Coordinator Startup Process. Selected Extended PAN ID (see § 8.4.2 Waspmote-zigbee-networking-guide.pdf)
  // MDH 03MAY2012: I suppose we can set the pan here as well
  xbeeZB.setPAN(PRM_COORDINATOR_PAN_ID);
  xbeeZB.setScanningChannels(0xFF,0xFF); // scan all channels
  xbeeZB.setDurationEnergyChannels(3);
  // ? do we need to tell it to actually start scanning?
  // blink the red LED while we're starting
  setLEDs(true,false); // turn red LED on and green LED off
}

boolean isCoordinatorStarted() {
  // determine based on the association indication whether we succeeded in creating the network (i.e. start the coordinator)
  xbeeZB.getAssociationIndication();
  switch (xbeeZB.associationIndication) { // success
    case 0: // Successful. Coordinator started or Router/End Device joined with a parent.
      // if the channel is also correct, we report it and return true
      xbeeZB.getChannel();
      if(!xbeeZB.error_AT) {
        XBee.print("Channel is: ");
        XBee.println(xbeeZB.channel,HEX);
        return true;
      }
      XBee.println("ERROR: Failed to get XBee channel."); 
      break;
    // all other values indicate some kind of failure
    case 0xAB: //Attempted to join a device did not respond.
      break;
    case 0xAC: // Secure join error. Network security key received unsecured.
      XBee.println("ERROR: Network security key received unsecured, joining.");
      break;
    case 0xAD: // Secure join error. Network security key not received.
      XBee.println("ERROR: Network security key not received, joining.");
      break;
    case 0xAF: // Secure join error. Joining device does not have the right preconfigured link key.
      XBee.println("ERROR: Joining device does have the right preconfigured link key.");
      break;
    case 0x21: // Scan found no PANs.
      XBee.println("ERROR: No PANs found.");
      break;
    case 0x22: // Scan found no valid PANs based on current ‘Scan Channels’ and ‘Extended PAN ID’.
      XBee.println("ERRROR: No PANs found on any of the scan channels and given PAN ID.");
      break;
    case 0x23: // Valid Coordinator or Routers found, but they are not allowing joining.
      XBee.println("ERROR: Coordinator/Router found does not allow joining.");
      break;
    case 0x27: // Node joining attempt failed.
      XBee.println("ERROR: Joining attempt failed.");
      break;
    case 0x2A: // Coordinator Start attempt failed.
      XBee.println("ERROR: Failed to start as coordinator.");
      // NOTE we might need to attempt to start again here
      xbeeStartCoordinator();
      break;
    case 0xFF: // Scanning for a parent.
      XBee.println("NOTE: Scanning for a parent.");
      break;
    case 0x2B: // Checking for an existing coordinator.
      XBee.println("NOTE: Checking for an existing coordinator.");
      break;
  }
  return false;
}

// GPRS stuff (copied over from TUD_MHV_Router_Test)
void reportGPRSStatus(char *statusPrefix) {
  int gprsFlag=GPRS.flag;
  USB.print("GPRS status after '");
  USB.print(statusPrefix);
  USB.print("': ");
  USB.print(gprsFlag);
  USB.println(".");
  if (gprsFlag&1) USB.println("  Error powering up the GRPS module.");
  if (gprsFlag&2) USB.println("  Error making a call.");
  if (gprsFlag&4) USB.println("  Error hanging up a call.");
  if (gprsFlag&8) USB.println("  Error while working with SMS.");
  if (gprsFlag&16) USB.println("  Error setting the PIN.");
  if (gprsFlag&32) USB.println("  Error receiving a call.");
  if (gprsFlag&64) USB.println("  Error receiving a SMS.");
  if (gprsFlag&128) USB.println("  Error configuring the GPRS connection.");
  if (gprsFlag&256) USB.println("  Error setting the pattern of the TCP connection.");
  if (gprsFlag&512) USB.println("  Error setting the timer for the TCP connection.");
  if (gprsFlag&1024) USB.println("  Error setting the profile for the TCP connection.");
  if (gprsFlag&2048) USB.println("  Error setting the socket.");
  if (gprsFlag&4096) USB.println("  Error checking the connection.");
  if (gprsFlag&8192) USB.println("  Error closing the socket.");
  if (gprsFlag&16384) USB.println("  Error while setting SMTP parameters.");
}

void sendToTcpServer(char* msg) {
  if (GPRS.socket_ID) { // some socket ID available to send data to
    char messageToSend[100];
    sprintf(messageToSend,"%s\t%s\n\r",PRM_USER_ID,msg);
    USB.print("'");
    USB.print(msg);
    if (GPRS.sendData(messageToSend,GPRS.socket_ID))
      USB.println("' sent to TCP server.");
    else
      USB.println("' NOT sent to TCP server.");
  } else {
    USB.print("ERROR: Failed to send '");
    USB.print(msg);
    USB.println("' to the TCP server: no TCP client available.");
  }
}

void sendHi() {
  sendToTcpServer("hi");
}

void loop() {

  USB.println("Looping");

  // try to get a tcp client socket up and running 
  if (!GPRS.socket_ID) { // no tcp client socket ID running
    GPRS.configureGPRS();
    reportGPRSStatus("GPRS configuration");
    // create the socket as soon as we manage to get a configuration
    if (!GPRS.flag) {
      USB.println("Will attempt to create a TCP client socket.");
      // create the socket to communicate over
      int gprsSocketCreated=GPRS.createSocket(PRM_TCP_SERVER_ADDRESS,PRM_TCP_SERVER_PORT,GPRS_CLIENT);
      reportGPRSStatus("GPRS create socket");
      if (!(GPRS.flag&2048))
        sendHi();
      else
        USB.println("NOTE: Hi message not sent: socket not created!");
      if (GPRS.socket_ID) {
        USB.print("Client socket to ");
        USB.print(PRM_TCP_SERVER_ADDRESS);
        USB.print(":");
        USB.print(PRM_TCP_SERVER_PORT);
        USB.println(" created!");
      } else {
        USB.print("ERROR: Failed to connect to ");
        USB.print(PRM_TCP_SERVER_ADDRESS);
        USB.print(":");
        USB.print(PRM_TCP_SERVER_PORT);
        USB.println("!");
      }
    }
  }
 
  // if we managed to start as coordinator, we can go ahead and receive packets from the joined sensor nodes
  if (isCoordinatorStarted()) {
    // I suppose we can turn off the blinking of the red LED
    setLEDs(false,true);
    USB.println("Coordinator started!");
    ///*
    // Waiting the answer
    long previous=millis();
    while ((millis()-previous)<10000) {
      if (XBee.available()) {
        xbeeZB.treatData();
        if (!xbeeZB.error_RX) {
          // Writing the parameters of the packet received
          while (xbeeZB.pos>0) {
            /*
            XBee.print("Network Address Source: ");
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naS[0],HEX);
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naS[1],HEX);
            XBee.println("");
            XBee.print("MAC Address Source: ");         
            for (int b=0;b<4;b++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macSH[b],HEX);
            }
            for (int c=0;c<4;c++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macSL[c],HEX);
            }
            XBee.println();
            XBee.print("Network Address Origin: ");         
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naO[0],HEX);
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naO[1],HEX);
            XBee.println();
            XBee.print("MAC Address Origin: ");         
            for (int d=0;d<4;d++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macOH[d],HEX);
            }
            for (int e=0;e<4;e++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macOL[e],HEX);
            }
            XBee.println();
            XBee.print("RSSI: ");                   
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->RSSI,HEX);
            XBee.println("");         
            XBee.print("16B(0) or 64B(1): ");                   
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->mode,HEX);
            XBee.println();
            */
            XBee.print("Data: ");
            // to send the stuff along we need to put it in a string
            char message[xbeeZB.packet_finished[xbeeZB.pos-1]->data_length];           
            for (int f=0;f<xbeeZB.packet_finished[xbeeZB.pos-1]->data_length;f++) {
              /////XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->data[f],BYTE);
              message[f]=(char)xbeeZB.packet_finished[xbeeZB.pos-1]->data[f];
            }
            XBee.print("Message: '");
            XBee.print(message);
            XBee.println("'.");
           
            ////sendSMS(message);
            ////sendToTcpServer(message);
            ////sendToGateway(message);
            /*
            XBee.print("PacketID: ");                   
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->packetID,HEX);
            XBee.println("");     
            XBee.print("Type Source ID: ");                             
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->typeSourceID,HEX);
            XBee.println();     
            XBee.print("Network Identifier Origin: ");         
            for (int g=0;g<4;g++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->niO[g],BYTE);
            }
            XBee.println(); 
            XBee.print("Source Destination: ");
            XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->SD,HEX);
            XBee.print("Destination Endpoint: ");         
            XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->DE,HEX);
            XBee.print("Cluster ID: ");         
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->CID[0],HEX);
            XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->CID[1],HEX);
            XBee.print("Profile ID: ");         
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->PID[0],HEX);
            XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->PID[1],HEX);
            */
            free(xbeeZB.packet_finished[xbeeZB.pos-1]);
            xbeeZB.packet_finished[xbeeZB.pos-1]=NULL;
            xbeeZB.pos--;
          }
          previous=millis();
        }
      }
    }
    //*/   
  }
  //*/
  delay(500);
 
}


Top
 Profile  
 
 Post subject: Re: connecting to GPRS failing
PostPosted: Thu May 10, 2012 2:08 pm 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7818
marc de hoogh,

Due to your second code is pretty big, we are going to give you some tips that may help to solve your issue.

  • Watch out free RAM memory available in your code and try to not run under 1K free. You can do this using freeMemory() funciton.
  • Due to high current spikes required by GPRS, it requires battery is always connected and properly charged.
  • Try not using Xbee and GPRS at same time, we mean turning them off when your a re not using it.
  • Besides that, did you test other GPRS examples. Did they worked fine?

Let us know before going further.

Regards.

One more tip: for large codes please use edit/copy for forum to make easier for us understanding your code.


Top
 Profile  
 
 Post subject: Re: connecting to GPRS failing
PostPosted: Thu May 10, 2012 7:02 pm 

Joined: Wed May 09, 2012 4:15 pm
Posts: 18
Hi,

I've removed some of the code, and now I'm running into another problem (among others).
I.e. when receiving data over Zigbee treatData() always fails.
freeMemory() returns low values like 170.
Does that mean there's not enough memory for it to work?
There's not much code I can remove now left anymore.

Code of my sketch:
Quote:
// Coordinator that collects data from the different sensors
// using a predefined 64-bit PAN ID that all nodes use

// Coordinator only parameters
char* PRM_SMS_RECEIVER="+31618408210"; // MdH's mobile phone number

char* PRM_GPRS_PIN=NULL; // NOT using a GPRS PIN

// as soon as we manage to get the TCP client working we can send the data over to 'Thomas' platform!!!!
char* PRM_TCP_SERVER_ADDRESS="131.180.117.130";
char* PRM_TCP_SERVER_PORT="23456";
char* PRM_USER_ID="A.1"; // to be prefixed in front of any message to send... (see sendToTcpServer)

// A gateway node might also be active to send log data to
uint8_t PRM_GATEWAY[8]={0x00,0x13,0xA2,0x00,0x40,0x55,0x95,0xB1}; // the MAC address of the gateway

char* PRM_NETKEY="WaspmoteKey";
char* PRM_LINKKEY="WaspmoteKey";

// end Coordinator only parameters

char* PRM_ID="COORDINATOR_MHV_1"; // ID of this node

uint8_t PRM_COORDINATOR_PAN_ID[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};

void setLEDs(boolean led0, boolean led1) {
  Utils.setLED(LED0,led0?LED_ON:LED_OFF);
  Utils.setLED(LED1,led1?LED_ON:LED_OFF);
}

void setup(){
  
  // setup for Serial port over USB
  USB.begin();
  USB.println("USB port started...");

  delay(10000);
    
  GPRS.begin();
  
  delay(1000);
  
  GPRS.setMode(GPRS_ON);
  
  //////////GPRS.ON();
  reportGPRSStatus("GPRS module");
  
  delay(1000);
  
  ////GPRS.configureGPRS();
  ////reportGPRSStatus("GPRS configure");
  
  ////GPRS.setPIN("6523");
  ////reportGPRSStatus("GPRS PIN");
  
  delay(1000);
  GPRS.flag=0;
  // waiting while GPRS connects to the network
  int i=0;
  while(++i<=5) {
    USB.print("Checking GPRS connection #");
    USB.print(i);
    USB.println("...");
    if (GPRS.check())
      break;
  }
  reportGPRSStatus("Check");
    
  USB.println("GPRS connected to the network...");
  
  // configure SMS and Incoming Calls
  if (GPRS.setInfoIncomingCall())
    USB.println("Info Incoming Call OK");
  else
    USB.println("Info Incoming Call Error");
  reportGPRSStatus("Info Incoming Call");
  
  if (GPRS.setInfoIncomingSMS())
    USB.println("Info Incoming SMS OK");
  else
    USB.println("Info Incoming SMS Error");
  reportGPRSStatus("Info Incoming SMS");
    
  if (GPRS.setTextModeSMS())
    USB.println("Text Mode SMS OK");
  else
    USB.println("Text Mode SMS Error");
  reportGPRSStatus("Text Mode SMS");

  // why does the following take so long???????
  if (GPRS.sendSMS("Een nieuwe lente, een nieuw geluid!!","+31618408210"))
    USB.println("SMS Sent OK."); // * should be replaced by the desired tlfn number
  else
    USB.println("Error sending sms.");

  delay(1000);
  // start XBee in order to be able to receive data from the routers
  xbeeInitializeCoordinator();
  USB.println("Coordinator initialized..");
  
  delay(1000);
  xbeeStartCoordinator();  
  USB.println("Coordinator started...");
  
}
/*
// methods for initializing a router and a coordinator
void setLinkKey() {
  xbeeZB.setLinkKey(LINKKEY);
  if (!xbeeZB.error_AT)
    XBee.println("Link Key set OK");
  else 
    XBee.println("Error while setting Key"); 
}
*/
void xbeeInitializeRouter() {
  
  // Inits the XBee ZigBee library
  xbeeZB.init(ZIGBEE,FREQ2_4G,NORMAL);
  
  // turn it on, so we can get something done
  xbeeZB.ON();
  
  // Enabling security
  xbeeZB.encryptionMode(0);
  if (!xbeeZB.error_AT)
    XBee.println("Security disabled");
  else
    XBee.println("Error while disabling security");
  
  // setLinkKey();
  
  xbeeZB.setAPSencryption(XBEE_ON);
  if (!xbeeZB.error_AT)
    XBee.println("APS Encryption turned on");
  else
    XBee.println("Error while setting APS Encryption");
  
}

void xbeeInitializeCoordinator() {
  
  // a coordinator is also a router
  xbeeInitializeRouter();

  // Setting Network Key (only in Coordinator)
  xbeeZB.setNetworkKey(PRM_NETKEY);
  if(!xbeeZB.error_AT)
    XBee.println("Network Key set OK");
  else
    XBee.println("Error while setting Key");
  
  // Configuring Trust Center (only in the coordinator)
  !xbeeZB.setEncryptionOptions(0x02);
  if (!xbeeZB.error_AT)
    XBee.println("Security options configured");
  else
    XBee.println("Error while configuring security");
  
  // store the coordinator values
  xbeeZB.writeValues();
  if(!xbeeZB.error_AT)
    XBee.println("Changes stored OK");
  else
    XBee.println("Error while storing values");
  
}

void xbeeStartCoordinator() {
  // Coordinator Startup Process. Selected Extended PAN ID (see § 8.4.2 Waspmote-zigbee-networking-guide.pdf)
  // MDH 03MAY2012: I suppose we can set the pan here as well
  xbeeZB.setPAN(PRM_COORDINATOR_PAN_ID);
  xbeeZB.setScanningChannels(0xFF,0xFF); // scan all channels
  xbeeZB.setDurationEnergyChannels(3);
  // ? do we need to tell it to actually start scanning?
  // blink the red LED while we're starting
  setLEDs(true,false); // turn red LED on and green LED off
}
///*
boolean isCoordinatorStarted() {
  // determine based on the association indication whether we succeeded in creating the network (i.e. start the coordinator)
  xbeeZB.getAssociationIndication();
  switch (xbeeZB.associationIndication) { // success
    case 0: // Successful. Coordinator started or Router/End Device joined with a parent.
      // if the channel is also correct, we report it and return true
      xbeeZB.getChannel();
      if(!xbeeZB.error_AT) {
        XBee.print("Channel is: ");
        XBee.println(xbeeZB.channel,HEX);
        return true;
      }
      XBee.println("ERROR: Failed to get XBee channel.");
      break;
    // all other values indicate some kind of failure
    case 0xAB: //Attempted to join a device did not respond.
      break;
    case 0xAC: // Secure join error. Network security key received unsecured.
      XBee.println("ERROR: Network security key received unsecured, joining.");
      break;
    case 0xAD: // Secure join error. Network security key not received.
      XBee.println("ERROR: Network security key not received, joining.");
      break;
    case 0xAF: // Secure join error. Joining device does not have the right preconfigured link key.
      XBee.println("ERROR: Joining device does have the right preconfigured link key.");
      break;
    case 0x21: // Scan found no PANs.
      XBee.println("ERROR: No PANs found.");
      break;
    case 0x22: // Scan found no valid PANs based on current ‘Scan Channels’ and ‘Extended PAN ID’.
      XBee.println("ERRROR: No PANs found on any of the scan channels and given PAN ID.");
      break;
    case 0x23: // Valid Coordinator or Routers found, but they are not allowing joining.
      XBee.println("ERROR: Coordinator/Router found does not allow joining.");
      break;
    case 0x27: // Node joining attempt failed.
      XBee.println("ERROR: Joining attempt failed.");
      break;
    case 0x2A: // Coordinator Start attempt failed.
      XBee.println("ERROR: Failed to start as coordinator.");
      // NOTE we might need to attempt to start again here
      xbeeStartCoordinator();
      break;
    case 0xFF: // Scanning for a parent.
      XBee.println("NOTE: Scanning for a parent.");
      break;
    case 0x2B: // Checking for an existing coordinator.
      XBee.println("NOTE: Checking for an existing coordinator.");
      break;
  }
  return false;
}
//*/
// GPRS stuff (copied over from TUD_MHV_Router_Test)
void reportGPRSStatus(char *statusPrefix) {
  int gprsFlag=GPRS.flag;
  USB.print("GPRS status after '");
  USB.print(statusPrefix);
  USB.print("': ");
  USB.print(gprsFlag);
  USB.println(".");
  if (gprsFlag&1) USB.println(" Error powering up the GRPS module.");
  if (gprsFlag&2) USB.println(" Error making a call.");
  if (gprsFlag&4) USB.println(" Error hanging up a call.");
  if (gprsFlag&8) USB.println(" Error while working with SMS.");
  if (gprsFlag&16) USB.println(" Error setting the PIN.");
  if (gprsFlag&32) USB.println(" Error receiving a call.");
  if (gprsFlag&64) USB.println(" Error receiving a SMS.");
  if (gprsFlag&128) USB.println(" Error configuring the GPRS connection.");
  if (gprsFlag&256) USB.println(" Error setting the pattern of the TCP connection.");
  if (gprsFlag&512) USB.println(" Error setting the timer for the TCP connection.");
  if (gprsFlag&1024) USB.println(" Error setting the profile for the TCP connection.");
  if (gprsFlag&2048) USB.println(" Error setting the socket.");
  if (gprsFlag&4096) USB.println(" Error checking the connection.");
  if (gprsFlag&8192) USB.println(" Error closing the socket.");
  if (gprsFlag&16384) USB.println(" Error while setting SMTP parameters.");
}

/*
boolean sendSMSavailable=false; // if succeeding in sending the initial SMS, we're ready to send additional ones
void gprsInitialize() {

  // setup for Serial port over USB
  USB.begin();
  USB.println("USB port started...");
  
  ////////GPRS.OFF();
  
  delay(10000);
  
  GPRS.begin();
  
  delay(1000);
  
  GPRS.setMode(GPRS_ON);
  
  //////////GPRS.ON();
  reportGPRSStatus("GPRS module");
  
  delay(1000);
  
  ////GPRS.configureGPRS();
  ////reportGPRSStatus("GPRS configure");
  
  ////GPRS.setPIN("6523");
  ////reportGPRSStatus("GPRS PIN");
  
  delay(1000);
  GPRS.flag=0;
  // waiting while GPRS connects to the network
  int i=0;
  while(++i<=5&&GPRS.check()==0)
    reportGPRSStatus("Check");
  
  // mostly the same as above
  // turn the thing on 
  GPRS.begin();
  reportGPRSStatus("begin");
  
  delay(1000);
  GPRS.setMode(GPRS_ON);
  
  /////  GPRS.ON(); // Powers GPRS on 
  reportGPRSStatus("set mode on");

  USB.print("GPRS power mode: ");
  switch (GPRS.getMode()) {
    case GPRS_ON:
      USB.print("ON");
      break;
    case GPRS_SLEEP:
      USB.print("SLEEP");
      break;
    case GPRS_HIBERNATE:
      USB.print("HIBERNATE");
      break;
    default:
      USB.print("?");
  }
  /////////USB.print(GPRS.getMode(),HEX); // Get GPRS power mode
  USB.println(".");
  
  // setting the PIN is essential
  if (PRM_GPRS_PIN) {
    delay(1000);
    // we'll be testing the flag
    int pinCount=0;
    do {
      USB.print("PIN set #");
      USB.print(++pinCount);
      USB.println(".");
      // NOTE interesting to see that I can change the flag!!!!
      GPRS.flag=GPRS.flag&0xFFFD; // getting rid of the 2 in there
      GPRS.setPIN(PRM_GPRS_PIN);
      reportGPRSStatus("PIN set");
      break;
    } while (GPRS.flag&2);
    // force the flag to be OK
    GPRS.flag=GPRS.flag&0xFFFD; // getting rid of the 2 in there
    reportGPRSStatus("GPRS PIN set");
  } else
    USB.println("No PIN to set!");

  // let's try a longer delay (seemed to work in WaspGPRS_2_sendingSMS2)
  delay(5000);
  GPRS.flag=0; // or perhaps this does the trick?
  setLEDs(true,false);
  while (!GPRS.check()) {
    reportGPRSStatus("checking");
    Utils.blinkLEDs(500);
    delay(1000);
    setLEDs(true,false);
  }
  // we did it!
  setLEDs(false,true);
  
  
  
  USB.println("GPRS connected to the network.");

  // configure SMS and Incoming Calls
  GPRS.setInfoIncomingCall();
  reportGPRSStatus("Info Incoming Call");
  
  GPRS.setInfoIncomingSMS(); 
  reportGPRSStatus("Info Incoming SMS");
  
  GPRS.setTextModeSMS();
  reportGPRSStatus("Text Mode SMS");
  
  char smsText[100];
  sprintf(smsText,"Battery level of %s: %u",PRM_ID,PWR.getBatteryLevel());
  sendSMSavailable=(GPRS.sendSMS(smsText,PRM_SMS_RECEIVER)==1);
  reportGPRSStatus("SMS test");
  
}

void sendSMS(char *smsText) {
  if (!PRM_SMS_RECEIVER) {
    USB.print("Can't SMS '");
    USB.print(smsText);
    USB.println("': SMS receiver not defined!");
  } else
  if (sendSMSavailable) {
    if (GPRS.sendSMS(smsText,PRM_SMS_RECEIVER)) {
      USB.println("Succeeded in sending '");
      USB.print(smsText);
      USB.println("' as SMS.");
    } else {
      USB.print("ERROR: Failed to SMS '");
      USB.print(smsText);
      USB.println("'.");
    }
  } else {
    USB.print("Can't SMS '");
    USB.print(smsText);
    USB.println("': Failed to send initial SMS.");
  }
}
// end GPRS stuff

packetXBee* paq_sent;
void unicastXB(char* message) { 
  paq_sent=(packetXBee*)calloc(1,sizeof(packetXBee)); 
  paq_sent->mode=UNICAST;  // set Broadcast mode
  paq_sent->MY_known=0;  // set 16-bit NA unknown
  paq_sent->packetID=0x52; // set ID application level
  paq_sent->opt=0; // set options. No option selected.
  xbeeZB.hops=0; 
  xbeeZB.setOriginParams(paq_sent,MAC_TYPE);
  xbeeZB.setDestinationParams(paq_sent,PRM_GATEWAY,message,MAC_TYPE,DATA_ABSOLUTE);

  xbeeZB.sendXBee(paq_sent); 
  // we can check for success here
  uint8_t transmitError=xbeeZB.error_TX;
  if (transmitError==0) {
    // NOTE we will see the RSSI leds flashing, so there's no need to change the LEDs
    // we turn both LEDs off indicatative of a successful send NOTE they will be turned on shortly!!!
    ////////setLEDs(LED_OFF,LED_OFF);
    USB.print("'");
    USB.print(message);
    USB.println("' sent!");
  } else {
    // turn both LEDs on indicating failing to send something
    setLEDs(LED_ON,LED_ON); // failed to send, both LEDs should be on to indicate this
    switch (transmitError) {
    case 1:
      USB.print("ERROR: Error in sending '");
      USB.print(message);
      USB.println("'.");
      break;
    case 2:
      USB.print("ERROR: Failed to send '");
      USB.print(message);
      USB.println("'.");
      break;
    case -1:
      USB.print("ERROR: Not allowed to send '");
      USB.print(message);
      USB.println("'.");
      break;
    }
  }
  free(paq_sent);
  paq_sent=NULL;
}

void sendToGateway(char* msg) {
  if (PRM_GATEWAY)
    unicastXB(msg);
}
*/
void sendToTcpServer(char* msg) {
  if (GPRS.socket_ID) { // some socket ID available to send data to
    char messageToSend[100];
    sprintf(messageToSend,"%s\t%s\n\r",PRM_USER_ID,msg);
    USB.print("'");
    USB.print(msg);
    if (GPRS.sendData(messageToSend,GPRS.socket_ID))
      USB.println("' sent to TCP server.");
    else
      USB.println("' NOT sent to TCP server.");
  } else {
    USB.print("ERROR: Failed to send '");
    USB.print(msg);
    USB.println("' to the TCP server: no TCP client available.");
  }
}

void sendHi() {
  sendToTcpServer("hi");
}
/*

void prepostsetup() {
  USB.begin();
  USB.print("'");
  USB.print(PRM_ID);
  USB.println("' starting.");
  
  // keep LEDs turned off initially
  // NOTE we could NOT do this given that start coordinator will set the LEDs as well BUT this does ascertain that we see something all the time
  setLEDs(LED_OFF,LED_OFF);
}

void postsetup() {
  //////prepostsetup();
  
  // start GPRS  
  gprsInitialize(); // will turn the LEDs on when failing!!!!!

  // start XBee in order to be able to receive data from the routers
  xbeeInitializeCoordinator();
  
  xbeeStartCoordinator();  

}
*/
// exactly the same setup as in sendingSMS2
/*
void setup() {

  // setup for Serial port over USB
  USB.begin();
  USB.println("USB port started...");
  
  ////////GPRS.OFF();
  
  delay(10000);
  
  GPRS.begin();
  
  delay(1000);
  
  GPRS.setMode(GPRS_ON);
  
  //////////GPRS.ON();
  reportGPRSStatus("GPRS module");
  
  delay(1000);
  
  ////GPRS.configureGPRS();
  ////reportGPRSStatus("GPRS configure");
  
  ////GPRS.setPIN("6523");
  ////reportGPRSStatus("GPRS PIN");
  
  delay(1000);
  GPRS.flag=0;
  // waiting while GPRS connects to the network
  int i=0;
  while(++i<=5&&GPRS.check()==0)
    reportGPRSStatus("Check");

  postsetup();
}
*/


void loop() {

  USB.println("Looping");

  // try to get a tcp client socket up and running
  if (!GPRS.socket_ID) { // no tcp client socket ID running
    GPRS.configureGPRS(); 
    reportGPRSStatus("GPRS configuration");
    // create the socket as soon as we manage to get a configuration
    if (!GPRS.flag) {
      USB.println("Will attempt to create a TCP client socket.");
      // create the socket to communicate over
      int gprsSocketCreated=GPRS.createSocket(PRM_TCP_SERVER_ADDRESS,PRM_TCP_SERVER_PORT,GPRS_CLIENT);
      reportGPRSStatus("GPRS create socket");
      if (!(GPRS.flag&2048))
        sendHi();
      else
        USB.println("NOTE: Hi message not sent: socket not created!");
      if (GPRS.socket_ID) {
        USB.print("Client socket to ");
        USB.print(PRM_TCP_SERVER_ADDRESS);
        USB.print(":");
        USB.print(PRM_TCP_SERVER_PORT);
        USB.println(" created!");
      } else {
        USB.print("ERROR: Failed to connect to ");
        USB.print(PRM_TCP_SERVER_ADDRESS);
        USB.print(":");
        USB.print(PRM_TCP_SERVER_PORT);
        USB.println("!");
      }
    }
  }
  
  // if we managed to start as coordinator, we can go ahead and receive packets from the joined sensor nodes
  if (isCoordinatorStarted()) {
    // I suppose we can turn off the blinking of the red LED
    setLEDs(false,true);
    USB.println("Coordinator started!");
    /*
    // Waiting the answer
    long previous=millis();
    while ((millis()-previous)<10000) {
      if (XBee.available()) {
        xbeeZB.treatData();
        if (!xbeeZB.error_RX) {
          // Writing the parameters of the packet received
          while (--xbeeZB.pos>=0) {
            ///*
            XBee.print("Network Address Source: ");
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naS[0],HEX);
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naS[1],HEX);
            XBee.println("");
            XBee.print("MAC Address Source: ");          
            for (int b=0;b<4;b++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macSH[b],HEX);
            }
            for (int c=0;c<4;c++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macSL[c],HEX);
            }
            XBee.println();
            XBee.print("Network Address Origin: ");          
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naO[0],HEX);
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->naO[1],HEX);
            XBee.println();
            XBee.print("MAC Address Origin: ");          
            for (int d=0;d<4;d++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macOH[d],HEX);
            }
            for (int e=0;e<4;e++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->macOL[e],HEX);
            }
            XBee.println();
            XBee.print("RSSI: ");                    
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->RSSI,HEX);
            XBee.println("");         
            XBee.print("16B(0) or 64B(1): ");                    
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->mode,HEX);
            XBee.println();
            
            XBee.print("Data: ");
            // to send the stuff along we need to put it in a string
            char message[xbeeZB.packet_finished[xbeeZB.pos]->data_length];            
            for (int f=0;f<xbeeZB.packet_finished[xbeeZB.pos]->data_length;f++) {
              /////XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->data[f],BYTE);
              message[f]=(char)xbeeZB.packet_finished[xbeeZB.pos]->data[f];
            }
            XBee.print("Message: '");
            XBee.print(message);
            XBee.println("'.");
            
            ////sendSMS(message);
            ////sendToTcpServer(message);
            ////sendToGateway(message);
            ///*
            XBee.print("PacketID: ");                    
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->packetID,HEX);
            XBee.println("");      
            XBee.print("Type Source ID: ");                              
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->typeSourceID,HEX);
            XBee.println();     
            XBee.print("Network Identifier Origin: ");          
            for (int g=0;g<4;g++) {
              XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->niO[g],BYTE);
            }
            XBee.println();  
            XBee.print("Source Destination: ");
            XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->SD,HEX);
            XBee.print("Destination Endpoint: ");          
            XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->DE,HEX);
            XBee.print("Cluster ID: ");          
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->CID[0],HEX);
            XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->CID[1],HEX);
            XBee.print("Profile ID: ");          
            XBee.print(xbeeZB.packet_finished[xbeeZB.pos-1]->PID[0],HEX);
            XBee.println(xbeeZB.packet_finished[xbeeZB.pos-1]->PID[1],HEX);
            
            free(xbeeZB.packet_finished[xbeeZB.pos]);
            xbeeZB.packet_finished[xbeeZB.pos]=NULL;
            ///////////////xbeeZB.pos--;
          }
          previous=millis();
        }
      }
    }
    */
  }
  //*/
  delay(500);
  
}



Top
 Profile  
 
 Post subject: Re: connecting to GPRS failing
PostPosted: Thu May 10, 2012 7:04 pm 

Joined: Wed May 09, 2012 4:15 pm
Posts: 18
Sorry, that was the wrong code, here it is:
Quote:
// Coordinator that collects data from the different sensors
// using a predefined 64-bit PAN ID that all nodes use
char* PRM_ID="COORDINATOR_MHV_1"; // ID of this node

// Coordinator only parameters
char* PRM_SMS_RECEIVER="+31618408210"; // MdH's mobile phone number

char* PRM_NETKEY="WaspmoteKey";
char* PRM_LINKKEY="WaspmoteKey";

uint8_t PRM_COORDINATOR_PAN_ID[8]={0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08};

void setLEDs(boolean led0, boolean led1) {
  Utils.setLED(LED0,led0?LED_ON:LED_OFF);
  Utils.setLED(LED1,led1?LED_ON:LED_OFF);
}

void setup() {
  
  // setup for Serial port over USB
  USB.begin();
  USB.println("USB port started...");

  delay(10000);
    
  GPRS.begin();
  
  delay(1000);
  GPRS.setMode(GPRS_ON);
 
  delay(1000);
  GPRS.flag=0;
  // waiting while GPRS connects to the network
  int i=0;
  while(++i<=5) {
    USB.print("Checking GPRS connection #");
    USB.print(i);
    USB.println("...");
    if (GPRS.check())
      break;
  }
    
  USB.println("GPRS connected to the network...");
  
  // configure SMS and Incoming Calls
  if (GPRS.setInfoIncomingCall())
    USB.println("Info Incoming Call OK.");
  else
    USB.println("Info Incoming Call Error.");
  
  if (GPRS.setInfoIncomingSMS())
    USB.println("Info Incoming SMS OK.");
  else
    USB.println("Info Incoming SMS Error.");
    
  if (GPRS.setTextModeSMS())
    USB.println("Text Mode SMS OK.");
  else
    USB.println("Text Mode SMS Error.");

  // why does the following take so long???????
  // we can start with sending the battery level!!!!
  char message[20];
  sprintf(message,"Battery level: %u.",PWR.getBatteryLevel());
  if (GPRS.sendSMS(message,PRM_SMS_RECEIVER))
    USB.println("Battery level SMS sent.");
  else
    USB.println("Error sending battery level SMS.");

  // start XBee in order to be able to receive data from the routers
  delay(1000);
  // Inits the XBee ZigBee library
  xbeeZB.init(ZIGBEE,FREQ2_4G,NORMAL);
  
  delay(1000);
  // turn it on, so we can get something done
  xbeeZB.ON();
  
  // follow the initialization procedure as indicated in the example
  delay(1000);
  xbeeZB.setPAN(PRM_COORDINATOR_PAN_ID);
  if(!xbeeZB.error_AT)
    USB.println("Zigbee PANID set OK.");
  else
    USB.println("Error while changing Zigbee PANID.");

  /*
  // Enable security
  xbeeZB.encryptionMode(1);
  if (!xbeeZB.error_AT)
    USB.println("Zigbee security enabled.");
  else 
    USB.println("Error while enabling Zigbee security.");  
  */
  // Disable security
  xbeeZB.encryptionMode(0);
  if (!xbeeZB.error_AT)
    USB.println("Zigbee security disabled.");
  else
    USB.println("Error while disabling Zigbee security.");
  
  xbeeZB.setLinkKey(PRM_LINKKEY);
  if (!xbeeZB.error_AT)
    USB.println("Link Key set OK.");
  else
    USB.println("Error while setting Link Key.");
  /*
  xbeeZB.setAPSencryption(XBEE_ON);
  if (!xbeeZB.error_AT) 
    USB.println("APS Encryption turned on.");
  else 
    USB.println("Error while setting APS Encryption.");

  // Configuring Trust Center (only in the coordinator)
  xbeeZB.setEncryptionOptions(0x02);
  if (!xbeeZB.error_AT)
    USB.println("Security options configured.");
  else 
    USB.println("Error while configuring security.");  
  */
  // Setting Network Key (only in Coordinator)
  xbeeZB.setNetworkKey(PRM_NETKEY);
  if(!xbeeZB.error_AT)
    USB.println("Network Key set OK.");
  else
    USB.println("Error while setting Network Key.");
  
  // store the coordinator values
  xbeeZB.writeValues();
  if(!xbeeZB.error_AT)
    USB.println("Changes stored OK.");
  else
    USB.println("Error while storing values.");
  
  USB.println("Zigbee coordinator initialized...");
  USB.println();
  
  delay(1000);
  xbeeStartCoordinator();

}

void xbeeStartCoordinator() {
  // Coordinator Startup Process. Selected Extended PAN ID (see § 8.4.2 Waspmote-zigbee-networking-guide.pdf)
  // MDH 03MAY2012: I suppose we can set the pan here as well
  xbeeZB.setScanningChannels(0xFF,0xFF); // scan all channels
  xbeeZB.setDurationEnergyChannels(3);
  // ? do we need to tell it to actually start scanning?
  // blink the red LED while we're starting
  setLEDs(true,false); // turn red LED on and green LED off
  USB.println("Starting Zigbee coordinator...");
}

boolean channelSMSsent=false;
boolean isCoordinatorStarted() {
  // determine based on the association indication whether we succeeded in creating the network (i.e. start the coordinator)
  xbeeZB.getAssociationIndication();
  if (!xbeeZB.associationIndication) {
    xbeeZB.getChannel();
    if(!xbeeZB.error_AT) {
      USB.print("Channel is: ");
      USB.println(xbeeZB.channel,HEX);
      if (!channelSMSsent) {
        char message[50];
        sprintf(message,"Channel '%s': %u.",PRM_ID,xbeeZB.channel);
        if (GPRS.sendSMS(message,PRM_SMS_RECEIVER))
          channelSMSsent=true;
      }
      return true;
    }
    USB.println("ERROR: Failed to get XBee channel.");
  } else
    xbeeStartCoordinator();
  return false;
}

char message[80]; // the message to send
uint8_t messageIndex=0;
void addToMessage(byte b) {
  message[messageIndex++]=b;
  message[messageIndex]=0; // to force an ending zero, so we won't need to clear
}
void sendMessage() {
  if (GPRS.sendSMS(message,PRM_SMS_RECEIVER))
    USB.println("Message sent!");
  else
    USB.println("ERROR: Message not sent!");
  messageIndex=0;
}

boolean receiveError;
void loop() {

  delay(500);
  // if we managed to start as coordinator, we can go ahead and receive packets from the joined sensor nodes
  if (isCoordinatorStarted()) {
    setLEDs(false,true);
    USB.print("Free memory: ");
    USB.print(freeMemory());
    USB.println(".");
    /*
    USB.print(" (Free memory: ");
    USB.print(Utils.getFreeMemory());
    USB.println(").");
    */
    // Waiting the answer
    long previous=millis();
    while ((millis()-previous)<20000) {
      if (XBee.available()) {
        USB.println("Data available!");
        delay(100);
        receiveError=false;
        if (xbeeZB.treatData()) {
          USB.println("Treat data failed!");
          receiveError=true;
        }
        if (xbeeZB.error_RX) {
          USB.println("Receive error!");
          receiveError=true;
        }
        if (xbeeZB.pos>0) {
          USB.println("Data received.");
          do {
            for (int f=0;f<xbeeZB.packet_finished[xbeeZB.pos-1]->data_length;f++)
              addToMessage(xbeeZB.packet_finished[xbeeZB.pos-1]->data[f]);
            sendMessage();
            free(xbeeZB.packet_finished[xbeeZB.pos-1]);
            xbeeZB.packet_finished[xbeeZB.pos-1]=NULL;
            xbeeZB.pos--;
          } while (xbeeZB.pos>0);
          previous=millis();
        } else
          USB.println("No data received!");
      } else
        USB.println("No data available!");
      delay(250);
    }
  } else // show red
    setLEDs(true,false);

  delay(500);
  setLEDs(false,false); // turn LEDs off
  
}



Top
 Profile  
 
 Post subject: Re: connecting to GPRS failing
PostPosted: Fri May 11, 2012 9:22 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7818
marc de hoogh,

Memory values of 170 are too low and can cause you code stops due to no RAM memory is available.

You can reduce memory used by using "calloc" function for large variables, and also avoiding using to much USB prints (every string printed by USB is allocated in RAM memory). Try to short them and delete unnecessary messages.

Try these advises and let us know if you achieve higher Free memory values.

Best Regards.


Top
 Profile  
 
 Post subject: Re: connecting to GPRS failing
PostPosted: Fri May 11, 2012 11:06 am 

Joined: Wed May 09, 2012 4:15 pm
Posts: 18
Hi,

I've followed your advice and shortened or removed USB print messages.
Perhaps there's a way not to have to do that?

Furthermore, at the start of setup the free memory was 1439, at the start of loop 1118, after receiving Zigbee messages down to 644, and after some more messages down to 280.

These messages are then SMSed, but every now and then treatData() fails, and messages are lost. Any way to prevent loosing these messages?

With kind regards.

Marc P. de Hoogh


Top
 Profile  
 
 Post subject: Re: connecting to GPRS failing
PostPosted: Thu May 17, 2012 11:54 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7818
marc de hoogh,

Sorry for our delay answering.

Looking at your last code we think that you should free packets built after they are sent by using instructions:
Quote:
free(paq_sent);
  paq_sent=NULL;

You can see it in the examples.

Please try this and let us know if your memory is still decreasing to zero.

Best regards.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 9 posts ]


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum

Search for:


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Libelium theme based on 610nm Style by Daniel St. Jules of http://www.gamexe.net


© Libelium Comunicaciones Distribuidas S.L. | Terms of use