Post a new topicPost a reply Page 1 of 1   [ 9 posts ]
Author Message
 Post subject: URGENT: waspmote Hanging with LED 0 continuously on.
PostPosted: Thu Jan 06, 2011 4:21 pm 

Joined: Tue Jan 04, 2011 5:29 pm
Posts: 11
Hi,
I have a setup of GPRS and XBee connected to the mote. The GPRS sends data to a pachube feed after every 5 mins and then the mote, the GPRS and the XBee are put into hibernation. Everything seems to work fine for 2 to 3 runs over the code, after that the mote just hangs with led 0 (Programable LED that blinks when the mote is reset) continuously on. I am not sure what's going wrong here, the mote is running on the battery provided in the kit. Any urgent help would be greatly appreciated.

CODE

Code:
char command[30];
uint8_t n=0;
unsigned long lastConnected=0;
unsigned long postingInterval=120000; // 2 minutes
boolean debug;
char* logFile="runLog.txt";
boolean SDOK=false;

void setup(){
  debug=false;
  // setup for Serial port over USB
  USB.begin();
  USB.println("USB port setup ok");
 
//  if(SD.isSD()==1){
//    prln("SD Card Found waiting for initilization");
//    char* initRes=SD.init();
//    if(SD.flag==0){
//      prln("SD Card Initilized");
//      if(SD.isFile(logFile)!=1){
//        SD.create(logFile);
//      }
//      SDOK=true;
//    }else{
//      pr("SD Card Initilization failed with error code : ");
//      char *eval= (char*) calloc(5,sizeof(char));
//      Utils.long2array((long)SD.flag,eval);
//      prln(eval);
//      prln(initRes);
//    }
//  }
//  else{
//    SDOK=false;
//    prln("No SD Card Detected Logging Disabled");
//  }
  prln("Setting up RTC");
  RTC.ON();
  prln("RTC setup ok");
  setupGPRS();
}

void setupGPRS(){
  /**********/
  if(debug){
    prln("Setting up XBee");
    setupXBee();
    prln("XBee setup ok");
  }
  /**********/
  // setup for GPRS seial port
  GPRS.ON();
  prln("GPRS module ready... Waiting for GPRS to Connect to Network");

  while(!GPRS.check());
  prln("GPRS connected to the network");


  // configure SMS and Incoming Calls
  if(GPRS.setInfoIncomingCall()){
    prln("Info Incoming Call OK");
  }
  if(GPRS.setInfoIncomingSMS()){
    prln("Info Incoming SMS OK");
  }
  if(GPRS.setTextModeSMS()){
    prln("Text Mode SMS OK");
  }
}

void loop(){

  Utils.setLED(LED0, LED_ON);
  Utils.setLED(LED1, LED_ON);

  RTC.getTemperature();
  delay(2000);
  USB.print("RTC temperature is ");
  USB.print(RTC.temp,DEC);
  USB.println(" C.");
  USB.print("Remaining Battery ");
  USB.print(PWR.getBatteryLevel(),DEC);
  USB.println(" %");
  float volt=calcBatteryVoltage(PWR.getBatteryLevel());
  prln("Temperature and Battery readings taken");
  // Configure GPRS Connection
  if(GPRS.configureGPRS()){
    prln("GPRS Configured waiting for socket to open");

    if(GPRS.createSocket("www.pachube.com","80",GPRS_CLIENT)){
      prln("Socket Opened OK. Sending data to pachube");
      USB.print("Session Number: ");
      int i=0;
      while( GPRS.socket_ID[i]!='\r' ){
        USB.print(GPRS.socket_ID[i]-'0',DEC);
        i++;
      }
      i=0;
      prln("Socket All OK");

      /********************** Send Data to Pachube *****************************************/
      lastConnected=millis();
      prln("Constructing Pachube Request ... ");
      char* pachubeReq=makePachubeRequest(RTC.temp,PWR.getBatteryLevel(),volt);
      // Send data via socket
      prln("Pachube Request Constructed");
      prln(pachubeReq);
      if(GPRS.sendData(pachubeReq,GPRS.socket_ID)){
        prln("Data sent to pachube");
      }
      /********************** Check Pachube Response *****************************************/
      if(!chkResponse()){
        prln("Error: Data not accepted by server");
        prln(GPRS.data_URL);
      }
    }
    else {
      prln("Error: Failed to open TCP socket");
    }
    delay(2000);
    // Close socket
    if(GPRS.closeSocket(GPRS.socket_ID)){
      prln("Socket closed");
    }
  }
  else{
    prln("Error: Failed to configure GPRS");
  }

  /********************** Save power and put the module in hibernation mode ***************************************/
  //Set Waspmote to Hibernate, waking up after 5 mins to send data again
  //   long remTime=millis()-lastConnected;
  //   remTime=postingInterval-remTime;
  //   remTime=remTime/1000; //conversion from mili seconds to seconds
  //   int remSec=(int)(remTime%60);
  //   int remMin=(int)(remTime/60);
  //   char *time;
  //   if(remSec>9){
  //     sprintf(time,"00:00:0%d:%d",remMin,remSec);
  //   }else{
  //     sprintf(time,"00:00:0%d:0%d",remMin,remSec);
  //   }

  prln("Going to sleep.");
  closeXbee();
  delay(postingInterval);

  /*** put gprs module back on ****/
  setupGPRS();
}

