Beehive project issue

Well, your code certainly wont work with those delays in the void loop - Blynk doesn’t like that because they block the execution of the code until the delay has completed.

I’m no expert on the use of timers, and haven’t used Wi-Fi force sleep/wake before. However, I would suspect that the solution is to have a timer that calls a “wakeup” routine every 40 seconds, which then updates the Blynk server and puts the Wi-Fi back to sleep once its executed.

However, I suspect that your OLED display consumes more power than the ESP’s Wi-Fi routines.

Pete.

1 Like

@christophebl
salut Christophe :wink:

il faut que tu vires ton code de void loop, sinon blynk ne peut pas fonctionner.
tu dois juste avoir :

void loop(){
  Blynk.run();
  timer.run();
}

@christophebl

pour le sleep mode tu dois faire un timer lambda
donc on a toutes les 40 secondes sleep, et 20 secondes plus tard wake up, puis 20 secondes plus tard sleep etc …
si c’est ce que tu recherches

void setup(){
timer.setInterval(40000L, SleepFunction);
.
.
}

void SleepFunction() {
 {
    Serial.println("déconnecté");
    WiFi.disconnect();
    WiFi.forceSleepBegin();
    timer.setTimeout(20000L, WakeUpFunction);
      }
}
void WakeUpFunction() {    
     Serial.println("connecté");
     WiFi.forceSleepWake();
  }
2 Likes

Merci beaucoup Alexis. Je vais essayer ça! :slight_smile:

2 Likes

Hi everyone,

I’m coming back to you regarding my beehive connected project. One month ago I found a solution to use my ESP8266 in modem sleep, coming back to finalize all because I received all electronic components now my program is not working any more.

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*********************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*****";
char pass[] = "*****";



#include <Wire.h>  // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include <DHT.h>
#include <HX711.h>

// Capteur de poids setting
const int SCALE_DOUT_PIN = D5;
const int SCALE_SCK_PIN = D6;
HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);
float weight;

// Capteur température DHT11

#define DHT1PIN D3     // what pin we're connected to
#define DHT1TYPE DHT11   // DHT 11
float t1;
float h1;

// Capteur température DHT22

#define DHT2PIN D7     // what pin we're connected to
#define DHT2TYPE DHT22   // DHT 22
float t2;
float h2;

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
BlynkTimer timer;


// Voltage batterie
float volt=0;
float volt1=0.00;

void batterie()
{
   pinMode(A0, INPUT);
   volt = analogRead(A0);
   volt1=volt/233,8;
   //Serial.println(volt1);
   Blynk.virtualWrite(V3, volt1);
}
  
 /* void getDht1Data(void){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h1 = dht1.readHumidity();
  // Read temperature as Celsius
  float t1 = dht1.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h1) || isnan(t1)){
    Serial.println("Défault DHT11");
    return;
     }
  Blynk.virtualWrite(V6, t1); //virtual pin V6 DHT11
  Blynk.virtualWrite(V5, h1); // virtual pin V5 DHT11
    } 

*/
   void getDht2Data(void){
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h2 = dht2.readHumidity();
  // Read temperature as Celsius
  float t2 = dht2.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h2) || isnan(t2)){
   Serial.println("Défault DHT22");
    return;
     }
     //Serial.println(t2);
     //Serial.println(h2);
  Blynk.virtualWrite(V7, t2); //virtual pin V7 DHT22
  Blynk.virtualWrite(V8, h2); // virtual pin V8 DHT22
    } 

  
void sendWeight()
{
  
  String weight = String(scale.get_units(1), 2);
  //Serial.println(weight);
  Blynk.virtualWrite(V4, weight);

}

/*void sendSensor()
{
  // You can send any value at any time.
  // Please don't send more that 10 values per second.
  Blynk.virtualWrite(V6, t1); //virtual pin V6 DHT11
  Blynk.virtualWrite(V5, h1); // virtual pin V5 DHT11
  Blynk.virtualWrite(V7, t2); //virtual pin V7 DHT22
  Blynk.virtualWrite(V8, h2); // virtual pin V8 DHT22
  //Blynk.virtualWrite(V4, weight); // virtual pin V12
}
*/
void setup(){

  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  scale.set_scale( -6000 / 0.3095 );
  scale.tare();
  //dht1.begin(); // initialize dht11
  dht2.begin(); // initialize dht22
 // timer.setInterval(4000L, getDht1Data);
  timer.setInterval(4000L, getDht2Data);
  pinMode(A0, INPUT);
  timer.setInterval(1000L, batterie);
  timer.setInterval(120000L, SleepFunction); //wifi activé pendant 120s
  timer.setInterval(1000L, sendWeight);
 }
 
