2015-07-29 21:18:08 +02:00
|
|
|
// **********************************************************************************
|
|
|
|
// ESP8266 Teleinfo WEB Server configuration Include file
|
|
|
|
// **********************************************************************************
|
|
|
|
// Creative Commons Attrib Share-Alike License
|
|
|
|
// You are free to use/extend this library but please abide with the CC-BY-SA license:
|
|
|
|
// Attribution-NonCommercial-ShareAlike 4.0 International License
|
|
|
|
// http://creativecommons.org/licenses/by-nc-sa/4.0/
|
|
|
|
//
|
|
|
|
// For any explanation about teleinfo ou use , see my blog
|
|
|
|
// http://hallard.me/category/tinfo
|
|
|
|
//
|
|
|
|
// This program works with the Wifinfo board
|
|
|
|
// see schematic here https://github.com/hallard/teleinfo/tree/master/Wifinfo
|
|
|
|
//
|
|
|
|
// Written by Charles-Henri Hallard (http://hallard.me)
|
|
|
|
//
|
|
|
|
// History : V1.00 2015-06-14 - First release
|
|
|
|
//
|
|
|
|
// All text above must be included in any redistribution.
|
|
|
|
//
|
|
|
|
// **********************************************************************************
|
|
|
|
#include "./config.h"
|
|
|
|
|
|
|
|
// Configuration structure for whole program
|
|
|
|
_Config config;
|
|
|
|
|
|
|
|
|
|
|
|
uint16_t crc16Update(uint16_t crc, uint8_t a)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
crc ^= a;
|
|
|
|
for (i = 0; i < 8; ++i) {
|
|
|
|
if (crc & 1)
|
|
|
|
crc = (crc >> 1) ^ 0xA001;
|
|
|
|
else
|
|
|
|
crc = (crc >> 1);
|
|
|
|
}
|
|
|
|
return crc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ======================================================================
|
|
|
|
Function: eeprom_dump
|
|
|
|
Purpose : dump eeprom value to serial
|
|
|
|
Input : -
|
|
|
|
Output : -
|
|
|
|
Comments: -
|
|
|
|
====================================================================== */
|
|
|
|
void eepromDump(uint8_t bytesPerRow)
|
|
|
|
{
|
|
|
|
uint16_t i,b;
|
|
|
|
char buf[10];
|
|
|
|
uint16_t j=0 ;
|
|
|
|
|
|
|
|
// default to 16 bytes per row
|
|
|
|
if (bytesPerRow==0)
|
|
|
|
bytesPerRow=16;
|
|
|
|
|
|
|
|
Debugln();
|
|
|
|
|
|
|
|
// loop thru EEP address
|
2015-12-23 01:51:12 +01:00
|
|
|
for (i = 0; i < sizeof(_Config); i++) {
|
2015-07-29 21:18:08 +02:00
|
|
|
// First byte of the row ?
|
|
|
|
if (j==0) {
|
|
|
|
// Display Address
|
|
|
|
Debugf("%04X : ", i);
|
|
|
|
}
|
|
|
|
|
|
|
|
// write byte in hex form
|
|
|
|
Debugf("%02X ", EEPROM.read(i));
|
|
|
|
|
|
|
|
// Last byte of the row ?
|
|
|
|
// start a new line
|
|
|
|
if (++j >= bytesPerRow) {
|
|
|
|
j=0;
|
|
|
|
Debugln();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ======================================================================
|
|
|
|
Function: readConfig
|
|
|
|
Purpose : fill config structure with data located into eeprom
|
2015-12-23 01:51:12 +01:00
|
|
|
Input : true if we need to clear actual struc in case of error
|
2015-07-29 21:18:08 +02:00
|
|
|
Output : true if config found and crc ok, false otherwise
|
|
|
|
Comments: -
|
|
|
|
====================================================================== */
|
2015-12-23 01:51:12 +01:00
|
|
|
bool readConfig (bool clear_on_error)
|
2015-07-29 21:18:08 +02:00
|
|
|
{
|
|
|
|
uint16_t crc = ~0;
|
|
|
|
uint8_t * pconfig = (uint8_t *) &config ;
|
|
|
|
uint8_t data ;
|
|
|
|
|
|
|
|
// For whole size of config structure
|
2015-12-23 01:51:12 +01:00
|
|
|
for (uint16_t i = 0; i < sizeof(_Config); ++i) {
|
2015-07-29 21:18:08 +02:00
|
|
|
// read data
|
|
|
|
data = EEPROM.read(i);
|
|
|
|
|
|
|
|
// save into struct
|
|
|
|
*pconfig++ = data ;
|
|
|
|
|
|
|
|
// calc CRC
|
|
|
|
crc = crc16Update(crc, data);
|
|
|
|
}
|
|
|
|
|
|
|
|
// CRC Error ?
|
|
|
|
if (crc != 0) {
|
2015-12-23 01:51:12 +01:00
|
|
|
// Clear config if wanted
|
|
|
|
if (clear_on_error)
|
|
|
|
memset(&config, 0, sizeof( _Config ));
|
2015-07-29 21:18:08 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true ;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ======================================================================
|
|
|
|
Function: saveConfig
|
|
|
|
Purpose : save config structure values into eeprom
|
|
|
|
Input : -
|
|
|
|
Output : true if saved and readback ok
|
|
|
|
Comments: once saved, config is read again to check the CRC
|
|
|
|
====================================================================== */
|
|
|
|
bool saveConfig (void)
|
|
|
|
{
|
|
|
|
uint8_t * pconfig ;
|
|
|
|
bool ret_code;
|
|
|
|
|
2015-12-23 01:51:12 +01:00
|
|
|
//eepromDump(32);
|
|
|
|
|
2015-07-29 21:18:08 +02:00
|
|
|
// Init pointer
|
|
|
|
pconfig = (uint8_t *) &config ;
|
|
|
|
|
|
|
|
// Init CRC
|
|
|
|
config.crc = ~0;
|
|
|
|
|
|
|
|
// For whole size of config structure, pre-calculate CRC
|
2015-12-23 01:51:12 +01:00
|
|
|
for (uint16_t i = 0; i < sizeof (_Config) - 2; ++i)
|
2015-07-29 21:18:08 +02:00
|
|
|
config.crc = crc16Update(config.crc, *pconfig++);
|
|
|
|
|
|
|
|
// Re init pointer
|
|
|
|
pconfig = (uint8_t *) &config ;
|
|
|
|
|
2015-12-23 01:51:12 +01:00
|
|
|
// For whole size of config structure, write to EEP
|
|
|
|
for (uint16_t i = 0; i < sizeof(_Config); ++i)
|
|
|
|
EEPROM.write(i, *pconfig++);
|
2015-07-29 21:18:08 +02:00
|
|
|
|
|
|
|
// Physically save
|
|
|
|
EEPROM.commit();
|
|
|
|
|
2015-12-23 01:51:12 +01:00
|
|
|
// Read Again to see if saved ok, but do
|
|
|
|
// not clear if error this avoid clearing
|
|
|
|
// default config and breaks OTA
|
|
|
|
ret_code = readConfig(false);
|
2015-07-29 21:18:08 +02:00
|
|
|
|
2015-12-23 01:51:12 +01:00
|
|
|
Debug(F("Write config "));
|
2015-07-29 21:18:08 +02:00
|
|
|
|
|
|
|
if (ret_code)
|
|
|
|
Debugln(F("OK!"));
|
|
|
|
else
|
|
|
|
Debugln(F("Error!"));
|
2015-12-23 01:51:12 +01:00
|
|
|
|
|
|
|
//eepromDump(32);
|
2015-07-29 21:18:08 +02:00
|
|
|
|
|
|
|
// return result
|
|
|
|
return (ret_code);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* ======================================================================
|
|
|
|
Function: showConfig
|
|
|
|
Purpose : display configuration
|
|
|
|
Input : -
|
|
|
|
Output : -
|
|
|
|
Comments: -
|
|
|
|
====================================================================== */
|
|
|
|
void showConfig()
|
|
|
|
{
|
2015-12-23 01:51:12 +01:00
|
|
|
DebuglnF("===== Wifi");
|
|
|
|
DebugF("ssid :"); Debugln(config.ssid);
|
|
|
|
DebugF("psk :"); Debugln(config.psk);
|
|
|
|
DebugF("host :"); Debugln(config.host);
|
|
|
|
DebugF("ap_psk :"); Debugln(config.ap_psk);
|
|
|
|
DebugF("OTA auth :"); Debugln(config.ota_auth);
|
|
|
|
DebugF("OTA port :"); Debugln(config.ota_port);
|
|
|
|
DebugF("Config :");
|
|
|
|
if (config.config & CFG_RGB_LED) DebugF(" RGB");
|
|
|
|
if (config.config & CFG_DEBUG) DebugF(" DEBUG");
|
|
|
|
if (config.config & CFG_LCD) DebugF(" LCD");
|
|
|
|
|
|
|
|
DebuglnF("\r\n===== Emoncms");
|
|
|
|
DebugF("host :"); Debugln(config.emoncms.host);
|
|
|
|
DebugF("port :"); Debugln(config.emoncms.port);
|
|
|
|
DebugF("url :"); Debugln(config.emoncms.url);
|
|
|
|
DebugF("key :"); Debugln(config.emoncms.apikey);
|
|
|
|
DebugF("node :"); Debugln(config.emoncms.node);
|
|
|
|
DebugF("freq :"); Debugln(config.emoncms.freq);
|
|
|
|
|
|
|
|
DebuglnF("\r\n===== Jeedom");
|
|
|
|
DebugF("host :"); Debugln(config.jeedom.host);
|
|
|
|
DebugF("port :"); Debugln(config.jeedom.port);
|
|
|
|
DebugF("url :"); Debugln(config.jeedom.url);
|
|
|
|
DebugF("key :"); Debugln(config.jeedom.apikey);
|
|
|
|
DebugF("compteur :"); Debugln(config.jeedom.adco);
|
|
|
|
DebugF("freq :"); Debugln(config.jeedom.freq);
|
2015-07-29 21:18:08 +02:00
|
|
|
}
|
|
|
|
|