//*** see last post before using this function****///

[color=#FF0000]float calcBatteryVoltage(float per){
  pinMode(25,OUTPUT);
  digitalWrite(25,HIGH);
  float val=analogRead(0);
  digitalWrite(25,LOW);
  char *pdata= (char*) calloc(10,sizeof(char));
  char *pval= (char*) calloc(10,sizeof(char));
  pr("Battery raw reading : ");
  Utils.float2String(val,pval,2);
  prln(pval);
  float volt=(4.2*3.3*val)/(1024*2.07);
  Utils.float2String(volt,pdata,2);
  pr("Battery voltage reading : ");
  prln(pdata);
  return volt;
}[/color]

boolean chkResponse(){
  // Get data from socket
  boolean allOk=false;
  sprintf(command,"%s%c%c","GET / HTTP/1.0",'\r','\n');
  if(GPRS.sendData(command,GPRS.socket_ID)) USB.println("Data sent"); 
  delay(2000);
  n=0;
  while(!n){
    n=GPRS.readData(GPRS.socket_ID,"100");
  }
  if(contains(GPRS.data_URL,"HTTP/1.1 200 OK")){
    return true;
  }
  else{
    return false;
  }
}

char* makePachubeRequest(float temp,float battery,float volt){
  char *pdata= (char*) calloc(300,sizeof(char));
  char *te = (char*) calloc(10,sizeof(char));
  char *btry = (char*) calloc(10,sizeof(char));
  char *vol = (char*) calloc(10,sizeof(char));
  char *data =(char*) calloc(50,sizeof(char));
  Utils.float2String(temp,te,2);
  replaceChar(te,',','.');
  Utils.float2String(battery,btry,2);
  replaceChar(btry,',','.');
  Utils.float2String(volt,vol,2);
  replaceChar(vol,',','.');
  sprintf(data,"%s%s%s%s%s%s%s","1,",te,"\n2,",btry,"\n3,",vol,"\n");
  int len=Utils.sizeOf(data);
  sprintf(pdata,"%s%d%s%s",
  "PUT /v2/feeds/14524.csv HTTP/1.1\nHost: api.pachube.com\nX-PachubeApiKey: KEY\nContent-Length: "
    ,len
    ,"\n\n"
    ,data);
  return pdata;
}
/********************************** Helper function for strings *********************************/
int indexOf(char* str,char find){
  int index=-1;
  int sz=Utils.sizeOf(str);
  for(int i=0;i<sz;i++){
    if(str[i]==find){
      index=i;
      i=sz+1;
    }
  }
  return index;
}

void replaceChar(char* str,char toReplace,char replaceWith){
  int index=indexOf(str,toReplace);
  str[index]=replaceWith;
}

