1

I have a Gboard module (from Itead) configured to recieve data from Xbee sensors, and log data to a server DB on inet (via GPRS). This module is programmed with Arduino IDE. I turn on the module (without any xbee sending, to check that the GPRS is working). The module works fine; The GPRS part sends some "control" packages to the DB to inet as expected. The problem appears after 1'5 hours because module is still on but the GPRS doesn't seem to be working (doesn't send any more packages to inet). Excuse me for my messy code, I´m newbie! (a lot of copy/pastes from others!). Thank you in advance for your tipps and recomendations!. Note: I use a power supply 12v and 2Amp.

Code:

#include <elapsedMillis.h>///para controles
#include <SoftwareSerial.h>
#include <Streaming.h> ///para xbee
#include <XBee.h>      ///para xbee
int xbeeRecibido;
char urlArreglo[100];
uint8_t slCmd[] = {'S', 'L'};
uint8_t dbCmd[] = {'D', 'B'};
char adr_part[8];
char rssiValor_part[8];
String adr = "";
String rssiValor = "";
AtCommandRequest atRequest = AtCommandRequest();
AtCommandResponse atResponse = AtCommandResponse();
elapsedMillis timeElapsed = 0;
unsigned long interval = 180000 ;////en milisegundos  cada 3 min.
String direccionRed;
int bateria;
String valorRssi;
int actividadPir;
String datosHttp; ///concatenacion de datos para enviar por HTTP
int8_t answer;
char data[512];
int data_size;
char aux_str[100];
char aux;
int x = 0;
XBee xbee = XBee();                //XBee object
Rx16Response rx = Rx16Response();
SoftwareSerial mySerial(2, 3); // RX, TX (ahora puesto en TX, RX)
int b01;
int b02;
int b03;
int b04; //source network adress 64Bit
int b05; //source network adress 64Bit
int b06; //source network adress 64Bit
int b07; //source network adress 64Bit
int b08; //source network adress 64Bit
int b09; //source network adress 64Bit
int b10; //source network adress 64Bit
int b11; //source network adress 64Bit
int b12; //source network adress 16Bit
int b13; //source network adress 16Bit
int b14;
int b15; //pir digital
int b16; //bateria
int b17; //checksum
char response[100];
unsigned long previous;

void setup() {
  pinMode(6, OUTPUT);///para encender SIM900 ...en pin6 HIGH de arduino GBOARD
  digitalWrite(6, HIGH);///para encender SIM900 ...en pin6 HIGH de arduino GBOARD
  mySerial.begin(9600);
  Serial.begin(9600);
  ///////GPRS////////
  power_on();
  delay(3000);
  sendATcommand("AT+ipr=9600", "OK", 2000);///////MIO PARA BAUDERATE A 9600 (por defecto 0=auto..¿se apaga?)
  // sets the PIN code
  sendATcommand("AT+CPIN=****", "OK", 2000);
  delay(3000);
  while (sendATcommand2("AT+CREG?", "+CREG: 0,1", "+CREG: 0,5", 2000) == 0);
  sendATcommand("AT+SAPBR=3,1,\"Contype\",\"GPRS\"", "OK", 2000);
  sendATcommand("AT+SAPBR=3,1,\"APN\",\"gprs.lebaramobile.es\"", "OK", 2000);
  sendATcommand("AT+SAPBR=3,1,\"USER\",\"\"", "OK", 2000);
  sendATcommand("AT+SAPBR=3,1,\"PWD\",\"\"", "OK", 2000);
  while (sendATcommand("AT+SAPBR=1,1", "OK", 20000) == 0)
  {
    delay(5000);
  }
  ////FIN GPRS////
  ////PRIMER AUTOCONTROL!
  lanzarHttpAutoControl();
  delay(15000);
}

void loop() {
  // PARA AUTOCONTROL!
  if (timeElapsed > interval)
  {
    lanzarHttpAutoControl();
    timeElapsed = 0;             // reset the counter to 0 so the counting starts over...
  }
  if (Serial.available() == 18) { //make sure the frame is all there
    if (Serial.read() == 0x7E) {
      b01 = Serial.read();
      b02 = Serial.read();
      b03 = Serial.read();
      b04 = Serial.read(); //source network adress 64Bit
      b05 = Serial.read(); //source network adress 64Bit
      b06 = Serial.read(); //source network adress 64Bit
      b07 = Serial.read(); //source network adress 64Bit
      b08 = Serial.read(); //source network adress 64Bit
      b09 = Serial.read(); //source network adress 64Bit
      b10 = Serial.read(); //source network adress 64Bit
      b11 = Serial.read(); //source network adress 64Bit
      b12 = Serial.read(); //source network adress 16Bit
      b13 = Serial.read(); //source network adress 16Bit
      b14 = Serial.read();
      b15 = Serial.read(); //pir digital
      b16 = Serial.read(); //bateria
      b17 = Serial.read();  //checksum
      direccionRed =  String(b04, HEX);
      direccionRed +=  String(b05, HEX);
      direccionRed +=  String(b06, HEX);
      direccionRed +=  String(b07, HEX);
      direccionRed +=  String(b08, HEX);
      direccionRed +=  String(b09, HEX);
      direccionRed +=  String(b10, HEX);
      direccionRed +=  String(b11, HEX);
      actividadPir = b15;
      bateria = int(b16);
      xbeeRecibido = 1;
    } //fin if 0x7E
  }///fin recorrer bytes
  Serial.flush();
  Serial.end();
  Serial.begin(9600);///paro el serial para evitar solapaciones, y hacer esperar a los sensores (al no recibir resp. del gateway)
  if (xbeeRecibido == 1) {///si se ha recibido info de xbee sensor
    colectorDatos();
    xbeeRecibido = 0;
  }
}//fin loop

///FUNCION PARA RSSI XBEE///
void rss() {  ///PARA RSSI
  /* returns received signal strength value for the last RF data packet */
  union {
    byte B;
    char C;
  } atCmd[3];
  AtCommandRequest atCmdReq;
  AtCommandResponse atResp;
  byte respLen, *resp, dBm;
  strcpy(&atCmd[0].C, "DB");
  atCmdReq = AtCommandRequest(&atCmd[0].B);
  atResp = AtCommandResponse();
  xbee.send(atCmdReq);
  if (xbee.readPacket(5000)) {
    if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(atResp);
      if (atResp.isOk()) {
        respLen = atResp.getValueLength();
        if (respLen == 1) {
          resp = atResp.getValue();
          dBm = resp[0];
          valorRssi = (String)dBm;

        }
        else {
        }
      }
      else {

      }
    }
    else {
    }
  }
  else {
  }
}///fin rss()


