hy, please help me.
I have project gps. I’ve tried several time and always can not send data lat and long to ubidots.
this is my program
please check it and help me
#include <SoftwareSerial.h>
#include <String.h>
#include "TinyGPS.h"
SoftwareSerial mySerial(6, 7); //your pins to serial communication
TinyGPS gps;
#define GPS_TX_DIGITAL_OUT_PIN 4
#define GPS_RX_DIGITAL_OUT_PIN 5
long startMillis;
long secondsToFirstLocation = 0;
#define DEBUG
float latitude = 0.0;
float longitude = 0.0;
String Location= "";
int value1=0;
int valor;
//-------------------------------------------------------------
//---------------------Ubidots Configuration-------------------s
//-------------------------------------------------------------
String token = "pIUs3esm2UYQTV3JW0A1PWasZfHccU"; //your token to post value
String idvariable = "5730507d7625424b921a8ebe"; //ID of your variable
int PowerKeySIM900=9;
void setup()
{
//#ifdef DEBUG8
Serial.begin(19200);
//#endif
// Serial1 is GPS
Serial1.begin(9600);
//serial module gsm
mySerial.begin(9600);
delay(1000);
// prevent controller pins 5 and 6 from interfering with the comms from GPS
pinMode(GPS_TX_DIGITAL_OUT_PIN, INPUT);
pinMode(GPS_RX_DIGITAL_OUT_PIN, INPUT);
pinMode(PowerKeySIM900,OUTPUT);
//SIM900power();
//delay(10000);
startMillis = millis();
Serial.println("Starting"); // if uncoment untuk ditampilkan pada serial
}
void loop ()
{ //String value ="";
Send2Ubidots(value1);
value1++;
}
boolean Send2Ubidots(int value)
{
int num = 0;
String var = "";
String le = "";
readLocation(); // Update the location value from the GPS feed
//var="{\"value\":"+ value + ", \"context\":"+ location + "}";
var="{\"value\": " && value + ", \"context\":"+ Location + "}";
num=var.length(); // How long is the payload
le=String(num); //this is to calcule the length of var
//////////////////////////////////////////
for(int i = 0;i<7;i++)
{
mySerial.println("AT+CGATT?"); //this is made repeatedly because it is unstable
delay(2000);
ShowSerialData();
}
mySerial.println("AT+CSTT=\"www.xlgprs.net\""); //replace with your providers' APN
delay(1000);
ShowSerialData();
mySerial.println("AT+CIICR"); //bring up wireless connection
delay(3000);
ShowSerialData();
mySerial.println("AT+CIFSR"); //get local IP adress
delay(2000);
ShowSerialData();
mySerial.println("AT+CIPSPRT=0");
delay(3000);
ShowSerialData();
mySerial.println("AT+CIPSTART=\"tcp\",\"things.ubidots.com\",\"80\""); //start up the connection
delay(3000);
ShowSerialData();
mySerial.println("AT+CIPSEND"); //begin send data to remote server
delay(3000);
ShowSerialData();
mySerial.print("POST /api/v1.6/variables/"+idvariable);
delay(100);
ShowSerialData();
mySerial.println("/values HTTP/1.1");
delay(100);
ShowSerialData();
mySerial.println("Content-Type: application/json");
delay(100);
ShowSerialData();
mySerial.println("Content-Length: "+le);
delay(100);
ShowSerialData();
mySerial.print("X-Auth-Token: ");
delay(100);
ShowSerialData();
mySerial.println(token);
delay(100);
ShowSerialData();
mySerial.println("Host: things.ubidots.com");
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println(var);
delay(100);
ShowSerialData();
mySerial.println();
delay(100);
ShowSerialData();
mySerial.println((char)26);
delay(7000);
mySerial.println();
ShowSerialData();
mySerial.println("AT+CIPCLOSE"); //close the communication
delay(1000);
ShowSerialData();
}
void ShowSerialData()
{
while(mySerial.available()!=0)
Serial.write(mySerial.read());
}
void readLocation()
{
bool newData = false;
unsigned long chars = 0;
unsigned short sentences, failed;
// For one second we parse GPS data and report some key values
for (unsigned long start = millis(); millis() - start < 1000;)
{
while (Serial1.available())
{
int c = Serial1.read();
// Serial.print((char)c); // if you uncomment this you will see the raw data from the GPS
++chars;
if (gps.encode(c)) // Did a new valid sentence come in?
newData = true;
}
}
if (newData)
{
/*
// we have a location fix so output the lat / long and time to acquire
if(secondsToFirstLocation == 0){
secondsToFirstLocation = (millis() - startMillis) / 1000;
Serial.print("Acquired in:");
Serial.print(secondsToFirstLocation);
Serial.println("s");
}
*/
unsigned long age;
gps.f_get_position(&latitude, &longitude, &age);
latitude == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : latitude;
longitude == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : longitude;
/*
Serial.print("Location: ");
Serial.print(latitude, 6);
Serial.print(" , ");
Serial.print(longitude, 6);
Serial.println("");
*/
String Lat = String (latitude,6);
String Long = String (longitude,6);
Location = "{\"lat\":" + String(Lat) + ",\"lng\":" + String(Long) + "}";
Serial.println(Location);
/*
char Lat[10];
char Long[10];
dtostrf(latitude,1,2,Lat);
dtostrf(longitude,5,2,Long);
location = "{\"lat\":" + String(Lat) + ",\"lng\":" + String(Long) + "}";
Serial.println(location);
*/
}
if (chars == 0){
// if you haven't got any chars then likely a wiring issue
Serial.println("Check wiring");
}
else if(secondsToFirstLocation == 0){
// still working
}
}
//===================================================================================//
//===================================================================================//
void SIM900power() // software berfungsi sama seperti memencet tombol power pada shield
{
digitalWrite(PowerKeySIM900, HIGH);
delay(1000);
digitalWrite(PowerKeySIM900, LOW);
delay(5000);
}