Looking to use HTTP Post to carriots platform

Wireless communications in Waspmote, topologies, node types...
jameskeogh
Posts: 1
Joined: Thu Feb 12, 2015 5:48 pm

Looking to use HTTP Post to carriots platform

Post by jameskeogh » Thu Feb 12, 2015 5:53 pm

Hi there,
Im looking to write a program on a waspmote to capture the audio level every few seconds and post that to a carriots device listener. Using the examples, i can get the sound values every few minutes and join a wireless AP and even do a GET request using the stock code, but not having any luck with joining it all up. Given that Carriots requires the use of an API key in the POST request header is this even possible using the WaspWifi functions?

Thanks in advance

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: Looking to use HTTP Post to carriots platform

Post by libelium-dev » Tue Feb 17, 2015 11:58 am

jameskeogh,

What is your header length? maybe it is being truncated.

Carnus
Posts: 7
Joined: Wed May 31, 2017 10:37 am

Re: Looking to use HTTP Post to carriots platform

Post by Carnus » Thu Oct 19, 2017 5:59 pm

Thanks for answering.
Still some difficulties with "carriots" connection through 4g librairie.
We try to specify Wasp4G::HTTP_HEAD parameters to set the header of http protocol, as spécifed on the carriots web site, for POST method, we try then to send data through Wasp4G::HTTP_POST parameter, no success. We don't know anymore how to proceed.
Could you provide us samples ?
We use de _4g.http() method.

Thanks a lot, Best regards.

-- CODE BELOW --

#include <Wasp4G.h>

// APN settings
///////////////////////////////////////
char apn[] = "internet.m2mportal.de";
char login[] = "***";
char password[] = "***";
///////////////////////////////////////

// SERVER settings
///////////////////////////////////////


char host[] = "api.carriots.com";
uint16_t port = 80;
char ressource[] = "";

///////////////////////////////////////
// variables
int error;

void setup()
{
USB.ON();
USB.println(F("Start program"));

//////////////////////////////////////////////////
// 1. sets operator parameters
//////////////////////////////////////////////////
_4G.set_APN(apn, login, password);


//////////////////////////////////////////////////
// 2. Show APN settings via USB port
//////////////////////////////////////////////////
_4G.show_APN();
}

void loop()
{
//////////////////////////////////////////////////
// 1. Switch ON
//////////////////////////////////////////////////
error = _4G.ON();

if (error == 0)
{
USB.println(F("1. 4G module ready..."));

////////////////////////////////////////////////
// 2. HTTP POST
////////////////////////////////////////////////

USB.print(F("2. HTTP HEAD request..."));

error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "POST /streams/ HTTP/1.1");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "Host: api.carriots.com");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "Accept: application/json");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "User-Agent: Waspmote@Carnus.Carnus");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "Content-Type: application/json");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "Content-Length: 145");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "carriots.apikey:***********");

USB.print(F("3. HTTP POST request..."));
char data2[] = "{\"protocol\": \"v2\", \"checksum\": \"\", \"device\": \"Waspmote@Carnus.Carnus\", \"at\": \"now\", \"data\": { \"essai\": \"balbla\"}}";

error = _4G.http( Wasp4G::HTTP_POST, host, port, ressource, data2);

// check the answer
if (error == 0)
{
USB.print(F("Done. HTTP code: "));
USB.println(_4G._httpCode);
USB.print("Server response: ");
USB.println(_4G._buffer, _4G._length);
}
else
{
USB.print(F("Failed. Error code: "));
USB.println(error, DEC);
}
}
else
{
// Problem with the communication with the 4G module
USB.println(F("4G module not started"));
USB.print(F("Error code: "));
USB.println(error, DEC);
}

////////////////////////////////////////////////
// 3. Powers off the 4G module
////////////////////////////////////////////////
USB.println(F("3. Switch OFF 4G module"));
_4G.OFF();


////////////////////////////////////////////////
// 4. Sleep
////////////////////////////////////////////////
USB.println(F("4. Enter deep sleep..."));
PWR.deepSleep("00:01:00:10", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);

USB.ON();
USB.println(F("5. Wake up!!\n\n"));

}

koki
Posts: 84
Joined: Sun Sep 18, 2016 11:02 am

Re: Looking to use HTTP Post to carriots platform

Post by koki » Thu Oct 19, 2017 11:15 pm

Hello,

I send HTTP POST packets from our Waspmote P&S to our data server but only through GPRS connection. I don't have any experience in configuring HTTP connection with a 4G module, and so I assume your configuration is just OK.
Here is one point I have noticed in your code, just in case it helps...

