@@ -91,6 +91,20 @@ WiFiClient ethClient;
WiFiClient ethClient ;
# endif
# ifdef ARDUINO_ARCH_STM32F1
//#include <EthernetClient.h>
//#include "UIPEthernet.h"
//#include "UIPUdp.h"
# include <SPI.h>
# include <Ethernet_STM.h>
# include "HttpClient.h"
# include "Dns.h"
//#include "utility/logging.h"
# include <EEPROM.h>
EthernetClient ethClient ;
# endif
lan_status lanStatus = INITIAL_STATE ;
const char outprefix [ ] PROGMEM = OUTTOPIC ;
@@ -131,7 +145,7 @@ int mqttErrorRate;
void watchdogSetup ( void ) {
//Serial.begin(115200);
//Serial.println("Watchdog armed.");
//debug Serial.println("Watchdog armed.");
} //Do not remove - strong re-definition WDT Init for DUE
@@ -140,22 +154,22 @@ void watchdogSetup(void) {
void mqttCallback ( char * topic , byte * payload , unsigned int length ) {
Serial . print ( F ( " \n [ " ) ) ;
Serial . print ( topic ) ;
Serial . print ( F ( " ] " ) ) ;
debug Serial. print ( F ( " \n [ " ) ) ;
debug Serial. print ( topic ) ;
debug Serial. print ( F ( " ] " ) ) ;
if ( ! payload ) return ;
payload [ length ] = 0 ;
int fr = freeRam ( ) ;
if ( fr < 250 ) {
Serial . println ( F ( " OOM! " ) ) ;
debug Serial. println ( F ( " OOM! " ) ) ;
return ;
}
for ( int i = 0 ; i < length ; i + + ) {
Serial . print ( ( char ) payload [ i ] ) ;
debug Serial. print ( ( char ) payload [ i ] ) ;
}
Serial . println ( ) ;
debug Serial. println ( ) ;
if ( ! strcmp ( topic , CMDTOPIC ) ) {
cmd_parse ( ( char * ) payload ) ;
@@ -168,11 +182,12 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
{
char buf [ MQTT_TOPIC_LENGTH + 1 ] ;
strncpy_P ( buf , inprefix , sizeof ( buf ) ) ;
intopic = strncmp ( topic , buf , strlen ( inprefix ) ) ;
}
// in Retaining status - trying to restore previous state from retained output topic. Retained input topics are not relevant.
if ( retaining & & ! intopic ) {
Serial . println ( F ( " Skipping.. " ) ) ;
debug Serial. println ( F ( " Skipping.. " ) ) ;
return ;
}
char subtopic [ MQTT_SUBJECT_LENGTH ] = " " ;
@@ -191,19 +206,19 @@ void mqttCallback(char *topic, byte *payload, unsigned int length) {
void printIPAddress ( IPAddress ipAddress ) {
for ( byte thisByte = 0 ; thisByte < 4 ; thisByte + + ) {
Serial . print ( ipAddress [ thisByte ] , DEC ) ;
debug Serial. print ( ipAddress [ thisByte ] , DEC ) ;
if ( thisByte < 3 )
Serial . print ( F ( " . " ) ) ;
debug Serial. print ( F ( " . " ) ) ;
}
}
void printMACAddress ( ) {
Serial . print ( F ( " Configured MAC: " ) ) ;
debug Serial. print ( F ( " Configured MAC: " ) ) ;
for ( byte thisByte = 0 ; thisByte < 6 ; thisByte + + ) {
Serial . print ( mac [ thisByte ] , HEX ) ;
Serial . print ( F ( " : " ) ) ;
debug Serial. print ( mac [ thisByte ] , HEX ) ;
debug Serial. print ( F ( " : " ) ) ;
}
Serial . println ( ) ;
debug Serial. println ( ) ;
}
void restoreState ( ) {
@@ -246,7 +261,7 @@ lan_status lanLoop() {
mqttClient . unsubscribe ( buf ) ;
lanStatus = OPERATION ; //3;
Serial . println ( F ( " Accepting commands... " ) ) ;
debug Serial. println ( F ( " Accepting commands... " ) ) ;
break ;
}
@@ -283,32 +298,32 @@ lan_status lanLoop() {
if ( lanStatus > 0 )
switch ( Ethernet . maintain ( ) ) {
case NO_LINK :
Serial . println ( F ( " No link " ) ) ;
debug Serial. println ( F ( " No link " ) ) ;
if ( mqttClient . connected ( ) ) mqttClient . disconnect ( ) ;
nextLanCheckTime = millis ( ) + 30000 ;
lanStatus = AWAITING_ADDRESS ; //-10;
break ;
case DHCP_CHECK_RENEW_FAIL :
Serial . println ( F ( " Error: renewed fail " ) ) ;
debug Serial. println ( F ( " Error: renewed fail " ) ) ;
if ( mqttClient . connected ( ) ) mqttClient . disconnect ( ) ;
nextLanCheckTime = millis ( ) + 1000 ;
lanStatus = AWAITING_ADDRESS ; //-10;
break ;
case DHCP_CHECK_RENEW_OK :
Serial . println ( F ( " Renewed success. IP address: " ) ) ;
debug Serial. println ( F ( " Renewed success. IP address: " ) ) ;
printIPAddress ( Ethernet . localIP ( ) ) ;
break ;
case DHCP_CHECK_REBIND_FAIL :
Serial . println ( F ( " Error: rebind fail " ) ) ;
debug Serial. println ( F ( " Error: rebind fail " ) ) ;
if ( mqttClient . connected ( ) ) mqttClient . disconnect ( ) ;
nextLanCheckTime = millis ( ) + 1000 ;
lanStatus = AWAITING_ADDRESS ; //-10;
break ;
case DHCP_CHECK_REBIND_OK :
Serial . println ( F ( " Rebind success. IP address: " ) ) ;
debug Serial. println ( F ( " Rebind success. IP address: " ) ) ;
printIPAddress ( Ethernet . localIP ( ) ) ;
break ;
@@ -341,25 +356,25 @@ void ip_ready_config_loaded_connecting_to_broker() {
if ( n > = 4 ) user = aJson . getArrayItem ( mqttArr , 3 ) - > valuestring ;
if ( ! loadFlash ( OFFSET_MQTT_PWD , passwordBuf , sizeof ( passwordBuf ) ) & & ( n > = 5 ) ) {
password = aJson . getArrayItem ( mqttArr , 4 ) - > valuestring ;
Serial . println ( F ( " Using MQTT password from config " ) ) ;
debug Serial. println ( F ( " Using MQTT password from config " ) ) ;
}
mqttClient . setServer ( servername , port ) ;
mqttClient . setCallback ( mqttCallback ) ;
Serial . print ( F ( " Attempting MQTT connection to " ) ) ;
Serial . print ( servername ) ;
Serial . print ( F ( " : " ) ) ;
Serial . print ( port ) ;
Serial . print ( F ( " user: " ) ) ;
Serial . print ( user ) ;
Serial . print ( F ( " ... " ) ) ;
debug Serial. print ( F ( " Attempting MQTT connection to " ) ) ;
debug Serial. print ( servername ) ;
debug Serial. print ( F ( " : " ) ) ;
debug Serial. print ( port ) ;
debug Serial. print ( F ( " user: " ) ) ;
debug Serial. print ( user ) ;
debug Serial. print ( F ( " ... " ) ) ;
wdt_dis ( ) ; //potential unsafe for ethernetIdle(), but needed to avoid cyclic reboot if mosquitto out of order
if ( mqttClient . connect ( client_id , user , password ) ) {
mqttErrorRate = 0 ;
Serial . print ( F ( " connected as " ) ) ;
Serial . println ( client_id ) ;
debug Serial. print ( F ( " connected as " ) ) ;
debug Serial. println ( client_id ) ;
wdt_en ( ) ;
configOk = true ;
// ... Temporary subscribe to status topic
@@ -378,16 +393,16 @@ void ip_ready_config_loaded_connecting_to_broker() {
// if (_once) {DMXput(); _once=0;}
lanStatus = RETAINING_COLLECTING ; //4;
nextLanCheckTime = millis ( ) + 5000 ;
Serial . println ( F ( " Awaiting for retained topics " ) ) ;
debug Serial. println ( F ( " Awaiting for retained topics " ) ) ;
} else {
Serial . print ( F ( " failed, rc= " ) ) ;
Serial . print ( mqttClient . state ( ) ) ;
Serial . println ( F ( " try again in 5 seconds " ) ) ;
debug Serial. print ( F ( " failed, rc= " ) ) ;
debug Serial. print ( mqttClient . state ( ) ) ;
debug Serial. println ( F ( " try again in 5 seconds " ) ) ;
nextLanCheckTime = millis ( ) + 5000 ;
# ifdef RESTART_LAN_ON_MQTT_ERRORS
mqttErrorRate + + ;
if ( mqttErrorRate > 50 ) {
Serial . print ( F ( " Too many MQTT connection errors. Restart LAN " ) ) ;
debug Serial. print ( F ( " Too many MQTT connection errors. Restart LAN " ) ) ;
mqttErrorRate = 0 ;
# ifdef RESET_PIN
resetHard ( ) ;
@@ -406,10 +421,10 @@ void onInitialStateInitLAN() {
# if defined(ESP8266) and defined(WIFI_MANAGER_DISABLE)
if ( ! wifiInitialized ) {
WiFi . mode ( WIFI_STA ) ;
Serial . print ( F ( " WIFI AP/Password: " ) ) ;
Serial . print ( QUOTE ( ESP_WIFI_AP ) ) ;
Serial . print ( F ( " / " ) ) ;
Serial . println ( QUOTE ( ESP_WIFI_PWD ) ) ;
debug Serial. print ( F ( " WIFI AP/Password: " ) ) ;
debug Serial. print ( QUOTE ( ESP_WIFI_AP ) ) ;
debug Serial. print ( F ( " / " ) ) ;
debug Serial. println ( QUOTE ( ESP_WIFI_PWD ) ) ;
wifi_set_macaddr ( STATION_IF , mac ) ;
WiFi . begin ( QUOTE ( ESP_WIFI_AP ) , QUOTE ( ESP_WIFI_PWD ) ) ;
wifiInitialized = true ;
@@ -420,17 +435,17 @@ void onInitialStateInitLAN() {
if ( ! wifiInitialized ) {
WiFi . mode ( WIFI_STA ) ;
WiFi . disconnect ( ) ;
Serial . print ( F ( " WIFI AP/Password: " ) ) ;
Serial . print ( QUOTE ( ESP_WIFI_AP ) ) ;
Serial . print ( F ( " / " ) ) ;
Serial . println ( QUOTE ( ESP_WIFI_PWD ) ) ;
debug Serial. print ( F ( " WIFI AP/Password: " ) ) ;
debug Serial. print ( QUOTE ( ESP_WIFI_AP ) ) ;
debug Serial. print ( F ( " / " ) ) ;
debug Serial. println ( QUOTE ( ESP_WIFI_PWD ) ) ;
WiFi . begin ( QUOTE ( ESP_WIFI_AP ) , QUOTE ( ESP_WIFI_PWD ) ) ;
int wifi_connection_wait = 10000 ;
while ( WiFi . status ( ) ! = WL_CONNECTED & & wifi_connection_wait > 0 ) {
delay ( 500 ) ;
wifi_connection_wait - = 500 ;
Serial . print ( " . " ) ;
debug Serial. print ( " . " ) ;
}
wifiInitialized = true ;
}
@@ -438,31 +453,31 @@ void onInitialStateInitLAN() {
# if defined(ARDUINO_ARCH_ESP32) || defined(ESP8266)
if ( WiFi . status ( ) = = WL_CONNECTED ) {
Serial . print ( F ( " WiFi connected. IP address: " ) ) ;
Serial . println ( WiFi . localIP ( ) ) ;
debug Serial. print ( F ( " WiFi connected. IP address: " ) ) ;
debug Serial. println ( WiFi . localIP ( ) ) ;
lanStatus = HAVE_IP_ADDRESS ; //1;
} else
{
Serial . println ( F ( " Problem with WiFi connected " ) ) ;
debug Serial. println ( F ( " Problem with WiFi connected " ) ) ;
nextLanCheckTime = millis ( ) + DHCP_RETRY_INTERVAL / 5 ;
}
# endif
# if defined(__AVR__) || defined(__SAM3X8E__)
# if defined(__AVR__) || defined(__SAM3X8E__)||defined(ARDUINO_ARCH_STM32F1)
IPAddress ip , dns , gw , mask ;
int res = 1 ;
Serial . println ( F ( " Starting lan " ) ) ;
debug Serial. println ( F ( " Starting lan " ) ) ;
if ( ipLoadFromFlash ( OFFSET_IP , ip ) ) {
Serial . print ( " Loaded from flash IP: " ) ;
debug Serial. print ( " Loaded from flash IP: " ) ;
printIPAddress ( ip ) ;
if ( ipLoadFromFlash ( OFFSET_DNS , dns ) ) {
Serial . print ( " DNS: " ) ;
debug Serial. print ( " DNS: " ) ;
printIPAddress ( dns ) ;
if ( ipLoadFromFlash ( OFFSET_GW , gw ) ) {
Serial . print ( " GW: " ) ;
debug Serial. print ( " GW: " ) ;
printIPAddress ( gw ) ;
if ( ipLoadFromFlash ( OFFSET_MASK , mask ) ) {
Serial . print ( " MASK: " ) ;
debug Serial. print ( " MASK: " ) ;
printIPAddress ( mask ) ;
Ethernet . begin ( mac , ip , dns , gw , mask ) ;
} else Ethernet . begin ( mac , ip , dns , gw ) ;
@@ -470,44 +485,62 @@ void onInitialStateInitLAN() {
} else Ethernet . begin ( mac , ip ) ;
}
else {
Serial . println ( " No IP data found in flash " ) ;
debug Serial. println ( " No IP data found in flash " ) ;
wdt_dis ( ) ;
# if defined(__AVR__) || defined(__SAM3X8E__)
res = Ethernet . begin ( mac , 12000 ) ;
# endif
# if defined(ARDUINO_ARCH_STM32F1)
res = Ethernet . begin ( mac ) ;
# endif
wdt_en ( ) ;
wdt_res ( ) ;
}
if ( res = = 0 ) {
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 " ) ) ;
debug Serial. println ( F ( " Failed to configure Ethernet using DHCP. You can set ip manually! " ) ) ;
debug Serial. print ( F ( " 'ip [ip[,dns[,gw[,subnet]]]]' - set static IP \n " ) ) ;
lanStatus = AWAITING_ADDRESS ; //-10;
nextLanCheckTime = millis ( ) + DHCP_RETRY_INTERVAL ;
# ifdef RESET_PIN
resetHard ( ) ;
# endif
} else {
Serial . print ( F ( " Got IP address: " ) ) ;
debug Serial. print ( F ( " Got IP address: " ) ) ;
printIPAddress ( Ethernet . localIP ( ) ) ;
lanStatus = HAVE_IP_ADDRESS ; //1;
}
# endif
}
# ifdef ARDUINO_ARCH_STM32F1
void softRebootFunc ( ) {
nvic_sys_reset ( ) ;
}
# endif
# if defined(__AVR__) || defined(__SAM3X8E__)
void ( * softRebootFunc ) ( void ) = 0 ;
# endif
# if defined(ESP8266) || defined(ARDUINO_ARCH_ESP32)
void softRebootFunc ( ) {
debugSerial . print ( F ( " ESP.restart(); " ) ) ;
ESP . restart ( ) ;
}
# endif
void resetHard ( ) {
# ifdef RESET_PIN
Serial . print ( F ( " Reset Arduino with digital pin " ) ) ;
Serial . println ( QUOTE ( RESET_PIN ) ) ;
debug Serial. print ( F ( " Reset Arduino with digital pin " ) ) ;
debug Serial. println ( QUOTE ( RESET_PIN ) ) ;
delay ( 500 ) ;
pinMode ( RESET_PIN , OUTPUT ) ;
digitalWrite ( RESET_PIN , LOW ) ;
delay ( 500 ) ;
digitalWrite ( RESET_PIN , HIGH ) ;
delay ( 500 ) ;
# endif
# endif
}
# ifdef _owire
@@ -527,12 +560,12 @@ void Changed(int i, DeviceAddress addr, int val) {
owEmit = aJson . getObjectItem ( owObj , " emit " ) - > valuestring ;
if ( owEmit ) {
strncpy ( addrbuf , owEmit , sizeof ( addrbuf ) ) ;
Serial . print ( owEmit ) ;
Serial . print ( F ( " = " ) ) ;
Serial . println ( val ) ;
debug Serial. print ( owEmit ) ;
debug Serial. print ( F ( " = " ) ) ;
debug Serial. println ( val ) ;
}
owItem = aJson . getObjectItem ( owObj , " item " ) - > valuestring ;
} else Serial . println ( F ( " 1w-item not found in config " ) ) ;
} else debug Serial. println ( F ( " 1w-item not found in config " ) ) ;
if ( ( val = = - 127 ) | | ( val = = 85 ) | | ( val = = 0 ) ) { //ToDo: 1-w short circuit mapped to "0" celsium
return ;
@@ -554,7 +587,8 @@ void cmdFunctionHelp(int arg_cnt, char **args)
//(char* tokens)
{
printFirmwareVersionAndBuildOptions ( ) ;
Serial . println ( F ( " Use the commands: 'help' - this text \n "
debug Serial. print ( F ( " free RAM: " ) ) ; debugSerial . print ( freeRam ( ) ) ;
debugSerial . println ( F ( " Use the commands: 'help' - this text \n "
" 'mac de:ad:be:ef:fe:00' set and store MAC-address in EEPROM \n "
" 'ip [ip[,dns[,gw[,subnet]]]]' - set static IP \n "
" 'save' - save config in NVRAM \n "
@@ -569,12 +603,12 @@ void cmdFunctionHelp(int arg_cnt, char **args)
void cmdFunctionKill ( int arg_cnt , char * * args ) {
for ( short i = 1 ; i < 20 ; i + + ) {
delay ( 1000 ) ;
Serial . println ( i ) ;
debug Serial. println ( i ) ;
} ;
}
void cmdFunctionReboot ( int arg_cnt , char * * args ) {
Serial . println ( F ( " Soft rebooting... " ) ) ;
debug Serial. println ( F ( " Soft rebooting... " ) ) ;
softRebootFunc ( ) ;
}
@@ -586,8 +620,8 @@ void applyConfig() {
dmxArr = aJson . getObjectItem ( root , " dmxin " ) ;
if ( dmxArr & & ( itemsCount = aJson . getArraySize ( dmxArr ) ) ) {
DMXinSetup ( itemsCount * 4 ) ;
Serial . print ( F ( " DMX in started. Channels: " ) ) ;
Serial . println ( itemsCount * 4 ) ;
debug Serial. print ( F ( " DMX in started. Channels: " ) ) ;
debug Serial. println ( itemsCount * 4 ) ;
}
# endif
# ifdef _dmxout
@@ -596,8 +630,8 @@ void applyConfig() {
if ( dmxoutArr & & aJson . getArraySize ( dmxoutArr ) > = 1 ) {
DMXoutSetup ( maxChannels = aJson . getArrayItem ( dmxoutArr , 1 ) - > valueint ) ;
//,aJson.getArrayItem(dmxoutArr, 0)->valueint);
Serial . print ( F ( " DMX out started. Channels: " ) ) ;
Serial . println ( maxChannels ) ;
debug Serial. print ( F ( " DMX out started. Channels: " ) ) ;
debug Serial. println ( maxChannels ) ;
}
# endif
# ifdef _modbus
@@ -609,12 +643,12 @@ void applyConfig() {
if ( owArr & & ! owReady ) {
aJsonObject * item = owArr - > child ;
owReady = owSetup ( & Changed ) ;
if ( owReady ) Serial . println ( F ( " One wire Ready " ) ) ;
if ( owReady ) debug Serial. println ( F ( " One wire Ready " ) ) ;
t_count = 0 ;
while ( item & & owReady ) {
if ( ( item - > type = = aJson_Object ) ) {
DeviceAddress addr ;
//Serial.print(F("Add:")),Serial.println(item->name);
//debug Serial.print(F("Add:")),debug Serial.println(item->name);
SetAddr ( item - > name , addr ) ;
owAdd ( addr ) ;
}
@@ -642,10 +676,10 @@ while (items && item)
int k ;
pinMode ( pin , OUTPUT ) ;
digitalWrite ( pin , k = ( ( cmd = = CMD_ON ) ? HIGH : LOW ) ) ;
Serial . print ( F ( " Pin: " ) ) ;
Serial . print ( pin ) ;
Serial . print ( F ( " = " ) ) ;
Serial . println ( k ) ;
debug Serial. print ( F ( " Pin: " ) ) ;
debug Serial. print ( pin ) ;
debug Serial. print ( F ( " = " ) ) ;
debug Serial. println ( k ) ;
}
break ;
} //switch
@@ -660,16 +694,16 @@ while (items && item)
}
void printConfigSummary ( ) {
Serial . println ( F ( " Configured: " ) ) ;
Serial . print ( F ( " items " ) ) ;
debug Serial. println ( F ( " Configured: " ) ) ;
debug Serial. print ( F ( " items " ) ) ;
printBool ( items ) ;
Serial . print ( F ( " inputs " ) ) ;
debug Serial. print ( F ( " inputs " ) ) ;
printBool ( inputs ) ;
Serial . print ( F ( " modbus " ) ) ;
debug Serial. print ( F ( " modbus " ) ) ;
printBool ( modbusArr ) ;
Serial . print ( F ( " mqtt " ) ) ;
debug Serial. print ( F ( " mqtt " ) ) ;
printBool ( mqttArr ) ;
Serial . print ( F ( " 1-wire " ) ) ;
debug Serial. print ( F ( " 1-wire " ) ) ;
printBool ( owArr ) ;
}
@@ -682,24 +716,24 @@ int loadConfigFromEEPROM(int arg_cnt, char **args)
//(char* tokens)
{
char ch ;
Serial . println ( F ( " loading Config " ) ) ;
debug Serial. println ( F ( " loading Config " ) ) ;
ch = EEPROM . read ( EEPROM_offset ) ;
if ( ch = = ' { ' ) {
aJsonEEPROMStream as = aJsonEEPROMStream ( EEPROM_offset ) ;
aJson . deleteItem ( root ) ;
root = aJson . parse ( & as ) ;
Serial . println ( ) ;
debug Serial. println ( ) ;
if ( ! root ) {
Serial . println ( F ( " load failed " ) ) ;
debug Serial. println ( F ( " load failed " ) ) ;
return 0 ;
}
Serial . println ( F ( " Loaded " ) ) ;
debug Serial. println ( F ( " Loaded " ) ) ;
applyConfig ( ) ;
ethClient . stop ( ) ; //Refresh MQTT connect to get retained info
return 1 ;
} else {
Serial . println ( F ( " No stored config " ) ) ;
debug Serial. println ( F ( " No stored config " ) ) ;
return 0 ;
}
@@ -715,33 +749,33 @@ int mqttConfigRequest(int arg_cnt, char **args)
//(char* tokens)
{
char buf [ 25 ] = " / " ;
Serial . println ( F ( " request MQTT Config " ) ) ;
debug Serial. println ( F ( " request MQTT Config " ) ) ;
SetBytes ( ( uint8_t * ) mac , 6 , buf + 1 ) ;
buf [ 13 ] = 0 ;
strncat ( buf , " /resp/# " , 25 ) ;
Serial . println ( buf ) ;
debug Serial. println ( buf ) ;
mqttClient . subscribe ( buf ) ;
buf [ 13 ] = 0 ;
strncat ( buf , " /req/conf " , 25 ) ;
Serial . println ( buf ) ;
debug Serial. println ( buf ) ;
mqttClient . publish ( buf , " 1 " ) ;
}
int mqttConfigResp ( char * as ) {
Serial . println ( F ( " got MQTT Config " ) ) ;
debug Serial. println ( F ( " got MQTT Config " ) ) ;
//aJsonEEPROMStream as=aJsonEEPROMStream(EEPROM_offset);
//aJson.deleteItem(root);
root = aJson . parse ( as ) ;
Serial . println ( ) ;
debug Serial. println ( ) ;
if ( ! root ) {
Serial . println ( F ( " load failed " ) ) ;
debug Serial. println ( F ( " load failed " ) ) ;
return 0 ;
}
Serial . println ( F ( " Loaded " ) ) ;
debug Serial. println ( F ( " Loaded " ) ) ;
applyConfig ( ) ;
return 1 ;
}
@@ -750,10 +784,10 @@ void cmdFunctionSave(int arg_cnt, char **args)
//(char* tokens)
{
aJsonEEPROMStream jsonEEPROMStream = aJsonEEPROMStream ( EEPROM_offset ) ;
Serial . println ( F ( " Saving config to EEPROM.. " ) ) ;
debug Serial. println ( F ( " Saving config to EEPROM.. " ) ) ;
aJson . print ( root , & jsonEEPROMStream ) ;
jsonEEPROMStream . putEOF ( ) ;
Serial . println ( F ( " Saved to EEPROM " ) ) ;
debug Serial. println ( F ( " Saved to EEPROM " ) ) ;
}
void cmdFunctionIp ( int arg_cnt , char * * args )
@@ -785,24 +819,24 @@ void cmdFunctionIp(int arg_cnt, char **args)
saveFlash ( OFFSET_MASK , current_mask ) ;
saveFlash ( OFFSET_GW , current_gw ) ;
saveFlash ( OFFSET_DNS , current_dns ) ;
Serial . print ( F ( " Saved current config(ip,dns,gw,subnet): " ) ) ;
debug Serial. print ( F ( " Saved current config(ip,dns,gw,subnet): " ) ) ;
printIPAddress ( current_ip ) ;
Serial . print ( F ( " , " ) ) ;
debug Serial. print ( F ( " , " ) ) ;
printIPAddress ( current_dns ) ;
Serial . print ( F ( " , " ) ) ;
debug Serial. print ( F ( " , " ) ) ;
printIPAddress ( current_gw ) ;
Serial . print ( F ( " , " ) ) ;
debug Serial. print ( F ( " , " ) ) ;
printIPAddress ( current_mask ) ;
Serial . println ( F ( " ; " ) ) ;
debug Serial. println ( F ( " ; " ) ) ;
}
Serial . println ( F ( " Saved " ) ) ;
debug Serial. println ( F ( " Saved " ) ) ;
}
void cmdFunctionClearEEPROM ( int arg_cnt , char * * args ) {
for ( int i = 0 ; i < 512 ; i + + )
EEPROM . write ( i , 0 ) ;
Serial . println ( F ( " EEPROM cleared " ) ) ;
debug Serial. println ( F ( " EEPROM cleared " ) ) ;
}
@@ -812,13 +846,13 @@ void cmdFunctionPwd(int arg_cnt, char **args)
if ( arg_cnt )
saveFlash ( OFFSET_MQTT_PWD , args [ 1 ] ) ;
else saveFlash ( OFFSET_MQTT_PWD , empty ) ;
Serial . println ( F ( " Password updated " ) ) ;
debug Serial. println ( F ( " Password updated " ) ) ;
}
void cmdFunctionSetMac ( int arg_cnt , char * * args ) {
//Serial.print("Got:");
//Serial.println(args[1]);
//debug Serial.print("Got:");
//debug Serial.println(args[1]);
if ( sscanf ( args [ 1 ] , " %x:%x:%x:%x:%x:%x%с " ,
& mac [ 0 ] ,
& mac [ 1 ] ,
@@ -826,13 +860,13 @@ void cmdFunctionSetMac(int arg_cnt, char **args) {
& mac [ 3 ] ,
& mac [ 4 ] ,
& mac [ 5 ] ) < 6 ) {
Serial . print ( F ( " could not parse: " ) ) ;
Serial . println ( args [ 1 ] ) ;
debug Serial. print ( F ( " could not parse: " ) ) ;
debug Serial. println ( args [ 1 ] ) ;
return ;
}
printMACAddress ( ) ;
for ( short i = 0 ; i < 6 ; i + + ) { EEPROM . write ( i , mac [ i ] ) ; }
Serial . println ( F ( " Updated " ) ) ;
debug Serial. println ( F ( " Updated " ) ) ;
}
void cmdFunctionGet ( int arg_cnt , char * * args ) {
@@ -841,7 +875,7 @@ void cmdFunctionGet(int arg_cnt, char **args) {
//restoreState();
}
void printBool ( bool arg ) { ( arg ) ? Serial . println ( F ( " on " ) ) : Serial . println ( F ( " off " ) ) ; }
void printBool ( bool arg ) { ( arg ) ? debug Serial. println ( F ( " on " ) ) : debug Serial. println ( F ( " off " ) ) ; }
void saveFlash ( short n , char * str ) {
@@ -885,9 +919,9 @@ lan_status getConfig(int arg_cnt, char **args)
snprintf ( URI , sizeof ( URI ) , " /%02x-%02x-%02x-%02x-%02x-%02x.config.json " , mac [ 0 ] , mac [ 1 ] , mac [ 2 ] , mac [ 3 ] , mac [ 4 ] ,
mac [ 5 ] ) ;
Serial . print ( F ( " Config URI: http:// " ) ) ;
Serial . print ( configServer ) ;
Serial . println ( URI ) ;
debug Serial. print ( F ( " Config URI: http:// " ) ) ;
debug Serial. print ( configServer ) ;
debug Serial. println ( URI ) ;
# if defined(__AVR__)
FILE * configStream ;
@@ -903,52 +937,52 @@ lan_status getConfig(int arg_cnt, char **args)
if ( configStream ! = NULL ) {
if ( responseStatusCode = = 200 ) {
Serial . println ( F ( " got Config " ) ) ;
debug Serial. println ( F ( " got Config " ) ) ;
char c ;
aJsonFileStream as = aJsonFileStream ( configStream ) ;
noInterrupts ( ) ;
aJson . deleteItem ( root ) ;
root = aJson . parse ( & as ) ;
interrupts ( ) ;
// Serial.println(F("Parsed."));
// debug Serial.println(F("Parsed."));
hclient . closeStream ( configStream ) ; // this is very important -- be sure to close the STREAM
if ( ! root ) {
Serial . println ( F ( " Config parsing failed " ) ) ;
debug Serial. println ( F ( " Config parsing failed " ) ) ;
nextLanCheckTime = millis ( ) + 15000 ;
return READ_RE_CONFIG ; //-11;
} else {
// char *outstr = aJson.print(root);
// Serial.println(outstr);
// debug Serial.println(outstr);
// free(outstr);
Serial . println ( F ( " Applying. " ) ) ;
debug Serial. println ( F ( " Applying. " ) ) ;
applyConfig ( ) ;
}
} else {
Serial . print ( F ( " ERROR: Server returned " ) ) ;
Serial . println ( responseStatusCode ) ;
debug Serial. print ( F ( " ERROR: Server returned " ) ) ;
debug Serial. println ( responseStatusCode ) ;
nextLanCheckTime = millis ( ) + 5000 ;
return READ_RE_CONFIG ; //-11;
}
} else {
Serial . println ( F ( " failed to connect " ) ) ;
Serial . println ( F ( " try again in 5 seconds " ) ) ;
debug Serial. println ( F ( " failed to connect " ) ) ;
debug Serial. println ( F ( " try again in 5 seconds " ) ) ;
nextLanCheckTime = millis ( ) + 5000 ;
return READ_RE_CONFIG ; //-11;
}
# endif
# if defined(__SAM3X8E__)
# if defined(__SAM3X8E__) || defined(ARDUINO_ARCH_STM32F1)
String response ;
EthernetClient configEthClient ;
HttpClient htclient = HttpClient ( configEthClient , configServer , 80 ) ;
//htclient.stop(); //_socket =MAX
htclient . setHttpResponseTimeout ( 4000 ) ;
wdt_res ( ) ;
//Serial.println("making GET request");get
//debug Serial.println("making GET request");get
htclient . beginRequest ( ) ;
htclient . get ( URI ) ;
htclient . endRequest ( ) ;
@@ -959,31 +993,31 @@ lan_status getConfig(int arg_cnt, char **args)
response = htclient . responseBody ( ) ;
htclient . stop ( ) ;
wdt_res ( ) ;
Serial . print ( F ( " HTTP Status code: " ) ) ;
Serial . println ( responseStatusCode ) ;
//Serial.print("GET Response: ");
debug Serial. print ( F ( " HTTP Status code: " ) ) ;
debug Serial. println ( responseStatusCode ) ;
//debug Serial.print("GET Response: ");
if ( responseStatusCode = = 200 ) {
aJson . deleteItem ( root ) ;
root = aJson . parse ( ( char * ) response . c_str ( ) ) ;
if ( ! root ) {
Serial . println ( F ( " Config parsing failed " ) ) ;
debug Serial. println ( F ( " Config parsing failed " ) ) ;
// nextLanCheckTime=millis()+15000;
return READ_RE_CONFIG ; //-11; //Load from NVRAM
} else {
/*
char * outstr=aJson.print(root);
Serial.println(outstr);
debug Serial.println(outstr);
free (outstr);
*/
Serial . println ( response ) ;
debug Serial. println ( response ) ;
applyConfig ( ) ;
}
} else {
Serial . println ( F ( " Config retrieving failed " ) ) ;
debug Serial. println ( F ( " Config retrieving failed " ) ) ;
//nextLanCheckTime=millis()+15000;
return READ_RE_CONFIG ; //-11; //Load from NVRAM
}
@@ -997,23 +1031,23 @@ lan_status getConfig(int arg_cnt, char **args)
int httpResponseCode = httpClient . GET ( ) ;
if ( httpResponseCode > 0 ) {
// HTTP header has been send and Server response header has been handled
Serial . printf ( " [HTTP] GET... code: %d \n " , httpResponseCode ) ;
debug Serial. printf ( " [HTTP] GET... code: %d \n " , httpResponseCode ) ;
// file found at server
if ( httpResponseCode = = HTTP_CODE_OK ) {
String response = httpClient . getString ( ) ;
Serial . println ( response ) ;
debug Serial. println ( response ) ;
aJson . deleteItem ( root ) ;
root = aJson . parse ( ( char * ) response . c_str ( ) ) ;
if ( ! root ) {
Serial . println ( F ( " Config parsing failed " ) ) ;
debug Serial. println ( F ( " Config parsing failed " ) ) ;
return READ_RE_CONFIG ; //-11; //Load from NVRAM
} else {
Serial . println ( F ( " Config OK, Applying " ) ) ;
debug Serial. println ( F ( " Config OK, Applying " ) ) ;
applyConfig ( ) ;
}
}
} else {
Serial . printf ( " [HTTP] GET... failed, error: %s \n " , httpClient . errorToString ( httpResponseCode ) . c_str ( ) ) ;
debug Serial. printf ( " [HTTP] GET... failed, error: %s \n " , httpClient . errorToString ( httpResponseCode ) . c_str ( ) ) ;
httpClient . end ( ) ;
return READ_RE_CONFIG ; //-11; //Load from NVRAM
}
@@ -1092,84 +1126,76 @@ void setup_main() {
//TODO: checkForRemoteSketchUpdate();
}
# ifdef MMMMMM
errro ! ! !
# endif
void printFirmwareVersionAndBuildOptions ( ) {
Serial . print ( F ( " \n Lazyhome.ru LightHub controller " ) ) ;
Serial . println ( F ( QUOTE ( PIO_SRC_REV ) ) ) ;
debug Serial. print ( F ( " \n Lazyhome.ru LightHub controller " ) ) ;
debug Serial. println ( F ( QUOTE ( PIO_SRC_REV ) ) ) ;
# ifdef CONTROLLINO
Serial . println ( F ( " (+)CONTROLLINO " ) ) ;
debug Serial. println ( F ( " (+)CONTROLLINO " ) ) ;
# endif
# ifdef WATCH_DOG_TICKER_DISABLE
Serial . println ( F ( " (-)WATCHDOG " ) ) ;
debug Serial. println ( F ( " (-)WATCHDOG " ) ) ;
# else
Serial . println ( F ( " (+)WATCHDOG " ) ) ;
debug Serial. println ( F ( " (+)WATCHDOG " ) ) ;
# endif
Serial . print ( F ( " Config server: " ) ) ;
Serial . println ( F ( CONFIG_SERVER ) ) ;
Serial . print ( F ( " Firmware MAC Address " ) ) ;
Serial . println ( F ( QUOTE ( CUSTOM_FIRMWARE_MAC ) ) ) ; //Q Macros didn't working with 6 args
debug Serial. print ( F ( " Config server: " ) ) ;
debug Serial. println ( F ( CONFIG_SERVER ) ) ;
debug Serial. print ( F ( " Firmware MAC Address " ) ) ;
debug Serial. println ( F ( QUOTE ( CUSTOM_FIRMWARE_MAC ) ) ) ; //Q Macros didn't working with 6 args
# ifdef DISABLE_FREERAM_PRINT
Serial . println ( F ( " (-)FreeRam printing " ) ) ;
debug Serial. println ( F ( " (-)FreeRam printing " ) ) ;
# else
Serial . println ( F ( " (+)FreeRam printing " ) ) ;
debug Serial. println ( F ( " (+)FreeRam printing " ) ) ;
# endif
# ifdef USE_1W_PIN
Serial . print ( F ( " (-)DS2482-100 USE_1W_PIN= " ) ) ;
Serial . println ( QUOTE ( USE_1W_PIN ) ) ;
debug Serial. print ( F ( " (-)DS2482-100 USE_1W_PIN= " ) ) ;
debug Serial. println ( QUOTE ( USE_1W_PIN ) ) ;
# else
Serial . println ( F ( " (+)DS2482-100 " ) ) ;
debug Serial. println ( F ( " (+)DS2482-100 " ) ) ;
# endif
# ifdef Wiz5500
Serial . println ( F ( " (+)WizNet5500 " ) ) ;
debug Serial. println ( F ( " (+)WizNet5500 " ) ) ;
# endif
# ifdef DMX_DISABLE
Serial . println ( F ( " (-)DMX " ) ) ;
debug Serial. println ( F ( " (-)DMX " ) ) ;
# else
Serial . println ( F ( " (+)DMX " ) ) ;
debug Serial. println ( F ( " (+)DMX " ) ) ;
# endif
# ifdef MODBUS_DISABLE
Serial . println ( F ( " (-)MODBUS " ) ) ;
debug Serial. println ( F ( " (-)MODBUS " ) ) ;
# else
Serial . println ( F ( " (+)MODBUS " ) ) ;
debug Serial. println ( F ( " (+)MODBUS " ) ) ;
# endif
# ifdef OWIRE_DISABLE
Serial . println ( F ( " (-)OWIRE " ) ) ;
debug Serial. println ( F ( " (-)OWIRE " ) ) ;
# else
Serial . println ( F ( " (+)OWIRE " ) ) ;
debug Serial. println ( F ( " (+)OWIRE " ) ) ;
# endif
# ifndef DHT_DISABLE
Serial . println ( F ( " (+)DHT " ) ) ;
debug Serial. println ( F ( " (+)DHT " ) ) ;
# else
Serial . println ( F ( " (-)DHT " ) ) ;
# endif
# ifdef Wiz5500
Serial . println ( F ( " (+)Wiz5500 " ) ) ;
debug Serial. println ( F ( " (-)DHT " ) ) ;
# endif
# ifdef SD_CARD_INSERTED
Serial . println ( F ( " (+)SDCARD " ) ) ;
debug Serial. println ( F ( " (+)SDCARD " ) ) ;
# endif
# ifdef RESET_PIN
Serial . print ( F ( " (+)HARDRESET on pin= " ) ) ;
Serial . println ( F ( QUOTE ( RESET_PIN ) ) ) ;
debug Serial. print ( F ( " (+)HARDRESET on pin= " ) ) ;
debug Serial. println ( F ( QUOTE ( RESET_PIN ) ) ) ;
# else
Serial . println ( " (-)HARDRESET, using soft " ) ;
debug Serial. println ( " (-)HARDRESET, using soft " ) ;
# endif
# ifdef RESTART_LAN_ON_MQTT_ERRORS
Serial . println ( F ( " (+)RESTART_LAN_ON_MQTT_ERRORS " ) ) ;
debug Serial. println ( F ( " (+)RESTART_LAN_ON_MQTT_ERRORS " ) ) ;
# else
Serial . println ( " (-)RESTART_LAN_ON_MQTT_ERRORS " ) ;
debug Serial. println ( " (-)RESTART_LAN_ON_MQTT_ERRORS " ) ;
# endif
}
@@ -1191,7 +1217,7 @@ void setupMacAddress() {
if ( mac [ i ] ! = 0 & & mac [ i ] ! = 0xff ) isMacValid = true ;
}
if ( ! isMacValid ) {
Serial . println ( F ( " Invalid MAC: set firmware's MAC " ) ) ;
debug Serial. println ( F ( " Invalid MAC: set firmware's MAC " ) ) ;
memcpy ( mac , firmwareMacAddress , 6 ) ;
}
printMACAddress ( ) ;
@@ -1199,7 +1225,7 @@ void setupMacAddress() {
void setupCmdArduino ( ) {
cmdInit ( uint32_t ( SERIAL_BAUD ) ) ;
Serial . println ( F ( " >>> " ) ) ;
debug Serial. println ( F ( " >>> " ) ) ;
cmdAdd ( " help " , cmdFunctionHelp ) ;
cmdAdd ( " save " , cmdFunctionSave ) ;
cmdAdd ( " load " , cmdFunctionLoad ) ;
@@ -1231,7 +1257,7 @@ void loop_main() {
// unsigned long lastpacket = DMXSerial.noDataSince();
DMXCheck ( ) ;
# endif
// if (lastpacket && (lastpacket%10==0)) Serial.println(lastpacket);
// if (lastpacket && (lastpacket%10==0)) debug Serial.println(lastpacket);
if ( items ) {
# ifndef MODBUS_DISABLE
@@ -1257,7 +1283,7 @@ void owIdle(void) {
wdt_res ( ) ;
return ; //TODO: unreached code
Serial . print ( F ( " o " ) ) ;
debug Serial. print ( F ( " o " ) ) ;
if ( lanLoop ( ) = = 1 ) mqttClient . loop ( ) ;
//if (owReady) owLoop();
@@ -1272,7 +1298,7 @@ void owIdle(void) {
void ethernetIdle ( void ) {
wdt_res ( ) ;
inputLoop ( ) ;
// Serial.print(".");
// debug Serial.print(".");
} ;
void modbusIdle ( void ) {
@@ -1357,8 +1383,8 @@ void thermoLoop(void) {
int curTemp = aJson . getArrayItem ( thermoExtensionArray , IET_TEMP ) - > valueint ;
if ( ! aJson . getArrayItem ( thermoExtensionArray , IET_ATTEMPTS ) - > valueint ) {
Serial . print ( thermoItem - > name ) ;
Serial . println ( F ( " Expired " ) ) ;
debug Serial. print ( thermoItem - > name ) ;
debug Serial. println ( F ( " Expired " ) ) ;
} else {
if ( ! ( - - aJson . getArrayItem ( thermoExtensionArray , IET_ATTEMPTS ) - > valueint ) )
@@ -1368,27 +1394,27 @@ void thermoLoop(void) {
if ( curTemp > THERMO_OVERHEAT_CELSIUS ) mqttClient . publish ( " /alarm/ovrht " , thermoItem - > name ) ;
Serial . print ( thermoItem - > name ) ;
Serial . print ( F ( " Set: " ) ) ;
Serial . print ( thermoSetting ) ;
Serial . print ( F ( " Cur: " ) ) ;
Serial . print ( curTemp ) ;
Serial . print ( F ( " cmd: " ) ) ;
Serial . print ( thermoStateCommand ) ;
debug Serial. print ( thermoItem - > name ) ;
debug Serial. print ( F ( " Set: " ) ) ;
debug Serial. print ( thermoSetting ) ;
debug Serial. print ( F ( " Cur: " ) ) ;
debug Serial. print ( curTemp ) ;
debug Serial. print ( F ( " cmd: " ) ) ;
debug Serial. print ( thermoStateCommand ) ;
pinMode ( thermoPin , OUTPUT ) ;
if ( thermoDisabledOrDisconnected ( thermoExtensionArray , thermoStateCommand ) ) {
digitalWrite ( thermoPin , LOW ) ;
Serial . println ( F ( " OFF " ) ) ;
debug Serial. println ( F ( " OFF " ) ) ;
} else {
if ( curTemp < thermoSetting - THERMO_GIST_CELSIUS ) {
digitalWrite ( thermoPin , HIGH ) ;
Serial . println ( F ( " ON " ) ) ;
debug Serial. println ( F ( " ON " ) ) ;
} //too cold
else if ( curTemp > = thermoSetting ) {
digitalWrite ( thermoPin , LOW ) ;
Serial . println ( F ( " OFF " ) ) ;
debug Serial. println ( F ( " OFF " ) ) ;
} //Reached settings
else Serial . println ( F ( " -target zone- " ) ) ; // Nothing to do
else debug Serial. println ( F ( " -target zone- " ) ) ; // Nothing to do
}
thermostatCheckPrinted = true ;
}
@@ -1398,9 +1424,9 @@ void thermoLoop(void) {
nextThermostatCheck = millis ( ) + THERMOSTAT_CHECK_PERIOD ;
# ifndef DISABLE_FREERAM_PRINT
( thermostatCheckPrinted ) ? Serial . print ( F ( " \n free: " ) ) : Serial . print ( F ( " " ) ) ;
Serial . print ( freeRam ( ) ) ;
Serial . print ( " " ) ;
( thermostatCheckPrinted ) ? debug Serial. print ( F ( " \n free: " ) ) : debug Serial. print ( F ( " " ) ) ;
debug Serial. print ( freeRam ( ) ) ;
debug Serial. print ( " " ) ;
# endif
}
@@ -1431,4 +1457,3 @@ short thermoSetCurTemp(char *name, short t) {
}
}