Hello forum,
it is nice to be here. Thanks for any help in advance
I am new to Arduino and would like to realize a project with a relay which I would like to control via SMS. The programs SMS - relay and deep sleep alone are working, but not together.
- Problem SIM800L ist not working with deep sleep active:
Normaly I get the OK in the monitor:
16:00:51.753 -> GSM SIM800L Ready
16:00:53.721 ->
16:00:53.721 -> OK
When I uncomment the line // startDeepSleep(); I do not get the OK and the relay is not controllable via SMS. I placed the startDeepSleep(); in different viods (setup, loop) with no effect.
The code:
#include "Adafruit_FONA.h"
#define SIM800L_RX 27
#define SIM800L_TX 26
#define SIM800L_PWRKEY 4
#define SIM800L_RST 5
#define SIM800L_POWER 23
#define uS_TO_S_FACTOR 1000000ULL /* Conversion factor for micro seconds to seconds */
#define LED_BLUE 13
#define RELAY 14
#define rstPin 32
RTC_DATA_ATTR int bootCount = 0;
HardwareSerial *sim800lSerial = &Serial1;
Adafruit_FONA sim800l = Adafruit_FONA(SIM800L_PWRKEY);
uint8_t readline(char *buff, uint8_t maxbuff, uint16_t timeout = 0);
String smsString = "";
int TIME_TO_SLEEP = 50;
int interval = 1000;
long prevMillis = 0;
char sim800lNotificationBuffer[64]; //for notifications from the FONA
char smsBuffer[250];
char replybuffer[255];
boolean ledState = false;
void setup() {
pinMode(LED_BLUE, OUTPUT);
pinMode(RELAY, OUTPUT);
pinMode(SIM800L_POWER, OUTPUT);
digitalWrite(LED_BLUE, HIGH);
digitalWrite(SIM800L_POWER, HIGH);
digitalWrite(rstPin, HIGH);
pinMode(rstPin, OUTPUT);
Serial.begin(115200);
Serial.println(F("ESP32 with GSM SIM800L"));
Serial.println(F("Initializing....(May take more than 10 seconds)"));
delay(10000);
// Make it slow so its easy to read!
sim800lSerial->begin(4800, SERIAL_8N1, SIM800L_TX, SIM800L_RX);
if (!sim800l.begin(*sim800lSerial)) {
Serial.println(F("Couldn't find GSM SIM800L"));
Serial.println("Reset by RST?");
digitalWrite(rstPin, LOW);
while (1);
}
Serial.println(F("GSM SIM800L is OK"));
char imei[16] = {0}; // MUST use a 16 character buffer for IMEI!
uint8_t imeiLen = sim800l.getIMEI(imei);
if (imeiLen > 0) {
Serial.print("SIM card IMEI: "); Serial.println(imei);
}
// Set up the FONA to send a +CMTI notification
// when an SMS is received
sim800lSerial->print("AT+CNMI=2,1\r\n");
Serial.println("GSM SIM800L Ready");
delay(2000);
// startDeepSleep();
}
void loop()
{
relay();
}
void relay(){
if (millis() - prevMillis > interval) {
ledState = !ledState;
digitalWrite(LED_BLUE, ledState);
prevMillis = millis();
}
char* bufPtr = sim800lNotificationBuffer; //handy buffer pointer
if (sim800l.available()) {
int slot = 0; // this will be the slot number of the SMS
int charCount = 0;
// Read the notification into fonaInBuffer
do {
*bufPtr = sim800l.read();
Serial.write(*bufPtr);
delay(1);
} while ((*bufPtr++ != '\n') && (sim800l.available()) && (++charCount < (sizeof(sim800lNotificationBuffer)-1)));
//Add a terminal NULL to the notification string
*bufPtr = 0;
//Scan the notification string for an SMS received notification.
// If it's an SMS message, we'll get the slot number in 'slot'
if (1 == sscanf(sim800lNotificationBuffer, "+CMTI: \"SM\",%d", &slot)) {
Serial.print("slot: "); Serial.println(slot);
char callerIDbuffer[32]; //we'll store the SMS sender number in here
// Retrieve SMS sender address/phone number.
if (!sim800l.getSMSSender(slot, callerIDbuffer, 31)) {
Serial.println("Didn't find SMS message in slot!");
}
Serial.print(F("FROM: ")); Serial.println(callerIDbuffer);
// Retrieve SMS value.
uint16_t smslen;
// Pass in buffer and max len!
if (sim800l.readSMS(slot, smsBuffer, 250, &smslen)) {
smsString = String(smsBuffer);
Serial.println(smsString);
}
if (smsString == "EIN") {
Serial.println("Futterautomat ist EIN!.");
digitalWrite(RELAY, LOW);
delay(100);
// Send SMS for status
if (!sim800l.sendSMS(callerIDbuffer, "Futterautomat ist EIN!")) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
}
else if (smsString == "AUS") {
Serial.println("Futterautomat ist AUS!");
digitalWrite(RELAY, HIGH);
delay(100);
// Send SMS for status
if (!sim800l.sendSMS(callerIDbuffer, "Futterautomat ist AUS!")) {
Serial.println(F("Failed"));
} else {
Serial.println(F("Sent!"));
}
}
while (1) {
if (sim800l.deleteSMS(slot)) {
Serial.println(F("OK!"));
break;
}
else {
Serial.print(F("Couldn't delete SMS in slot ")); Serial.println(slot);
sim800l.print(F("AT+CMGD=1,4\n\r"));
//sim800l.print(F("AT+CMGD=?\r\n"));
}
}
}
}
}
void startDeepSleep(){
Serial.println("Going to sleep...");
Serial.flush();
esp_sleep_enable_timer_wakeup(TIME_TO_SLEEP * 1000000);
esp_deep_sleep_start();
}
- The SIM800L does not restart properly after deep sleep
After the deep sleep the SIM800L does not work right. The red led is not flashing and the module does not receive or send SMS. What is the problem? I added the REST via a pin. That works but does a reset and then the relay is changing to initial state also.
Is it possible to use deep sleep with the SIM800L? Online I found some AT commands, but they did not work.
THANKS ALOT and best regards,
Kreiki