Wasn't sure if this is the right place for this question but here goes.
I have setup my hardware the same as: https://www.instructables.com/id/Solar-Powered-WiFi-Weather-Station-V20/
I changed the code to be my own as I wanted to use MQTT instead of Thingspeak or Blynk.
I am having an issue where the weather station will work once I turn it on but then will stop all of a sudden. Sometimes it stops after 10 minutes, sometimes after 6 hours, or really anytime.
The battery hasn't died because if I go out and turn it off and on (or press the reset button on the wemos) it starts working again for a while.
I can't make it "stop" while it is connected to my laptop when I'm reading serial data, it all looks happy then.
What is the best way to debug something that uses deep sleep?
I didn't have a delay between publishing the data and going into deep sleep, I thought this was my problem but it ran for 6 hours yesterday and has now been "stopped" for ~8 hours.
The wifi and MQTT server are up and fine as I have other wemos inside my house running fine and publishing to the same server.
If I can't connect to wifi after 15 seconds or then connect to the mqtt server after 15 seconds I restart the wemos so that it doesn't get stuck in a loop while not able to connect.
I ran a wifi scanner strength check from the wemos and the strength of the wifi is over 40%.
Here is the full code incase I am doing something silly (I hope I am as its driving me mad now):
#include <Adafruit_BME280.h>
#include <ESP8266WiFi.h>
#include <PubSubClient.h>
// configure period between reports
const long interval = 300000; // 5 minutes
// bme280 bme0(0, false) ; // creates object bme0 of type bme280, base address
Adafruit_BME280 bme;
// Network details
const char ssid[] = "proper-ssid";
const char wifi_password[] = "proper-password";
// MQTT broker
const char mqtt_server[] = "proper-ip";
const char mqtt_topic[] = "proper-topic";
const char mqtt_username[] = "proper-username";
const char mqtt_password[] = "proper-password";
const char mqtt_client_id[] = "proper-id";
WiFiClient wifi_client;
PubSubClient client(mqtt_server, 1883, wifi_client);
double temperature;
double pressure;
double humidity;
float altitude = 0;
float volt;
void setup()
{
Serial.begin(115200);
bool bme_status = bme.begin(0x76); //address either 0x76 or 0x77
if (!bme_status)
{
Serial.println("Could not find a valid BME280 sensor, check wiring!");
}
bme.setSampling(Adafruit_BME280::MODE_FORCED,
Adafruit_BME280::SAMPLING_X1, // temperature
Adafruit_BME280::SAMPLING_X1, // pressure
Adafruit_BME280::SAMPLING_X1, // humidity
Adafruit_BME280::FILTER_OFF);
get_data();
connect_to_wifi();
connect_to_mqtt();
post_data();
go_to_sleep();
}
void loop()
{
}
void connect_to_wifi()
{
WiFi.persistent(false);
WiFi.disconnect();
WiFi.mode(WIFI_STA);
// Connect to the network
WiFi.begin(ssid, wifi_password);
Serial.println("Connecting to WiFi");
int retry_count = 0;
while (WiFi.status() != WL_CONNECTED && retry_count < 15)
{
delay(1000);
retry_count++;
Serial.print(".");
}
if (WiFi.status() != WL_CONNECTED)
{
ESP.restart();
}
Serial.println("");
}
void connect_to_mqtt()
{
int retry_count = 0;
while (!client.connected() && retry_count < 15)
{
// Connect to MQTT broker
if (client.connect(mqtt_client_id, mqtt_username, mqtt_password))
{
Serial.println("Connected to MQTT Broker!");
}
else
{
Serial.println("Connection to MQTT Broker failed...");
Serial.print(client.state());
retry_count++;
delay(1000);
}
}
if (!client.connected())
{
ESP.restart();
}
}
void get_data()
{
bme.takeForcedMeasurement();
temperature = bme.readTemperature();
humidity = bme.readHumidity();
pressure = bme.readPressure() / 100.0F;
Serial.print("Atm press = ");
Serial.print(pressure, 2);
Serial.print(" hPa. Temperature = ");
Serial.print(temperature, 2);
Serial.print( " deg C. Humidity = ");
Serial.print(humidity, 2);
Serial.print( " %RH. Altitude = ");
Serial.print(altitude, 2);
// Battery Voltage Monitoring
// Voltage divider R1 = 220k+100k+220k =540k and R2=100k
float calib_factor = 5.24; // change this value to calibrate the battery voltage
unsigned long raw = analogRead(A0);
volt = raw * calib_factor / 1024;
Serial.print( " m. \nVoltage = ");
Serial.print(volt, 2);
Serial.println(" V");
}
void post_data()
{
String serialised = String(temperature, 2);
serialised += "|";
serialised += String(pressure, 2);
serialised += "|";
serialised += String(humidity, 2);
serialised += "|";
serialised += String(altitude, 2);
serialised += "|";
serialised += String(volt, 2);
client.publish(mqtt_topic, serialised.c_str(), false);
client.loop();
}
void go_to_sleep()
{
// calculate required sleep time and go to sleep
long sleepTime = interval - millis();
if (sleepTime < 100)
{
sleepTime = 100; // set minimum sleep of 0.1 second
}
delay(2000);
ESP.deepSleep(sleepTime * 1000, WAKE_RF_DEFAULT); // convert to microseconds
}