introduce external watchdog

This commit is contained in:
hg
2015-06-10 15:43:33 +02:00
parent 9d8a8d9ebd
commit 659193bed2
4 changed files with 41 additions and 3 deletions

View File

@ -21,7 +21,9 @@
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;
String MqttConfig::exec(String params) {
@ -172,6 +174,17 @@ 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)) {
Serial << "Watchdog message received" << endl;
if (0 != strcmp((char*)payload, WATCHDOG_MSG)) {
Serial << "Wrong content in watchdog message" << endl;
} else {
lastWatchdogMessageReceived = millis();
}
}
}
@ -179,7 +192,7 @@ Metro secondTick = Metro(1000);
MqttClient::MqttClient(RequestSender *meterBusMaster) : m_mqttConfig(this),
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(m_client),
m_client(), m_meterBusMaster(meterBusMaster), m_mqttClient(m_client), m_alarmState(false),
m_disconnectState(3), m_disconnectTime(millis()), m_uptime(0), m_deviceIdx(0)
{
}
@ -262,6 +275,23 @@ void MqttClient::begin(CmdServer *cmdServer) {
}
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;

View File

@ -53,6 +53,7 @@ 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;

View File

@ -19,7 +19,13 @@ void resetDevice() {
}
}
void alarmOn() {
}
void alarmOff() {
}
void watchdogReset() {
static uint32_t last = 0;

View File

@ -11,7 +11,8 @@
void resetDevice();
void watchdogReset();
void alarmOn();
void alarmOff();
#endif /* RESET_H_ */