Struggling to send data to google sheets with sim800l

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;
  }
}

USe your serial.Print(counter); Where you increment counter to verify it is being incremented.

The counter does increment, and it does reset when the if statement is executed. I can even see the results in the serial monitor but the data just fails to upload the way I want it to.

What data is actually being uploaded when the counter reaches 5, and exactly how is that different from what you want? Is everything correct except for the balance data, or does the other data get corrupted, etc?

Can you post code that actually compiles? The function updateSerial() is not defined, and String.h doesn't exist, did you mean string.h?

After the counter reaches 5 it starts uploading the same values over and over again until i reset the arduino.

You can remove String.h from the code.

For updateSerial(), just create a void updateSerial() after the void loop() and cut the piece of code below from void loop and paste it in updateSerial().

image

Notice how the data stays constant after 12:36:37 AM
image

So the data never actually shows the balance, does the balance print correctly in the serial monitor?

Which arduino are you using? Running two software serial ports can be problematic, as can the extensive use of String data type - you could have a memory issue.

The balance never shows. I do see it in the serial monitor.

I'm using Arduino Uno.

Try using the F() macro when printing text literals, that will save some RAM if this is a memory issue.


      //mySim.println("AT+SAPBR=3,1,\"Contype\",\"GPRS\""); //put F() around the quoted test
      mySim.println(F("AT+SAPBR=3,1,\"Contype\",\"GPRS\"")); //stores the text in flash memory instead of ram

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.