BLYNK
BLYNK.IO       📲 GETTING STARTED       📗 DOCS       ❓HELP CENTER       👉 SKETCH BUILDER

ESP8266 JSNSR04T 2.0 Deep Sleep post Blynk Publish

So I have a Blynk project that was working fine until I wanted to incorporate a battery and deep sleep. Been reading around the board and incorporated a few changes I thought made a lot of sense, but still don’t work. My board won’t sleep and wake up. Also, onboard ESP LED and JST board LED stay lit once powered (even after reset).

Goal Read value from utrasonic sensor, conduct some math to get gallons and tank level, publish to Blynk, go to deep sleep for 60min (though for testing purposes I think I have this set lower)

HW ESP8266-12E (NODEMCU) + JSNSR04T 2.0 + 3.7V 1000mah LIPO
Only difference in wiring I changed was to add a jumper to RST and D0 pins of ESP

Code (embarrassed to show it but here goes)

//ESP8266 Tank Level Sensor using JSNSR04T 2.0 and 3.7V LIPO 
//Goal read level, post to blynk, deep sleep, repeat
//REV 7_13_20 545PM 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define sleepLength 20 //how long to deep sleep in seconds 

//ESP8266 Wire Connections to battery and JSNSR04T 2.0 Board
//Vin to LIPO 3.7V battery
//GND to LIPO Bat Gnd
//RST to D0  <----- enables the wake from deep sleep 
//D4 to Sensor Trig
//D2 to Sensor Echo
//3V3 to Sensor Vin
//GND to Sensor GND


// defines pins numbers
const int trigPin = D4;//2 for arduino
const int echoPin = D2;//5 for arduino
const int maxDist = 60;
const int r = 35.5; //radius of tank
const long piRsqd = 3959.192; //based on r=35.5 
const long pi = 3.14159;


// defines variables
int h; //height of tank
long duration;
long dist_cm;
long dist_in;
long tankLevel;
long tankLevel_ft;
int gallons;

//Set WIFI and Blynk token
char ssid[] = "xxx";                                                
char pass[] = "xxx";                                   
char auth[] = "xxx";                                       

//********************Setup Call
void setup() {
pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
pinMode(echoPin, INPUT); // Sets the echoPin as an Input
Serial.begin(115200); // Starts the serial communication
     


Blynk.begin(auth, ssid, pass);
  delay(20);
  while (Blynk.connect() == false) {
  //Wait until connected
  //Serial.print("x");
  }

Blynk.run();   
}


//********************************Get and Send sensor readings and deep sleep call
void sendSensorReadings() {
// Clears the trigPin
digitalWrite(trigPin, LOW);
delayMicroseconds(15); //Do I really need this delay

// Sets the trigPin on HIGH state for 15 micro seconds
digitalWrite(trigPin, HIGH);
delayMicroseconds(15);  //Was told this is a trick to get JSNSR04T ultrasonic sensor to work well
digitalWrite(trigPin, LOW);

// Reads the echoPin, returns the sound wave travel time in microseconds
duration = pulseIn(echoPin, HIGH);

//Would like to do a do loop whereby if three consecutive readings equal each other , that's the "duration" value 
//thoughts?


// Calculating the distance
//58 represents assumption speed of sound = 1,000,000 / 58 * 2 / 100 = 344.8 mps
dist_cm= duration/58;
// multiplied by .39 to get inches
dist_in= dist_cm *0.393701;

//tank level is distance between sensor and bottom of empty tank (maxDistance) - distance reading
tankLevel = maxDist - dist_in;
tankLevel_ft = tankLevel/12;
//Gallons remaining 
gallons = piRsqd*tankLevel/231;  //231 cubic inches per gallon



//***********SEND INFO TO BLYNK
Blynk.virtualWrite(V1, tankLevel_ft);  //send level to Blynk server
Blynk.virtualWrite(V2, gallons);       //send gallons to Blynk server
delay(50); //i read somewhere to do this delay


//** Prints the distance on the Serial Monitor
Serial.print("Gallons Remaining: ");
Serial.println(gallons);
Serial.print("Tank Level: ");
Serial.println(tankLevel_ft);
Serial.print("Going to Sleep");
//Adding deep sleep 
ESP.deepSleep(sleepLength * 1000000,WAKE_RF_DEFAULT);   //default mode
delay(100);
}