HTTP/1.1 protocol defines:
1. the sequence CR LF as the end-of-line marker for all protocol elements (https://tools.ietf.org/html/rfc2616#section-2.2)
2. an empty line (i.e., a line with nothing preceding the CRLF) indicating the end of the header fields (https://tools.ietf.org/html/rfc2616#section-4.1)

Based on above, I would add a carriage return ('\r') and a linefeed ('\n') at the end of each HTTP header entry as follows. The last header entry ends with two sets of CRLF to indicate the end of the header fields.

Code: Select all

error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "POST /streams/ HTTP/1.1\r\n");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "Host: api.carriots.com\r\n");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "Accept: application/json\r\n");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "User-Agent: Waspmote@Carnus.Carnus\r\n");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "Content-Type: application/json\r\n");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "Content-Length: 145\r\n");
error = _4G.http( Wasp4G::HTTP_HEAD, host, port, ressource, "carriots.apikey:***********\r\n\r\n");

error = _4G.http( Wasp4G::HTTP_POST, host, port, ressource, data2);
I send HTTP POST headers in this way with the WaspGPRS_Pro_core::sendData() method.

I hope this helps, though I will not be able to do any support. ;)

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: Looking to use HTTP Post to carriots platform

Post by libelium-dev » Fri Oct 20, 2017 12:47 pm

Hi Carnus,

We don't know how Carriots server expect the data, so we don't know if the request is correct. We think that you could try to send the HTTP request in the format that Koki said but using a TCP connection. Here you can see an example, you should create the post request in the http_format variable.
http://www.libelium.com/development/was ... cp-client/

@Koki, thanks for your collaboration.

Regards

Carnus
Posts: 7
Joined: Wed May 31, 2017 10:37 am

Re: Looking to use HTTP Post to carriots platform

Post by Carnus » Sat Oct 21, 2017 5:04 pm

Thank you very much Koki and Libelium for your answers.
I will test your solution next week.
I keep up to date about of the result !

Regards.

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: Looking to use HTTP Post to carriots platform

Post by libelium-dev » Mon Oct 23, 2017 11:57 am

All right. We look forward to your results.

Carnus
Posts: 7
Joined: Wed May 31, 2017 10:37 am

Re: Looking to use HTTP Post to carriots platform

Post by Carnus » Thu Nov 30, 2017 10:45 am

Hi Libelium,
This is the first (GOOD) result of our tries to transmit a message to Carriots cloud :
We hope these informations will be interesting for everyone ...
Bernard

Code: Select all

#include <WaspFrameConstantsv12.h>
#include <WaspFrameConstantsv15.h>
#include <Wasp4G.h>

/*
    --- 4G_11 - TCP client  ---


    //////////////Version 23/11/2017 Fonctionnelle///////
    
    Explanation: This example shows how to open a TCP client socket
    to the specified server address and port. Besides, the functions
    for sending/receiving data are used.

*/



// APN settings
///////////////////////////////////////
char apn[] = "xxxxx.xxx";
char login[] = "yyyyy";
char password[] = "zzz";
///////////////////////////////////////

// SERVER settings
///////////////////////////////////////
char host[] = "api.carriots.com";
uint16_t remote_port = 80;
///////////////////////////////////////

// define Socket ID (from 'CONNECTION_1' to 'CONNECTION_6')
///////////////////////////////////////
uint8_t connId = Wasp4G::CONNECTION_1;
///////////////////////////////////////

// define data to send through TCP socket
////////////////Mon essai//////////////
char str_http_head_string[] =   "POST /streams/ HTTP/1.1\r\n"\
                                "Host: api.carriots.com\r\n"\
                                "Accept: application/json\r\n"\
                                "User-Agent: Waspmote@xxxxxx.xxxxx\r\n"\
                                "Content-Type: application/json\r\n";
char str_http_len[255];
char str_http_end_string[] =  "carriots.apikey: 8fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
// Data to send
char data_to_send[] = "{\"protocol\":\"v2\",\"checksum\":\"\",\"device\":\"Waspmote@xxxxxxxxxx\",\"at\":\"now\",\"data\":{\"essai\":\"12345\"}}";

// define variables
uint8_t  error;
uint32_t previous;
uint8_t  socketIndex;
char data[1024];          // data sent to trought the socket
                          // CAREFULL : Buffer size may cause an error 