void SleepFunction() {
 {
    Serial.println("will be disconnected soon");
    delay(2000);
    wifi_set_sleep_type(LIGHT_SLEEP_T);
    WiFi.disconnect();
    WiFi.forceSleepBegin();
    Serial.println("wifi disconnected");
 //   timer.setTimeout(20000L, WakeUpFunction); //Pas de wifi pendant 100s
   delay(80000);
   Serial.println("after delay");
   wifi_set_sleep_type(NONE_SLEEP_T);
   WiFi.forceSleepWake();
   Blynk.begin(auth, ssid, pass);
   delay(2000);
   Serial.println("connected");
     
      }
}
void loop(){

  Blynk.run();
  timer.run();  
}

Aftert 120s I got this error:

[20188] Connected to WiFi
[20188] IP: 192.168.1.25
[20188] 
    ___  __          __
   / _ )/ /_ _____  / /__
  / _  / / // / _ \/  '_/
 /____/_/\_, /_//_/_/\_\
        /___/ v0.5.0 on NodeMCU

[20194] Connecting to blynk-cloud.com:8442
[20323] Ready (ping: 41ms).
very soon disconnect

Soft WDT reset

ctx: cont 
sp: 3ffffd60 end: 3fffffd0 offset: 01b0

>>>stack>>>
3fffff10:  00000000 000002f6 00000000 3ffffea3  
3fffff20:  00000000 0000018d 0000018d 4010020c  
3fffff30:  3fffff50 3fffff50 00000004 402240a0  
3fffff40:  0fffffff 00000008 3ffe95a0 40100518  
3fffff50:  3ffe9368 3ffe9370 000002f6 00000000  
3fffff60:  0001260d 00000600 3ffef2fc 402237e0  
3fffff70:  0fffffff 40203b1e 00000000 40203b09  
3fffff80:  3fffdad0 3ffef2fc 3ffef468 402038d1  
3fffff90:  3fffdad0 3ffef068 3ffef068 40204c5c  
3fffffa0:  3fffdad0 00000000 3ffef538 40203935  
3fffffb0:  3fffdad0 00000000 3ffef538 402055ac  
3fffffc0:  feefeffe feefeffe 3ffe8624 40100739  
<<<stack<<<

 ets Jan  8 2013,rst cause:2, boot mode:(1,7)


 ets Jan  8 2013,rst cause:4, boot mode:(1,7)

wdt reset

I just want to disconnect the ESP8266 wifi during a while and turn on few second. I spent the week trying to figure out why it does not work anymore … thanks for your help :yum:

First of all, I think you need to re-visit the logic of these timers.
You’re trying to send the weight readings to Blynk every second, but then trying to put the Wi-Fi to sleep every 120 seconds.
It’s almost as though you’re thinking that these numbers represent the duration rather than the frequency of the event?

I think you probably only w at one f7nction that’s called every 120 seconds. When it’s called it does the following:

  • Take a humidity reading

  • Take a temperature reading

  • Take a battery reading

  • Turn on Wi-Fi

  • Upload the readings to Blynk

  • Turn off Wi-Fi

The delays in your current SleepFunction won’t be needed then. It’s probably these delays that are causing your WDT resets.

Pete.

Many thanks for your answer Pete,

ok I understand what you explained to me regarding the timer. I also try this code with the same result:

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

extern "C" {
  #include "user_interface.h"
}

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "******************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "Livebox-667E";
char pass[] = "***********";


#include <Arduino.h>

BlynkTimer timer;

 void setup() 
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  timer.setInterval(120000L, SleepFunction); 

}

void SleepFunction() {
 {
    Serial.println("very soon disconnect");
    delay(2000);
    wifi_set_sleep_type(LIGHT_SLEEP_T);
    WiFi.disconnect();
    WiFi.forceSleepBegin();
    Serial.println("wifi disconnected");
   timer.setTimeout(20000L, WakeUpFunction); //Pas de wifi pendant 100s
   delay(80000);
   Serial.println(" after delay");
   wifi_set_sleep_type(NONE_SLEEP_T);
   WiFi.forceSleepWake();
   Blynk.begin(auth, ssid, pass);
   delay(2000);
   Serial.println("connected");
     
      }
}


