mirror of
https://github.com/anklimov/lighthub
synced 2025-12-06 11:49:51 +03:00
lanStatus enum
This commit is contained in:
@@ -83,6 +83,8 @@ EthernetClient ethClient;
|
|||||||
WiFiClient ethClient;
|
WiFiClient ethClient;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
lan_status lanStatus = INITIAL_STATE;
|
||||||
|
|
||||||
const char outprefix[] PROGMEM = OUTTOPIC;
|
const char outprefix[] PROGMEM = OUTTOPIC;
|
||||||
const char inprefix[] PROGMEM = INTOPIC;
|
const char inprefix[] PROGMEM = INTOPIC;
|
||||||
const char configserver[] PROGMEM = CONFIG_SERVER;
|
const char configserver[] PROGMEM = CONFIG_SERVER;
|
||||||
@@ -106,7 +108,6 @@ aJsonObject *pollingItem = NULL;
|
|||||||
|
|
||||||
bool owReady = false;
|
bool owReady = false;
|
||||||
bool configOk = false;
|
bool configOk = false;
|
||||||
int lanStatus = 0;
|
|
||||||
|
|
||||||
#ifdef _modbus
|
#ifdef _modbus
|
||||||
ModbusMaster node;
|
ModbusMaster node;
|
||||||
@@ -114,20 +115,12 @@ ModbusMaster node;
|
|||||||
|
|
||||||
byte mac[6];
|
byte mac[6];
|
||||||
|
|
||||||
|
|
||||||
PubSubClient mqttClient(ethClient);
|
PubSubClient mqttClient(ethClient);
|
||||||
|
|
||||||
|
|
||||||
bool wifiInitialized;
|
bool wifiInitialized;
|
||||||
|
|
||||||
int mqtt_error_rate;
|
int mqtt_error_rate;
|
||||||
|
|
||||||
bool IsThermostat(const aJsonObject *item);
|
|
||||||
|
|
||||||
bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLatestCommand);
|
|
||||||
|
|
||||||
void resetFunc();
|
|
||||||
|
|
||||||
void watchdogSetup(void) {
|
void watchdogSetup(void) {
|
||||||
//Serial.begin(115200);
|
//Serial.begin(115200);
|
||||||
//Serial.println("Watchdog armed.");
|
//Serial.println("Watchdog armed.");
|
||||||
@@ -216,16 +209,16 @@ void restoreState() {
|
|||||||
//mqttClient.publish("/myhome/out/RestoreState", "ON");
|
//mqttClient.publish("/myhome/out/RestoreState", "ON");
|
||||||
};
|
};
|
||||||
|
|
||||||
int lanLoop() {
|
|
||||||
|
|
||||||
|
lan_status lanLoop() {
|
||||||
|
|
||||||
#ifdef NOETHER
|
#ifdef NOETHER
|
||||||
lanStatus=-14;
|
lanStatus=-14;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
//Serial.println(lanStatus);
|
|
||||||
switch (lanStatus) {
|
switch (lanStatus) {
|
||||||
//Initial state
|
case INITIAL_STATE:
|
||||||
case 0: //Ethernet.begin(mac,ip);
|
|
||||||
{
|
{
|
||||||
#ifdef __ESP__
|
#ifdef __ESP__
|
||||||
if(!wifiInitialized) {
|
if(!wifiInitialized) {
|
||||||
@@ -254,57 +247,55 @@ int lanLoop() {
|
|||||||
IPAddress mask;
|
IPAddress mask;
|
||||||
int res = 1;
|
int res = 1;
|
||||||
Serial.println(F("Starting lan"));
|
Serial.println(F("Starting lan"));
|
||||||
if (loadFlash(OFFSET_IP,ip))
|
if (loadFlash(OFFSET_IP, ip))
|
||||||
if (loadFlash(OFFSET_DNS,dns))
|
if (loadFlash(OFFSET_DNS, dns))
|
||||||
if (loadFlash(OFFSET_GW,gw))
|
if (loadFlash(OFFSET_GW, gw))
|
||||||
if (loadFlash(OFFSET_MASK,mask)) Ethernet.begin(mac,ip,dns,gw,mask);
|
if (loadFlash(OFFSET_MASK, mask)) Ethernet.begin(mac, ip, dns, gw, mask);
|
||||||
else Ethernet.begin(mac,ip,dns,gw);
|
else Ethernet.begin(mac, ip, dns, gw);
|
||||||
else Ethernet.begin(mac,ip,dns);
|
else Ethernet.begin(mac, ip, dns);
|
||||||
else Ethernet.begin(mac,ip);
|
else Ethernet.begin(mac, ip);
|
||||||
else
|
else {
|
||||||
{
|
wdt_dis();
|
||||||
wdt_dis();
|
res = Ethernet.begin(mac, 12000);
|
||||||
res = Ethernet.begin(mac, 12000);
|
wdt_en();
|
||||||
wdt_en();
|
wdt_res();
|
||||||
wdt_res();
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
if (res == 0) {
|
if (res == 0) {
|
||||||
Serial.println(F("Failed to configure Ethernet using DHCP. You can set ip manually!"));
|
Serial.println(F("Failed to configure Ethernet using DHCP. You can set ip manually!"));
|
||||||
Serial.print(F("'ip [ip[,dns[,gw[,subnet]]]]' - set static IP\n"));
|
Serial.print(F("'ip [ip[,dns[,gw[,subnet]]]]' - set static IP\n"));
|
||||||
lanStatus = -10;
|
lanStatus = AWAITING_ADDRESS;//-10;
|
||||||
lanCheck = millis() + 60000;
|
lanCheck = millis() + 60000;
|
||||||
#ifdef RESET_PIN
|
#ifdef RESET_PIN
|
||||||
resetFunc();
|
resetFunc();
|
||||||
#endif
|
#endif
|
||||||
} else {
|
} else {
|
||||||
printIPAddress();
|
printIPAddress();
|
||||||
lanStatus = 1;
|
lanStatus = HAVE_IP_ADDRESS;//1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
//Have IP address
|
case HAVE_IP_ADDRESS:
|
||||||
case 1:
|
|
||||||
if (!configOk)
|
if (!configOk)
|
||||||
lanStatus = getConfig(0, NULL); //got config from server or load from NVRAM
|
lanStatus = getConfig(0, NULL); //got config from server or load from NVRAM
|
||||||
else lanStatus = 2;
|
else lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;
|
||||||
#ifdef _artnet
|
#ifdef _artnet
|
||||||
if (artnet) artnet->begin();
|
if (artnet) artnet->begin();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
break;
|
break;
|
||||||
case 2: // IP Ready, config loaded, Connecting broker & subscribeArming Watchdog
|
case IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER:
|
||||||
wdt_res();
|
wdt_res();
|
||||||
{
|
{
|
||||||
short n = 0;
|
short n = 0;
|
||||||
int port = 1883;
|
int port = 1883;
|
||||||
char empty = 0;
|
char empty = 0;
|
||||||
char *user = ∅
|
char *user = ∅
|
||||||
char passwordBuf[16]="";
|
char passwordBuf[16] = "";
|
||||||
char *password = passwordBuf;
|
char *password = passwordBuf;
|
||||||
|
|
||||||
|
|
||||||
@@ -313,11 +304,10 @@ int lanLoop() {
|
|||||||
char *servername = aJson.getArrayItem(mqttArr, 1)->valuestring;
|
char *servername = aJson.getArrayItem(mqttArr, 1)->valuestring;
|
||||||
if (n >= 3) port = aJson.getArrayItem(mqttArr, 2)->valueint;
|
if (n >= 3) port = aJson.getArrayItem(mqttArr, 2)->valueint;
|
||||||
if (n >= 4) user = aJson.getArrayItem(mqttArr, 3)->valuestring;
|
if (n >= 4) user = aJson.getArrayItem(mqttArr, 3)->valuestring;
|
||||||
if (!loadFlash(OFFSET_MQTT_PWD, passwordBuf,sizeof(passwordBuf)) && (n >= 5))
|
if (!loadFlash(OFFSET_MQTT_PWD, passwordBuf, sizeof(passwordBuf)) && (n >= 5)) {
|
||||||
{
|
password = aJson.getArrayItem(mqttArr, 4)->valuestring;
|
||||||
password = aJson.getArrayItem(mqttArr, 4)->valuestring;
|
Serial.println(F("Using MQTT password from config"));
|
||||||
Serial.println(F("Using MQTT password from config"));
|
}
|
||||||
}
|
|
||||||
|
|
||||||
mqttClient.setServer(servername, port);
|
mqttClient.setServer(servername, port);
|
||||||
mqttClient.setCallback(mqttCallback);
|
mqttClient.setCallback(mqttCallback);
|
||||||
@@ -332,11 +322,11 @@ int lanLoop() {
|
|||||||
|
|
||||||
wdt_dis(); //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order
|
wdt_dis(); //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order
|
||||||
if (mqttClient.connect(client_id, user, password)) {
|
if (mqttClient.connect(client_id, user, password)) {
|
||||||
mqtt_error_rate=0;
|
mqtt_error_rate = 0;
|
||||||
Serial.print(F("connected as "));
|
Serial.print(F("connected as "));
|
||||||
Serial.println(client_id);
|
Serial.println(client_id);
|
||||||
wdt_en();
|
wdt_en();
|
||||||
configOk=true;
|
configOk = true;
|
||||||
// ... Temporary subscribe to status topic
|
// ... Temporary subscribe to status topic
|
||||||
char buf[MQTT_TOPIC_LENGTH];
|
char buf[MQTT_TOPIC_LENGTH];
|
||||||
|
|
||||||
@@ -351,7 +341,7 @@ int lanLoop() {
|
|||||||
|
|
||||||
//restoreState();
|
//restoreState();
|
||||||
// if (_once) {DMXput(); _once=0;}
|
// if (_once) {DMXput(); _once=0;}
|
||||||
lanStatus = 4;
|
lanStatus = RETAINING_COLLECTING;//4;
|
||||||
lanCheck = millis() + 5000;
|
lanCheck = millis() + 5000;
|
||||||
Serial.println(F("Awaiting for retained topics"));
|
Serial.println(F("Awaiting for retained topics"));
|
||||||
} else {
|
} else {
|
||||||
@@ -366,13 +356,13 @@ int lanLoop() {
|
|||||||
resetFunc();
|
resetFunc();
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
lanStatus = 12;
|
lanStatus = RECONNECT;//12;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 4: //retaining ... Collecting
|
case RETAINING_COLLECTING:
|
||||||
if (millis() > lanCheck) {
|
if (millis() > lanCheck) {
|
||||||
char buf[MQTT_TOPIC_LENGTH];
|
char buf[MQTT_TOPIC_LENGTH];
|
||||||
|
|
||||||
@@ -381,37 +371,34 @@ int lanLoop() {
|
|||||||
strncat(buf, "#", sizeof(buf));
|
strncat(buf, "#", sizeof(buf));
|
||||||
mqttClient.unsubscribe(buf);
|
mqttClient.unsubscribe(buf);
|
||||||
|
|
||||||
lanStatus = 3;
|
lanStatus = OPERATION;//3;
|
||||||
Serial.println(F("Accepting commands..."));
|
Serial.println(F("Accepting commands..."));
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
case 3: //operation
|
case OPERATION:
|
||||||
if (!mqttClient.connected()) lanStatus = 2;
|
if (!mqttClient.connected()) lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;//2;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
//Awaiting address
|
case AWAITING_ADDRESS:
|
||||||
case -10:
|
|
||||||
if (millis() > lanCheck)
|
if (millis() > lanCheck)
|
||||||
lanStatus = 0;
|
lanStatus = INITIAL_STATE;//0;
|
||||||
break;
|
break;
|
||||||
//Reconnect
|
|
||||||
case 12:
|
case RECONNECT:
|
||||||
if (millis() > lanCheck)
|
if (millis() > lanCheck)
|
||||||
|
|
||||||
lanStatus = 2;
|
lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;//2;
|
||||||
break;
|
break;
|
||||||
// read or Re-read config
|
case READ_RE_CONFIG:
|
||||||
case -11:
|
if (loadConfigFromEEPROM(0, NULL)) lanStatus = IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;//2;
|
||||||
if (loadConfigFromEEPROM(0, NULL)) lanStatus = 2;
|
|
||||||
else {
|
else {
|
||||||
lanCheck = millis() + 5000;
|
lanCheck = millis() + 5000;
|
||||||
lanStatus = -10;
|
lanStatus = AWAITING_ADDRESS;//-10;
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case -14:;
|
case DO_NOTHING:;
|
||||||
// do notghing with net
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@@ -424,14 +411,14 @@ int lanLoop() {
|
|||||||
Serial.println(F("No link"));
|
Serial.println(F("No link"));
|
||||||
if (mqttClient.connected()) mqttClient.disconnect();
|
if (mqttClient.connected()) mqttClient.disconnect();
|
||||||
lanCheck = millis() + 30000;
|
lanCheck = millis() + 30000;
|
||||||
lanStatus = -10;
|
lanStatus = AWAITING_ADDRESS;//-10;
|
||||||
break;
|
break;
|
||||||
case DHCP_CHECK_RENEW_FAIL:
|
case DHCP_CHECK_RENEW_FAIL:
|
||||||
//renewed fail
|
//renewed fail
|
||||||
Serial.println(F("Error: renewed fail"));
|
Serial.println(F("Error: renewed fail"));
|
||||||
if (mqttClient.connected()) mqttClient.disconnect();
|
if (mqttClient.connected()) mqttClient.disconnect();
|
||||||
lanCheck = millis() + 1000;
|
lanCheck = millis() + 1000;
|
||||||
lanStatus = -10;
|
lanStatus = AWAITING_ADDRESS;//-10;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DHCP_CHECK_RENEW_OK:
|
case DHCP_CHECK_RENEW_OK:
|
||||||
@@ -443,7 +430,7 @@ int lanLoop() {
|
|||||||
Serial.println(F("Error: rebind fail"));
|
Serial.println(F("Error: rebind fail"));
|
||||||
if (mqttClient.connected()) mqttClient.disconnect();
|
if (mqttClient.connected()) mqttClient.disconnect();
|
||||||
lanCheck = millis() + 1000;
|
lanCheck = millis() + 1000;
|
||||||
lanStatus = -10;
|
lanStatus = AWAITING_ADDRESS;//-10;
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case DHCP_CHECK_REBIND_OK:
|
case DHCP_CHECK_REBIND_OK:
|
||||||
@@ -469,8 +456,8 @@ void (*softResetFunc)(void) = 0;
|
|||||||
|
|
||||||
void resetFunc() {
|
void resetFunc() {
|
||||||
#ifdef RESET_PIN
|
#ifdef RESET_PIN
|
||||||
Serial.println(F("Reset arduino with digital pin "));
|
Serial.print(F("Reset arduino with digital pin "));
|
||||||
Serial.print(QUOTE(RESET_PIN));
|
Serial.println(QUOTE(RESET_PIN));
|
||||||
delay(1000);
|
delay(1000);
|
||||||
pinMode(RESET_PIN, OUTPUT);
|
pinMode(RESET_PIN, OUTPUT);
|
||||||
digitalWrite(RESET_PIN,LOW);
|
digitalWrite(RESET_PIN,LOW);
|
||||||
@@ -816,7 +803,7 @@ int loadFlash(short n, IPAddress& ip) {
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getConfig(int arg_cnt, char **args)
|
lan_status getConfig(int arg_cnt, char **args)
|
||||||
{
|
{
|
||||||
int responseStatusCode = 0;
|
int responseStatusCode = 0;
|
||||||
char ch;
|
char ch;
|
||||||
@@ -876,14 +863,14 @@ int getConfig(int arg_cnt, char **args)
|
|||||||
Serial.print(F("ERROR: Server returned "));
|
Serial.print(F("ERROR: Server returned "));
|
||||||
Serial.println(responseStatusCode);
|
Serial.println(responseStatusCode);
|
||||||
lanCheck = millis() + 5000;
|
lanCheck = millis() + 5000;
|
||||||
return -11;
|
return READ_RE_CONFIG;//-11;
|
||||||
}
|
}
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
Serial.println(F("failed to connect"));
|
Serial.println(F("failed to connect"));
|
||||||
Serial.println(F(" try again in 5 seconds"));
|
Serial.println(F(" try again in 5 seconds"));
|
||||||
lanCheck = millis() + 5000;
|
lanCheck = millis() + 5000;
|
||||||
return -11;
|
return READ_RE_CONFIG;//-11;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if defined(__SAM3X8E__)
|
#if defined(__SAM3X8E__)
|
||||||
@@ -915,7 +902,7 @@ int getConfig(int arg_cnt, char **args)
|
|||||||
if (!root) {
|
if (!root) {
|
||||||
Serial.println(F("Config parsing failed"));
|
Serial.println(F("Config parsing failed"));
|
||||||
// lanCheck=millis()+15000;
|
// lanCheck=millis()+15000;
|
||||||
return -11; //Load from NVRAM
|
return READ_RE_CONFIG;//-11; //Load from NVRAM
|
||||||
} else {
|
} else {
|
||||||
/*
|
/*
|
||||||
char * outstr=aJson.print(root);
|
char * outstr=aJson.print(root);
|
||||||
@@ -930,7 +917,7 @@ int getConfig(int arg_cnt, char **args)
|
|||||||
} else {
|
} else {
|
||||||
Serial.println(F("Config retrieving failed"));
|
Serial.println(F("Config retrieving failed"));
|
||||||
//lanCheck=millis()+15000;
|
//lanCheck=millis()+15000;
|
||||||
return -11; //Load from NVRAM
|
return READ_RE_CONFIG;//-11; //Load from NVRAM
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
#if defined(__ESP__)
|
#if defined(__ESP__)
|
||||||
@@ -965,7 +952,7 @@ int getConfig(int arg_cnt, char **args)
|
|||||||
httpClient.end();
|
httpClient.end();
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
return 2;
|
return IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER;//2;
|
||||||
}
|
}
|
||||||
|
|
||||||
void preTransmission() {
|
void preTransmission() {
|
||||||
@@ -1095,8 +1082,8 @@ void printFirmwareVersionAndBuildOptions() {
|
|||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifdef RESET_PIN
|
#ifdef RESET_PIN
|
||||||
Serial.println(F("(+)HARDRESET on pin="));
|
Serial.print(F("(+)HARDRESET on pin="));
|
||||||
Serial.print(F(QUOTE(RESET_PIN)));
|
Serial.println(F(QUOTE(RESET_PIN)));
|
||||||
#else
|
#else
|
||||||
Serial.println("(-)HARDRESET, using soft");
|
Serial.println("(-)HARDRESET, using soft");
|
||||||
#endif
|
#endif
|
||||||
|
|||||||
@@ -95,6 +95,18 @@
|
|||||||
extern Artnet *artnet;
|
extern Artnet *artnet;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
|
enum lan_status {
|
||||||
|
INITIAL_STATE = 0,
|
||||||
|
HAVE_IP_ADDRESS = 1,
|
||||||
|
IP_READY_CONFIG_LOADED_CONNECTING_TO_BROKER = 2,
|
||||||
|
OPERATION = 3,
|
||||||
|
RETAINING_COLLECTING = 4,
|
||||||
|
AWAITING_ADDRESS = -10,
|
||||||
|
RECONNECT = 12,
|
||||||
|
READ_RE_CONFIG = -11,
|
||||||
|
DO_NOTHING = -14
|
||||||
|
};
|
||||||
|
|
||||||
//void watchdogSetup(void);
|
//void watchdogSetup(void);
|
||||||
|
|
||||||
void mqttCallback(char *topic, byte *payload, unsigned int length);
|
void mqttCallback(char *topic, byte *payload, unsigned int length);
|
||||||
@@ -109,7 +121,7 @@ void printMACAddress();
|
|||||||
|
|
||||||
void restoreState();
|
void restoreState();
|
||||||
|
|
||||||
int lanLoop();
|
lan_status lanLoop();
|
||||||
|
|
||||||
void Changed(int i, DeviceAddress addr, int val);
|
void Changed(int i, DeviceAddress addr, int val);
|
||||||
|
|
||||||
@@ -147,7 +159,7 @@ void saveFlash(short n, IPAddress& ip);
|
|||||||
|
|
||||||
int loadFlash(short n, IPAddress& ip);
|
int loadFlash(short n, IPAddress& ip);
|
||||||
|
|
||||||
int getConfig(int arg_cnt=0, char **args=NULL);
|
lan_status getConfig(int arg_cnt=0, char **args=NULL);
|
||||||
|
|
||||||
void preTransmission();
|
void preTransmission();
|
||||||
|
|
||||||
@@ -177,8 +189,12 @@ void setupCmdArduino();
|
|||||||
|
|
||||||
void setupMacAddress();
|
void setupMacAddress();
|
||||||
|
|
||||||
int getConfig(int arg_cnt, char **args);
|
|
||||||
|
|
||||||
void printFirmwareVersionAndBuildOptions();
|
void printFirmwareVersionAndBuildOptions();
|
||||||
|
|
||||||
|
bool IsThermostat(const aJsonObject *item);
|
||||||
|
|
||||||
|
bool disabledDisconnected(const aJsonObject *thermoExtensionArray, int thermoLatestCommand);
|
||||||
|
|
||||||
|
void resetFunc();
|
||||||
|
|
||||||
#endif //LIGHTHUB_MAIN_H
|
#endif //LIGHTHUB_MAIN_H
|
||||||
|
|||||||
@@ -10,13 +10,14 @@
|
|||||||
[platformio]
|
[platformio]
|
||||||
src_dir = lighthub
|
src_dir = lighthub
|
||||||
env_default =
|
env_default =
|
||||||
megaatmega2560
|
; megaatmega2560
|
||||||
; due
|
due
|
||||||
; esp8266
|
; esp8266
|
||||||
; megaatmega2560-5500
|
; megaatmega2560-5500
|
||||||
; due-5500
|
; due-5500
|
||||||
; controllino
|
; controllino
|
||||||
|
build_dir = /tmp/pioenvs
|
||||||
|
libdeps_dir = /tmp/piolibdeps
|
||||||
[env:due]
|
[env:due]
|
||||||
platform = atmelsam
|
platform = atmelsam
|
||||||
framework = arduino
|
framework = arduino
|
||||||
|
|||||||
Reference in New Issue
Block a user