void setup()
{
  USB.ON();
  USB.println(F("Start program"));

  // 1. Sets operator parameters
  _4G.set_APN(apn, login, password);
  // 2. Show APN settings via USB port
  _4G.show_APN();
}

void loop()
{
  // 1. Switch ON
  error = _4G.ON();
  if (error == 0)
  {
    USB.println(F("1. 4G module ready..."));

    // 2. TCP socket
    error = _4G.openSocketClient(connId, Wasp4G::TCP, host, remote_port);
    if (error == 0)
    {
      USB.println(F("2.1. Opening a socket... done!"));
      USB.print(F("IP address:"));
      USB.println(_4G._ip);
 
      // Build the data to send
      int len_data;
      char str_len_data[20];

      // manage the lenght of the data to transmit to set it in the string to send
      len_data = strlen(data_to_send);
      
      snprintf( data, sizeof(data), "%sContent-Length: %d\r\n%s\r\n%s ", str_http_head_string, len_data, str_http_end_string, data_to_send);
      USB.print(F("data to send:\n"));
      USB.println(data);

      // 2.2. Send it through the connection
      // send TCP packet
      error = _4G.send(connId, data);
      if (error == 0)
      {
        USB.println(F("2.2. Sending a frame... done!"));
      }
      else
      {
        USB.print(F("ERROR : 2.3. Error sending a frame. Code: "));
        USB.println(error, DEC);
      }

      // 2.3. Receive data
      // Wait for incoming data from the socket (if the other side responds)
      USB.print(F("2.3. Waiting to receive data from carriots ...\n"));
      error = _4G.receive(connId, 60000);
      if (error == 0)
      {
        if (_4G.socketInfo[connId].size > 0)
        {
          USB.println(F("\n-----------------------------------"));
          USB.print(F("SOCKET INFO : Data received:"));
          USB.println(_4G._buffer, _4G._length);
          USB.println(F("-----------------------------------"));
        }
        else
        {
          USB.println(F("SOCKET INFO : NO data received"));
        }
      }
      else
      {
        USB.println(F("ERROR : No data received."));
        USB.println(error, DEC);
      }
    }
    else
    {
      USB.print(F("ERROR : 2.1. Error opening socket. Error code: "));
      USB.println(error, DEC);
    }

    // 2.5. Close socket
    error = _4G.closeSocketClient(connId);
    if (error == 0)
    {
      USB.println(F("2.4. Socket closed OK"));
    }
    else
    {
      USB.print(F("ERROR : 2.5. Error closing socket. Error code: "));
      USB.println(error, DEC);
    }
  }
  else
  {
    // Problem with the communication with the 4G module
    USB.println(F("ERROR : 1. 4G module not started"));
  }

  // 3. Powers off the 4G module
  USB.println(F("3. Switch OFF 4G module"));
  _4G.OFF();

  // 4. Sleep
  USB.println(F("4. Enter deep sleep..."));
  PWR.deepSleep("00:01:00:10", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);
  USB.ON();
  USB.println(F("5. Wake up!!\n\n"));

}

libelium-dev
Posts: 27967
Joined: Mon Sep 28, 2009 1:06 pm

Re: Looking to use HTTP Post to carriots platform

Post by libelium-dev » Fri Dec 01, 2017 8:46 am

Hi Carnus,

Thanks for sharing your development with the community! :D Hope this help another users.

Regards

PJSantos
Posts: 20
Joined: Mon Feb 26, 2018 1:50 pm
Company: Action International

Re: Looking to use HTTP Post to carriots platform

Post by PJSantos » Tue Feb 27, 2018 7:51 am

Carnus wrote:
Thu Nov 30, 2017 10:45 am
Hi Libelium,
This is the first (GOOD) result of our tries to transmit a message to Carriots cloud :
We hope these informations will be interesting for everyone ...
Bernard

Code: Select all

#include <WaspFrameConstantsv12.h>
#include <WaspFrameConstantsv15.h>
#include <Wasp4G.h>

/*
    --- 4G_11 - TCP client  ---


    //////////////Version 23/11/2017 Fonctionnelle///////
    
    Explanation: This example shows how to open a TCP client socket
    to the specified server address and port. Besides, the functions
    for sending/receiving data are used.

*/



// APN settings
///////////////////////////////////////
char apn[] = "xxxxx.xxx";
char login[] = "yyyyy";
char password[] = "zzz";
///////////////////////////////////////

// SERVER settings
///////////////////////////////////////
char host[] = "api.carriots.com";
uint16_t remote_port = 80;
///////////////////////////////////////