boolean contains(char* str,char* con){
  boolean ok=true;
  int szStr=Utils.sizeOf(str);
  int szCon=Utils.sizeOf(con);
  int sIndex=indexOf(str,con[0]);
  for(int i=0;i<szCon;i++){
    if(str[sIndex+i]!=con[i]){
      ok=false;
    }
  }
  return ok;
}

float map(float x, float in_min, float in_max, float out_min, float out_max)
{
  return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
}

void pr(char* msg){
  USB.print(msg);
  if(debug){
    sendDataXbee(msg);
  }
  if(SDOK){
    SD.append(logFile,msg);
  }
}

void prln(char* msg){
  USB.println(msg);
  if(debug){
    sendDataXbee(msg);
  }
  if(SDOK){
    SD.appendln(logFile,msg);
  }
}

/************* XBeee *********************/

packetXBee* paq_sent;
int8_t state=0;
long previous=0;
char aux[300];
char mac[16];
int aux_1 = 0;
int aux_2 = 0;

uint8_t direccion[8]={
  0x00,0x13,0xA2,0x00,0x40,0x55,0x34,0x03};

void setupXBee(){
  xbee.init(XBEE_802_15_4,FREQ2_4G,NORMAL);
  xbee.ON();
  delay(500);

  int counter = 0; 
  while(xbee.getOwnMac()==1&&counter<4){
    xbee.getOwnMac();
    counter++;
  }

  xbee.getSoftVersion();

  delay(10);

  for(int i=0;i<4;i++){
    aux_1=xbee.sourceMacHigh[i]/16;
    aux_2=xbee.sourceMacHigh[i]%16;

    if (aux_1<10){
      mac[2*i]=aux_1+48;
    }
    else{
      mac[2*i]=aux_1+55;
    }
    if (aux_2<10){
      mac[2*i+1]=aux_2+48;
    }
    else{
      mac[2*i+1]=aux_2+55;
    }
  }
  for(int i=0;i<4;i++){
    aux_1=xbee.sourceMacLow[i]/16;
    aux_2=xbee.sourceMacLow[i]%16;
    if (aux_1<10){
      mac[2*i+8]=aux_1+48;
    }
    else{
      mac[2*i+8]=aux_1+55;
    }
    if (aux_2<10){
      mac[2*i+9]=aux_2+48;
    }
    else{
      mac[2*i+9]=aux_2+55;
    }
  }
  delay(2000);
}

void closeXbee(){
  xbee.OFF();
  USB.begin();
  delay(1000);
}

void sendDataXbee(char* data){
  for(int i=0;i<300;i++){
    aux[i]=' ';
  }
  sprintf(aux,"%s%s%s%c%c",data,mac,"--",'\r','\n');
  paq_sent=(packetXBee*) calloc(1,sizeof(packetXBee));
  paq_sent->mode=BROADCAST;
  paq_sent->MY_known=0;
  paq_sent->packetID=0x52;
  paq_sent->opt=0;
  xbee.hops=0;
  xbee.setOriginParams(paq_sent,MAC_TYPE);
  xbee.setDestinationParams(paq_sent, direccion, aux, MAC_TYPE, DATA_ABSOLUTE);
  xbee.sendXBee(paq_sent);
  free(paq_sent);
  paq_sent=NULL;
  delay(2000);
}


Top
 Profile  
 
 Post subject: Re: URGENT: waspmote Hanging with LED 0 continuously on.
PostPosted: Thu Jan 06, 2011 4:43 pm 

Joined: Tue Jan 04, 2011 5:29 pm
Posts: 11
Sorry i posted the incorrect code (testing version) for hibernation part, the correct code is

Code:
prln("Going to sleep.");
   closeXbee();
   GPRS.setMode(GPRS_HIBERNATE);
   PWR.hibernate("00:00:05:00",RTC_OFFSET,RTC_ALM1_MODE2);


Instead of

