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.
CODECode:
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);
}