NetMeterbusMaster/NetMeterBusMaster2.cpp

122 lines
2.7 KiB
C++
Raw Normal View History

2014-05-14 19:37:04 +02:00
// Do not remove the include below
// #include <HardwareSerial.h>
#include "NetMeterBusMaster2.h"
#include "cmd.h"
#include "test.h"
#include "uptime.h"
2015-05-08 23:40:36 +02:00
// #include "meterBusServer.h"
2014-05-14 19:37:04 +02:00
#include "meterBusMaster.h"
#include "overCurrentProt.h"
#include <Ethernet.h>
2015-05-09 00:08:54 +02:00
#include "MqttClient.h"
2015-05-09 23:15:45 +02:00
#include <Streaming.h>
2015-05-11 22:36:58 +02:00
#include <Metro.h>
#include "config.h"
2014-05-14 19:37:04 +02:00
const uint8_t POWER_LED = 4;
const uint8_t ETHERNET_RESET = 14;
static uint8_t mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xEF };
static CmdServer cmdServer(2000);
static TestCmd testCmd;
static Uptime uptime;
static MeterBusMaster meterBusMaster;
2015-05-09 00:08:54 +02:00
static MqttClient mqttClient(&meterBusMaster);
2015-05-08 23:40:36 +02:00
// static MeterBusServer meterBusServer(2001, &meterBusMaster);
2014-05-14 19:37:04 +02:00
static OverCurrentProt overCurrentProt;
2015-05-11 22:36:58 +02:00
Metro wdogTick = Metro(1000);
2015-05-13 17:37:05 +02:00
Metro dhcpTick = Metro(60 * 1000);
2015-05-11 22:36:58 +02:00
extern "C" {
void startup_early_hook( ) __attribute__ ((weak));
void startup_early_hook() {
// enable watchdog
WDOG_UNLOCK = WDOG_UNLOCK_SEQ1;
WDOG_UNLOCK = WDOG_UNLOCK_SEQ2;
// one minute
WDOG_TOVALL = 0xea60;
WDOG_TOVALH = 0;
WDOG_PRESC = 0;
WDOG_STCTRLH = WDOG_STCTRLH_WDOGEN | WDOG_STCTRLH_ALLOWUPDATE | WDOG_STCTRLH_STOPEN | WDOG_STCTRLH_WAITEN;
}
} // extern "C"
2014-05-14 19:37:04 +02:00
void setup() {
2015-05-11 22:36:58 +02:00
pinMode(POWER_LED, OUTPUT);
digitalWrite(POWER_LED, HIGH);
2014-05-14 19:37:04 +02:00
Serial.begin(115200);
delay(100);
Serial.println("Starting up ...");
digitalWrite(POWER_LED, LOW);
2015-05-11 22:36:58 +02:00
configInit();
2014-05-14 19:37:04 +02:00
pinMode(ETHERNET_RESET, OUTPUT);
digitalWrite(ETHERNET_RESET, LOW);
delay(100);
digitalWrite(ETHERNET_RESET, HIGH);
delay(100);
Ethernet.begin(mac);
Serial.println("Ethernet started");
cmdServer.begin();
testCmd.registerYourself(&cmdServer);
uptime.begin(&cmdServer);
overCurrentProt.begin(&cmdServer);
meterBusMaster.begin(&cmdServer);
2015-05-08 23:40:36 +02:00
// meterBusServer.begin();
2015-05-11 22:36:58 +02:00
mqttClient.begin(&cmdServer);
2014-05-14 19:37:04 +02:00
digitalWrite(POWER_LED, HIGH);
2015-05-11 22:36:58 +02:00
2014-05-14 19:37:04 +02:00
}
void loop() {
2015-05-13 17:37:05 +02:00
static uint16_t maxTmroutl = 0;
2015-05-11 22:36:58 +02:00
// watchdog refresh
cli();
WDOG_REFRESH = 0xa602;
WDOG_REFRESH = 0xb480;
sei();
if (wdogTick.check() == 1) {
uint16_t h = WDOG_TMROUTH;
uint16_t l = WDOG_TMROUTL;
2015-05-13 17:37:05 +02:00
maxTmroutl = (maxTmroutl > l) ? maxTmroutl : l;
2015-05-11 22:36:58 +02:00
uint16_t c = WDOG_RSTCNT;
2015-05-13 17:37:05 +02:00
Serial << "WDog, h: " << _HEX(h) << ", l: " << _HEX(l) << ", c: " << _HEX(c) << ", m: " << _HEX(maxTmroutl) << endl;
2015-05-11 22:36:58 +02:00
}
if (dhcpTick.check() == 1) {
byte r = Ethernet.maintain();
Serial << "Ethernet.maintain: " << r << endl;
}
2015-05-09 23:15:45 +02:00
//Serial << "*** 1" << endl;
2014-05-14 19:37:04 +02:00
cmdServer.exec();
2015-05-09 23:15:45 +02:00
//Serial << "*** 2" << endl;
2014-05-14 19:37:04 +02:00
uptime.exec();
2015-05-09 23:15:45 +02:00
//Serial << "*** 3" << endl;
2014-05-14 19:37:04 +02:00
overCurrentProt.exec();
2015-05-09 23:15:45 +02:00
//Serial << "*** 4" << endl;
2014-05-14 19:37:04 +02:00
meterBusMaster.exec();
2015-05-09 23:15:45 +02:00
//Serial << "*** 5" << endl;
2015-05-09 00:08:54 +02:00
mqttClient.exec();
2015-05-09 23:15:45 +02:00
//Serial << "*** 6" << endl;
2014-05-14 19:37:04 +02:00
}