Thanks for the response. Here is a copy of the current code running - I’ve now tried to simplify it completely but it still exhibits the same problem. Using the latest version 1.5.2
I forgot to mention - resetting the device brings it back online straight away but this is a poor solution - I suppose I could reset it in code every 12 hours but would rather not.
#include <MQTT.h>
#include <CE_BME280.h>
#define SEALEVELPRESSURE_HPA (1013.25)
CE_BME280 bme; // I2C
MQTT mqtt("192.168.0.14", 1883, callback);
int relay1State = 0;
int relay2State = 0;
float bmeTemperature;
float bmeHumidity;
float bmePressure;
float Dewpoint;
float currentTime;
float sensorValue;
float tempMin = 40;
float tempMax = 0;
long timerPublishData;
long timerFlash;
bool ledToggle;
bool D3State = false;
bool lastD3State = false;
bool D4State = false;
bool lastD4State = false;
bool flagOnce = false;
String mqttStr;
String tempStr;
String humidityStr;
String pressureStr;
String dewpointStr;
String DeviceID = "MQTT Relay Board v1";
String TimeNow;
int RELAY1_ADDR = 1;
int RELAY2_ADDR = 5;
void callback(char* topic, byte* payload, unsigned int length) // mqtt receive message
{
char p[length + 1];
memcpy(p, payload, length);
p[length] = NULL;
Particle.publish("mqtt_rec", p);
if (!strcmp(p, "relay1/off")) relay1(0);
else if (!strcmp(p, "relay1/on")) relay1(1);
else if (!strcmp(p, "relay2/off")) relay2(0);
else if (!strcmp(p, "relay2/on")) relay2(1);
else if (!strcmp(p, "tempReset")) reset_min_max();
delay(200);
}
void reset_min_max()
{
tempMin = bmeTemperature;
tempMax = bmeTemperature;
mqtt.publish("howells/home/rb1/tempMin", String(tempMin), true);
mqtt.publish("howells/home/rb1/tempMax", String(tempMax), true);
Particle.publish("Temperature", "min/max reset");
}
void relay1(int state1)
{
digitalWrite(D3, state1);
EEPROM.put(RELAY1_ADDR, state1);
if (state1)
{
mqtt.publish("howells/home/rb1/relay1/status", "on");
if (Particle.connected()) Particle.publish("relay1", "on");
}
else
{
mqtt.publish("howells/home/rb1/relay1/status", "off");
if (Particle.connected()) Particle.publish("relay1", "off");
}
}
void relay2(int state2)
{
digitalWrite(D4, state2);
EEPROM.put(RELAY2_ADDR, state2);
if (state2)
{
mqtt.publish("howells/home/rb1/relay2/status", "on");
if (Particle.connected()) Particle.publish("relay2", "on");
}
else
{
mqtt.publish("howells/home/rb1/relay2/status", "off");
if (Particle.connected()) Particle.publish("relay2", "off");
}
}
void temperature_pub()
{
if (bme.begin()) getBME280Data();
tempStr = String(bmeTemperature);
humidityStr = String(bmeHumidity);
pressureStr = String(bmePressure);
dewpointStr = String(Dewpoint);
if (mqtt.isConnected())
{
mqtt.publish("howells/home/rb1/temperature", tempStr, true);
mqtt.publish("howells/home/rb1/tempMin", String(tempMin), true);
mqtt.publish("howells/home/rb1/tempMax", String(tempMax), true);
//mqtt.publish("howells/home/rb1/humidity", humidityStr, true);
//mqtt.publish("howells/home/rb1/pressure", pressureStr, true);
//mqtt.publish("howells/home/rb1/dewpoint", dewpointStr, true);
}
else
{
if (Particle.connected()) Particle.publish("MQTT", "not connected");
mqtt.connect("rVd6K8onGMGQ0ZJgnaaW");
}
}
void setup()
{
//Particle.disconnect();
pinMode(D3, OUTPUT);
pinMode(D4, OUTPUT);
pinMode(D7, OUTPUT);
mqtt.connect("rVd6K8onGMGQ0ZJgnaaW"); //connect to mqtt server
if (mqtt.isConnected()) //subscribe to messages
{
mqtt.subscribe("howells/home/rb1/power");
if (Particle.connected()) Particle.publish("MQTT", "subscribed");
}
// initial state of relays same as before power was lost
EEPROM.get(RELAY1_ADDR, relay1State);
EEPROM.get(RELAY2_ADDR, relay2State);
if (relay1State) relay1(1);
if (relay2State) relay2(1);
if(!bme.begin())
{
if (Particle.connected()) Particle.publish(DeviceID, "BME280 Not found");
}
timerPublishData = millis();
}
void loop()
{
if (millis() >= timerFlash + 1000)
{
timerFlash = millis();
ledToggle = !ledToggle;
digitalWrite(D7, ledToggle);
}
if (millis() > timerPublishData + 60000)
{
timerPublishData = millis();
temperature_pub();
}
if (mqtt.isConnected()) {
mqtt.loop();
}
}
void getBME280Data()
{
bmeTemperature = bme.readTemperature();
if (bmeTemperature > tempMax) tempMax = bmeTemperature;
if (bmeTemperature < tempMin) tempMin = bmeTemperature;
bmeHumidity = bme.readHumidity();
bmePressure = (bme.readPressure()/100);
Dewpoint = bmeTemperature - ((100 - bmeHumidity)/5);
}