Hi Thanks in advance… I am a programming novice.
I am trying to send my GPS Data to my “Position” variable. I would like to send the lat & long in the context format to create a map.
However i am only able to send the “Value” (which is just a 1 currently) to my variable.
I’ve seen many posts in which the fix was to use the ubidots gprs library. However this calls upon software serial which i cannot use as the gsm library conflicts with the use of software serial. (i’m having to use my gps with Altsoftserial as a result).
I believe the code should be similar to:
void setup(){
// initialize serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial){
; // wait for serial port to connect. Needed for Leonardo only
}
Serial.println("Starting Arduino web client.");
// connection state
boolean notConnected = true;
// After starting the modem with GSM.begin()
// attach the shield to the GPRS network with the APN, login and password
while (notConnected){
if ((gsmAccess.begin(PINNUMBER) == GSM_READY) &
(gprs.attachGPRS(GPRS_APN, GPRS_LOGIN, GPRS_PASSWORD) == GPRS_READY)){
notConnected = false;
}else{
Serial.println("Not connected");
delay(1000);
}
}
}
void loop(){
int value = analogRead(A0);
if(save_value(value)){
}
else{
reset++;
if (reset == 10){
asm volatile (" jmp 0"); // reset the Arduino board if the data transmission fail
}
}
// if the server's disconnected, stop the client:
if (!client.available() && !client.connected()){
Serial.println();
Serial.println("disconnecting.");
client.stop();
// do nothing forevermore:
}
}
boolean save_value(int value){
Serial.println("connecting...");
int num=0;
String var = "{\"value\":"+ String(value) + "}";
num = var.length();
// if you get a connection, report back via serial:
if (client.connect("things.ubidots.com", 80)){
Serial.println("connected");
// Make a HTTP request:
client.print("POST /api/v1.6/variables/"+idvariable+"/values HTTP/1.1\nContent-Type: application/json\nContent-Length: "+String(num)+"\nX-Auth-Token: "+token+"\nHost: things.ubidots.com\n\n");
client.println(var);
client.println();
}else{
// if you didn't get a connection to the server:
Serial.println("connection failed");
return false;
}
while (client.available())
{
char c = client.read();
Serial.print(c);
}
}
But with the context added, should i use a sprintf function for this? converting my latitude and longitude floats to chars using the dtostrf function ?
void loop() {
int value = 1;
float latitude = 1.2345;
float longitude = 54.334;
char latoutput[15];
char longoutput[15];
String var = ("{\"value\":" + String(value) + ", ");
dtostrf(latitude, 9, 7, latoutput);
dtostrf(longitude, 9, 7, longoutput);
char context[40];
sprintf(context, " \"context\":{\"lat\":%s, \"lng\":%s""}}", (latoutput), (longoutput));
int num = 76;
num = var.length();
if (client.connect(server, port)) {
Serial.println("connected");
// Make a HTTP request:
client.println("POST /api/v1.6/devices/my-gps-data/Position/values HTTP/1.1\nContent-Type: application/json\nContent-Length: " + String(num) + "\nX-Auth-Token: " + token + "\nHost: things.ubidots.com\n\n");
client.print(var);
Serial.print(var);
client.println(context);
Serial.println(context);
client.println();
client.println("Connection: close");
Serial.println("Connection: close");
Currently this isn’t working at all however when i remove the context element i will work adding int value = 1 to my variable in ubidots.
Below is the serial display:
Starting Arduino web client.
connecting...
connected
{"value":1, "context":{"lat":1.2345001, "lng":54.3340000}}
Connection: close
Reponse from Server:
H