[SOLVED] Loop in Adafruit FONA example code not working!

Hi,

I am trying to use the FONA example code to upload 5 variable values to Ubidots at regular intervals but the values upload only once. I am using the following code as a template:

http://ubidots.com/docs/devices/FONA.html

I commented out the parts of the FONA library that I didn’t need - such as SMS, audio, call. Also, although the example code is for 4 variables, I altered the code to accomodate 5 variables. Otherwise the code is almost exactly the same.

Could anyone please take a look and tell me why the code is not uploading data to Ubidots at regular intervals, like it is supposed to, and instead just uploads the data only once?

Here is my code:

/*

Sample code to send data from an Arduino to four Ubidots variables using the Adafruit’s FONA

Components:

  • Arduino Uno
  • Adafruit FONA

Created 23 Feb 2015
This code is based on the Adafruit FONAtest Example
and was modified by Alejandro Gomez for Ubidots.

This code is in the public domain.


#include "SoftwareSerial.h"
#include "Adafruit_FONA.h"
#include "Wire.h"
#include "HTU21D.h"
#include "BH1750FVI.h"
#include "stdlib.h"

//Create an instance of the object
HTU21D myHumidity;
BH1750FVI LightSensor;

// Pin connections:
#define FONA_RX 2
#define FONA_TX 3
#define FONA_RST 4
#define FONA_KEY A2
#define FONA_PS A3

// Ubidots token and variables
char token[] = "BGxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
char id1[] = "5xxxxxxxxxxxxxxxxxxxxxxx";
char id2[] = "5xxxxxxxxxxxxxxxxxxxxxxx";
char id3[] = "5xxxxxxxxxxxxxxxxxxxxxxx";
char id4[] = "5xxxxxxxxxxxxxxxxxxxxxxx";
char id5[] = "5xxxxxxxxxxxxxxxxxxxxxxx";

// 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(&fonaSS, FONA_RST);

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

float value4, value5;
char buffer [33];

void setup() {

 //For FONA MODULE
 //Serial.begin(115200);
 fonaSS.begin(4800);
 myHumidity.begin();
 LightSensor.begin();
 LightSensor.SetAddress(0x23);
 LightSensor.SetMode(Continuous_H_resolution_Mode);
 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(1000);

}

void senseValues(){
  
 byte humd = myHumidity.readHumidity();
 byte temp = myHumidity.readTemperature();
 uint16_t irradiance = LightSensor.GetLightIntensity(); 
 value4 = analogRead(A0);
 value5 = analogRead(A1);  

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

 sendDataUbidots(temp, id1);
 sendDataUbidots(humd, id2);
 sendDataUbidots(irradiance, id3);
 sendDataUbidots(value4, id4);
 sendDataUbidots(value5, id5);

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

}

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(c);
//  #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(c);
 }
 Serial.print(c);
 x = c - '0';
 while (isdigit(c = readBlocking())) {
   Serial.print(c);
   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(4800)) {           // 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("giffgaff.com"), F("giffgaff"), 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"));
}

Were you ever able to get your code to work? I to am using the same example however for the life of me I can not send values to Ubidots based on the same tutorial. http://ubidots.com/docs/devices/FONA.html

SO Frustrated!!!

Hi all,

We just published a new library to make it simpler to communicate the FONA with Ubidots: https://github.com/ubidots/Ubidots-FONA

Please take a look at the Read me to make sure you are following all steps.

Have tried new code and still no luck with data being sent:

#include <Ubidots_FONA.h>
#include <SoftwareSerial.h> 
#include <Adafruit_FONA.h>
#define APN "epc.tmobile.com" 
#define USER ""  // if your apn doesnt have username just put ""
#define PASS ""  // if your apn doesnt have password just put ""
#define TOKEN "twG9BwrMyj23KL455VXxRQGdZ89Q06"  // Remplace it with your token
#define ID "56910c787625423044885ab2" // Remplace it with your variable ID

Ubidots client(TOKEN);  
  
void setup() {
  Serial.begin(115200);
  delay(2000);
  client.gprsNetwork(F(APN),F(USER),F(PASS));  // First set your apn data
}

void loop() {
  //float value = analogRead(A0);  // Reading analog pin A0
  float value = 90;
  
  client.flushSerial();
  client.checkFona();
  client.gprsOnFona();
  client.save_value(value, ID);  
  Serial.println(value);
  delay(600);  // 600 milliseconds 
---> AT
	<--- OK
	---> AT
	<--- OK
	---> AT
	<--- OK
	---> ATE0
	<--- OK
	---> ATE0
	<--- OK
	---> AT+CVHU=0
	<--- OK
	---> ATI
	<--- SIM800 R13.08

OK

FONA is OK
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR
	---> AT+CIPSHUT
	<--- SHUT OK
	---> AT+CGATT=1
	<--- OK
	---> AT+SAPBR=3,1,"CONTYPE","GPRS"
	<--- OK
	---> AT+SAPBR=3,1,"APN","epc.tmobile.com"
	<--- OK
	---> AT+SAPBR=3,1,"USER",""
	<--- OK
	---> AT+SAPBR=3,1,"PWD",""
	<--- OK
	---> AT+SAPBR=1,1
	<--- ERROR

Hello,

The error as seen in the SIM800 manual from Adafruit (https://www.adafruit.com/datasheets/sim800_series_at_command_manual_v1.01.pdf) is because your SIM is not being able to connect to your mobile network:

---> AT+SAPBR=1,1
	<--- ERROR

I would suggest putting the SIM card in a mobile phone, then going to “network settings”, choose “2G only” (the FONA doe not support 3G or 4G and many operators only offer those), set these same APN settings in your mobile, restart the radio, and then see if you can surf the web.

Marked as solved by @kbnetguy: http://community.ubidots.com/t/solved-ds18b20temp-sensor-to-uno-fona-to-ubidots-no-data-avalible/239