-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfig.cpp
More file actions
216 lines (182 loc) · 6 KB
/
config.cpp
File metadata and controls
216 lines (182 loc) · 6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
// **********************************************************************************
// 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
for (i = 0; i < sizeof(_Config); i++) {
// 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
Input : true if we need to clear actual struc in case of error
Output : true if config found and crc ok, false otherwise
Comments: -
====================================================================== */
bool readConfig (bool clear_on_error)
{
uint16_t crc = ~0;
uint8_t * pconfig = (uint8_t *) &config ;
uint8_t data ;
// For whole size of config structure
for (uint16_t i = 0; i < sizeof(_Config); ++i) {
// read data
data = EEPROM.read(i);
// save into struct
*pconfig++ = data ;
// calc CRC
crc = crc16Update(crc, data);
}
// CRC Error ?
if (crc != 0) {
// Clear config if wanted
if (clear_on_error)
memset(&config, 0, sizeof( _Config ));
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;
//eepromDump(32);
// Init pointer
pconfig = (uint8_t *) &config ;
// Init CRC
config.crc = ~0;
// For whole size of config structure, pre-calculate CRC
for (uint16_t i = 0; i < sizeof (_Config) - 2; ++i)
config.crc = crc16Update(config.crc, *pconfig++);
// Re init pointer
pconfig = (uint8_t *) &config ;
// For whole size of config structure, write to EEP
for (uint16_t i = 0; i < sizeof(_Config); ++i)
EEPROM.write(i, *pconfig++);
// Physically save
EEPROM.commit();
// 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);
Debug(F("Write config "));
if (ret_code)
Debugln(F("OK!"));
else
Debugln(F("Error!"));
//eepromDump(32);
// return result
return (ret_code);
}
/* ======================================================================
Function: showConfig
Purpose : display configuration
Input : -
Output : -
Comments: -
====================================================================== */
void showConfig()
{
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("Dbg/file :"); Debugln(config.dbgfile);
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);
DebuglnF("\r\n===== HTTP request");
DebugF("host :"); Debugln(config.httpReq.host);
DebugF("port :"); Debugln(config.httpReq.port);
DebugF("path :"); Debugln(config.httpReq.path);
DebugF("freq :"); Debugln(config.httpReq.freq);
DebugF("sw idx :"); Debugln(config.httpReq.swidx);
DebugF("I idx :"); Debugln(config.httpReq.iidx); //Intensité
DebugF("sw idx :"); Debugln(config.httpReq.adpsidx);//ADPS
}