I've got an Arduino Uno, SIM900 GPRS, and the Adafruit Breakout GPS.
Can this code be optimized?
#include <SoftwareSerial.h>
#include <TinyGPS++.h>
#include <Ubidots_Arduino_GPRS.h>
//Serial Relay - Arduino will patch a
//serial link between the computer and the GPRS Shield
//at 19200 bps 8-N-1
//Computer is connected to Hardware UART
//GPRS Shield is connected to the Software UART
#define APN "myapn"
#define USER "" // If your apn doesnt have username just put ""
#define PASS "" // If your apn doesnt have password just put ""
#define TOKEN "ubidotsToken" // Replace it with your Ubidots token
#define ID "Your_id_here" // Replace it with your Ubidots' variable ID
// You can send 1 to 10 variable at the same time
#define ID1 "idHere" // Replace it with your variable ID
//#define ID2 "" // Replace it with your variable ID
//#define ID3 "Your_id_here" // Replace it with your variable ID
TinyGPSPlus gps; // The TinyGPS++ object for interfacing with the GPS
//Declare indexes, and strings
char latitude[12];
char longitude[12];
float value_1 = 1;
char var[18] = "Lat: ";
char var2[18] = "Long: ";
Ubidots client(TOKEN);
SoftwareSerial ss(2,3);
void setup() {
// the Serial port of Arduino baud rate.
Serial.begin(9600);
client.powerUpOrDown();
client.setApn(APN,USER,PASS);
//.endC is for _client.end() in Ubidots_GPRS.cpp
client.endC();
ss.begin(9600);
}
void loop() {
//handling GPS data parsing
while (ss.available() > 0) {
gps.encode(ss.read());}
//Once a valid location comes up it updates Lat/Long variables
if (gps.location.isUpdated())
{
strcpy(var, "Lat: ");
strcpy(var2,"Long: ");
dtostrf(gps.location.lat(),10,6,latitude);
strcat(var, latitude);
dtostrf(gps.location.lng(),10,6,longitude);
strcat(var2, longitude);
//Added ss.flush();
ss.flush();
ss.end();
Serial.flush()
//Added client.beginC() for _client.begin(9600) is Ubidots_GPRS.cpp
client.beginC();
client.add(ID1, value_1, var, var2);
client.sendAll();
client.endC();
ss.begin(9600);
}
Serial.println(var);
Serial.println(var2);
}
From this link, I moved the println from inside .isUpdated() to outside the if statement. I was able to get GPS data sent from the unit to my Ubidots account while driving around town. Afterwards I was able to input the data into Google Maps and the route produced was accurate. Do I need flush()?
listen()
s in your code but this approach is also alright. If all you want to do is optimize the code, you could save the APN settings permanently to the sim900. Just addclient.println("AT&W")
at the end ofsetApn()
and usereadData()
to ensure OK was received. Run the sketch and then you can comment out thesetApn()
call insetup()
. Also provide a link to your updated library with its new functions.