void loop(){
  Blynk.run();
  timer.run();
    
}

The 120 are just used for test, after I want to send my data every 2 hours. But just for testing the stability of my program I used 120s

@christophebl may I ask why you are not using deepSleep() ?

The code you posted won’t compile because it calls a function called WakeUpFunction which doesnt exist.

But your SleepFunction still contains delays, and is structured in the opposite way to what I suggested.

I’ve modified your earlier code to work the way I think it should. It compiles okay, but I’ve not tested it…

#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <BlynkSimpleEsp8266.h>
#include <DHT.h>

// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "*********************";

// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "*****";
char pass[] = "*****";


#include <Wire.h>  // Only needed for Arduino 1.6.5 and earlier
#include "SSD1306.h" // alias for `#include "SSD1306Wire.h"`
#include <DHT.h>
#include <HX711.h>

// Capteur de poids setting
const int SCALE_DOUT_PIN = D5;
const int SCALE_SCK_PIN = D6;
HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);
float weight;

// Capteur température DHT11
#define DHT1PIN D3     // what pin we're connected to
#define DHT1TYPE DHT11   // DHT 11
float t1;
float h1;

// Capteur température DHT22
#define DHT2PIN D7     // what pin we're connected to
#define DHT2TYPE DHT22   // DHT 22
float t2;
float h2;

// Initialize DHT sensor for normal 16mhz Arduino
DHT dht1(DHT1PIN, DHT1TYPE);
DHT dht2(DHT2PIN, DHT2TYPE);
BlynkTimer timer;


// Voltage batterie
float volt=0;
float volt1=0.00;

void getBatterie()
{
   pinMode(A0, INPUT);
   volt = analogRead(A0);
   volt1=volt/233,8;
   Serial.print("Voltage = ");  
   Serial.println(volt1);
}
  
void getDht2Data()
{
  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  h2 = dht2.readHumidity();
  // Read temperature as Celsius
  t2 = dht2.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h2) || isnan(t2))
  {
    Serial.println("Défault DHT22");
    return;
  }
  Serial.print("Temperature = ");    
  Serial.println(t2);
  Serial.print("Humidity = ");        
  Serial.println(h2);
} 
  
void getWeight()
{
  String weight = String(scale.get_units(1), 2);
  Serial.print("Weight = ");  
  Serial.println(weight);
}


void setup()
{
  Serial.begin(115200);
  Blynk.begin(auth, ssid, pass);
  scale.set_scale( -6000 / 0.3095 );
  scale.tare();
  dht2.begin(); // initialize dht22
  pinMode(A0, INPUT);
  timer.setInterval(120000L, Wakeup_and_do_something); //wifi activé pendant 120s
}
 
void Wakeup_and_do_something()
{
  // Take readings of Battery Voltage, Temperature, Humidity and Weight...
  getBatterie();  
  getDht2Data();
  getWeight();    

  // Turn on the Wi-Fi and connect to Blynk...
  Serial.println("Turning Wi-Fi on...");
  wifi_set_sleep_type(NONE_SLEEP_T);
  WiFi.forceSleepWake();
  Blynk.begin(auth, ssid, pass);
  delay(2000); // Not sure if this is needed, or desireable - PeteKnight
  Serial.println("Connected to Blynk");

  //Update Blynk with the latest readings...
  Blynk.virtualWrite(V3, volt1);
  Blynk.virtualWrite(V7, t2); //virtual pin V7 DHT22
  Blynk.virtualWrite(V8, h2); // virtual pin V8 DHT22
  Blynk.virtualWrite(V4, weight);   

  // Disconnect Blynk then turn off the Wi-Fi...
  Blynk.disconnect();
  Serial.println("Blynk disconnected");
  wifi_set_sleep_type(LIGHT_SLEEP_T);
  WiFi.disconnect();
  WiFi.forceSleepBegin();
  Serial.println("Wi-Fi disconnected");
  Serial.println();  
}

void loop(){
  Blynk.run();
  timer.run();  
}

Pete.

I think the original plan was to have a display that could be activated at any point to show the current readings. Although this could be done whilst using Deep Sleep, it would be a bit more complex.

I think Deep Sleep might actually be worth revisiting now, as @christophebl’s understanding of the way that Blynk works has improved, and the requirements have been revised somewhat.

Pete.

1 Like

