OTGW-firmware/OTGW-firmware.ino

189 lines
5.1 KiB
Arduino
Raw Normal View History

2020-10-25 20:48:57 +01:00
/*
***************************************************************************
** Program : OTGW-firmware.ino
2020-10-25 23:56:26 +01:00
** Version : v0.1.0
2020-10-25 20:48:57 +01:00
**
** Copyright (c) 2020 Robert van den Breemen
** Borrowed from OpenTherm library from:
** https://github.com/jpraus/arduino-opentherm
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
#include "version.h"
#define _FW_VERSION _VERSION
#define _HOSTNAME "OTGWfirmware"
2020-10-25 20:48:57 +01:00
#include "OTGW-firmware.h"
// WiFi Server object and parameters
WiFiServer server(80);
//=====================================================================
void setup()
{
Serial.begin(9600, SERIAL_8N1);
//while(!Serial) { /* wait a bit */ }
//setup randomseed the right way
randomSeed(RANDOM_REG32); //This is 8266 HWRNG used to seed the Random PRNG: Read more: https://config9.com/arduino/getting-a-truly-random-number-in-arduino/
lastReset = ESP.getResetReason();
//setup the status LED
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, HIGH); //OFF
//start the debug port 23
startTelnet();
Debug("\r\n[OTGW firmware - Nodoshop version]\r\n");
DebugTf("Booting....[%s]\r\n\r\n", String(_FW_VERSION).c_str());
//================ SPIFFS ===========================================
if (SPIFFS.begin())
{
DebugTln(F("SPIFFS Mount succesfull\r"));
SPIFFSmounted = true;
} else {
DebugTln(F("SPIFFS Mount failed\r")); // Serious problem with SPIFFS
SPIFFSmounted = false;
}
readSettings(true);
// Connect to and initialise WiFi network
DebugTln("Attempting to connect to WiFi network\r");
digitalWrite(LED_BUILTIN, HIGH);
startWiFi(_HOSTNAME, 240); // timeout 4 minuten
digitalWrite(LED_BUILTIN, LOW);
startMDNS(settingHostname);
// Start MQTT connection
startMQTT();
// Initialisation ezTime
setDebug(INFO);
waitForSync();
CET.setLocation(F("Europe/Amsterdam"));
CET.setDefault();
Debugln("UTC time: "+ UTC.dateTime());
Debugln("CET time: "+ CET.dateTime());
snprintf(cMsg, sizeof(cMsg), "Last reset reason: [%s]\r", ESP.getResetReason().c_str());
DebugTln(cMsg);
Serial.print("\nGebruik 'telnet ");
Serial.print (WiFi.localIP());
Serial.println("' voor verdere debugging\r\n");
//================ Start HTTP Server ================================
setupFSexplorer();
httpServer.serveStatic("/FSexplorer.png", SPIFFS, "/FSexplorer.png");
httpServer.on("/", sendIndexPage);
httpServer.on("/index", sendIndexPage);
httpServer.on("/index.html",sendIndexPage);
httpServer.serveStatic("/index.css", SPIFFS, "/index.css");
httpServer.serveStatic("/index.js", SPIFFS, "/index.js");
// all other api calls are catched in FSexplorer onNotFounD!
httpServer.on("/api", HTTP_GET, processAPI);
httpServer.begin();
DebugTln("\nServer started\r");
// Set up first message as the IP address
sprintf(cMsg, "%03d.%03d.%d.%d", WiFi.localIP()[0], WiFi.localIP()[1], WiFi.localIP()[2], WiFi.localIP()[3]);
DebugTf("\nAssigned IP[%s]\r\n", cMsg);
initWatchDog(); // setup the WatchDog
} // setup()
//=====================================================================
//===[ blink status led ]===
void blinkLEDms(uint32_t iDelay){
//blink the statusled, when time passed
DECLARE_TIMER_MS(timerBlink, iDelay);
if (DUE(timerBlink)) {
blinkLEDnow();
}
}
void blinkLEDnow(){
pinMode(LED_BUILTIN, OUTPUT);
digitalWrite(LED_BUILTIN, !digitalRead(LED_BUILTIN));
}
//===[ no-blocking delay with running background tasks in ms ]===
void delayms(unsigned long delay_ms)
{
DECLARE_TIMER_MS(timerDelayms, delay_ms);
while (DUE(timerDelayms))
doBackgroundTasks();
}
//=====================================================================
//===[ Do task every 1s ]===
void doTaskEvery1s(){
//== do tasks ==
upTimeSeconds++;
feedWatchDog(); // Feed the dog before it bites!
//DebugTln("Feed the dog before he bites!");
}
//===[ Do task every 5s ]===
void doTaskEvery5s(){
//== do tasks ==
}
//===[ Do task every 30s ]===
void doTaskEvery30s(){
//== do tasks ==
DebugTf("Alive, Serial Available=[%d]\r\n", Serial.available());
}
//===[ Do task every 60s ]===
void doTaskEvery60s(){
//(re)configure homeassistant integration
setupHomeassitantIntegration();
}
//===[ Do the background tasks ]===
void doBackgroundTasks()
{
2020-10-25 23:25:49 +01:00
blinkLEDms(1000); // 'blink' the status led every x ms
handleOTGW(); // OTGW handling
2020-10-25 23:25:49 +01:00
handleMQTT(); // MQTT transmissions
httpServer.handleClient();
MDNS.update();
events(); // trigger ezTime update etc.
yield();
}
void loop()
{
DECLARE_TIMER_SEC(timer1s, 1, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer5s, 5, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer30s, 30, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_SEC(timer60s, 60, CATCH_UP_MISSED_TICKS);
if (DUE(timer1s)) doTaskEvery1s();
if (DUE(timer5s)) doTaskEvery5s();
if (DUE(timer30s)) doTaskEvery30s();
if (DUE(timer60s)) doTaskEvery60s();
doBackgroundTasks();
}