void loop() {
//Nothing here
}
1 Like

After the ESP32 wakes up the first thing you need to do is re-connect to Blynk . . . although looking at your code you don’t have any Blynk connection routine . . . also you need to clean up your loop() and remove the delays . . .

Check this out, lost of info, examples and explanation . . .

help.blynk.cc

Keep your void loop() clean

Troubleshooting of one of the most popular mistakes of newbie Blynk users.

I’d recommend you learn how to Connect to Blynk, sleep, wake-up and re-connect before you start adding sensors.

billd

1 Like

Bill - Wow thanks for the swift reply. I’m on it. I should have thought of practicing sending/passing fake (non-sensor) values to Blynk using deep sleep first with my battery prior to plunging into the deep end by trying to incorporate my “functional” sensor code that was working with Blynk without deep sleep via USB. Lesson 1: Bite size chunks. Got it!

1 Like

@Tularosa whne you are using Deep Sleep, the usual rules for structuring your code with a clean void loop go out of the window.
With deep sleep you shouldn’t use any timers, as you want everything to execute just once then go to sleep.
Also, if you use then normal Blynk.begin command, if a WiFi or Blynk server connection can’t be established then the code execution will halt at that point, so your device will never go back to sleep because the deepsleep command section of your code will never be reached.

The solution is to manage your own WiFi connection then use Blynk.config and Blynk.begin (which aren’t blocking functions) to establish the Blynk connection. It also helps to use a static IP address for your device, rather than rely on DHCP, as thus speeds-up the connection process.

The “beehive connected” project from @christophebl is a good example of how to structure your code correctly. I’ve pointed many users towards that project in the past, and each person has their own set of requirements - so reading some of those topics may be useful for you…

https://community.blynk.cc/search?q=beehive%20connected

Pete.

2 Likes

@Tularosa You posted a question in a different topic, then created your own on the same subject.
I’ve merged the two together, and as a result some of the comments don’t make that much sense, but it’s better than having different but connected discussions taking place in different places.

In future, I’d suggest starting your own topic to begin with and linking to other related topics from there.

Pete.

Sir I do not know how in the world I did that. I could have sworn I posted my own topic, but indeed I commented on SCH’s topic. I must have pressed that link button. Sorry about that.

Most excellent. Thank you so much.

Got it to work with the following code! Thank you so much. Now I need to understand it better. I’ve noticed I can put the blink.run at the top of the void loop or right before the deep sleep call and it works in either scenario. How is that? Tyring to dig into that now.

//ESP8266 Tank Level Sensor using JSNSR04T 2.0 and 3.7V LIPO 
//Goal read level, post to blynk, deep sleep, repeat
//REV 7_14_20 810PM 
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#define sleepLength 20 //how long to deep sleep in seconds 

//ESP8266 Wire Connections to battery and JSNSR04T 2.0 Board
//Vin to LIPO 3.7V battery
//GND to LIPO Bat Gnd
//RST to D0  <----- enables the wake from deep sleep 
//D4 to Sensor Trig
//D2 to Sensor Echo
//3V3 to Sensor Vin
//GND to Sensor GND


// defines pins numbers
const int trigPin = D4;//2 for arduino
const int echoPin = D2;//5 for arduino
const int maxDist = 60;
const int r = 35.5; //radius of tank
const long piRsqd = 3959.192; //based on r=35.5 
const long pi = 3.14159;


