Hi, very great project, can you share the code for ESP32 instead ESP8266 ?
Thanks Mirek
Hi, very great project, can you share the code for ESP32 instead ESP8266 ?
Thanks Mirek
i think you need to re-write your code and optimize the loop function is overloaded , i know that you are using deep sleep but you shouldn’t keep the loop reading and sending data to server in every
MCU cycle when it is awake.
i suggest you need to use a bare ESP-12 with removed LED also the nodemcu has voltage regulator which use the battery even when it is almost idle
If you look at the void loop you’ll see that it only executes once, as the last line of the void loop puts the device to sleep. That code could be in void setup and have an empty void loop and it would work exactly the same way.
The ESP8266 code has actually moved-on quite a bit since the version that @christophebl has posted here, as he realised that Blynk.begin was causing problems if the device wasn’t able to connect to Wi-Fi, or Blynk, for any reason. The ESP was constantly trying to connect and draining his battery because it was never going to sleep.
We made some changes to use Blynk.connect instead, and limit the number of Wi-Fi connection attempts and added some logic that allowed a different sleep duration after a failed connection attempt.
Pete.
thanks for this info. nice to know
Thanks Pete for your repply.
All now it’s working perfectly. My new project is to get data access when my beehives are in a forest… GSM can be one solution but if a have many beehive to connect it will cost a lot!!! so one solution is to use Lora. So I will try to make a Gateway with The Things Network and after try to connect my ESP32 with a Lora module. Some of you have experience with Lora?
Here my new code:
Again without the help of Pete, this code will be not working propely. Many thanks again Pete!
#include <WiFi.h>
#include <WiFiClient.h>
#include <BlynkSimpleEsp32.h>
#include "max6675.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h> // Only needed for Arduino 1.6.5 and earlier
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include "HX711.h"
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 7200 /* Time ESP32 will go to sleep (in seconds) */
#define BLYNK_PRINT Serial
//capteur de poids
#define DOUT 17
#define CLK 16
//void getWeight(void);
float weight = 0;
//HX711 scale(DOUT, CLK);
float calibration_factor = - 19800; //You must change this factor depends on your scale,sensors and etc.
// Data wire is plugged into port 4 on the Arduino or ESP32
#define ONE_WIRE_BUS 15
#define TEMPERATURE_PRECISION 100
// Setup a oneWire instance to communicate with any OneWire devices (not just Maxim/Dallas temperature ICs)
OneWire oneWire(ONE_WIRE_BUS);
// Pass our oneWire reference to Dallas Temperature.
DallasTemperature DS18B20(&oneWire);
float tempruche;
//BME sensor
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME280 bme; // I2C
unsigned long delayTime;
RTC_DATA_ATTR int bootCount = 0;
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
const char auth[] = "################";
//const char blynk_server [] = "blynk-cloud.com"; // new variable to hold the name of the Blynk server
//const int blynk_port = 8080; // new variable to hold the port used by the Blynk server
// Your WiFi credentials.
// Set password to "" for open networks.
char ssid[] = "#######";
char pass[] = "#######";
int wifi_connect_count = 0; // New variable to keep track of how manty times we've tried to connect to the Wi-Fi
int wifi_connect_max_retries = 50; // New variable to specify how many attempts we will have at connecting to the Wi-Fi
float sleep_time_minutes = 1; // New variable - how long (in minutes) we sleep for. As this is a float variable then it can be 0.5 for a 30 second test
//thermocouple
int thermoDO = 18;
int thermoCS = 19;
int thermoCLK = 5;
MAX6675 thermo(thermoCLK, thermoCS, thermoDO);
// supply control
const int transistorPin = 14;
// Voltage batterie
#define ANALOG_PIN_0 32
int analog_value = 0;
float volt=0.00;
float volt1=0.00;
void getBatterie()
{
analog_value = analogRead(ANALOG_PIN_0);
Serial.println(analog_value);
volt = analog_value;
volt1=volt/794;
Serial.print("Voltage = ");
Serial.println(volt1);
}
void getBme()
{
Wire.begin(); //just added
float t = bme.readTemperature();
float p = (bme.readPressure() / 100.0F);
float h = bme.readHumidity();
float a = bme.readAltitude(SEALEVELPRESSURE_HPA);
if (isnan(t) || isnan(p) || isnan(h) || isnan(a)) {
Serial.println("Failed to read from BME sensor!");
return;
}
Serial.print("Temperature = ");
Serial.print(t);
//Serial.print(bme.readTemperature());
Serial.println(" *C");
Serial.print("Pressure = ");
//Serial.print(bme.readPressure() / 100.0F);
Serial.print(p);
Serial.println(" hPa");
Serial.print("Approx. Altitude = ");
//Serial.print(bme.readAltitude(SEALEVELPRESSURE_HPA));
Serial.print(a);
Serial.println(" m");
Serial.print("Humidity = ");
//Serial.print(bme.readHumidity());
Serial.print(h);
Serial.println(" %");
Serial.println();
}
void getWeight()
{
HX711 scale(DOUT, CLK);
scale.set_scale(calibration_factor); //Adjust to this calibration factor
Serial.print("Reading: ");
weight = scale.get_units();
Serial.print(weight, 1);
Serial.print(" kg"); // You can change this to other type of weighing value and re-adjust the calibration factor.
Serial.print(" calibration_factor: ");
Serial.print(calibration_factor);
Serial.println();
}
void getSendData()
{
DS18B20.requestTemperatures();
tempruche = DS18B20.getTempCByIndex(0); // Sensor 0 will capture Temp in Celcius
Serial.print("tempruche: ");
Serial.println(tempruche);
}
void thermoTemp()
{
float tempThermo = thermo.readCelsius(); // reading sensor from analog pin
Serial.print("tempThermo = ");
Serial.println(tempThermo);
}
void WiFi_Connect() // New functon to handle the connectuon to the Wi-Fi network
{
Serial.println(F("Connecting to Wi-Fi"));
//WiFi.config(device_ip, dns, gateway, subnet); // Not needed if you just want to have a DHCP assigned IP address. If you diont use this then delete the device_ip, dns, gateway & subnet declarations
if (WiFi.status() != WL_CONNECTED)
{
WiFi.begin(ssid, pass); // connect to the network
}
while (WiFi.status() != WL_CONNECTED && wifi_connect_count < wifi_connect_max_retries) // Loop until we've connected, or reached the maximum number of attemps allowed
{
delay(500);
wifi_connect_count++;
Serial.print(F("Wi-Fi connection - attempt number "));
Serial.println(wifi_connect_count);
}
if (WiFi.status() == WL_CONNECTED)
{
WiFi.mode(WIFI_STA);
Serial.println(F("Wi-Fi CONNECTED"));
Serial.println();
}
} // End of void WiFi_Connect
void setup()
{
Serial.begin(115200);
pinMode (transistorPin, OUTPUT);
// New section of code - stop using Blynk.begin, which is a blocking function, and instead do the following:
//
// 1) Attempt to connect to Wi-Fi a few times (how many times we try is specified by the 'wifi_connect_max_retries' variable)
// 2) If we successfully connected to Wi-Fi then attempt to connect to Blynk in a non-blocking way. If we aren't connected to Wi-Fi then go to sleep
// 3) If we connected to Blynk then run the rest of the code as normal. If we aren't connected to Blynk then go to sleep
// Blynk.begin(auth, ssid, pass);//starts wifi and Blynk - Not used in the new code as it's a blocking function
WiFi_Connect(); // Attempt to connect to Wi-Fi
if (WiFi.status() == WL_CONNECTED) // If we managed to connect to Wi-Fi then try to connect to Blynk, else go to sleep
{
Blynk.config(auth, blynk_server, blynk_port); // Initialise the Blynk connection settings
Blynk.connect(); // Attempt to connect to Blynk
}
else
{
Serial.println ("Wi-Fi connection failed - going to sleep");
//sleep_time_minutes = sleep_time_minutes * 2; // If you enable this line of code the it will make the device go to sleep for twice as long before trying again. Changing to 0.5 would make it try again sooner than normal
Deep_Sleep_Now();
}
if (Blynk.connected()) // If we manages to connect to Blynk then carry-on as normal, else go to sleep
{
Serial.println ("Connected to Blynk");
}
else
{
//sleep_time_minutes = sleep_time_minutes * 2; // If you enable this line of code the it will make the device go to sleep for twice as long before trying again. Changing to 0.5 would make it try again sooner than normal
Serial.println("Blynk connection failed - going to sleep");
Deep_Sleep_Now();
}
++bootCount;
Serial.println("Boot number: " + String(bootCount));
digitalWrite (transistorPin, HIGH); // turn on the supply
delay(2000);
bme.begin(0x76);
// scale.set_scale();
DS18B20.begin();
// scale.tare(); //Reset the scale to 0
//long zero_factor = scale.read_average();
}
void loop()
{
delay(2000);
float tempThermo = thermo.readCelsius();
float t = bme.readTemperature();
float p = (bme.readPressure() / 100.0F);
float h = bme.readHumidity();
float a = bme.readAltitude(SEALEVELPRESSURE_HPA);
thermoTemp();
getBme();
getWeight();
getSendData();
getBatterie();
delay(2000);
Blynk.virtualWrite(V0, bootCount);
Blynk.virtualWrite(V1, volt1);
Blynk.virtualWrite(V4, tempThermo);
Blynk.virtualWrite(V5,t);
Blynk.virtualWrite(V6, p);
Blynk.virtualWrite(V7, h);
Blynk.virtualWrite(V8, a);
Blynk.virtualWrite(V9, weight);
Blynk.virtualWrite(V10, tempruche);
Blynk.virtualWrite(V11, TIME_TO_SLEEP);
float wake_time = (float)millis()/float(1000); // Find out how long since the ESP rebooted
Blynk.virtualWrite(V2, 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(100);
digitalWrite (transistorPin, LOW); // turn off the supply
Deep_Sleep_Now();
}
void Deep_Sleep_Now() // New function - moded code out of void loop so that the sleep function can be called if we fail to connect to Wi-Fi or Blynk
{
esp_sleep_enable_timer_wakeup((uint64_t)(TIME_TO_SLEEP) * uS_TO_S_FACTOR);
Serial.println("Setup ESP32 to sleep for every " + String(TIME_TO_SLEEP) +
" Seconds");
Serial.println("Going to sleep now");
Serial.flush();
esp_deep_sleep_start();
delay(2000);
}
On the subject of LORA, have you watched any of Andreas Spiess’s videos?
He doesn’t use Blynk, but he has done quite a few experiments with long range LORA communications. His recent videos seem to have become a bit boring because he hints at various projects without sharing any detail, but some of the older ones are more in-depth.
Pete.
Yes i watched many time all the video that Andreas made. the guy with the… lol
Hello everyone,
I’m coming back to my evolution of my connected hive project. The main problem I had, was having a wifi connection available around my house, and I moved to a LORA connection. I first made a gateway with a Raspberry PI with a dragon module connected to the TTN server. Then with a nano ESP32 TTGO and a Lora modulre RFM 95 I made a transmitter module called LoraNode. This module sends the weight, temperature, pressure, humidity, connection number and Lora parameters every X minutes (With a deep sleep function). This data is then sent to TTN, retrieved by Node-red (installed in the raspberry) and sent back to the Blynk server.
My LoraNode
Next for me is to creat a outdoor lora gateway, test the maximun distance range I can have and install my new Loranode under a hive! and if all work creat a PCB!
Wow! I love the way that you’ve developed your skills so much in the last year or so.
Keep us posted on how this works out.
Pete.
Hi Guys
Im new to Arduino and Blynk ive just started bee keeping aswell, im trying to recreate this project as its exactly what i want to do with my hives, however i seem to be falling at the first hurdle i keep getting error messages around the libraries required. multiple libraries found when i try to compile the code
are the libraries specific ones if so is there a link ?
any help or pointers would be great
thanks.
Presumably you have already installed the Blynk library?
With the others, its best to install them via the Arduino IDE - Sketch>Include Library>Manage Libraries or if they aren’t available there then google them.
Pete.
HI Pete
yes i think so ive also installed several other libraries as well. if i search for say hx711 several libraries come up im thinking they are all different? so how do i know which one i need? ill start again from scratch tonight and re install them
Hi @Spanker. the point you make is a very valid one, and I think its something we could all learn from when we’re developing code that uses libraries that are outside the standard “out of the box” Arduino IDE install.
If I search the Arduino IDE library manager for “hx711” I get three results (one of these may be because I’ve installed a library myself, but you should probably see at least two results anyway)…
Here’s how I would go about trying to work-out which is the correct library to use. It’s not a foolproof system, and to a degree it depends on the authors of the libraries using the keywords file correctly, but it’s a good starting place.
@christophebl’s latest code (the ESP32 version) contains these two calls to the HX711 library:
scale.set_scale(calibration_factor)
scale.get_units()
If we click the “more info” link in the Arduino Library Manager for each of the HX711 libraries it will open the GitHub page for each library.
In each library you’ll see a keywords.txt file. This is used to tell the Arduino IDE which commands to highlight in a different colour on the screen, but it’s a handy way to quickly view the available function calls (probably ‘methods’ we’re being picky, but I’ll call them functions here) that are available in the library (assuming it’s been populated correctly by the developer).
The first library in my screenshot contain a keywords file that looks like this (I’ve removed some irrelevant parts):
is_ready KEYWORD2
set_gain KEYWORD2
read_average KEYWORD2
get_value KEYWORD2
get_units KEYWORD2 <--------
tare KEYWORD2
set_scale KEYWORD2 <--------
get_scale KEYWORD2
set_offset KEYWORD2
get_offset KEYWORD2
power_down KEYWORD2
power_up KEYWORD2
You’ll see that both set_scale
and get_units
exist in this list.
The next library in my screenshot has these keywords:
begin KEYWORD2
start KEYWORD2
startMultiple KEYWORD2
setGain KEYWORD2
tare KEYWORD2
tareNoDelay KEYWORD2
setCalFactor KEYWORD2
getCalFactor KEYWORD2
getData KEYWORD2
getSingleConversion KEYWORD2
getSingleConversionRaw KEYWORD2
getReadIndex KEYWORD2
getConversionTime KEYWORD2
getSPS KEYWORD2
getTareTimeoutFlag KEYWORD2
disableTareTimeout KEYWORD2
getSettlingTime KEYWORD2
powerDown KEYWORD2
powerUp KEYWORD2
getTareOffset KEYWORD2
getTareStatus KEYWORD2
setTareOffset KEYWORD2
setSamplesInUse KEYWORD2
getSamplesInUse KEYWORD2
You’ll see that although there are more available functions in this library, and there are some duplications such as tare
, neither set_scale
and get_units
exist in this list.
The last library’s keywords file look like this:
readyToSend KEYWORD2
read KEYWORD2
Which is very different to either of the other two and doesn’t contain the set_scale
orget_units
functions.
This means that it’s a fairly good bet that the library that @christophebl has used is the first one in the screenshot above, or it’s a 3rd party library that he’s fond from somewhere (fingers crossed that it’s the first).
So, the lesson is that it’s good practice to add a comment with the URL of the library alongside each of the libraries that you include. It’s also good practice to record which version of the library was used when you wrote/tested/compiled the code, as its not unheard of for new versions to drop retired commands, or for a newly added command top only be available from a certain version onwards.
If the keywords file doesn’t exist (it is optional), or you want to be more certain about the whole range of functions available, it’s possible to view the .cpp file (usually located in the src folder) and pick your way through it looking for functions that are a available within the library.
I also take a copy of every zip file library that I install and keep them in a separate folder, just in case I want to re-install the Arduino IDE with exactly the same libraries that I originally used.
Pete.
Thanks for the info pete that will be usefull, ive reloaded the ide and put fresh libraries in im trying to comile christophebl first code for the esp8266, which is the board i have
im still getting the error multiple libraries found, and an error saying SSD1306.h could not be found, i commented out wire.h and ssd1306.h as i think these are for an oled display?
and now get error
no matching function for call to ‘HX711::HX711(const int&, const int&)’
ive installed all 3 libraries out of the manager for the hx711 and looked at the example code that comes with them as a ref but they all seem to ref loadcell instead of scale
// Capteur de poids setting
const int SCALE_DOUT_PIN = D5;
const int SCALE_SCK_PIN = D6;
HX711 scale(SCALE_DOUT_PIN, SCALE_SCK_PIN);
H i Pete ive managed to get it to compile:grinning: im not 100 % sure how. I worked through a couple in error messages with google and it verified. ill upload it and see if it works:crossed_fingers:
This isn’t really an error, it’s a warning message to tell you that the compiler may not be using the library that you expected.
This isn’t really the best approach. The compiler will only use one of the libraries, and giving it three to choose from will give you a 33% chance of it choosing the correct one.
This is the type of error message that you’d expect to get if the compiler is using the wrong library. As I explained in my earlier post, the code is trying to call functions called set_scale
and get_units
and if these aren’t in the library the you’ll get the “no matching function” message.
The best approach is to un-install the incorrect HX711 libraries so that the compiler is forced to use the correct one.
Anyway, glad you’ve managed to get it to compile, but you need to do some tidying-up of your libraries so that you have repeatable results every time you compile the code in future.
Pete.
Thanks Pete is there an uninstall for libraries or is it just a case of deleting them out of the libraries folder?
or can you create a project folder and just add the libraries that are needed?
so for future libraries as long as they have the functions that are listed in the key words or somewhere in the code they should work?
sorry for all the questions im just trying to get my head around how this all works.
I loaded it last night which seemed to go ok but i didn’t see any readings i tried shortening the deep sleep time but it didn’t like that.
is there a way to disable this bit while im testing?
Thanks
Hey how did this turn out? I’m looking to do a project just like this.
Jim