OTGW-firmware/OTGW-firmware.ino

238 lines
7.1 KiB
Arduino
Raw Normal View History

2020-10-25 20:48:57 +01:00
/*
***************************************************************************
** Program : OTGW-firmware.ino
** Version : v0.8.1
2020-10-25 20:48:57 +01:00
**
** Copyright (c) 2021 Robert van den Breemen
2020-10-25 20:48:57 +01:00
**
** TERMS OF USE: MIT License. See bottom of file.
***************************************************************************
*/
/*
2021-01-31 23:43:52 +01:00
* How to install the OTGW on your nodeMCU:
* Read this: https://github.com/rvdbreemen/OTGW-firmware/wiki/How-to-compile-OTGW-firmware-yourself
*
2021-01-31 23:43:52 +01:00
* How to upload to your LittleFS?
* Read this: https://github.com/rvdbreemen/OTGW-firmware/wiki/Upload-files-to-LittleFS-(filesystem)
*
* How to compile this firmware?
* - NodeMCU v1.0
* - Flashsize (4MB - FS:2MB - OTA ~1019KB)
* - CPU frequentcy: 160MHz
* - Normal defaults should work fine.
* First time: Make sure to flash sketch + wifi or flash ALL contents.
*
*/
2020-10-25 20:48:57 +01:00
#include "version.h"
#define _FW_VERSION _VERSION
2020-10-25 20:48:57 +01:00
#include "OTGW-firmware.h"
#define ON LOW
#define OFF HIGH
//=====================================================================
2021-02-14 19:26:37 +01:00
void setup() {
// Serial is initialized by OTGWSerial. It resets the pic and opens serialdevice.
// OTGWSerial.begin();//OTGW Serial device that knows about OTGW PIC
// while (!Serial) {} //Wait for OK
OTGWSerial.println(F("\r\n[OTGW firmware - Nodoshop version]\r\n"));
OTGWSerial.printf("Booting....[%s]\r\n\r\n", String(_FW_VERSION).c_str());
2021-02-02 00:36:36 +01:00
rebootCount = updateRebootCount();
//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/
2021-03-06 10:52:51 +01:00
lastReset = ESP.getResetReason();
OTGWSerial.printf("Last reset reason: [%s]\r\n", CSTR(ESP.getResetReason()));
//setup the status LED
setLed(LED1, ON);
setLed(LED2, ON);
2021-02-14 19:26:37 +01:00
LittleFS.begin();
readSettings(true);
// Connect to and initialise WiFi network
OTGWSerial.println(F("Attempting to connect to WiFi network\r"));
setLed(LED1, ON);
startWiFi(_HOSTNAME, 240); // timeout 240 seconds
2021-03-06 10:52:51 +01:00
blinkLED(LED1, 3, 100);
setLed(LED1, OFF);
2021-03-07 10:55:52 +01:00
startTelnet(); // start the debug port 23
startMDNS(CSTR(settingHostname));
2021-03-06 19:14:08 +01:00
startLLMNR(CSTR(settingHostname));
startMQTT();
2021-02-14 19:26:37 +01:00
startNTP();
2021-03-07 10:55:52 +01:00
setupFSexplorer();
2021-02-19 01:48:31 +01:00
startWebserver();
OTGWSerial.println(F("Setup finished!\r\n"));
2021-02-02 19:24:31 +01:00
// After resetting the OTGW PIC never send anything to Serial for debug
// and switch to telnet port 23 for debug purposed.
2021-01-31 23:43:52 +01:00
// Setup the OTGW PIC
2021-01-18 21:53:53 +01:00
resetOTGW(); // reset the OTGW pic
2021-02-02 00:56:38 +01:00
startOTGWstream(); // start port 25238
2021-03-06 10:52:51 +01:00
checkOTWGpicforupdate();
initSensors(); // init DS18B20
2021-03-06 10:36:07 +01:00
2021-03-06 10:52:51 +01:00
initWatchDog(); // setup the WatchDog
//Blink LED2 to signal setup done
2021-03-06 10:36:07 +01:00
setLed(LED1, OFF);
2021-03-06 10:52:51 +01:00
blinkLED(LED2, 3, 100);
setLed(LED2, OFF);
2021-03-06 10:36:07 +01:00
}
//=====================================================================
//===[ blink status led ]===
void setLed(uint8_t led, uint8_t status){
pinMode(led, OUTPUT);
digitalWrite(led, status);
}
void blinkLEDms(uint32_t delay){
//blink the statusled, when time passed
DECLARE_TIMER_MS(timerBlink, delay);
if (DUE(timerBlink)) {
blinkLEDnow();
}
}
2021-03-06 10:54:53 +01:00
void blinkLED(uint8_t led, int nr, uint32_t waittime_ms){
2021-03-06 10:52:51 +01:00
for (int i = nr; i>0; i--){
blinkLEDnow(led);
delayms(waittime_ms);
blinkLEDnow(led);
delayms(waittime_ms);
}
}
void blinkLEDnow(uint8_t led = LED1){
pinMode(led, OUTPUT);
digitalWrite(led, !digitalRead(led));
}
//===[ 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++;
}
//===[ Do task every 5s ]===
void doTaskEvery5s(){
//== do tasks ==
pollSensors();
}
//===[ Do task every 30s ]===
void doTaskEvery30s(){
//== do tasks ==
}
//===[ Do task every 60s ]===
void doTaskEvery60s(){
2020-11-19 16:47:10 +01:00
//== do tasks ==
//if no wifi, try reconnecting (once a minute)
if (WiFi.status() != WL_CONNECTED)
{
//disconnected, try to reconnect then...
2021-02-21 14:12:51 +01:00
startWiFi(_HOSTNAME, 240);
//check OTGW and telnet
startTelnet();
startOTGWstream();
}
}
//===[ Do task every 5min ]===
void do5minevent(){
DebugTf("Uptime seconds: %d", upTimeSeconds);
2021-03-08 02:38:24 +01:00
String sUptime = String(upTimeSeconds);
sendMQTTData("otgw-firmware/uptime", sUptime, false);
}
2021-02-20 17:49:56 +01:00
//===[ check for new pic version ]===
void docheckforpic(){
String latest = checkforupdatepic("gateway.hex");
if (!bOTGWonline) {
sMessage = sPICfwversion;
} else if (latest != sPICfwversion) {
sMessage = "New PIC version " + latest + " available!";
}
}
//===[ Do the background tasks ]===
void doBackgroundTasks()
{
2020-11-02 08:09:26 +01:00
feedWatchDog(); // Feed the dog before it bites!
2020-10-25 23:25:49 +01:00
handleMQTT(); // MQTT transmissions
2020-11-20 00:43:22 +01:00
handleOTGW(); // OTGW handling
httpServer.handleClient();
MDNS.update();
events(); // trigger ezTime update etc.
2021-03-04 22:54:22 +01:00
// // 'blink' the status led every x ms
// if (settingLEDblink) blinkLEDms(1000);
2020-11-20 00:43:22 +01:00
delay(1);
handleDebug();
}
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);
2021-02-20 17:49:56 +01:00
DECLARE_TIMER_MIN(tmrcheckpic, 1440, CATCH_UP_MISSED_TICKS);
DECLARE_TIMER_MIN(timer5min, 5, CATCH_UP_MISSED_TICKS);
if (DUE(timer1s)) doTaskEvery1s();
if (DUE(timer5s)) doTaskEvery5s();
if (DUE(timer30s)) doTaskEvery30s();
if (DUE(timer60s)) doTaskEvery60s();
2021-02-20 17:49:56 +01:00
if (DUE(tmrcheckpic)) docheckforpic();
if (DUE(timer5min)) do5minevent();
doBackgroundTasks();
}
2021-01-30 18:35:11 +01:00
/***************************************************************************
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sublicense, and/or sell copies of the Software, and to permit
* persons to whom the Software is furnished to do so, subject to the
* following conditions:
*
* The above copyright notice and this permission notice shall be included
* in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
* OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
* THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
****************************************************************************
*/