// defines variables
int h; //height of tank
long duration;
long durationA;
long durationB;
long dist_cm;
long dist_in;
long tankLevel;
long tankLevel_ft;
int gallons;
int x = 0;

//Set WIFI and Blynk token
char ssid[] = "mmm";                                                
char pass[] = "mm";                                   
char auth[] = "mm";                                       


//********Get and Send sensor readings
void getSensorData() 
{
  // Clears the trigPin
  digitalWrite(trigPin, LOW);
  delayMicroseconds(15); //Do I really need this delay

 // while (x == 0) 
 // { //get two readings in a row before calling it good 
  // Sets the trigPin on HIGH state for 15 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(15);  //Was told this is a trick to get JSNSR04T ultrasonic sensor to work well
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  durationA = pulseIn(echoPin, HIGH);
  delayMicroseconds(15);
 
  // Sets the trigPin on HIGH state for 15 micro seconds
  digitalWrite(trigPin, HIGH);
  delayMicroseconds(15);  //Was told this is a trick to get JSNSR04T ultrasonic sensor to work well
  digitalWrite(trigPin, LOW);
  // Reads the echoPin, returns the sound wave travel time in microseconds
  durationB = pulseIn(echoPin, HIGH);

  //if (durationA == durationB) x=1;

 // }//endwhile

  duration = durationA;

  // Calculating the distance
  //58 represents assumption speed of sound = 1,000,000 / 58 * 2 / 100 = 344.8 mps
  dist_cm= duration/58;
  // multiplied by .39 to get inches
  dist_in= dist_cm *0.393701;

  //tank level is distance between sensor and bottom of empty tank (maxDistance) - distance reading
  tankLevel = maxDist - dist_in;
  tankLevel_ft = tankLevel/12;
  //Gallons remaining 
  gallons = piRsqd*tankLevel/231;  //231 cubic inches per gallon
}

void getFakeData()
{
  tankLevel_ft=5;
  gallons=555;
}


//********************Setup Call
void setup() 
{
  pinMode(trigPin, OUTPUT); // Sets the trigPin as an Output
  pinMode(echoPin, INPUT); // Sets the echoPin as an Input
  Serial.begin(115200); // Starts the serial communication
 
  Blynk.begin(auth, ssid, pass);
  delay(200);
 
  Serial.println("Connected to Blynk");

}




void loop()
{
  delay(4000);
  //getFakeData();
  getSensorData();
  
  //***********SEND INFO TO BLYNK
  Blynk.virtualWrite(V1, tankLevel_ft);  //send level to Blynk server
  Blynk.virtualWrite(V2, gallons);       //send gallons to Blynk server
  
  //** Prints the distance on the Serial Monitor
  Serial.print("Gallons Remaining: ");
  Serial.println(gallons);
  Serial.print("Tank Level: ");
  Serial.println(tankLevel_ft);
  
  float wake_time = (float)millis()/float(1000); // Find out how long since the ESP rebooted
  Blynk.virtualWrite(V3, wake_time);  // Wake time
  Serial.print("Wake Time = ");
  Serial.print(wake_time);
  Serial.println(" seconds");
  
  Blynk.run(); // Needed to ensure that the Wake Time value is always uploaded to Blynk before going to sleep
  delay(500);
  Serial.println(">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>Going to sleep");

  //ESP.deepSleep(3.6e+6);  // Deep Sleep for 1hr
  ESP.deepSleep(30e6);//testing sleep
  delay(2000);
}

I’m not sure. You’ve chosen to use an early version of the code which uses Blynk.begin. Later posts in the same topic have a much better structure, although you may need to change the included libraries back to the ESP8266 ones rather than ESP32.

Pete.

1 Like

Hi,
I am using the same project (HW wise) with the same target.
I used the ESP.deepSleep() exactly where you put it.
It works on the serial, but I could not see that it updated the Blynk Dashboard…
Did you succeed?
BR
Y3G