#include <ESP8266WiFi.h>
#include <TimeLib.h>
#include <NtpClientLib.h>
#include <MQTTClient.h>
#include <LinkedList.h>
#include <ArduinoJson.h>
#include <FS.h>
#include <ESP8266WebServer.h>
#include <ESP8266HTTPUpdateServer.h>
#include <DallasTemperature.h>
#include <OneWire.h>
const int RELAY = 12;
const int LED = 13;
const char* SSID = "WLAN";
const char* PWD = "12345678910";
WiFiClient NET;
MQTTClient MQTT(10240);
struct SETTINGS
{
byte MODE;
float DAY_TEMPERATURE;
float NIGHT_TEMPERATURE;
};
struct SCHEDULE
{
word TIME_START;
word TIME_STOP;
bool MONDAY;
bool TUESDAY;
bool WEDNESDAY;
bool THURSDAY;
bool FRIDAY;
bool SATURDAY;
bool SUNDAY;
};
SETTINGS SETTINGS_STRUCT;
LinkedList<SCHEDULE> SCHEDULE_STRUCTS;
ESP8266WebServer WEB_SERVER(80);
ESP8266HTTPUpdateServer WEB_UPDATER;
OneWire ONE_WIRE(14);
DallasTemperature DS18B20(&ONE_WIRE);
String CHIP_ID = String(ESP.getChipId());
bool CONNECTION;
bool LOCATION;
unsigned long MILLIS_CHECK = 0;
unsigned long MILLIS_LED = 0;
void setup()
{
pinMode(RELAY, OUTPUT);
pinMode(LED, OUTPUT);
Serial.begin(115200);
MQTT.begin("10.100.10.2", NET);
MQTT.onMessage(settings);
WEB_UPDATER.setup(&WEB_SERVER, "/", "admin", "admin");
/*
WEB_SERVER.on("/status", []()
{
WEB_SERVER.send(200, "text/plain", String(relay()));
});
WEB_SERVER.on("/temperature", []()
{
WEB_SERVER.send(200, "text/plain", String(sensor()));
});
*/
WEB_SERVER.begin();
DS18B20.begin();
SPIFFS.format();
configuration();
}
void loop()
{
MQTT.loop();
delay(10);
WEB_SERVER.handleClient();
if(WiFi.status() != WL_CONNECTED || !MQTT.connected())
{
CONNECTION = false;
}
else if(WiFi.status() == WL_CONNECTED && MQTT.connected())
{
CONNECTION = true;
}
if(millis() - MILLIS_CHECK >= 10000)
{
if(second() == 0)
{
MILLIS_CHECK = millis();
if(CONNECTION == false)
{
connect();
}
calculation();
switch(minute())
{
case 0:
data();
break;
case 10:
data();
break;
case 20:
data();
break;
case 30:
data();
break;
case 40:
data();
break;
case 50:
data();
break;
}
}
}
if(LOCATION == true)
{
if(millis() - MILLIS_LED >= 1000)
{
MILLIS_LED = millis();
if(led("STATE") == "ON")
{
led("OFF");
}
else if(led("STATE") == "OFF")
{
led("ON");
}
}
}
//Serial.println(String(String(day()) + "." + String(month()) + "." + String(year())));
//Serial.println(String(String(hour()) + ":" + String(minute())));
}
void connect()
{
if(WiFi.status() != WL_CONNECTED)
{
NTP.stop();
Serial.println("WIFI");
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
WiFi.config(IPAddress(10, 100, 10, 3), IPAddress(10, 100, 10, 1), IPAddress(10, 100, 10, 1));
WiFi.begin(SSID, PWD);
for(int COUNTER = 0; COUNTER < 100; COUNTER++)
{
Serial.print(".");
if(WiFi.status() == WL_CONNECTED)
{
break;
}
}
Serial.println();
}
if(WiFi.status() == WL_CONNECTED && !MQTT.connected())
{
Serial.println("MQTT");
for(int COUNTER = 0; COUNTER < 100; COUNTER++)
{
Serial.print(".");
if(MQTT.connect(String(CHIP_ID).c_str()))
{
MQTT.subscribe(String(CHIP_ID + "/Config"));
MQTT.subscribe("Info");
break;
}
}
Serial.println();
if(WiFi.status() == WL_CONNECTED)
{
NTP.begin("10.100.10.2", 1, true);
NTP.setInterval(10, 30);
Serial.print("IP: ");
Serial.println(WiFi.localIP());
}
if(MQTT.connected())
{
MQTT.publish("Serial", CHIP_ID);
}
}
}
void settings(String &TOPIC, String &PAYLOAD)
{
if(TOPIC == String(CHIP_ID + "/Config"))
{
if(PAYLOAD == "LOCATE")
{
LOCATION = true;
}
else if(PAYLOAD == "LOCATED")
{
LOCATION = false;
}
else
{
DynamicJsonBuffer BUFFER;
JsonObject& root = BUFFER.parseObject(PAYLOAD);
if(!root.success())
{
return;
}
SPIFFS.begin();
File FILE = SPIFFS.open("CONFIGURATION.json", "w");
root.printTo(FILE);
FILE.close();
SPIFFS.end();
Serial.print("WRITE: ");
root.printTo(Serial);
Serial.println();
configuration();
}
}
else if(TOPIC == "Info")
{
if(PAYLOAD == "")
{
String INFO;
DynamicJsonBuffer BUFFER;
JsonObject& root = BUFFER.createObject();
root["SERIAL"] = CHIP_ID;
if(relay("STATE") == "ON")
{
root["STATUS"] = 1;
}
else if(relay("STATE") == "OFF")
{
root["STATUS"] = 0;
}
root["TEMPERATURE"] = sensor();
root.printTo(INFO);
MQTT.publish("Info", INFO);
}
}
}
void data()
{
String DATA;
DynamicJsonBuffer BUFFER;
JsonObject& root = BUFFER.createObject();
if(relay("STATE") == "ON")
{
root["STATUS"] = 1;
}
else if(relay("STATE") == "OFF")
{
root["STATUS"] = 0;
}
root["TEMPERATURE"] = sensor();
root["DATE"] = String(String(day()) + "." + String(month()) + "." + String(year()));
root["TIME"] = String(String(hour()) + ":" + String(minute()));
root.printTo(DATA);
MQTT.publish(String(CHIP_ID + "/Data"), DATA);
}
String relay(String ACTION)
{
if(ACTION == "STATE")
{
if(digitalRead(RELAY) == HIGH)
{
return "ON";
}
else if(digitalRead(RELAY) == LOW)
{
return "OFF";
}
}
else if(ACTION == "ON")
{
digitalWrite(RELAY, HIGH);
}
else if(ACTION == "OFF")
{
digitalWrite(RELAY, LOW);
}
else if(ACTION == "ON_CHECK")
{
if(digitalRead(RELAY) != HIGH)
{
digitalWrite(RELAY, HIGH);
}
}
else if(ACTION == "OFF_CHECK")
{
if(digitalRead(RELAY) != LOW)
{
digitalWrite(RELAY, LOW);
}
}
}
float sensor()
{
float TEMPERATURE;
for(int COUNTER = 0; COUNTER < 10; COUNTER++)
{
DS18B20.requestTemperatures();
TEMPERATURE = DS18B20.getTempCByIndex(0);
if(!(TEMPERATURE == 85.0 || TEMPERATURE == (-127.0)))
{
TEMPERATURE = (float)((int)(TEMPERATURE * 100)) / 100;
return TEMPERATURE;
}
}
}
String led(String ACTION)
{
if(ACTION == "STATE")
{
if(digitalRead(LED) == LOW)
{
return "ON";
}
else if(digitalRead(LED) == HIGH)
{
return "OFF";
}
}
else if(ACTION == "ON")
{
digitalWrite(LED, LOW);
}
else if(ACTION == "OFF")
{
digitalWrite(LED, HIGH);
}
else if(ACTION == "ON_CHECK")
{
if(digitalRead(LED) != LOW)
{
digitalWrite(LED, LOW);
}
}
else if(ACTION == "OFF_CHECK")
{
if(digitalRead(LED) != HIGH)
{
digitalWrite(LED, HIGH);
}
}
}
void configuration()
{
SPIFFS.begin();
File FILE = SPIFFS.open("CONFIGURATION.json", "r");
if(!FILE)
{
return;
}
else
{
if(FILE.size() == 0)
{
return;
}
else
{
DynamicJsonBuffer BUFFER;
JsonObject& root = BUFFER.parseObject(FILE);
Serial.print("READ: ");
root.printTo(Serial);
Serial.println();
SETTINGS_STRUCT = {root["MODE"], root["DAY_TEMPERATURE"], root["NIGHT_TEMPERATURE"]};
Serial.println(SETTINGS_STRUCT.MODE);
Serial.println(SETTINGS_STRUCT.DAY_TEMPERATURE);
Serial.println(SETTINGS_STRUCT.NIGHT_TEMPERATURE);
SCHEDULE_STRUCTS.clear();
for(int COUNTER = 0; COUNTER < root["SCHEDULE"].size(); COUNTER++)
{
SCHEDULE_STRUCTS.add
(
{
root["SCHEDULE"][COUNTER]["TIME_START"].as<word>(),
root["SCHEDULE"][COUNTER]["TIME_STOP"].as<word>(),
root["SCHEDULE"][COUNTER]["MONDAY"].as<bool>(),
root["SCHEDULE"][COUNTER]["TUESDAY"].as<bool>(),
root["SCHEDULE"][COUNTER]["WEDNESDAY"].as<bool>(),
root["SCHEDULE"][COUNTER]["THURSDAY"].as<bool>(),
root["SCHEDULE"][COUNTER]["FRIDAY"].as<bool>(),
root["SCHEDULE"][COUNTER]["SATURDAY"].as<bool>(),
root["SCHEDULE"][COUNTER]["SUNDAY"].as<bool>()
}
);
Serial.println(SCHEDULE_STRUCTS.get(COUNTER).TIME_START);
Serial.println(SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP);
Serial.println(SCHEDULE_STRUCTS.get(COUNTER).MONDAY);
Serial.println(SCHEDULE_STRUCTS.get(COUNTER).TUESDAY);
Serial.println(SCHEDULE_STRUCTS.get(COUNTER).WEDNESDAY);
Serial.println(SCHEDULE_STRUCTS.get(COUNTER).THURSDAY);
Serial.println(SCHEDULE_STRUCTS.get(COUNTER).FRIDAY);
Serial.println(SCHEDULE_STRUCTS.get(COUNTER).SATURDAY);
Serial.println(SCHEDULE_STRUCTS.get(COUNTER).SUNDAY);
}
}
FILE.close();
}
SPIFFS.end();
}
void calculation()
{
if(SETTINGS_STRUCT.MODE == 0)
{
for(int COUNTER = 0; COUNTER < SCHEDULE_STRUCTS.size(); COUNTER++)
{
if(weekday() == 2)
{
if(SCHEDULE_STRUCTS.get(COUNTER).MONDAY == true)
{
if((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP))
{
if(sensor() > SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("ON_CHECK");
}
}
else if(!((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP)))
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(SCHEDULE_STRUCTS.get(COUNTER).MONDAY == false)
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(weekday() == 3)
{
if(SCHEDULE_STRUCTS.get(COUNTER).TUESDAY == true)
{
if((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP))
{
if(sensor() > SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("ON_CHECK");
}
}
else if(!((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP)))
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(SCHEDULE_STRUCTS.get(COUNTER).TUESDAY == false)
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(weekday() == 4)
{
if(SCHEDULE_STRUCTS.get(COUNTER).WEDNESDAY == true)
{
if((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP))
{
if(sensor() > SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("ON_CHECK");
}
}
else if(!((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP)))
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(SCHEDULE_STRUCTS.get(COUNTER).WEDNESDAY == false)
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(weekday() == 5)
{
if(SCHEDULE_STRUCTS.get(COUNTER).THURSDAY == true)
{
if((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP))
{
if(sensor() > SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("ON_CHECK");
}
}
else if(!((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP)))
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(SCHEDULE_STRUCTS.get(COUNTER).THURSDAY == false)
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(weekday() == 6)
{
if(SCHEDULE_STRUCTS.get(COUNTER).FRIDAY == true)
{
if((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP))
{
if(sensor() > SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("ON_CHECK");
}
}
else if(!((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP)))
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(SCHEDULE_STRUCTS.get(COUNTER).FRIDAY == false)
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(weekday() == 7)
{
if(SCHEDULE_STRUCTS.get(COUNTER).SATURDAY == true)
{
if((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP))
{
if(sensor() > SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("ON_CHECK");
}
}
else if(!((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP)))
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(SCHEDULE_STRUCTS.get(COUNTER).SATURDAY == false)
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(weekday() == 1)
{
if(SCHEDULE_STRUCTS.get(COUNTER).SUNDAY == true)
{
if((((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP))
{
if(sensor() > SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("ON_CHECK");
}
}
else if(!(((hour() * 100) + minute()) > SCHEDULE_STRUCTS.get(COUNTER).TIME_START) && (((hour() * 100) + minute()) < SCHEDULE_STRUCTS.get(COUNTER).TIME_STOP))
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
else if(SCHEDULE_STRUCTS.get(COUNTER).SUNDAY == false)
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
}
}
}
else if(SETTINGS_STRUCT.MODE == 1)
{
relay("OFF_CHECK");
}
else if(SETTINGS_STRUCT.MODE == 2)
{
if(sensor() > SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.DAY_TEMPERATURE)
{
relay("ON_CHECK");
}
}
else if(SETTINGS_STRUCT.MODE == 3)
{
if(sensor() > SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("OFF_CHECK");
}
else if(sensor() < SETTINGS_STRUCT.NIGHT_TEMPERATURE)
{
relay("ON_CHECK");
}
}
if(relay("STATE") == "ON")
{
led("ON_CHECK");
}
else if(relay("STATE") == "OFF")
{
led("OFF_CHECK");
}
}
WIFI
....................................................................................................
MQTT
.
IP: 10.100.10.3
WRITE: {"MODE":"1","DAY_TEMPERATURE":"20.0","NIGHT_TEMPERATURE":"10.0","SCHEDULE":[]}
READ: {"MODE":"1","DAY_TEMPERATURE":"20.0","NIGHT_TEMPERATURE":"10.0","SCHEDULE":[]}
1
20.00
10.00
Exception (9):
epc1=0x4023337c epc2=0x00000000 epc3=0x00000000 excvaddr=0x00c6003e depc=0x00000000
ctx: sys
sp: 3ffffd80 end: 3fffffb0 offset: 01a0
>>>stack>>>
3fffff20: 3fff09c4 3fff09c0 0e819f4a 40100690
3fffff30: 3fff09c4 3fff09c0 0e819f4a 4021b635
3fffff40: 3ffee930 2fdba0aa 60000600 40236219
3fffff50: 4021b62c 00000000 00000001 40213769
3fffff60: 4023ae88 3ffee908 3ffee930 60000600
3fffff70: 2fdbfaed 3ffee930 3ffee908 4023ae95
3fffff80: 4023aeda 3fffdab0 00000000 3fffdcb0
3fffff90: 3ffee948 3fffdad0 3ffef4a0 40213703
3fffffa0: 40000f49 3ffe9778 3fffdab0 40000f49
<<<stack<<<
ets Jan 8 2013,rst cause:2, boot mode:(1,6)
ets Jan 8 2013,rst cause:4, boot mode:(1,6)
wdt reset
There are no compile errors and the code works until I send a MQTT Message to the ESP8266. Then after some seconds this error occurs.
I hope someone can help me.