[SOLVED] Need to send value to ubidots from 4G+GPS Shield


#1

Dear All,
I have 4G shield and connected to Arduino Mega board.

4G+GPS Shield board:
https://www.cooking-hacks.com/documentation/tutorials/4g-gps-lte-wcdma-hspa-3g-gprs-shield-arduino-raspberry-pi-waspmote-tutorial/

4G+GPS shield POST example code:
http://www.libelium.com/development/waspmote/examples/4g-07-http-post/

Able to run the HTTP POST/GET examples and working without any problem.
Now I want to send values to Ubidots server, I’ve given the Ubidots server and Token values etc., but I’m getting server response as 403.

If we have arduino board + Ethernet shield, here is the example how we can send the value to ubidots, like HTTP URL formation, but this 4Garduino library doesn’t support “client.println” APIs.

In same way, how we can form URL for 4G shield, our example looks like this.

I need to push the value 10 to the variable ID.

char host[] = “things.ubidots.com”;
uint16_t port = 80;
char resource[] = “/api/v1.6/variables/58379c7376543216eea6a485/values”;
char data[] =“token=cRsjMrUvtRFYABCDEyRLkSsyg7oHCh” ;
//don’t know how to send this value in JSON format --> “{“value”:10}”;

Using above, I’m getting 403 HTTP error ID.

The below settings worked for me for POST URL:
char host[] = “mockbin.org”;
uint16_t port = 80;
char resource[] = “/bin/bbe7f656-12d6-4877-9fa8-5cd61f9522a9”;
char data[] = “foo=bar&foo=baz”;

Can you please help me on this problem ?

Regards,
Titus S.


#2

Hello, according to Libelium developers this shield doesn’t support JSON objects:

https://www.libelium.com/forum/viewtopic.php?p=56821&sid=8cc3a5abde1a42f1d24869b31d17483d

Actually Ubidots doesn’t support this board with some library but you may post your values in other way, directly in the url, for this please construct the next url format and post it:

http://translate.ubidots.com/api/postvalue?token=TOKEN&variable=VARIABLE_ID&value=2

Simply replace your TOKEN and your variable id (this last one can be found on the left side of the variable where you will store your value).

EXAMPLE:

If you post something like this:

http://translate.ubidots.com/api/postvalue?token=a240T2OIIFJwv7ncABlmVqcfImwRQW&variable=432628f87625427cae440b7a&value=2

you’ll save a value of ‘2’ to the variable with ID 432628f87625427cae440b7a.

I think that you can construct the url with something like this, but I cannot probe it because we don’t have that shield here:

char host[] = “translate.ubidots.com”;
uint16_t port = 80;
char resource[] = “/api/postvalue”;
char data[] = “token={TOKEN}&variable={VARIABLE_ID}&value={VALUE}”;

Hope it works, you can ask too for help on libelium forum post in order to know how to build properly the url:

https://www.libelium.com/forum/


#3

Thank you so much for your reply Jotathebest.
I’ve got “HTTP code 201” i.e unauthorized.

What could be the problem ?

Can you please help me on this issue ?

Regards,
Titus S.


#4

Hello, please use GET instead POST when you construct the request. You can guide from the example at the end of this post:

http://community.ubidots.com/t/esp8266-con-ubidotsesp8266-library-no-envia-datos/608/10


#5

Hi Again,
Thanks for your reply.
I’ve tried it, but I’ve got error this time.
Seems, we can use POST.

This is the forum link that we have posted, not getting replies :frowning:
https://www.cooking-hacks.com/forum/viewtopic.php?f=43&t=9388&sid=bfa4954df040c218b8f3c7f75729a656

Can you please help me on this ?


#6

Hello titusece, if you use the url type: http://translate.ubidots.com/api/postvalue?token=TOKEN&variable=VARIABLE_ID&value=2 you need to make a GET request and not a POST, if you wanna use a POST HTTP request you need to build a JSON dictionary that as I have read is not possible with your board.

Actually we don’t have any library for your board so any previous test have been made, I saw in the post of cooking-hacks that you have had some replies, have you tried them? In that post they constructed a socket with a JSON so it should work if the board sends it properly.


