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"));
}