// define Socket ID (from 'CONNECTION_1' to 'CONNECTION_6')
///////////////////////////////////////
uint8_t connId = Wasp4G::CONNECTION_1;
///////////////////////////////////////

// define data to send through TCP socket
////////////////Mon essai//////////////
char str_http_head_string[] =   "POST /streams/ HTTP/1.1\r\n"\
                                "Host: api.carriots.com\r\n"\
                                "Accept: application/json\r\n"\
                                "User-Agent: Waspmote@xxxxxx.xxxxx\r\n"\
                                "Content-Type: application/json\r\n";
char str_http_len[255];
char str_http_end_string[] =  "carriots.apikey: 8fxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx\n";
// Data to send
char data_to_send[] = "{\"protocol\":\"v2\",\"checksum\":\"\",\"device\":\"Waspmote@xxxxxxxxxx\",\"at\":\"now\",\"data\":{\"essai\":\"12345\"}}";

// define variables
uint8_t  error;
uint32_t previous;
uint8_t  socketIndex;
char data[1024];          // data sent to trought the socket
                          // CAREFULL : Buffer size may cause an error 

void setup()
{
  USB.ON();
  USB.println(F("Start program"));

  // 1. Sets operator parameters
  _4G.set_APN(apn, login, password);
  // 2. Show APN settings via USB port
  _4G.show_APN();
}

void loop()
{
  // 1. Switch ON
  error = _4G.ON();
  if (error == 0)
  {
    USB.println(F("1. 4G module ready..."));

    // 2. TCP socket
    error = _4G.openSocketClient(connId, Wasp4G::TCP, host, remote_port);
    if (error == 0)
    {
      USB.println(F("2.1. Opening a socket... done!"));
      USB.print(F("IP address:"));
      USB.println(_4G._ip);
 
      // Build the data to send
      int len_data;
      char str_len_data[20];

      // manage the lenght of the data to transmit to set it in the string to send
      len_data = strlen(data_to_send);
      
      snprintf( data, sizeof(data), "%sContent-Length: %d\r\n%s\r\n%s ", str_http_head_string, len_data, str_http_end_string, data_to_send);
      USB.print(F("data to send:\n"));
      USB.println(data);

      // 2.2. Send it through the connection
      // send TCP packet
      error = _4G.send(connId, data);
      if (error == 0)
      {
        USB.println(F("2.2. Sending a frame... done!"));
      }
      else
      {
        USB.print(F("ERROR : 2.3. Error sending a frame. Code: "));
        USB.println(error, DEC);
      }

      // 2.3. Receive data
      // Wait for incoming data from the socket (if the other side responds)
      USB.print(F("2.3. Waiting to receive data from carriots ...\n"));
      error = _4G.receive(connId, 60000);
      if (error == 0)
      {
        if (_4G.socketInfo[connId].size > 0)
        {
          USB.println(F("\n-----------------------------------"));
          USB.print(F("SOCKET INFO : Data received:"));
          USB.println(_4G._buffer, _4G._length);
          USB.println(F("-----------------------------------"));
        }
        else
        {
          USB.println(F("SOCKET INFO : NO data received"));
        }
      }
      else
      {
        USB.println(F("ERROR : No data received."));
        USB.println(error, DEC);
      }
    }
    else
    {
      USB.print(F("ERROR : 2.1. Error opening socket. Error code: "));
      USB.println(error, DEC);
    }

    // 2.5. Close socket
    error = _4G.closeSocketClient(connId);
    if (error == 0)
    {
      USB.println(F("2.4. Socket closed OK"));
    }
    else
    {
      USB.print(F("ERROR : 2.5. Error closing socket. Error code: "));
      USB.println(error, DEC);
    }
  }
  else
  {
    // Problem with the communication with the 4G module
    USB.println(F("ERROR : 1. 4G module not started"));
  }

  // 3. Powers off the 4G module
  USB.println(F("3. Switch OFF 4G module"));
  _4G.OFF();

  // 4. Sleep
  USB.println(F("4. Enter deep sleep..."));
  PWR.deepSleep("00:01:00:10", RTC_OFFSET, RTC_ALM1_MODE1, ALL_OFF);
  USB.ON();
  USB.println(F("5. Wake up!!\n\n"));

}
Hi All , this is a quiet old post ... But i'm just testing with my waspmote and I get this error:

{ "response": "Not authorized: apikey does not have sufficient privileges" }<html>

Any idea?

Thanks

Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest