Hello,
I've created a small weather station, consisting of a solar/battery powered Wemos Lolin32 Lite, an BME280, TSL2591, a rain gauge (MS-WH-SP-RG) and wind speed/direction (WH-SP-WS01, MS-WH-SP-WD). Measurements will be collected every 30sec and send out via mqtt to my ioBroker installation.
In theory everything works fine (usb as well as solar/battery powered). But after about a day of running the weather station just stops working (not connected to mqtt/wifi, no led; just seems to be dead). As soon as I unplug/plug the power it works again (just hitting the reset button has no effect). Since the 3.3v bus is active I reckon that it is not a hardware problem.
I've appended the source code and fritzing diagram to proof read. Would be great if anyone can point me to a potential problem which might cause this behaviour.
I"ve added the ESP.restart just to troubleshoot. Behaviour was present before that change and is still present after this change. Battery voltage seems to be ok as well. Did not find any drop in voltage, which might result in a cut off.
Thanks in advance.
#include <WiFi.h>
#include <NTPClient.h>
#include <PubSubClient.h>
#include <Adafruit_BME280.h>
#include <Adafruit_TSL2591.h>
#define I2C_SDA 13
#define I2C_SCL 15
#define PIN_WIND_SPEED 16
#define PIN_WIND_DIR 34
#define PIN_RAIN 17
#define INTERVAL 30
const char *ssid = "***";
const char *password = "***";
WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_BME280 bme;
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
WiFiUDP ntpUDP;
NTPClient timeClient(ntpUDP, "pool.ntp.org");
unsigned int windSpeedCounter = 0;
unsigned int rainCounter = 0;
unsigned int rainCounterHourly = 0;
int lastHour = 0;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.print("Connecting to ");
Serial.print(ssid);
Serial.print(" with mac ");
Serial.print(WiFi.macAddress());
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer("***", 1883);
Wire.begin(I2C_SDA, I2C_SCL);
bool bmeStatus = bme.begin(0x76, &Wire);
if (!bmeStatus) {
Serial.println("Could not find a valid BME280 sensor, check wiring!");
while (1);
}
bool tslStatus = tsl.begin(&Wire, TSL2591_ADDR);
if (!tslStatus) {
Serial.println("Could not find a valid TSL2591 sensor, check wiring!");
while (1);
}
tsl.setGain(TSL2591_GAIN_LOW);
pinMode(PIN_WIND_SPEED, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_WIND_SPEED), countWindSpeed, RISING);
pinMode(PIN_RAIN, INPUT_PULLUP);
attachInterrupt(digitalPinToInterrupt(PIN_RAIN), countRain, RISING);
pinMode(PIN_WIND_DIR, INPUT);
timeClient.begin();
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
timeClient.update();
char temperature[8];
sprintf(temperature, "%.2f", bme.readTemperature());
client.publish("weather-station/temperature", temperature);
char humidity[8];
sprintf(humidity, "%.2f", bme.readHumidity());
client.publish("weather-station/humidity", humidity);
char pressure[8];
sprintf(pressure, "%.2f", (bme.readPressure() / 100.0F));
client.publish("weather-station/pressure", pressure);
char luminosity[9];
uint16_t lum = tsl.getLuminosity(TSL2591_VISIBLE);
sprintf(luminosity, "%i", lum);
client.publish("weather-station/lux", luminosity);
Serial.print("Temperature ");
Serial.print(temperature);
Serial.print(" *C");
Serial.print(", Pressure ");
Serial.print(pressure);
Serial.print(" hPa");
Serial.print(", Humidity ");
Serial.print(humidity);
Serial.print(" %");
Serial.print(", LUX ");
Serial.print(lum);
Serial.print(" ( ");
Serial.print(luminosity);
Serial.println(") lux");
char windSpeed[8];
sprintf(windSpeed, "%.2f", ((windSpeedCounter/INTERVAL) * 2.4));
client.publish("weather-station/windSpeed", windSpeed);
windSpeedCounter = 0;
char rain[8];
sprintf(rain, "%.2f", ((rainCounter/2)*0.2794));
client.publish("weather-station/rain", rain);
rainCounter = 0;
int currentHour = timeClient.getHours();
if (lastHour != currentHour) {
char rainHourly[8];
sprintf(rainHourly, "%.2f", ((rainCounterHourly/2)*0.2794));
client.publish("weather-station/rainHourly", rainHourly);
rainCounterHourly = 0;
lastHour = currentHour;
}
String wd = "-";
// create a mean value of the analog value
int dirValue = 0;
for (int i = 0; i < 10; i++){
dirValue += analogRead(PIN_WIND_DIR);
}
dirValue = dirValue / 10.0;
if (dirValue > 2978 && dirValue < 3226) {
wd = "N";
} else if (dirValue > 1387 && dirValue < 1734) {
wd = "NNE";
} else if (dirValue > 1735 && dirValue < 2120) {
wd = "NE";
} else if (dirValue > 300 && dirValue < 353) {
wd = "ENE";
} else if (dirValue > 354 && dirValue < 438) {
wd = "E";
} else if (dirValue > 10 && dirValue < 299) {
wd = "ESE";
} else if (dirValue > 623 && dirValue < 859) {
wd = "SE";
} else if (dirValue > 439 && dirValue < 622) {
wd = "SSE";
} else if (dirValue > 1065 && dirValue < 1386) {
wd = "S";
} else if (dirValue > 860 && dirValue < 1064) {
wd = "SSW";
} else if (dirValue > 2460 && dirValue < 2666) {
wd = "SW";
} else if (dirValue > 2121 && dirValue < 2459) {
wd = "WSW";
} else if (dirValue > 3666 && dirValue < 4095) {
wd = "W";
} else if (dirValue > 3227 && dirValue < 3429) {
wd = "WNW";
} else if (dirValue > 3430 && dirValue < 3665) {
wd = "NW";
} else if (dirValue > 2667 && dirValue < 2977) {
wd = "NNW";
}
char windDirection[3];
sprintf(windDirection, "%s", wd);
client.publish("weather-station/windDirection", windDirection);
Serial.print("Rain: ");
Serial.print(rain);
Serial.print(", WindSpeed: ");
Serial.print(windSpeed);
Serial.print(", WindDir: ");
Serial.print(wd);
Serial.print(" (");
Serial.print(dirValue);
Serial.println(")");
delay(INTERVAL * 1000);
int currentSecs = timeClient.getSeconds();
if (currentHour == 0 && timeClient.getMinutes() == 0 && currentSecs > 0 && currentSecs < 30) {
ESP.restart();
}
}
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Create a random client ID
String clientId = "ESP-Weather-Station-";
clientId += String(random(0xffff), HEX);
// Attempt to connect
if (client.connect(clientId.c_str())) {
Serial.println("connected");
} else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// Wait 5 seconds before retrying
delay(5000);
}
}
}
// interrupt functions
void countWindSpeed() {
windSpeedCounter++;
}
void countRain() {
rainCounter++;
rainCounterHourly++;
}