Hi,
I have done a program with the arduino GPRS, I got the lat and long,however I cant send over to Ubidots, anyone can help me? Thank you so much.
here’s the code:
#include <SIM900.h>
#include <SoftwareSerial.h>
#include <TinyGPS.h>
TinyGPS gps;
SoftwareSerial ss(10, 11);
char token[] = “SYPO3pL3OtCl5Axbd6ueBZcDTrRE9pWrZcxPwjXuipbWhMpkaAXnFgtfBSI8”; //your token to post value
char idvariable[] = “5610da05762542729c789a19”;
float flat, flon;
unsigned long age;
void setup() {
Serial.begin(9600);
ss.begin(9600);
gsm.begin(9600);
}
void loop() {
gps.f_get_position(&flat, &flon, &age);
readGPS(gps);
//********************************************
// uncomment to test if gps is working
//********************************************
unsigned long age, date, time, chars = 0;
unsigned short sentences = 0, failed = 0;
gps.stats(&chars, &sentences, &failed);
print_int(chars, 0xFFFFFFFF, 6);
print_int(sentences, 0xFFFFFFFF, 10);
print_int(failed, 0xFFFFFFFF, 9);
Serial.println();
save_value("123");
delay(2000);
}
static void print_int(unsigned long val, unsigned long invalid, int len)
{
char sz[32];
if (val == invalid)
strcpy(sz, “*******”);
else
sprintf(sz, “%ld”, val);
sz[len] = 0;
for (int i=strlen(sz); i<len; ++i)
sz[i] = ’ ';
if (len > 0)
sz[len-1] = ’ ';
Serial.print(sz);
smartdelay(0);
}
//this function is to send the sensor data to the Ubidots, you can see the new value in the Ubidots after execute this function
void save_value(char* valor)
{
gsm.listen();
int num;
String le;
char temp[50];
//num=var.length();
le=String(num);
for(int i = 0;i<7;i++)
{
gsm.SimpleWriteln(“AT+CGATT?”); //it is made repeatedly because it is unstable
delay(2000);
ShowSerialData();
}
// gsm.SimpleWriteln(“AT+CSTT=“sunsurf””); //start task and setting the APN
gsm.SimpleWriteln(“AT+CSTT=“sunsurf”,“65”,“user123”);
delay(1000);
ShowSerialData();
gsm.SimpleWriteln(“AT+CIICR”); //bring up wireless connection
delay(3000);
ShowSerialData();
gsm.SimpleWriteln(“AT+CIFSR”); //get local IP adress
delay(2000);
ShowSerialData();
gsm.SimpleWriteln(“AT+CIPSPRT=0”);
delay(3000);
ShowSerialData();
gsm.SimpleWriteln(“AT+CIPSTART=“tcp”,“things.ubidots.com”,“80"”); //start up the connection
delay(3000);
ShowSerialData();
gsm.SimpleWriteln(“AT+CIPSEND”); //begin send data to remote server
delay(3000);
ShowSerialData();
sprintf(temp,“POST /api/v1.6/variables/%s/values HTTP/1.1”,idvariable);
gsm.SimpleWriteln(temp);
// gsm.SimpleWrite(“POST /api/v1.6/variables/”+idvariable);
// delay(100);
// ShowSerialData();
// gsm.SimpleWriteln(”/values HTTP/1.1”);
delay(100);
ShowSerialData();
gsm.SimpleWriteln(“Content-Type: application/json”);
delay(100);
ShowSerialData();
gsm.SimpleWriteln(“Content-Length: “+sizeof(temp));
delay(100);
ShowSerialData();
sprintf(temp,“X-Auth-Token: %s”,token);
gsm.SimpleWriteln(temp);
delay(100);
ShowSerialData();
gsm.SimpleWriteln(“Host: things.ubidots.com”);
delay(100);
ShowSerialData();
gsm.SimpleWriteln(””);
delay(100);
ShowSerialData();
sprintf(temp,"{“value”:%s}",valor);
gsm.SimpleWriteln(temp);
delay(100);
ShowSerialData();
gsm.SimpleWriteln("");
delay(100);
ShowSerialData();
gsm.SimpleWriteln((char)26);
delay(7000);
gsm.SimpleWriteln("");
ShowSerialData();
gsm.SimpleWriteln(“AT+CIPCLOSE”); //close the communication
delay(1000);
ShowSerialData();
}
void ShowSerialData()
{
while(gsm.available()!=0)
Serial.write(gsm.read());
}
void readGPS(TinyGPS &gps)
{
int year;
byte month, day, hour, minute, second, hundredths;
unsigned long age;
gps.crack_datetime(&year, &month, &day, &hour, &minute, &second, &hundredths, &age);
if (age == TinyGPS::GPS_INVALID_AGE){
// no signal
}
else
{
char sz[32];
sprintf(sz, "%02d/%02d/%02d %02d:%02d:%02d ",
month, day, year, hour, minute, second);
Serial.print(sz);
Serial.print("Lat: “);Serial.print(flat);Serial.print(”, Lon: ");Serial.println(flon);
}
smartdelay(0);
}
void smartdelay(unsigned long ms)
{
ss.listen();
unsigned long start = millis();
do
{
while (ss.available())
gps.encode(ss.read());
} while (millis() - start < ms);
}