My project sometimes hang with the modem not connecting

Hi, Im also struggling with this…my project sometimes hang with the modem either not connecting or not enough battery power. In those events id like to have my sketch to move on to the next part of my sketch. I had a look at the BlynkGsmClient.h and can see where the error messages prints…could I possibly add a sleep command in the BlynkGsmClient.h file? Im also still using

Blynk.begin(auth, modem, apn, user, pass);

How would I change this to Blynk.config? Blynk.config(modem, auth, server) doesnt work with my setup cause i dont use server?? pls can someone help :slight_smile:

Sure You do! But the default one is hardcoded into library, so try just simple

Blynk.config(modem, auth)

Thanks or the quick reply…I tried using that, but then what about my auth? Using that it connects, but I cant see it connecting on the app on my phone?

What you mean asking “what about my auth”? It is passed to blynk.config() and processed. Then, after blynk.connect a connection with server should be established.

Cause now my app seems to connect, and read my sensor, but doesnt update the values on my app. I can see it connects, but no values change…maybe I should include my complete sketch?

> #include <DS3232RTC.h>
> #include <TimeLib.h>
> #include <LowPower.h>
> #include <Wire.h>
> #include <NewPing.h>
> #define BLYNK_PRINT Serial
> #define TINY_GSM_MODEM_SIM800
> #include <TinyGsmClient.h>
> #include <SoftwareSerial.h>
> #include <BlynkSimpleSIM800.h>
> #define SIM800_TX_PIN 5
> #define SIM800_RX_PIN 13
> #define uS_TO_m_FACTOR 60000000   // Conversion factor for micro seconds to minutes
> char auth[] = "a892d375ccccccc6670d5c086d6b1";
> char apn[]  = "";
> char user[] = "";
> char pass[] = "";
> const int MAX_DISTANCE = 120;                       //max distance to measure
> const int Diameter1 = 110;                                //internal Diameter of tank 1 in cm
> const int Depth1 = 110;                                 //total depth of tank 1 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 = 8;                          //     GPIO5,  D1
> const int EchoPin1 = 9;                        //     GPIO4,  D2
> const int Area1 = PI * ((Diameter1 / 2) * (Diameter1 / 2));          //area of base of tank 1
> int Litres1, Distance1, WaterDepth1, Decimal, Persentage, battLevel, csq, res, imei;
> int count = 1;
> String gsmTime;
> #include <SoftwareSerial.h>
> SoftwareSerial SerialAT(SIM800_TX_PIN, SIM800_RX_PIN);
> TinyGsm modem(SerialAT);
> BlynkTimer timer;
> NewPing sonar = NewPing(PingPin1, EchoPin1, MAX_DISTANCE);
> 
> tmElements_t tm;
> const byte rtcAlarmPin = 3; // External interrupt on pin D3
> 
> void setup()
> {
>   pinMode(rtcAlarmPin, INPUT_PULLUP); // Set interrupt pin
>   RTC.squareWave(SQWAVE_NONE); // Disable the default square wave of the SQW pin
> 
>   // setAlarm Syntax (RTC.setAlarm(alarmType, seconds, minutes, hours, dayOrDate);)
>   //RTC.setAlarm(ALM1_MATCH_MINUTES, 0, 53, 0, 1);
>   RTC.setAlarm(ALM1_MATCH_SECONDS, 10, 0, 0, 1);
> 
>   RTC.alarm(ALARM_1);
>   RTC.alarmInterrupt(ALARM_1, true); // Enable alarm 1 interrupt A1IE
> }
> 
> // Interrupt Service Routine (ISR) function
> void wake ()
> {
>   detachInterrupt (digitalPinToInterrupt (3)); // Disable interrupts on D3
>   count++;
> }
> 
> void sleepNow ()
> {
>   noInterrupts (); // Disable interrupts before entering sleep
>   attachInterrupt (digitalPinToInterrupt(3), wake, FALLING);  // Wake on falling edge of D3
>   interrupts (); // Enable interrupts to ensure next instruction is executed
>   LowPower.powerDown(SLEEP_FOREVER, ADC_OFF, BOD_OFF);
> }
> void sendWarning()
> {
>   //modem.sendSMS("+27834079675", "Water level 30% or less, please check!");
>   Serial.println();
>   Serial.println("SMS sent...");
> }
> 
> void GPRScheck() {
>   if (!modem.isGprsConnected()) {
>     modem.restart();
>     Blynk.config(modem, auth);
>     if (Blynk.connectNetwork(apn, user, pass)) {
>       //String COP = modem.getOperator();
>       //COP.remove(15);
>       Blynk.connect();
>       Serial.println("should connect now");
>     }
>   }
> }
> 
> void myFunction()
> {
>   Serial.begin(19200);
>   delay(10);
>   SerialAT.begin(9600);
>   delay(3000);
>   Serial.println("Initializing modem...");
>   Blynk.config(modem, auth);
>   GPRScheck();
>   delay(3000);
>   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 = ((Depth1 * (PI * (sq(Diameter1 / 2)))) - (Distance1 * (PI * (sq(Diameter1 / 2))))) / 1000;
>   Persentage = (Litres1 / 1000.00) * 100;
>   battLevel = modem.getBattPercent();
>   csq = modem.getSignalQuality();
>   Blynk.virtualWrite(V1, WaterDepth1);
>   Blynk.virtualWrite(V2, Litres1);
>   Blynk.virtualWrite(V3, Litres1 / 10);
>   Blynk.virtualWrite(V4, Persentage);
>   Blynk.virtualWrite(V5, battLevel);
>   Blynk.virtualWrite(V6, csq);
>   Blynk.virtualWrite(V7, count);
>   Serial.println();
>   Serial.println();
>   Serial.println("Water depth: " + String(WaterDepth1));
>   Serial.println("Water distance: " + String(Distance1));
>   Serial.println("Litres: " + String(Litres1));
>   Serial.println("Percentage: " + String(Persentage));
>   Serial.println("Battery: " + String(battLevel));
>   Serial.println("Signal Qlty: " + String(csq));
>   Serial.println();
>   Serial.print(count);
>   Serial.println();
>   if (Persentage <= 30) 
>    sendWarning();
>   delay(1000);
> }
> 
> void loop()
> {
>   Serial.println();
>   Serial.println("Entering sleep");
>   delay(100);
> 
>   sleepNow(); // Enter sleep
> 
>   Serial.println("Awake...");
>   if (RTC.alarm(ALARM_1))
>   {
>     RTC.read(tm);
>     RTC.setAlarm(ALM1_MATCH_SECONDS, 10, 0, 0, 1); // once a minute
>     //RTC.setAlarm(ALM1_MATCH_MINUTES, 0, 53, 0, 1);
>     //RTC.setAlarm(ALM1_MATCH_MINUTES, 0, 26, 0, 1); // once a hour
>     TimeDateDisplay();
>     powerON();
>     myFunction();
>     powerOFF();
>   }
> }
> 
> void powerON()
> {
>   pinMode(2, OUTPUT);
>   digitalWrite(2, HIGH); 
> }
> void powerOFF()
> {
>   pinMode(2, OUTPUT);
>   digitalWrite(2, LOW);
> }
> 
> void TimeDateDisplay()
> {
>   RTC.read(tm);
>   char dateBuffer[19];
>   sprintf(dateBuffer, "%04u-%02u-%02u %02u:%02u:%02u", (tm.Year + 1970), tm.Month, tm.Day, tm.Hour, tm.Minute, tm.Second);
>   Serial.println(dateBuffer);
> }

