Working sketch on NodeMCU, SIM800L - need to enable power saving

Hi all,

I was hoping someone can help me with my project. At the moment my sketch works, and values are updating correctly on my Blynk app. My problem is trying to save some power.

I’m running a nodeMCU with a GSM800L and a JSN-SR04T distance sensor…also running on battery. The app sends values to Blynk successfully and the NodeMCU sleeps for as long as I set it, but while the nodeMCU sleeps, the light on the SIM800L still blinks…I wanna know if there is a way to save more power? My sim800 DTR pin is connected to pin 10 (read this is needed to activate sleep mode on the sim800L. Any help will be greatly appreciated.

#define SIM800_TX_PIN 12
#define SIM800_RX_PIN 13
#define TINY_GSM_MODEM_SIM800
#define SONAR_NUM 2                                         // Number of sensors. Change to suit your requirements.
#define PI 3.1415926535897932384626433832795
#include <ESP8266WiFi.h>
#include <TinyGsmClient.h>
#include <BlynkSimpleSIM800.h>
#include <SoftwareSerial.h>
#include <NewPing.h>
#define uS_TO_m_FACTOR 60000000   // Conversion factor for micro seconds to minutes
#define SerialMon Serial
//#define SerialMon2 Serial

// Time to sleep (in minutes):
const int sleepTimeM = 10;
char auth[] = "3a585bcf0xxxxxxa8189e3624579e12";
char apn[]  = "";
char user[] = "";
char pass[] = "";
const int MAX_DISTANCE = 225;                       //max distance to measure
const int Diameter1 = 182;                                //internal Diameter of tank 1 in cm
//const int Diameter2 = 182;                                //internal Diameter of tank 2 in cm
const int Depth1 = 220;                                 //total depth of tank 1 in cm , from sensor to base inside
//const int Depth2 = 220;                                 //total depth of tank 2 in cm, from sensor to base inside
const unsigned int Period = 60000;    //period between pings, in milliseconds. i.e  1 munute = 60,000. max 65535. Want longer? use unsigned long
const int PingPin1 = 5;                          //     GPIO5,  D1
const int EchoPin1 = 4;                        //     GPIO4,  D2
const int Area1 = PI * ((Diameter1 / 2) * (Diameter1 / 2));          //area of base of tank 1


int Litres1, Distance1, WaterDepth1; // WaterDepth2, Litres2,Distance2;
#include <SoftwareSerial.h>
SoftwareSerial SerialAT(SIM800_TX_PIN, SIM800_RX_PIN);
TinyGsm modem(SerialAT);
BlynkTimer timer;                                                                   //config timer
NewPing sonar = NewPing(PingPin1, EchoPin1, MAX_DISTANCE);        // Each sensor's trigger pin, echo pin, and max distance to ping.

void sendSensorReadings()
{
  //***********Readings Tank 1
  Distance1 = sonar.ping_cm();                                       //get distance to the top of the water tank 1
  if (Distance1 >= Depth1 || Distance1 == 0 )  Distance1 = Depth1;                   //check it does not go negative
  WaterDepth1 = Depth1 - Distance1;                     //calculate the depth of the water
  Litres1 = (Area1 * WaterDepth1) / 1000;                          //calculate the volume of the water in litres

  //***********SEND INFO TO BLYNK
  Blynk.virtualWrite(V1, WaterDepth1);                                //send depth to Blynk server
  Blynk.virtualWrite(V2, Litres1);                                            //send litres to Blynk server
  Blynk.virtualWrite(V3, Litres1 / 10);                                   //send litres to Blynk server. for vertical level widget & chart,
  //scaled to 1/10 as Blynk only goes up to 9999 and we need up to 16000
  delay(50);
  digitalWrite(13, HIGH);                                                //flash the LED on D7, just to let us know it's running
  delay(50);
  digitalWrite(13, LOW);
  //************************* can be commented out, test use only
  Serial.println();
  Serial.println();
  Serial.println("Tank 1 water distance: " + String(Distance1));   //print depth
  Serial.println("Tank 1 water depth: " + String(WaterDepth1));  //print depth
  Serial.println("Tank 1 Litres: " + String(Litres1));                //print litres
  //Go to sleep
  ESP.deepSleep(sleepTimeM * uS_TO_m_FACTOR, WAKE_RF_DEFAULT); // Sleep for 5 Minutes
}

void setup()
{
  WiFi.disconnect();
  WiFi.mode(WIFI_OFF);
  WiFi.forceSleepBegin();
  delay(1);
  timer.setInterval(Period, sendSensorReadings);    // Setup a function to be called every n seconds
  delay(10);
  Serial.begin(9600);
  delay(10);
  SerialAT.begin(57600);
  delay(3000);
  Serial.println("Initializing modem...");
  modem.restart();
  Blynk.begin(auth, modem, apn, user, pass);
  
  String modemInfo = modem.getModemInfo();
  SerialMon.print("Modem: ");
  SerialMon.println(modemInfo);
  String ip = modem.getLocalIP();
  SerialMon.print("IP: ");
  SerialMon.println(ip);

  pinMode(10, OUTPUT);
  digitalWrite(3, HIGH);
  SerialMon.println("AT+CSCLK=4");

}

void loop()
{
  Blynk.run();
  timer.run(); // Initiates BlynkTimer
}

A brief Google later and it looks like you need to send a few AT commands to enter and wake from sleep with that module… I think the DTR pin just needs to be HIGH, apparently for the wake?? I don’t have any such device to test, but you can Google for the solution with a few search parameters of your own.

PS… since you are using the Blynk connection through that device, you would need to use the disconnect command first before trying to send any AT commands through, then of course upon waking the ESP, then wake the SIM800, and then reconnect to the Blynk Server. Or at least that’s my guess from a purely theoretical point of view :stuck_out_tongue:

http://docs.blynk.cc/#blynk-firmware-connection-management

You can also search this forum for keywords like SIM800 and see what, if anything, others might have dom regarding sleep modes, etc.

Thanks for your input Gunner…yes, ive been trying to put it to sleep with the following commands
" pinMode(10, OUTPUT);
digitalWrite(10, HIGH);"

And I think its working, but the LED on the SIM800 is stil on, which in my mind doesnt makes sense for saving power?

As I briefly read after some Googling, that seems to be only part of the process… I don’t have those links avail anymore (working on something else) but Google keywords like SIM800 sleep and see what you can find.