Code:
/********************** Save power and put the module in hibernation mode ***************************************/
  //Set Waspmote to Hibernate, waking up after 5 mins to send data again
  //   long remTime=millis()-lastConnected;
  //   remTime=postingInterval-remTime;
  //   remTime=remTime/1000; //conversion from mili seconds to seconds
  //   int remSec=(int)(remTime%60);
  //   int remMin=(int)(remTime/60);
  //   char *time;
  //   if(remSec>9){
  //     sprintf(time,"00:00:0%d:%d",remMin,remSec);
  //   }else{
  //     sprintf(time,"00:00:0%d:0%d",remMin,remSec);
  //   }

  prln("Going to sleep.");
  closeXbee();
  delay(postingInterval);

  /*** put gprs module back on ****/
  setupGPRS();


Top
 Profile  
 
 Post subject: Re: URGENT: waspmote Hanging with LED 0 continuously on.
PostPosted: Mon Jan 10, 2011 10:35 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7818
Hi,

You have to use the function 'PWR.ifHibernate' in the setup to avoid hibernation problems.

Are you also using the latest API version (13) ?

Regards


Top
 Profile  
 
 Post subject: Re: URGENT: waspmote Hanging with LED 0 continuously on.
PostPosted: Mon Jan 10, 2011 3:57 pm 

Joined: Tue Jan 04, 2011 5:29 pm
Posts: 11
i'll try the if.hibernate function. I tried the latest api v0.13 however the code gets stuck at the getBatteryLevel function. Which hasn't been changed in the core api as per the change log. However there is a change in the return data types of the core library from uint8 to uint16 (dont remember exactly). So i reverted back to v 0.12


Top
 Profile  
 
 Post subject: Re: URGENT: waspmote Hanging with LED 0 continuously on.
PostPosted: Tue Jan 11, 2011 10:24 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7818
Hi,

The 'getBatteryLevel' function hasn't been changed and it returns the same data type as before.

Regards


Top
 Profile  
 
 Post subject: Re: URGENT: waspmote Hanging with LED 0 continuously on.
PostPosted: Wed Jan 12, 2011 7:30 pm 

Joined: Tue Jan 04, 2011 5:29 pm
Posts: 11
Sorry its the Utils.getFreeMemory() function i was taking about not the getBatteryLevel(). I have tried it again the code does hang on the call to Utils.getFreeMemory() when using v0.13 but works ok on v0.12.


Top
 Profile  
 
 Post subject: Re: URGENT: waspmote Hanging with LED 0 continuously on.
PostPosted: Thu Jan 13, 2011 10:19 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7818
Hi,

That function was changed to return a 16-bit number instead of a 8-bit that returned previously. If you experience problems you can use the 0.13 version but copy the 'getFreeMemory' from the 0.12 version.

Regards


Top
 Profile  
 
 Post subject: Re: URGENT: waspmote Hanging with LED 0 continuously on.
PostPosted: Tue Nov 22, 2011 12:09 pm 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7818
We have realized that the function calcBatteryVoltage() shown in the first post is wrong and can cause Waspmote malfunctioning due to a memory lost.

If you are going to use it take care of free the pointers after their usage.

The correct function is shown below.

Quote:
//    calcBatteryVoltage
//----------------------------------
float calcBatteryVoltage(float per){
  pinMode(25,OUTPUT);
  digitalWrite(25,HIGH);
  float val=analogRead(0);
  digitalWrite(25,LOW);
  char *pdata= (char*) calloc(10,sizeof(char));
  char *pval= (char*) calloc(10,sizeof(char));
  DBG("Battery raw reading : ");
  Utils.float2String(val,pval,2);
  DBGln(pval);
  float volt=(4.2*3.3*val)/(1024*2.07);
  Utils.float2String(volt,pdata,2);
  DBG("Battery voltage reading : ");
  DBGln(pdata);
  
  free(pdata);
  pdata = NULL;
  free(pval);
  pval = NULL;  
  return volt;
}



Top
 Profile  
 
 Post subject: Re: URGENT: waspmote Hanging with LED 0 continuously on.
PostPosted: Tue May 15, 2012 7:06 pm 

Joined: Tue May 15, 2012 6:47 pm
Posts: 8
Hey malikobaid,
I am trying to use your code but ia m facing trouble, can you please check the floowing link:
viewtopic.php?f=16&t=9136


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:
cron


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