and my output looks like this:

> Awake...
> 2019-02-19 11:43:10
> Initializing modem...
> [470736] 
>     ___  __          __
>    / _ )/ /_ _____  / /__
>   / _  / / // / _ \/  '_/
>  /____/_/\_, /_//_/_/\_\
>         /___/ v0.6.0 on Arduino Pro
> 
> [478949] 
>     ___  __          __
>    / _ )/ /_ _____  / /__
>   / _  / / // / _ \/  '_/
>  /____/_/\_, /_//_/_/\_\
>         /___/ v0.6.0 on Arduino Pro
> 
> [478996] Modem init...
> [480251] Connecting to network...
> [484136] Network: Mobile Telephone Network
> [484136] Connecting to  ...
> [489723] Connected to GPRS
> [489795] Connecting to blynk-cloud.com:80
> [491984] Ready (ping: 770ms).
> [498001] Connecting to blynk-cloud.com:80
> should connect now
> 
> 
> Water depth: 65
> Water distance: 45
> Litres: 617
> Percentage: 61
> Battery: 87
> Signal Qlty: 25
> 
> 10
> 
> Entering sleep

You should keep your void loop empty. Just Blynk.run there and the eventual timer.
And BTW, where is your blynk.run?

Hey - I managed to get my values updating now (not sure how tho :confused: )…so ur saying is I should try and remove what happens in the void loop and move it to the void setup? I still dont have a blynk.run, but my app still works?

The rules are a bit different when you’re wanting to use sleep modes. I’m not familiar with the various ways of making the ESP32 sleep, but I’m guessing that you probably don’t want Blynk running in your void loop, otherwise your ESP won’t sleep.

If it’s working and doing what you want then don’t change it, just don’t expect to be able to have interaction from the app to the ESP32. For the ESP to be able to process commands (such as pressing a widget button) from the app then you need to have Blynk.run in your void loop, constantly looking for those commands.

Pete.

Thanks for the reply Pete…I am actually using a pro mini at the moment, using alarm interrupt from a RTC to wake up. Tried the same with an ESP, but couldnt make it work.

My sketch seems to work 100% atm, apart from the occasional getting stuck at “no connection” .

Sorry, I was getting mixed-up with another topic I’d been following.
The issues are the same though regarding sleep modes - the device cant sleep and talk to Blynk as well.

Pete.