#7

Thank you. Its working now.
Thank you very much. I appreciate your kind help.
I want to send two values to ubidots (longitude and latitude)
Can you please tell me how can I send two variables in single url ?
Also any documentation for maps widget ?
How can I see the location in maps widget using my two values of longitude and latitude ?


#8

Hello Titusece, I’m glad to listen that you made it work, Would you share with us your code please for future references and for trying to guide you?

If you need to send multiple values, you can construct this url:

http://translate.ubidots.com/api/postvalue?token=TOKEN&variable=VARIABLE_ID_1&value=VALUE_1&variable=VARIABLE_ID_2&value=VALUE_2.

For widgets documentation, please refer to our youtube channel:


#9

For sending the context please construct this url:

http://translate.ubidots.com/api/postvalue?token=TOKEN&variable=VARIABLE_ID&value=VALUE&context=lat=LAT,lng=LNG

Regards


#10

Thanks, now I’m able to send the long/lang GPS location to ubidots.
Thanks for your help.

Here is the complete code snippet for others.

/*  
    -------------------------- 4G_06 - HTTP GET  ------------------------- 
    
    Explanation: This example shows how to send HTTP GET requests

    Note 1: in Arduino UNO the same UART is used for user debug interface 
    and LE910 AT commands. Handle with care, user interface messages could 
    interfere with AT commands.

    Example: 
          Serial.print("operATo"); 
    It is seen as wrong AT command by the LE910 module.

    Copyright (C) 2016 Libelium Comunicaciones Distribuidas S.L.
    http://www.libelium.com

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.

    Version:           1.1
    Design:            David Gascon
    Implementation:    Alejandro Gallego, Yuri Carmona, Luis Miguel Marti
    Port to Arduino:   Ruben Martin
*/

#include "arduino4G.h"

// APN settings
///////////////////////////////////////
char apn[] = "DST.internet";
char login[] = "";
char password[] = "";
///////////////////////////////////////

// SERVER settings
///////////////////////////////////////
//http://translate.ubidots.com/api/postvalue?token=cRsjMrABCDFYTZFRGyRLkSsyg7oHCh&variable=58379cabc6254256eea6a485&value=2.2
char host[] = "translate.ubidots.com";
uint16_t port = 80;
char resource[100];
char resource2[100];
///////////////////////////////////////

// variables
uint8_t error;
float longi = 1.56;
float langi = 2.56;
int test = 100;
char str_temp[16];
char str_temp2[16];


uint8_t gps_status;
float gps_latitude = 2.3456;
float gps_longitude = 1.2345;
uint32_t previous;
bool gps_autonomous_needed = true;


void setup()
{
  error = _4G.ON();
  Serial.println(F("Start program"));

  //********************************************************************
  // GET method to the Libelium's test url                              
  // You can use this php to test the HTTP connection of the module.    
  // The php returns the parameters the user sends with the URL.        
  //********************************************************************

  //////////////////////////////////////////////////
  // 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)
  {
    Serial.println(F("1. 4G module ready..."));
    

    ////////////////////////////////////////////////
    // 2. HTTP GET gps_latitude
    ////////////////////////////////////////////////
    Serial.println(F("2. Getting URL with GET method..."));

    dtostrf(gps_latitude, 4, 2, str_temp);
    dtostrf(gps_longitude, 4, 2, str_temp2);
    sprintf(resource,"/api/postvalue?token=cRsjMrUvtRFABCFRGyRLkSsyg7oHCh&variable=58466bf0762512345b08b60f&value=%d&context=lat=%s,lng=%s",test,str_temp,str_temp2);

    Serial.println(resource);

    // send the request
    error = _4G.http( GET_METHOD, host, port, resource);

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

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

  ////////////////////////////////////////////////
  // 4. Sleep
  ////////////////////////////////////////////////
 delay(10000);
    
      }

#11

please tell me the hardware you used.


#12

Hi,

This is the board I used to test.

https://www.cooking-hacks.com/documentation/tutorials/4g-gps-lte-wcdma-hspa-3g-gprs-shield-arduino-raspberry-pi-waspmote-tutorial/

Thanks.