Very weird problem that my project (on PCB) didn't have until today when I took it on a car drive. Basically I send an SMS to my SIM800L via my phone and if it matches a certain string, the ESP32 (Devkit C) tells it to reply GPS coordinates back. Except, now it sometimes ignores it completely. Like hundreds of times. Rendering my project pretty useless.
On breadboard this issue is happening too.
I am baffled completely because until morning it worked fine on PCB and breadboard aswell. When i sent a location request I got a reply.
Here is the code and my feeble effort to resurrect it within asterisks:
#include <TinyGPSPlus.h>
#include "HardwareSerial.h"
#include <WiFi.h>
HardwareSerial GPSSerial1(1);
HardwareSerial SIMSerial2(2);
TinyGPSPlus gps;
char Received_SMS;
short SMS_OK = -1;
short SMS_Battery = -1;
short SMS_LOC = -1;
char battery_status[27];
unsigned long previousMillis = 0;
unsigned long previousM = 0;
const long resetBurst = 13000;
const long interval = 1800000;
String Data_SMS;
void setup() {
WiFi.disconnect(true);
WiFi.mode(WIFI_OFF);
Serial.begin(9600);
SIMSerial2.begin(9600,SERIAL_8N1, 21,22);
GPSSerial1.begin(9600,SERIAL_8N1, 13,14);
Serial.println("Starting");
delay(3000);
ReceiveMode();
}
void loop() {
String RSMS;
while (GPSSerial1.available() > 0){
if (gps.encode(GPSSerial1.read()))
if (millis() > 5000 && gps.charsProcessed() < 10)
{Serial.println(F("No GPS detected: check wiring."));
}}
while (SIMSerial2.available()) {
Received_SMS = SIMSerial2.read();
RSMS.concat(Received_SMS);
SMS_OK = RSMS.indexOf("Send");
SMS_Battery = RSMS.indexOf("Bat");
SMS_LOC = RSMS.indexOf("GPS");
}
**Serial.println(SIMSerial2.available());**
** delay(1000); //<---------- I hate putting delays but unless I print these two lines with
//delay I can never receive a reply back on PCB version.
//On breadboard version sometimes I do get a reply.
//Sometimes I can see the value changing to non-zero on
//serial monitor but it ignores it. **
** Serial.println(SIMSerial2.read());**
** delay(1000); //<---- ditto for this too**
if (SMS_OK != -1) {
Data_SMS = "Message received";
Send_Data();
ReceiveMode();
Data_SMS = "";
SMS_OK = -1;
}
if (SMS_LOC != -1) {
Serial.println(gps.location.lat(),6);
Serial.println(gps.location.lng(),6);
Send_GPS();
ReceiveMode();
Data_SMS = "";
SMS_LOC = -1;
}
if(SMS_Battery != -1){
SIMSerial2.print("AT+CBC\r");
for(int i = 0 ; i<27 ; i++) {
battery_status[i] = SIMSerial2.read();
Serial.println("Battery status is:");
Serial.println(battery_status[i]);
if(battery_status[i] == ',' && battery_status[i-4] == ','){
Data_SMS.concat(battery_status[i-3]);
Data_SMS.concat(battery_status[i-2]);
Data_SMS.concat(battery_status[i-1]);
}
}
Send_Data();
ReceiveMode();
Data_SMS = "";
SMS_Battery = -1;
}
unsigned long currentMillis = millis();
if(currentMillis -previousMillis >= interval){
previousMillis = currentMillis;
ESP.restart();
}
}
void ReceiveMode() {
Serial.println("Inside Receive Mode");
SIMSerial2.println("AT\r");
updateSerial();
SIMSerial2.print("AT+CMGF=1\r");
updateSerial();
SIMSerial2.print("AT+CNMI=2,2,0,0,0\r");
updateSerial();
}
void Send_GPS() {
SIMSerial2.print("AT+CMGF=1\r");
delay(100);
SIMSerial2.print("AT+CMGS=\"+xxxxxxxxxxx\"\r");
delay(500);
SIMSerial2.print(gps.location.lat(),6);
SIMSerial2.print(",");
SIMSerial2.print(gps.location.lng(),6);
delay(500);
SIMSerial2.print((char)26);
delay(500);
}
void Send_Data() {
SIMSerial2.print("AT+CMGF=1\r");
delay(100);
SIMSerial2.print("AT+CMGS=\"+xxxxxxxxxxxxx\"\r");
delay(500);
SIMSerial2.print(Data_SMS);
delay(500);
SIMSerial2.print((char)26);
delay(500);
delay(500);
}
void updateSerial() {
delay(500);
while (Serial.available()) {
SIMSerial2.write(Serial.read());
}
while (SIMSerial2.available()) {
Serial.write(SIMSerial2.read());
}
}
My guess is that something has gone caput with ESP32 as I took the project for a drive. RX,TX of SIM800L and ESP32 give precisely same voltage values on breadboard and PCB. AT commands work fine always. What do you guys think is the problem?