Post a new topicPost a reply Page 1 of 1   [ 5 posts ]
Author Message
 Post subject: Sending Data to Pachube (now Cosm)
PostPosted: Tue May 15, 2012 6:57 pm 

Joined: Tue May 15, 2012 6:47 pm
Posts: 8
Hey, I am using the hilo GPRS module to open a tcp socket and send data to be viewed on pachube.
I found a code on the forums and edited it for my needs. It opens the socket and sends but when i check the response, it says that the server didnt accept the connection. Help Please!!!
Please find code and serial monitor output
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");

  setupGPRS();
}

void setupGPRS(){
  // setup for GPRS seial port
  GPRS.ON();
   USB.println("GPRS module ready... Waiting for GPRS to Connect to Network");

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


  // configure SMS and Incoming Calls
  if(GPRS.setInfoIncomingCall())
  {
     USB.println("Info Incoming Call OK");
  }
  if(GPRS.setInfoIncomingSMS())
  {
     USB.println("Info Incoming SMS OK");
  }
  if(GPRS.setTextModeSMS())
  {
     USB.println("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.");
 
  // Configure GPRS Connection
  if(GPRS.configureGPRS())
  {
   USB.println("GPRS Configured waiting for socket to open");

    if(GPRS.createSocket("216.52.233.121","80",GPRS_CLIENT))//could replace GPRS_CLIENT with 0
    {
      USB.println("Socket Opened OK. Sending data to COSM");
     USB.println("Session Number: ");
      int i=0;
      while( GPRS.socket_ID[i]!='\r' )
      {
        USB.println(GPRS.socket_ID[i]-'0',DEC);
        i++;
      }
      i=0;
      USB.println("Socket All OK");

      /********************** Send Data to COSM *****************************************/
      lastConnected=millis();
      USB.println("Constructing Cosm Request ... ");
      char* COSMReq=makeCOSMRequest(RTC.temp);
      // Send data via socket
      USB.println("COSM Request Constructed");
      USB.println(COSMReq);
      if(GPRS.sendData(COSMReq,GPRS.socket_ID))
      {
        USB.println("Data sent to COSM");
      }
      /********************** Check COSM Response *****************************************/
      if(!chkResponse())
      {
        USB.println("Error: Data not accepted by server");
        USB.println(GPRS.data_URL);
      }
    }
    else
    {
      USB.println("Error: Failed to open TCP socket");
    }
    delay(2000);
    // Close socket
    if(GPRS.closeSocket(GPRS.socket_ID))
    {
      USB.println("Socket closed");
    }
    if(GPRS.deleteSocket(GPRS.socket_ID))
    {
      USB.println("Socket deleted");
    }
  }
  else
  {
    USB.println("Error: Failed to configure GPRS");
  }
 

}



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

char* makeCOSMRequest(float temp)
{
  char *pdata= (char*) calloc(300,sizeof(char));
  char *te = (char*) calloc(10,sizeof(char));
  char *data =(char*) calloc(50,sizeof(char));
  USB.println("Creating Arrays");
  Utils.float2String(temp,te,2);
  USB.println("convering string to float ");
  replaceChar(te,',','.');
  USB.println("change every . with te");
  sprintf(data,"%s%s%s","1,",te,"\n");
  USB.println("change data to lines");
  int len=Utils.sizeOf(data);
  USB.println("getting size of data");
  sprintf(pdata,"%s%d%s%s", "PUT /v2/feeds/58790.csv HTTP/1.1\nHost: api.cosm.com\nX-CosmApiKey: _ieNOCTcknvBbrm4S-z_QIN6it6SAKxFRXVJVDhaYklCOD0g\nContent-Length: " ,len ,"\n\n",data);
  USB.println("change Pdata F");
  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;
}


Image


Top
 Profile  
 
 Post subject: Re: Sending Data to Pachube (now Cosm)
PostPosted: Wed May 16, 2012 8:46 am 

Joined: Mon Sep 28, 2009 1:06 pm
Posts: 7516
k_zikry,

Are you receiving data correctly in your socket? Are you able to read it?

Let us know.

One tip: For large codes please use Edit/copy for forum from Waspmote IDE.

Regards


Top
 Profile  
 
 Post subject: Re: Sending Data to Pachube (now Cosm)
PostPosted: Thu May 17, 2012 4:11 pm 

Joined: Tue May 15, 2012 6:47 pm
Posts: 8
no i checked on the csv file on pachube and it has not received any data.
the link for it is :
https://api.cosm.com/v2/feeds/58790.csv


Top
 Profile  
 
 Post subject: Re: Sending Data to Pachube (now Cosm)
PostPosted: Thu May 17, 2012 4:15 pm 

Joined: Tue May 15, 2012 6:47 pm
Posts: 8
here is the code again
Quote:
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");

  setupGPRS();
}

