[SOLVED] Adafruit FONA GSM

Hi all, Firstly im a noob to all of this, but i have been successfully able to post values to ubidots … but i am stuck on how to post values and context…i wish to post lat and long into the context, i used the code from http://ubidots.com/docs/devices/FONA.html
Does anyone know how to change this code, in order to post the context values as well. I have read the docs containing context and posting , but still cant figure it out.
Any help would be really appreciated.

void sendDataUbidots(int value, char* myid){

uint16_t statuscode;
int16_t length;

char url1[] = “things.ubidots.com/api/v1.6/variables/”;
char* url2 = myid;
char url3[] = “/values?token=”;
char* url4 = token;
int lurl = strlen(url1) + strlen(url2) + strlen(url3) + strlen(url4);

char url[lurl];
sprintf(url,"%s%s%s%s",url1,url2,url3,url4);
// Serial.println(url);

char data1[] = “{“value”:”;
char data2[get_int_len(value)+1];
char data3[] = “}”;
itoa(value,data2,10);
int ldata = strlen(data1) + strlen(data2) + strlen(data3);

char data[ldata];
sprintf(data,"%s%s%s",data1,data2,data3);
//Serial.println(data);

Serial.print(F(“http://”));
Serial.println(url);
Serial.println(data);

Serial.println(F("****"));
if (!fona.HTTP_POST_start(url, F(“application/json”), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
Serial.println(“Failed!”);
}
while (length > 0) {
while (fona.available()) {
char c = fona.read();

Instead of:

char data1[] = "{\"value\":";
char data2[get_int_len(value)+1];
char data3[] = "}";
itoa(value,data2,10);
int ldata = strlen(data1) + strlen(data2) + strlen(data3);
char data[ldata];
sprintf(data,"%s%s%s",data1,data2,data3);

Try a whole sprintf statement: (in this snippet, “value”, “lat” and “long” are strings that you declare and assign somewhere before in your code):

//Declare a fixed but large array for data
char data[100];

Then in the sending loop, prepare the payload for the FONA function:

sprintf(data,"{\"value\":\"%s\", \"context\":{\"lat\": %s, \"lng\":%s}}", valor, lat, long);

In the next loop, it’s healthy to erase the char data:

memset(data, 0, sizeof(data)); 

In case you need this in the future:

  • to get the length of the payload as String:

    String le = String(String(payload).length());

Mike - You indicated you were able to send data to Ubidots. I have been unsuccessful in doing so. Can you share your entire code. Im just using the code from the example in the tutorial and im only passing one value which for now I hard coded.

http://ubidots.com/docs/devices/FONA.html#devices-fona

… with no luck. I keep getting failed

Here is my Code:
#include <SoftwareSerial.h>
#include “Adafruit_FONA.h”
#include <stdlib.h>

// Pin connections:
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
#define FONA_KEY 7
#define FONA_PS 8

// Ubidots token and variables
char token[] = “twG9BwrMyj23KL455VXxRQGdZ89Q06”;
char id1[] = “56910c787625423044885ab2”;
//char id2[] = “”;
//char id3[] = “”;
//char id4[] = “”;

// this is a large buffer for replies
char replybuffer[255];

// or comment this out & use a hardware serial port like Serial1 (see below)
SoftwareSerial fonaSS = SoftwareSerial(FONA_TX, FONA_RX);
Adafruit_FONA fona = Adafruit_FONA(FONA_RST);

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
int get_int_len(int);
void sendDataUbidots(void);

int value1, value2, value3, value4;
char buffer [33];

void setup() {
//For FONA MODULE
Serial.begin(115200);
fonaSS.begin(4800);
pinMode(FONA_KEY, OUTPUT);
pinMode(FONA_PS, INPUT);
analogReference(INTERNAL);
delay(2000);
TurnOffFona();

}

void loop() {
flushSerial();
TurnOnFona();
checkFona();
gprsOnFona();
senseValues();

while (fona.available()) {
Serial.write(fona.read());
}

gprsOffFona();
TurnOffFona();
delay(600000);

}

void senseValues(){
value1 = 78;
//value1 = analogRead(A6);
//value2 = analogRead(A7);
//value3 = analogRead(A0);
//value4 = analogRead(A1);

////////////////////////////////////////////

sendDataUbidots(value1, id1);
//sendDataUbidots(value2, id2);
//sendDataUbidots(value3, id3);
//sendDataUbidots(value4, id4);

/////////////////////////////////////////////

}

void sendDataUbidots(int value, char* myid){

uint16_t statuscode;
int16_t length;

char url1[] = “things.ubidots.com/api/v1.6/variables/”;
char* url2 = myid;
char url3[] = “/values?token=”;
char* url4 = token;
int lurl = strlen(url1) + strlen(url2) + strlen(url3) + strlen(url4);

char url[lurl];
sprintf(url,"%s%s%s%s",url1,url2,url3,url4);
// Serial.println(url);

char data1[] = “{“value”:”;
char data2[get_int_len(value)+1];
char data3[] = “}”;
itoa(value,data2,10);
int ldata = strlen(data1) + strlen(data2) + strlen(data3);

char data[ldata];
sprintf(data,"%s%s%s",data1,data2,data3);
//Serial.println(data);

Serial.print(F(“http://”));
Serial.println(url);
Serial.println(data);

Serial.println(F("****"));
if (!fona.HTTP_POST_start(url, F(“application/json”), (uint8_t *) data, strlen(data), &statuscode, (uint16_t *)&length)) {
Serial.println(“Failed!”);
}
while (length > 0) {
while (fona.available()) {
char c = fona.read();

#if defined(AVR_ATmega328P) || defined(AVR_ATmega168)
loop_until_bit_is_set(UCSR0A, UDRE0); /* Wait until data register empty. */
UDR0 = c;
#else
Serial.write©;
#endif

   length--;
   if (! length) break;
 }

}
Serial.println(F("\n****"));
fona.HTTP_POST_end();

// flush input
flushSerial();
}

void flushSerial() {
while (Serial.available())
Serial.read();
}

char readBlocking() {
while (!Serial.available());
return Serial.read();
}

uint16_t readnumber() {
uint16_t x = 0;
char c;
while (! isdigit(c = readBlocking())) {
//Serial.print©;
}
Serial.print©;
x = c - ‘0’;
while (isdigit(c = readBlocking())) {
Serial.print©;
x *= 10;
x += c - ‘0’;
}
return x;
}

uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout) {
uint16_t buffidx = 0;
boolean timeoutvalid = true;
if (timeout == 0) timeoutvalid = false;

while (true) {
if (buffidx > maxbuff) {
//Serial.println(F(“SPACE”));
break;
}

while(Serial.available()) {
  char c =  Serial.read();

  //Serial.print(c, HEX); Serial.print("#"); Serial.println(c);

  if (c == '\r') continue;
  if (c == 0xA) {
    if (buffidx == 0)   // the first 0x0A is ignored
      continue;

    timeout = 0;         // the second 0x0A is the end of the line
    timeoutvalid = true;
    break;
  }
  buff[buffidx] = c;
  buffidx++;
}

if (timeoutvalid && timeout == 0) {
  //Serial.println(F("TIMEOUT"));
  break;
}
delay(1);

}
buff[buffidx] = 0; // null term
return buffidx;
}

int get_int_len (int value){
int l=1;
while(value>9){ l++; value/=10; }
return l;
}

void checkFona(){
// See if the FONA is responding
if (! fona.begin(fonaSS)) { // can also try fona.begin(Serial1)
Serial.println(F(“Couldn’t find FONA”));
while (1);
}
Serial.println(F(“FONA is OK”));

//configure a GPRS APN
fona.setGPRSNetworkSettings(F(“epc.tmobile.com”), F(""), F(""));
//fona.setGPRSNetworkSettings(F(“fast.t-mobile.com”), F(""), F(""));
}

void TurnOnFona(){
Serial.println("Turning on Fona: ");
while(digitalRead(FONA_PS)==LOW)
{
digitalWrite(FONA_KEY, LOW);
}
digitalWrite(FONA_KEY, HIGH);
delay(4000);
}

void TurnOffFona(){
Serial.println("Turning off Fona ");
while(digitalRead(FONA_PS)==HIGH)
{
digitalWrite(FONA_KEY, LOW);
}
digitalWrite(FONA_KEY, HIGH);
delay(4000);
}

void gprsOnFona(){
while(!fona.enableGPRS(true));
Serial.println(F(“Turn on”));
}

void gprsOffFona(){
while (!fona.enableGPRS(false));
Serial.println(F(“Turn off”));
}

Hi @mikefonaproject, and thanks @joe486 for your help. We just published a new library to wrap all these functions and make it simpler to interact with Ubidots using the FONA.

Once you install the library, the code would be as simple as:

#include <Ubidots_FONA.h>
#include <SoftwareSerial.h> 
#include <Adafruit_FONA.h>


#define APN "The APN of your operator here 
#define USER "Your_username_here"  // if your apn doesnt have username just put ""
#define PASS "Your_password_here"  // if your apn doesnt have password just put ""
#define TOKEN "Your_token_here"  // Remplace it with your token
#define ID "Your_id_here" // Replace it with your variable ID

Ubidots client(TOKEN);  
  
void setup() {
  Serial.begin(115200);
  delay(2000);
  client.gprsNetwork(F(APN),F(USER),F(PASS)); 
}

void loop() {
  float value = analogRead(A0);
  client.flushSerial();
  client.checkFona();
  client.gprsOnFona();
  client.save_value(value, ID);  
  delay(600); 
}

This process has been optimized thanks to the latest FONA-Ubidots library: https://github.com/ubidots/ubidots-fona