If I’m using the deepSleep mode I lost my data of the weight. When the ESP start the 0 weight is set like a calibration and after I can get the weight. If I’m using the deepsleep after it my weight will back to 0 even if I have some weight on it. I dont know if it’s clear for you :grinning:

I don’t see anywhere in your current code that you’re saving a previous weight reading.
I would be possible to save the starting weight, or even the last weight reading, in SPIFFS and re-load this at every reboot (which would occur when the ESP wakes up from deep sleep.

Can you explain more about what you wnat your final solution to look like? Have you abandoned the OLED display, and what do you want the weight functionality to do exactly?

Pete.

Yes I removed my OLED because I want to save my battery. I used a solar system to recharge my battery and it’s the reason that I want to save power. So for me get data every hour from my beehive it’s enough.

Many thanks for the code you modified and I try to uderstand what you did :slight_smile: sorry I’m so bad.
So I load your code and I juste get this:
⸮Voltage = 4.27
Temperature = 19.60
Humidity = 74.50
Weight = 0.03
Turning Wi-Fi on…

And nothing more. I get the conenction to my phone and after the connection stop but it’s not coming back after.
Regarding the weight, I don’t save it. It is difficult to explain. When I supply my ESP there is at the beginning like a calibration, and after he measures well the weight. if for example the esp goes off and I have for example a beehive on my sensor with a certain weight, I lose the initial value and this value returns to 0 even if I have a weight on my sensor

Pete, i look at the SPIFFS option and I didn’t no that I can save data like this. So every esp off like deep sleep I can just before save my weight data and used it when the esp goes on.

1 Like

Thinking about it, there might be a better way than using SPIFFS, which would be to use Blynk to save the weight value then retrieve it.

To suggest the best solution, I need to know that your ideal system would be for the weight system. I would think that it might be something like this (hopefully the beekeeping terms in French aren’t too different)…

At the start of the season you set-up your hive with the colony in a brood box and maybe 1 or 2 supers.
At this point, you want to take a measurement of the weight of the hive. This is your baseline figure.

As the season progresses, the hive increases in weight because the colony is growing and they’re collecting honey that they’re storing in the supers.
Your weight reading should show the current weight minus the baseline value.

The problem arises when you want to add more supers and/or another brood box, as this adds to your base hive weight. At this point, you want to be able to take your last weight reading before adding the new parts to the hive and immediately after adding the new parts you want to be able to adjust your baseline figure so that the new weight is the same as it was before.

If this is what you want to do, then the simplest way I can think of is to have a numeric input widget in your app, where you type-in the baseline value.

Say your initial basic hive plus colony weighs 25kg. You’d type 25 into the numeric widget and the weight widget would then show zero.

Later in the season the weight widget shows 20kg, which is the weight of the honey and increased brood. At this point you add another super and your weight widget shows 26kg, so we know the super weighed 6kg. You change the numeric input widget value from 25kg to 31kg (the base 25kg plus the extra 6kg for the new super). The weight widget now shows 20kg again, because this is still the weight of the honey plus extra brood.

Does this make sense, and is it how you’d like it to work?
If so, I’m happy to modify your code to work in this way.

On a different note, I’ve just loaded the code I posted earlier onto an ESP8266 and I’m trying to work-out why it’s not working (although this may not be necessary if you wanted to go down the deep sleep route).

Pete.

le retour de Christophe :wink:

So, I’ll explain to you how I would like my system to work. basic I have a support on which I put my hive. on this support I have my weight sensor and a part that supports the hive. all year I need to have the full weight of the hive and especially between the months of April and July. and in the spring the total weight to know if the bees have enough food to spend the winter.after I need a system that I can install everywhere without electricity so I made a solar power with a battery of 5000ma / h so esp wifi uses too much power and as I do not need to have data regularly every hour I thought to use the modem sleep. after i have already used the deep sleep but i do not know how to save my weight measure just before the esp goes out …

Salut Alexis et oui de retour et toujours le même problème…

1 Like

Okay, can you give me a link to the HX711.h library that you’re using, so I understand a bit more about how this works, and which value it is that we need to be saving to SPIFFS or the Blynk server?

Edited to add - can you also post a link to the specific HX711 board that you’re using please.

Pete.

Since you are using Blynk, send it to the server, populate a Table Widget, generate and send regular reports to email via the Report Widget, or something. Even the SuperChart will work, although you have to watch out for the averaging.