void power_on() {
  uint8_t answer = 0;
  // checks if the module is started
  answer = sendATcommand("AT", "OK", 2000);
  if (answer == 0)
  {
    // waits for an answer from the module
    while (answer == 0) {
      // Send AT every two seconds and wait for the answer
      answer = sendATcommand("AT", "OK", 2000);
    }
  }
}

int8_t sendATcommand(char* ATcommand, char* expected_answer1, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  memset(response, 0, sizeof(response));///a cero el array ///15-08-2015
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( mySerial.available() > 0) mySerial.read();   // Clean the input buffer
  mySerial.println(ATcommand);    // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (mySerial.available() != 0) {
      response[x] = mySerial.read();
      x++;
      if (strstr(response, expected_answer1) != NULL)
      {
        answer = 1;
      }
    }
  }
  while ((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}

int8_t sendATcommand2(char* ATcommand, char* expected_answer1,
                      char* expected_answer2, unsigned int timeout) {
  uint8_t x = 0,  answer = 0;
  memset(response, 0, sizeof(response));///a cero el array ///15-08-2015
  memset(response, '\0', 100);    // Initialize the string
  delay(100);
  while ( mySerial.available() > 0) mySerial.read();   // Clean the input buffer
  mySerial.println(ATcommand);    // Send the AT command
  x = 0;
  previous = millis();
  do {
    if (mySerial.available() != 0) {
      response[x] = mySerial.read();
      x++;
      // check if the desired answer 1 is in the response of the module
      if (strstr(response, expected_answer1) != NULL)
      {
        answer = 1;
      }
      // check if the desired answer 2 is in the response of the module
      if (strstr(response, expected_answer2) != NULL)
      {
        answer = 2;
      }
    }
    // Waits for the asnwer with time out
  } while ((answer == 0) && ((millis() - previous) < timeout));
  return answer;
}

void colectorDatos() {
  rss(); ////recupero rssi que se pone en variable valorRssi
  datosHttp = direccionRed + ";"; ///cualquier valor del array, todos deverian tener mismo valor (direccion64bits)
  datosHttp += bateria;
  datosHttp += ";";
  datosHttp += valorRssi;
  datosHttp += ";";
  datosHttp += actividadPir; //pir
  envio(datosHttp); ///envio por gprs
}///fin colectorDatos

void lanzarHttpAutoControl() {
  adr = "";
  // set command to SL to get low part of MAC address
  atRequest.setCommand(slCmd);
  while (!sendAtCommand()) {};
  for (int i = 0; i < atResponse.getValueLength(); i++) {
    mySerial.print(atResponse.getValue()[i], HEX);
    sprintf(adr_part, "%lX", (unsigned long)(atResponse.getValue()[i]));
    if (atResponse.getValue()[i] <= 0xF)
      adr += 0;
    adr += adr_part;
  }
  envio(adr); ///envio por gprs
}

///envio GPRS...recibe String con datos (o control o sensor)
void envio(String entrada) {
  Serial.end();///paro el serial para evitar solapaciones, y hacer esperar a los sensores (al no recibir resp. del gateway)
  memset(urlArreglo, 0, sizeof(urlArreglo));///a cero el array
  answer = sendATcommand("AT+HTTPINIT", "OK", 10000);
  if (answer == 1)
  {
    // Sets CID parameter
    answer = sendATcommand("AT+HTTPPARA=\"CID\",1", "OK", 5000);
    if (answer == 1)
    {
      //char urlArreglo[100];
      sprintf(urlArreglo,  "AT+HTTPPARA=\"URL\",\"www.pestmonitors.com/dream/prueba_gsm_get.php?datos=%s\"", entrada.c_str());
      answer = sendATcommand(urlArreglo, "OK", 5000);
      delay(5000);
      if (answer == 1)
      {
        answer = sendATcommand("AT+HTTPACTION=0", "+HTTPACTION:0,200", 10000);
      }
      else
      {
      }
      sendATcommand("AT+HTTPTERM", "OK", 5000);
      delay(5000);
    }
  }
  Serial.begin(9600);///paro el serial para evitar solapaciones, y hacer esperar a los sensores (al no recibir resp. del gateway)
}

// used here to sens AT command to the Xbee to get information such as the MAC address
int sendAtCommand() {
  xbee.send(atRequest);
  if (xbee.readPacket(5000)) {
    if (xbee.getResponse().getApiId() == AT_COMMAND_RESPONSE) {
      xbee.getResponse().getAtCommandResponse(atResponse);
      if (atResponse.isOk()) {
        if (atResponse.getValueLength() > 0) {
          mySerial.println(atResponse.getValueLength(), DEC);
          for (int i = 0; i < atResponse.getValueLength(); i++) {
            mySerial.print(atResponse.getValue()[i], HEX);
          }
        }
        return 1;
      }
      else {
      }
    } else {
      mySerial.print(xbee.getResponse().getApiId(), HEX);
    }
  } else {
    if (xbee.getResponse().isError()) {
    }
    else {
    }
  }
  return 0;
}
  • When it's not working anymore, is it still attached to the network? Does it always stop at 1h30? Or does it differ? Might be something that the connection will be automatically dropped if it's longer as 1h30? You should ocassionally check your connection and handle the errors? – Paul Aug 17 '15 at 18:56
  • Thanks for comment FuaZe! I don`t know if still attached because I don't write any debug LED or something (I'm very noob newbie, jejej). Yes, is just 1h30 (1 or 2 min up or down). Now I´m trying a possible solution: Reseting all sketch (with "asm volatile" command) and reseting hardware SIM900 (gboard have a especific PIN for reset SIM900 by software(7)), on at 1 hour... I don't know if that is a good solution...sorry for my english! Thanks man! – xavi Aug 17 '15 at 19:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.