void setupGPRS(){
  // setup for GPRS seial port
  GPRS.ON();
   USB.println("GPRS module ready... Waiting for GPRS to Connect to Network");

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


  // configure SMS and Incoming Calls
  if(GPRS.setInfoIncomingCall())
  {
     USB.println("Info Incoming Call OK");
  }
  if(GPRS.setInfoIncomingSMS())
  {
     USB.println("Info Incoming SMS OK");
  }
  if(GPRS.setTextModeSMS())
  {
     USB.println("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.");
  
  // Configure GPRS Connection
  if(GPRS.configureGPRS())
  {
   USB.println("GPRS Configured waiting for socket to open");

    if(GPRS.createSocket("216.52.233.121","80",GPRS_CLIENT))//could replace GPRS_CLIENT with 0
    {
      USB.println("Socket Opened OK. Sending data to COSM");
     USB.println("Session Number: ");
      int i=0;
      while( GPRS.socket_ID[i]!='\r' )
      {
        USB.println(GPRS.socket_ID[i]-'0',DEC);
        i++;
      }
      i=0;
      USB.println("Socket All OK");

      /********************** Send Data to COSM *****************************************/
      lastConnected=millis();
      USB.println("Constructing Cosm Request ... ");
      char* COSMReq=makeCOSMRequest(RTC.temp);
      // Send data via socket
      USB.println("COSM Request Constructed");
      USB.println(COSMReq);
      if(GPRS.sendData(COSMReq,GPRS.socket_ID))
      {
        USB.println("Data sent to COSM");
      }
      /********************** Check COSM Response *****************************************/
      if(!chkResponse())
      {
        USB.println("Error: Data not accepted by server");
        USB.println(GPRS.data_URL);
      }
    }
    else
    {
      USB.println("Error: Failed to open TCP socket");
    }
    delay(2000);
    // Close socket
    if(GPRS.closeSocket(GPRS.socket_ID))
    {
      USB.println("Socket closed");
    }
    if(GPRS.deleteSocket(GPRS.socket_ID))
    {
      USB.println("Socket deleted");
    }
  }
  else
  {
    USB.println("Error: Failed to configure GPRS");
  }
  

}



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

char* makeCOSMRequest(float temp)
{
  char *pdata= (char*) calloc(300,sizeof(char));
  char *te = (char*) calloc(10,sizeof(char));
  char *data =(char*) calloc(50,sizeof(char));
  USB.println("Creating Arrays");
  Utils.float2String(temp,te,2);
  USB.println("convering string to float ");
  replaceChar(te,',','.');
  USB.println("change every . with te");
  sprintf(data,"%s%s%s","1,",te,"\n");
  USB.println("change data to lines");
  int len=Utils.sizeOf(data);
  USB.println("getting size of data");
  sprintf(pdata,"%s%d%s%s", "PUT /v2/feeds/58790.csv HTTP/1.1\nHost: api.cosm.com\nX-CosmApiKey: _ieNOCTcknvBbrm4S-z_QIN6it6SAKxFRXVJVDhaYklCOD0g\nContent-Length: " ,len ,"\n\n",data);
  USB.println("change Pdata F");
  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;
}



Top
 Profile  
 
 Post subject: Re: Sending Data to Pachube (now Cosm)
PostPosted: Fri May 18, 2012 10:21 am 

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

the message "data not accepted by the server" indicates a problem in the server, not in the module. The most probably problem is that a firewall is blocking your access to the port, if you release it your data will be accepted.

Regards.


Top
 Profile  
 
Display posts from previous:  Sort by  
Post a new topicPost a reply Page 1 of 1   [ 5 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