Please help. I can't seem to find what it is that i'm doing wrong. When I run the code it works well for the first 4 uploads until it gets to "if(counter == 5) loop where it stops uploading the data correctly.
I want to upload the lat, lon, signal and batt every 20 seconds and everytime the counter reaches 5, i want bal to be added to the upload.
Here is my code.
#include <TinyGPS++.h>
#include <SoftwareSerial.h>
#include <AltSoftSerial.h>
#include <String.h>
SoftwareSerial mySim(5, 4);
AltSoftSerial neogps;
TinyGPSPlus gps;
unsigned long previousMillis = 0;
long interval = 20000;
String latitude, longitude, sig, result, batt, result2, bal, result5;
unsigned int index1, index2, timeout=0;
int counter = 0;
void setup() {
// put your setup code here, to run once:
Serial.begin(9600); //Begin serial communication with Arduino and Arduino IDE (Serial Monitor)
mySim.begin(9600); //Begin serial communication with Arduino and SIM800L
neogps.begin(9600); //Begin serial communication with Arduino and NEO 6M GPS module
}
void loop() {
// put your main code here, to run repeatedly:
while (Serial.available())
{
mySim.write(Serial.read());//Forward what Serial received to Software Serial Port
}
while(mySim.available())
{
Serial.write(mySim.read());//Forward what Software Serial received to Serial Port
}
unsigned long currentMillis = millis();
if(currentMillis - previousMillis > interval) {
previousMillis = currentMillis;
boolean newData = false;
for (unsigned long start = millis(); millis() - start < 2000;){
while (neogps.available()){
if (gps.encode(neogps.read())){
newData = true;
break;
}
}
}
if(true)
{
newData = false; //If newData is true
float altitude;
unsigned long date, time;
latitude = String(gps.location.lat(), 6); // Latitude in degrees (double)
longitude = String(gps.location.lng(), 6); // Longitude in degrees (double)
if (gps.date.isValid()) //Reading GPS date
{
Serial.print(gps.date.day());
Serial.print("/");
Serial.print(gps.date.month());
Serial.print("/");
Serial.println(gps.date.year());
}
else //If data not available, print "Not Available".
{
Serial.println("Not Available");
}
Serial.print("Time: ");
if (gps.time.isValid()) //Reading GPS time
{
if (gps.time.hour() < 10) Serial.print(F("0"));
Serial.print(gps.time.hour());
Serial.print(":");
if (gps.time.minute() < 10) Serial.print(F("0"));
Serial.print(gps.time.minute());
Serial.print(":");
if (gps.time.second() < 10) Serial.print(F("0"));
Serial.print(gps.time.second());
Serial.print(".");
if (gps.time.centisecond() < 10) Serial.print(F("0"));
Serial.println(gps.time.centisecond());
}
mySim.println("AT+CSQ"); //Signal quality test, value range is 0-31 , 31 is the best (sig)
for (unsigned long start = millis(); millis() - start < 300;){
while(mySim.available()){
sig = mySim.readString();
timeout = 1;
break;
}
}
if (timeout == 0){return 0;}
Serial.println(sig);
index1 = sig.indexOf("\r"); //Identifies the index in the response string.
sig.remove(0, index1+2); //Removes the AT command from the response string.
sig.trim();
index1 = sig.indexOf(":"); //Identifies the index (":") in the response string.
index2 = sig.indexOf(","); //Identifies the index (",") in the response string.
sig = sig.substring(index1+1, index2);
sig.trim();
result = sig;
Serial.println(result);
updateSerial();
mySim.println("AT+CBC"); //Returns battery level in % (batt)
for (unsigned long start = millis(); millis() - start < 300;){
while(mySim.available()){
batt = mySim.readString();
timeout = 1;
break;
}
}
if (timeout == 0){return 0;}
Serial.println(batt);
index1 = batt.indexOf("\r");
batt.remove(0, index1+2);
batt.trim();
index1 = batt.indexOf(",");
index2 = batt.indexOf(",", index1+1);
result2 = batt.substring(index1+1, index2);
result2.trim();
Serial.print(result2);
updateSerial();
if(counter == 5){
mySim.println("AT+CUSD=1,\"*100#\""); //Returns airtime balance (bal)
for (unsigned long start = millis(); millis() - start < 5000;){
while(mySim.available()){
bal = mySim.readString();
timeout = 1;
break;
}
}
if (timeout == 0){return 0;}
//Serial.println(bal);
index1 = bal.indexOf("\r");
bal.remove(0, index1+2);
bal.trim();
index1 = bal.indexOf(",");
index2 = bal.indexOf(".", index1+2);
result5 = bal.substring(index1+2, index2);
result5.trim();
Serial.println(result5);
updateSerial();
counter = 0;
}else {
result5 = "-";
}
mySim.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\"");
delay(500);
updateSerial();
mySim.println("AT+CSTT=\"internet\",\"guest\",\"guest\"");
delay(500);
updateSerial();
mySim.println("AT+SAPBR=1,1");
delay(1000);
updateSerial();
mySim.println("AT+HTTPINIT");
delay(200);
updateSerial();
mySim.println("AT+HTTPPARA=\"CID\",1");
delay(200);
updateSerial();
mySim.println("AT+HTTPPARA=\"URL\",\"https://script.google.com/macros/s/ *MY GOOGLE SCRIPT ADDRESS* /exec?lat="+latitude+"&lon="+longitude+"&signal="+result+"&batt="+result2+"&bal="+result5+"\"");
delay(1000);
updateSerial();
Serial.println(" ");
mySim.println("AT+HTTPSSL=1");
delay(200);
updateSerial();
mySim.println("AT+HTTPACTION=0");
delay(1000);
updateSerial();
mySim.println("AT+HTTPREAD");
delay(200);
updateSerial();
mySim.println("AT+HTTPTERM");
delay(500);
counter++;
}
return 1;
}
}