watchdog stuff
This commit is contained in:
parent
659193bed2
commit
b4788c257a
@ -18,12 +18,15 @@
|
||||
#include "reset.h"
|
||||
|
||||
|
||||
char WATCHDOG_TOPIC[] = "IoT/Watchdog";
|
||||
|
||||
|
||||
char MQTT_BROKER_DEFAULT[] = "192.168.75.1";
|
||||
const uint16_t MQTT_PORT = 1883;
|
||||
|
||||
uint32_t lastWatchdogMessageReceived = 0;
|
||||
const uint32_t WATCHDOG_MESSAGE_DELAY = 10000;
|
||||
const uint32_t WATCHDOG_MESSAGE_TIMEOUT = 20000;
|
||||
const uint32_t WATCHDOG_MESSAGE_TIMEOUT = 60000;
|
||||
|
||||
|
||||
String MqttConfig::exec(String params) {
|
||||
@ -174,25 +177,18 @@ String MqttConfig::exec(String params) {
|
||||
|
||||
|
||||
void callback(char* topic, byte* payload, unsigned int length) {
|
||||
const char WATCHDOG_TOPIC[] = "IoT/Watchdog";
|
||||
const char WATCHDOG_MSG[] = "WauWau!";
|
||||
|
||||
if (0 == strcmp(topic, WATCHDOG_TOPIC)) {
|
||||
if (0 == strncmp(topic, WATCHDOG_TOPIC, length)) {
|
||||
Serial << "Watchdog message received" << endl;
|
||||
if (0 != strcmp((char*)payload, WATCHDOG_MSG)) {
|
||||
Serial << "Wrong content in watchdog message" << endl;
|
||||
} else {
|
||||
lastWatchdogMessageReceived = millis();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Metro secondTick = Metro(1000);
|
||||
|
||||
|
||||
MqttClient::MqttClient(RequestSender *meterBusMaster) : m_mqttConfig(this),
|
||||
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(m_client), m_alarmState(false),
|
||||
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(m_client),
|
||||
m_disconnectState(3), m_disconnectTime(millis()), m_uptime(0), m_deviceIdx(0)
|
||||
{
|
||||
}
|
||||
@ -270,28 +266,10 @@ void MqttClient::begin(CmdServer *cmdServer) {
|
||||
configRead(CONFIG_BROKER, sizeof(m_mqttBroker), (char*)m_mqttBroker);
|
||||
|
||||
|
||||
|
||||
m_mqttClient = PubSubClient(m_mqttBroker, MQTT_PORT, callback, m_client);
|
||||
}
|
||||
|
||||
void MqttClient::exec() {
|
||||
if (lastWatchdogMessageReceived + WATCHDOG_MESSAGE_DELAY < millis()) {
|
||||
if (! m_alarmState) {
|
||||
alarmOn();
|
||||
Serial << "Entering alarm state due to missing watchdog message" << endl;
|
||||
}
|
||||
} else {
|
||||
if (m_alarmState) {
|
||||
alarmOff();
|
||||
Serial << "Leaving alarm state due to received watchdog message" << endl;
|
||||
}
|
||||
}
|
||||
|
||||
if (lastWatchdogMessageReceived + WATCHDOG_MESSAGE_TIMEOUT < millis()) {
|
||||
Serial << "Resetting device because of too long missing watchdog message" << endl;
|
||||
resetDevice();
|
||||
}
|
||||
|
||||
//Serial << "*** a" << endl;
|
||||
if ((m_disconnectState == 0) && (! m_mqttClient.loop())) {
|
||||
m_disconnectState = 1;
|
||||
@ -318,6 +296,7 @@ void MqttClient::exec() {
|
||||
Serial << "Trying to re-connect" << endl;
|
||||
if (m_mqttClient.connect("MeterbusHub")) {
|
||||
Serial << "MQTT connected" << endl;
|
||||
m_mqttClient.subscribe(WATCHDOG_TOPIC);
|
||||
m_disconnectTime = millis();
|
||||
m_disconnectState = 0;
|
||||
} else {
|
||||
@ -335,6 +314,16 @@ void MqttClient::exec() {
|
||||
m_uptime++;
|
||||
//Serial << "Tick " << m_uptime << endl;
|
||||
|
||||
uint32_t now = millis();
|
||||
if (lastWatchdogMessageReceived + WATCHDOG_MESSAGE_DELAY < now) {
|
||||
Serial << "Alarm due to missing watchdog message, " << endl;
|
||||
}
|
||||
|
||||
if (lastWatchdogMessageReceived + WATCHDOG_MESSAGE_TIMEOUT < now) {
|
||||
Serial << "Resetting device because of too long missing watchdog message" << endl;
|
||||
resetDevice();
|
||||
}
|
||||
|
||||
byte wdogCnt = WDOG_RSTCNT;
|
||||
String msg = String("{ \"metadata\": { \"device\": \"MeterbusHub\" }, \"data\": { \"uptime\": ") + m_uptime + String(", \"watchdogCnt\": ") + wdogCnt + String("}}");
|
||||
if (m_disconnectState == 0) {
|
||||
|
@ -15,7 +15,7 @@
|
||||
#include "cmd.h"
|
||||
|
||||
|
||||
#define NUM_OF_DEVICES 10
|
||||
#define NUM_OF_DEVICES 20
|
||||
#define MAX_LEN_OF_NAME 16
|
||||
typedef struct {
|
||||
uint8_t token;
|
||||
@ -53,7 +53,6 @@ private:
|
||||
EthernetClient m_client;
|
||||
RequestSender *m_meterBusMaster;
|
||||
PubSubClient m_mqttClient;
|
||||
bool m_alarmState;
|
||||
uint8_t m_disconnectState;
|
||||
uint32_t m_disconnectTime;
|
||||
uint32_t m_uptime;
|
||||
|
@ -37,15 +37,15 @@ void configReset() {
|
||||
configWrite(CONFIG_MAGIC, 4, (char*)&setMagic);
|
||||
}
|
||||
|
||||
void configRead(int addr, uint8_t len, char *buffer) {
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
void configRead(int addr, uint16_t len, char *buffer) {
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
*(buffer + i) = EEPROM.read(addr + i);
|
||||
//Serial << "Read " << *(buffer + i) << " from " << addr + i << endl;
|
||||
}
|
||||
}
|
||||
|
||||
void configWrite(int addr, uint8_t len, char *buffer) {
|
||||
for (uint8_t i = 0; i < len; i++) {
|
||||
void configWrite(int addr, uint16_t len, char *buffer) {
|
||||
for (uint16_t i = 0; i < len; i++) {
|
||||
//Serial << "Write " << _HEX(*(buffer + i)) << " to " << addr + i << endl;
|
||||
EEPROM.write(addr + i, *(buffer + i));
|
||||
}
|
||||
|
4
config.h
4
config.h
@ -13,8 +13,8 @@ void configInit();
|
||||
bool configIsValid();
|
||||
void configReset();
|
||||
|
||||
void configRead(int addr, uint8_t len, char *buffer);
|
||||
void configWrite(int addr, uint8_t len, char *buffer);
|
||||
void configRead(int addr, uint16_t len, char *buffer);
|
||||
void configWrite(int addr, uint16_t len, char *buffer);
|
||||
|
||||
|
||||
|
||||
|
10
reset.cpp
10
reset.cpp
@ -19,14 +19,6 @@ void resetDevice() {
|
||||
}
|
||||
}
|
||||
|
||||
void alarmOn() {
|
||||
|
||||
}
|
||||
|
||||
void alarmOff() {
|
||||
|
||||
}
|
||||
|
||||
void watchdogReset() {
|
||||
static uint32_t last = 0;
|
||||
static uint16_t maxTmroutl = 0;
|
||||
@ -58,7 +50,7 @@ extern "C" {
|
||||
WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
|
||||
|
||||
// one minute
|
||||
WDOG_TOVALL = 0xea60;
|
||||
WDOG_TOVALL = 20000;
|
||||
WDOG_TOVALH = 0;
|
||||
WDOG_PRESC = 0;
|
||||
|
||||
|
Loading…
x
Reference in New Issue
Block a user