Note: Pete this is a different temp project
I have had this BME280 going for sometime. I am running it on battery with deep sleep. Our internet is very bad here and sometimes our power is out for a day or two so therefore I removed the “while” loop for the connections because it was running down my battery. If it is not connected after the 3200ms then it goes to sleep and tries later. Any way I am using the RTC library for storing the min and max temps, humidity and trying to for the barometric pressure. For some reason all of the min/max’s work except the max for barometric pressure. At first I thought maybe my RTC memory was full but I moved it up in the line. (I imagine it starts storing at the first variable and works down.) I tried just storing it as an integer instead of float to also save space but nothing worked. I have stared at my logic till my head hurt… What am I doing wrong.
By not working I mean that it follows the baro back down where as the temp and humidity stay at the max.
Also you probably noted the predefined variable for the minimums. Is there a better way of doing this?
#define BLYNK_PRINT Serial
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <BlynkSimpleEsp8266.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
//Rtc store value
#include <RTCVars.h>
RTCVars state; // create the state object
float rtc_maxTemp;
float rtc_minTemp = 40.0;
float rtc_maxBar;
float rtc_minBar = 1500.0;
int rtc_maxHumid;
int rtc_minHumid = 101;
long Sleep_Time = 6e+7; //One minute in microseconds
int rtc_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 = 3600; // New variable to specify how many attempts we will have at connecting to the Wi-Fi
int rtc_Blynk_Connect_tries = 0;
int Blynk_Connect_tries_max = 60;
const char auth[] = "";
char ssid[] = ""; //Enter your WIFI Name
char pass[] = ""; //Enter your WIFI Password
// Set your Static IP address
IPAddress local_IP(192, 168, 0, 33);
// Set your Gateway IP address
IPAddress gateway(192, 168, 0, 1);
IPAddress subnet(255, 255, 255, 0);
IPAddress primaryDNS(8, 8, 8, 8); //optional
IPAddress secondaryDNS(8, 8, 4, 4); //optional
BlynkTimer timer;
Adafruit_BME280 bme; // I2C NodeMCU D1-SCK D2-SDI G-SDO for D1 Mini D3-SDO and Write LOW!!
void Deep_Sleep_Now()
{
ESP.deepSleep(Sleep_Time); //sleep NOTE THIS IS IN MICROSECONDS!!!!!!!!
}
void setup()
{
Serial.begin(115200);
Serial.println("Setting up");
Serial.println();
state.registerVar(&rtc_maxTemp); // we send a pointer to each of our RTC_variables
state.registerVar(&rtc_minTemp);
state.registerVar(&rtc_maxBar);
state.registerVar(&rtc_minBar);
state.registerVar(&rtc_maxHumid);
state.registerVar(&rtc_minHumid);
state.registerVar(&rtc_wifi_connect_count);
state.registerVar(&rtc_Blynk_Connect_tries);
//Load RTC values
state.loadFromRTC(); // we load the values from rtc memory back into the registered variables
//digitalWrite(D4, HIGH);
if (WiFi.status() != WL_CONNECTED)
{
WiFi.config(local_IP, gateway, subnet, primaryDNS, secondaryDNS);
WiFi.begin(ssid, pass); // connect to the network
timer.setTimeout(3200L,[](){
if ((WiFi.status() != WL_CONNECTED) && (rtc_wifi_connect_count <= wifi_connect_max_retries))
{
rtc_wifi_connect_count ++;
state.saveToRTC();
Serial.print("WiFi Reconnect tries ");
Serial.println(rtc_wifi_connect_count);
Serial.print("1 Going to sleep ");
Serial.println(Sleep_Time);
Serial.println();
Deep_Sleep_Now();
}
if (rtc_wifi_connect_count >= wifi_connect_max_retries)
{
Serial.println("Total WiFi failure");
Serial.print("Reconnect tries ");
Serial.println(rtc_wifi_connect_count);
Sleep_Time = 0;
Serial.print("2 Going to sleep ");
Serial.println(Sleep_Time);
Serial.println();
rtc_wifi_connect_count = 0;
state.saveToRTC();
Deep_Sleep_Now();
}
if (WiFi.status() == WL_CONNECTED)
{
Serial.println(F("Wi-Fi CONNECTED"));
Blynk.config(auth);
Blynk.connect();
Serial.println("Trying Blynk");
Serial.println();
delay(300);
}
if ((Blynk.connected()== false) && (rtc_Blynk_Connect_tries <= Blynk_Connect_tries_max)) // If we manages to connect to Blynk then carry-on as normal, else go to sleep
{
Serial.print ("Connecting to Blynk number of retries ");
Serial.println (rtc_Blynk_Connect_tries);
rtc_Blynk_Connect_tries ++;
state.saveToRTC();
Serial.print("3 Going to sleep ");
Serial.println(Sleep_Time);
Serial.println();
Deep_Sleep_Now();
}
if (rtc_Blynk_Connect_tries >= Blynk_Connect_tries_max)
{
Serial.println("Blynk connection failed - going to sleep");
Serial.print ("Connecting to Blynk number of retries ");
Serial.println (rtc_Blynk_Connect_tries);
Sleep_Time = 0;
Serial.print("4 Going to sleep ");
Serial.println(Sleep_Time);
Serial.println();
rtc_Blynk_Connect_tries = 0;
state.saveToRTC();
Deep_Sleep_Now();
}
Serial.println("Blynk connected");
bme.begin();
float temp_BME = bme.readTemperature();
temp_BME = roundf(temp_BME * 10) / 10; //attempt to short and save rtc mem
float bar_BME = ((bme.readPressure()*0.0133)); //((bme.readPressure()/100.0)*0.030);
//barBME = roundf(barBME * 10) / 10; //attempt to short and save rtc mem
float c = bme.readAltitude(1013.25);
int humid_BME = bme.readHumidity();
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V7, temp_BME); //V7 is for Temperature on BMP280
Blynk.virtualWrite(V8, bar_BME); //V8 is for Pressure on BMP280
delay(500); //tried to see if more time would help save Max baro
if(bar_BME >= rtc_maxBar);
{
rtc_maxBar = bar_BME;
state.saveToRTC();
}
if(bar_BME <= rtc_minBar)
{
rtc_minBar = bar_BME;
state.saveToRTC();
}
if(humid_BME >= rtc_maxHumid)
{
rtc_maxHumid = humid_BME;
state.saveToRTC();
}
if(humid_BME <= rtc_minHumid)
{
rtc_minHumid = humid_BME;
state.saveToRTC();
}
Blynk.virtualWrite(V10, humid_BME);
Blynk.virtualWrite(V18, rtc_maxHumid);
Blynk.virtualWrite(V17, rtc_minHumid);
if(temp_BME >= rtc_maxTemp) //Update max temp if larger than RTC
{
rtc_maxTemp = temp_BME;
state.saveToRTC();
}
if(temp_BME <= rtc_minTemp) //Update max temp if larger than RTC
{
rtc_minTemp = temp_BME;
state.saveToRTC();
}
float reading = analogRead(A0);
float volts = (reading / 248.2);
Blynk.virtualWrite(V11,volts);
if(volts <= 3.2)
{
Blynk.notify("Check Battery");
}
long rssi = WiFi.RSSI();
Blynk.virtualWrite(V12,rssi);
Blynk.virtualWrite(V13,rtc_minTemp);
Blynk.virtualWrite(V14,rtc_maxTemp);
Blynk.virtualWrite(V15,rtc_minBar);
Blynk.virtualWrite(V16,rtc_maxBar);
digitalWrite(D4,HIGH);
Blynk.run();
Serial.println("Saving wifi and blynk counts");
rtc_wifi_connect_count = 0;
rtc_Blynk_Connect_tries = 0;
state.saveToRTC();
timer.setTimeout(500L,[](){
Deep_Sleep_Now();
});
});
}
}
void loop()
{
Blynk.run(); // Initiates Blynk
timer.run();
}