@ -6,9 +6,9 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
*/
*/
# include "Embedis.h"
# include <EEPROM.h>
# include <EEPROM.h>
# include "spi_flash.h"
# include "spi_flash.h"
# include "libs/EmbedisWrap.h"
# include <StreamString.h>
# include <StreamString.h>
# if TELNET_SUPPORT
# if TELNET_SUPPORT
@ -18,12 +18,12 @@ Copyright (C) 2016-2017 by Xose Pérez <xose dot perez at gmail dot com>
# else
# else
StreamInjector _serial = StreamInjector ( Serial ) ;
StreamInjector _serial = StreamInjector ( Serial ) ;
# endif
# endif
Embedis embedis ( _serial ) ;
EmbedisWrap embedis ( _serial ) ;
# else
# else
# ifdef DEBUG_PORT
# ifdef DEBUG_PORT
Embedis embedis ( DEBUG_PORT ) ;
EmbedisWrap embedis ( DEBUG_PORT ) ;
# else
# else
Embedis embedis ( _serial ) ;
EmbedisWrap embedis ( _serial ) ;
# endif
# endif
# endif
# endif
@ -90,6 +90,28 @@ String settingsKeyName(unsigned int index) {
}
}
bool settingsRestore ( JsonObject & data ) {
const char * app = data [ " app " ] ;
if ( strcmp ( app , APP_NAME ) ! = 0 ) return false ;
for ( unsigned int i = EEPROM_DATA_END ; i < SPI_FLASH_SEC_SIZE ; i + + ) {
EEPROM . write ( i , 0xFF ) ;
}
for ( auto element : data ) {
if ( strcmp ( element . key , " app " ) = = 0 ) continue ;
if ( strcmp ( element . key , " version " ) = = 0 ) continue ;
setSetting ( element . key , element . value . as < char * > ( ) ) ;
}
saveSettings ( ) ;
DEBUG_MSG_P ( PSTR ( " [SETTINGS] Settings restored successfully \n " ) ) ;
return true ;
}
void settingsFactoryReset ( ) {
void settingsFactoryReset ( ) {
for ( unsigned int i = 0 ; i < SPI_FLASH_SEC_SIZE ; i + + ) {
for ( unsigned int i = 0 ; i < SPI_FLASH_SEC_SIZE ; i + + ) {
EEPROM . write ( i , 0xFF ) ;
EEPROM . write ( i , 0xFF ) ;
@ -97,6 +119,18 @@ void settingsFactoryReset() {
EEPROM . commit ( ) ;
EEPROM . commit ( ) ;
}
}
void settingsHelp ( ) {
unsigned char len = embedis . getCommandsCount ( ) ;
DEBUG_MSG_P ( PSTR ( " \n Available commands: \n \n " ) ) ;
for ( unsigned char i = 0 ; i < len ; i + + ) {
DEBUG_MSG_P ( PSTR ( " * %s \n " ) , embedis . getCommandName ( i ) . c_str ( ) ) ;
if ( embedis . getCommandName ( i ) . equals ( " WRITE " ) ) {
DEBUG_MSG_P ( PSTR ( " \n " ) ) ;
}
}
DEBUG_MSG_P ( PSTR ( " \n " ) ) ;
}
void settingsSetup ( ) {
void settingsSetup ( ) {
EEPROM . begin ( SPI_FLASH_SEC_SIZE ) ;
EEPROM . begin ( SPI_FLASH_SEC_SIZE ) ;
@ -126,33 +160,82 @@ void settingsSetup() {
// -------------------------------------------------------------------------
// -------------------------------------------------------------------------
Embedis : : command ( F ( " RESET.WIFI " ) , [ ] ( Embedis * e ) {
wifiConfigure ( ) ;
wifiDisconnect ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
Embedis : : command ( F ( " BRIGHTNESS " ) , [ ] ( Embedis * e ) {
if ( e - > argc > 1 ) {
lightBrightness ( String ( e - > argv [ 1 ] ) . toInt ( ) ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Brightness: %d \n " ) , lightBrightness ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# endif
# if MQTT_SUPPORT
Embedis : : command ( F ( " RESET.MQTT " ) , [ ] ( Embedis * e ) {
mqttConfigure ( ) ;
mqttDisconnect ( ) ;
# if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
Embedis : : command ( F ( " CHANNEL " ) , [ ] ( Embedis * e ) {
if ( e - > argc < 2 ) {
DEBUG_MSG_P ( PSTR ( " -ERROR: Wrong arguments \n " ) ) ;
}
int id = String ( e - > argv [ 1 ] ) . toInt ( ) ;
if ( e - > argc > 2 ) {
int value = String ( e - > argv [ 2 ] ) . toInt ( ) ;
lightChannel ( id , value ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Channel #%d: %d \n " ) , id , lightChannel ( id ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
} ) ;
# endif
# endif
Embedis : : command ( F ( " INFO " ) , [ ] ( Embedis * e ) {
welcome ( ) ;
# if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
Embedis : : command ( F ( " COLOR " ) , [ ] ( Embedis * e ) {
if ( e - > argc > 1 ) {
String color = String ( e - > argv [ 1 ] ) ;
lightColor ( color . c_str ( ) ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Color: %s \n " ) , lightColor ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# endif
# if DEBUG_SUPPORT
Embedis : : command ( F ( " CRASH " ) , [ ] ( Embedis * e ) {
debugDumpCrashInfo ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# endif
Embedis : : command ( F ( " DUMP " ) , [ ] ( Embedis * e ) {
unsigned int size = settingsKeyCount ( ) ;
for ( unsigned int i = 0 ; i < size ; i + + ) {
String key = settingsKeyName ( i ) ;
String value = getSetting ( key ) ;
DEBUG_MSG_P ( PSTR ( " +%s => %s \n " ) , key . c_str ( ) , value . c_str ( ) ) ;
}
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
} ) ;
Embedis : : command ( F ( " UPTIME " ) , [ ] ( Embedis * e ) {
DEBUG_MSG_P ( PSTR ( " Uptime: %d seconds \n " ) , getUptime ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
Embedis : : command ( F ( " DUMP.RAW " ) , [ ] ( Embedis * e ) {
bool ascii = false ;
if ( e - > argc = = 2 ) ascii = String ( e - > argv [ 1 ] ) . toInt ( ) = = 1 ;
for ( unsigned int i = 0 ; i < SPI_FLASH_SEC_SIZE ; i + + ) {
if ( i % 16 = = 0 ) DEBUG_MSG_P ( PSTR ( " \n [%04X] " ) , i ) ;
byte c = EEPROM . read ( i ) ;
if ( ascii & & 32 < = c & & c < = 126 ) {
DEBUG_MSG_P ( PSTR ( " %c " ) , c ) ;
} else {
DEBUG_MSG_P ( PSTR ( " %02X " ) , c ) ;
}
}
DEBUG_MSG_P ( PSTR ( " \n +OK \n " ) ) ;
} ) ;
} ) ;
Embedis : : command ( F ( " RESET " ) , [ ] ( Embedis * e ) {
Embedis : : command ( F ( " EEPROM " ) , [ ] ( Embedis * e ) {
unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize ( ) ;
DEBUG_MSG_P ( PSTR ( " Number of keys: %d \n " ) , settingsKeyCount ( ) ) ;
DEBUG_MSG_P ( PSTR ( " Free EEPROM: %d bytes (%d%%) \n " ) , freeEEPROM , 100 * freeEEPROM / SPI_FLASH_SEC_SIZE ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
deferredReset ( 100 , CUSTOM_RESET_TERMINAL ) ;
} ) ;
} ) ;
Embedis : : command ( F ( " ERASE.CONFIG " ) , [ ] ( Embedis * e ) {
Embedis : : command ( F ( " ERASE.CONFIG " ) , [ ] ( Embedis * e ) {
@ -162,13 +245,6 @@ void settingsSetup() {
* ( ( int * ) 0 ) = 0 ; // see https://github.com/esp8266/Arduino/issues/1494
* ( ( int * ) 0 ) = 0 ; // see https://github.com/esp8266/Arduino/issues/1494
} ) ;
} ) ;
# if NOFUSS_SUPPORT
Embedis : : command ( F ( " NOFUSS " ) , [ ] ( Embedis * e ) {
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
nofussRun ( ) ;
} ) ;
# endif
Embedis : : command ( F ( " FACTORY.RESET " ) , [ ] ( Embedis * e ) {
Embedis : : command ( F ( " FACTORY.RESET " ) , [ ] ( Embedis * e ) {
settingsFactoryReset ( ) ;
settingsFactoryReset ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
@ -179,6 +255,48 @@ void settingsSetup() {
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
} ) ;
Embedis : : command ( F ( " HELP " ) , [ ] ( Embedis * e ) {
settingsHelp ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
Embedis : : command ( F ( " INFO " ) , [ ] ( Embedis * e ) {
welcome ( ) ;
wifiStatus ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
Embedis : : command ( F ( " KELVIN " ) , [ ] ( Embedis * e ) {
if ( e - > argc > 1 ) {
String color = String ( " K " ) + String ( e - > argv [ 1 ] ) ;
lightColor ( color . c_str ( ) ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Color: %s \n " ) , lightColor ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# endif
# if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
Embedis : : command ( F ( " MIRED " ) , [ ] ( Embedis * e ) {
if ( e - > argc > 1 ) {
String color = String ( " M " ) + String ( e - > argv [ 1 ] ) ;
lightColor ( color . c_str ( ) ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Color: %s \n " ) , lightColor ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# endif
# if NOFUSS_SUPPORT
Embedis : : command ( F ( " NOFUSS " ) , [ ] ( Embedis * e ) {
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
nofussRun ( ) ;
} ) ;
# endif
Embedis : : command ( F ( " RELAY " ) , [ ] ( Embedis * e ) {
Embedis : : command ( F ( " RELAY " ) , [ ] ( Embedis * e ) {
if ( e - > argc < 2 ) {
if ( e - > argc < 2 ) {
DEBUG_MSG_P ( PSTR ( " -ERROR: Wrong arguments \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " -ERROR: Wrong arguments \n " ) ) ;
@ -196,105 +314,30 @@ void settingsSetup() {
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
} ) ;
# if LIGHT_PROVIDER != LIGHT_PROVIDER_NONE
if ( lightHasColor ( ) ) {
Embedis : : command ( F ( " COLOR " ) , [ ] ( Embedis * e ) {
if ( e - > argc > 1 ) {
String color = String ( e - > argv [ 1 ] ) ;
lightColor ( color . c_str ( ) ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Color: %s \n " ) , lightColor ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
Embedis : : command ( F ( " BRIGHTNESS " ) , [ ] ( Embedis * e ) {
if ( e - > argc > 1 ) {
lightBrightness ( String ( e - > argv [ 1 ] ) . toInt ( ) ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Brightness: %d \n " ) , lightBrightness ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
Embedis : : command ( F ( " MIRED " ) , [ ] ( Embedis * e ) {
if ( e - > argc > 1 ) {
String color = String ( " M " ) + String ( e - > argv [ 1 ] ) ;
lightColor ( color . c_str ( ) ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Color: %s \n " ) , lightColor ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
Embedis : : command ( F ( " KELVIN " ) , [ ] ( Embedis * e ) {
if ( e - > argc > 1 ) {
String color = String ( " K " ) + String ( e - > argv [ 1 ] ) ;
lightColor ( color . c_str ( ) ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Color: %s \n " ) , lightColor ( ) . c_str ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
}
Embedis : : command ( F ( " RESET " ) , [ ] ( Embedis * e ) {
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
deferredReset ( 100 , CUSTOM_RESET_TERMINAL ) ;
} ) ;
Embedis : : command ( F ( " CHANNEL " ) , [ ] ( Embedis * e ) {
if ( e - > argc < 2 ) {
DEBUG_MSG_P ( PSTR ( " -ERROR: Wrong arguments \n " ) ) ;
}
int id = String ( e - > argv [ 1 ] ) . toInt ( ) ;
if ( e - > argc > 2 ) {
int value = String ( e - > argv [ 2 ] ) . toInt ( ) ;
lightChannel ( id , value ) ;
lightUpdate ( true , true ) ;
}
DEBUG_MSG_P ( PSTR ( " Channel #%d: %d \n " ) , id , lightChannel ( id ) ) ;
# if MQTT_SUPPORT
Embedis : : command ( F ( " RESET.MQTT " ) , [ ] ( Embedis * e ) {
mqttConfigure ( ) ;
mqttDisconnect ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
} ) ;
# endif
# endif
Embedis : : command ( F ( " EEPROM " ) , [ ] ( Embedis * e ) {
unsigned long freeEEPROM = SPI_FLASH_SEC_SIZE - settingsSize ( ) ;
DEBUG_MSG_P ( PSTR ( " Number of keys: %d \n " ) , settingsKeyCount ( ) ) ;
DEBUG_MSG_P ( PSTR ( " Free EEPROM: %d bytes (%d%%) \n " ) , freeEEPROM , 100 * freeEEPROM / SPI_FLASH_SEC_SIZE ) ;
Embedis : : command ( F ( " RESET.WIFI " ) , [ ] ( Embedis * e ) {
wifiConfigure ( ) ;
wifiDisconnect ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
} ) ;
Embedis : : command ( F ( " DUMP " ) , [ ] ( Embedis * e ) {
unsigned int size = settingsKeyCount ( ) ;
for ( unsigned int i = 0 ; i < size ; i + + ) {
String key = settingsKeyName ( i ) ;
String value = getSetting ( key ) ;
DEBUG_MSG_P ( PSTR ( " +%s => %s \n " ) , key . c_str ( ) , value . c_str ( ) ) ;
}
Embedis : : command ( F ( " UPTIME " ) , [ ] ( Embedis * e ) {
DEBUG_MSG_P ( PSTR ( " Uptime: %d seconds \n " ) , getUptime ( ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
} ) ;
# if DEBUG_SUPPORT
Embedis : : command ( F ( " CRASH " ) , [ ] ( Embedis * e ) {
debugDumpCrashInfo ( ) ;
DEBUG_MSG_P ( PSTR ( " +OK \n " ) ) ;
} ) ;
# endif
Embedis : : command ( F ( " DUMP.RAW " ) , [ ] ( Embedis * e ) {
bool ascii = false ;
if ( e - > argc = = 2 ) ascii = String ( e - > argv [ 1 ] ) . toInt ( ) = = 1 ;
for ( unsigned int i = 0 ; i < SPI_FLASH_SEC_SIZE ; i + + ) {
if ( i % 16 = = 0 ) DEBUG_MSG_P ( PSTR ( " \n [%04X] " ) , i ) ;
byte c = EEPROM . read ( i ) ;
if ( ascii & & 32 < = c & & c < = 126 ) {
DEBUG_MSG_P ( PSTR ( " %c " ) , c ) ;
} else {
DEBUG_MSG_P ( PSTR ( " %02X " ) , c ) ;
}
}
DEBUG_MSG_P ( PSTR ( " \n +OK \n " ) ) ;
} ) ;
DEBUG_MSG_P ( PSTR ( " [SETTINGS] EEPROM size: %d bytes \n " ) , SPI_FLASH_SEC_SIZE ) ;
DEBUG_MSG_P ( PSTR ( " [SETTINGS] EEPROM size: %d bytes \n " ) , SPI_FLASH_SEC_SIZE ) ;
DEBUG_MSG_P ( PSTR ( " [SETTINGS] Settings size: %d bytes \n " ) , settingsSize ( ) ) ;
DEBUG_MSG_P ( PSTR ( " [SETTINGS] Settings size: %d bytes \n " ) , settingsSize ( ) ) ;