Skip to content

Commit b2f8ab9

Browse files
committed
Change EEPROM begin/end to be sure it is open
Update print_config
1 parent fa9b87f commit b2f8ab9

File tree

3 files changed

+169
-21
lines changed

3 files changed

+169
-21
lines changed

esp8266/command.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ void COMMAND::execute_command(int cmd,String cmd_params)
7676
if (cmd_params=="RESET")
7777
{
7878
CONFIG::reset_config();
79-
web_interface->restartmodule=true;
8079
}
8180
if (cmd_params=="SAFEMODE")
8281
{

esp8266/config.cpp

Lines changed: 169 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,17 @@
2020
#include "config.h"
2121
#include <EEPROM.h>
2222
#include "wifi.h"
23+
extern "C" {
24+
#include "user_interface.h"
25+
}
2326

2427
//read a string
2528
//a string is multibyte + \0, this is won't work if 1 char is multibyte like chinese char
2629
bool CONFIG::read_string(int pos, char byte_buffer[], int size_max)
2730
{
2831
//check if parameters are acceptable
2932
if (size_max==0 || pos+size_max+1 > EEPROM_SIZE || byte_buffer== NULL)return false;
33+
EEPROM.begin(EEPROM_SIZE);
3034
byte b=0;
3135
int i=0;
3236
//read first byte
@@ -42,6 +46,7 @@ bool CONFIG::read_string(int pos, char byte_buffer[], int size_max)
4246
}
4347
//if it is a string be sure there is an 0 at the end
4448
if (b!=0)byte_buffer[i-1]=0x00;
49+
EEPROM.end();
4550
return true;
4651
}
4752

@@ -52,6 +57,7 @@ bool CONFIG::read_string(int pos, String & sbuffer, int size_max)
5257
byte b=0;
5358
int i=0;
5459
sbuffer="";
60+
EEPROM.begin(EEPROM_SIZE);
5561
//read first byte
5662
b = EEPROM.read(pos + i);
5763
sbuffer+=char(b);
@@ -63,6 +69,7 @@ bool CONFIG::read_string(int pos, String & sbuffer, int size_max)
6369
sbuffer+=char(b);
6470
i++;
6571
}
72+
EEPROM.end();
6673
return true;
6774
}
6875

@@ -72,12 +79,14 @@ bool CONFIG::read_buffer(int pos, byte byte_buffer[], int size_buffer)
7279
//check if parameters are acceptable
7380
if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL)return false;
7481
int i=0;
82+
EEPROM.begin(EEPROM_SIZE);
7583
//read until max size is reached
7684
while (i<size_buffer )
7785
{
7886
byte_buffer[i]=EEPROM.read(pos+i);
7987
i++;
8088
}
89+
EEPROM.end();
8190
return true;
8291
}
8392

@@ -86,7 +95,9 @@ bool CONFIG::read_byte(int pos, byte * value)
8695
{
8796
//check if parameters are acceptable
8897
if (pos+1 > EEPROM_SIZE)return false;
98+
EEPROM.begin(EEPROM_SIZE);
8999
value[0] = EEPROM.read(pos);
100+
EEPROM.end();
90101
return true;
91102
}
92103

@@ -104,13 +115,15 @@ bool CONFIG::write_string(int pos, const char * byte_buffer)
104115
//check if parameters are acceptable
105116
if (size_buffer==0 || pos+size_buffer+1 > EEPROM_SIZE || byte_buffer== NULL)return false;
106117
//copy the value(s)
118+
EEPROM.begin(EEPROM_SIZE);
107119
for (int i = 0; i < size_buffer; i++) {
108120
EEPROM.write(pos + i, byte_buffer[i]);
109121
}
110122

111123
//0 terminal
112124
EEPROM.write(pos + size_buffer, 0x00);
113125
EEPROM.commit();
126+
EEPROM.end();
114127
return true;
115128
}
116129

@@ -119,11 +132,13 @@ bool CONFIG::write_buffer(int pos, const byte * byte_buffer, int size_buffer)
119132
{
120133
//check if parameters are acceptable
121134
if (size_buffer==0 || pos+size_buffer > EEPROM_SIZE || byte_buffer== NULL)return false;
135+
EEPROM.begin(EEPROM_SIZE);
122136
//copy the value(s)
123137
for (int i = 0; i < size_buffer; i++) {
124138
EEPROM.write(pos + i, byte_buffer[i]);
125139
}
126140
EEPROM.commit();
141+
EEPROM.end();
127142
return true;
128143
}
129144

@@ -132,8 +147,10 @@ bool CONFIG::write_byte(int pos, const byte value)
132147
{
133148
//check if parameters are acceptable
134149
if (pos+1 > EEPROM_SIZE)return false;
150+
EEPROM.begin(EEPROM_SIZE);
135151
EEPROM.write(pos, value);
136152
EEPROM.commit();
153+
EEPROM.end();
137154
return true;
138155
}
139156

@@ -168,24 +185,157 @@ void CONFIG::print_config()
168185
char sbuf[MAX_PASSWORD_LENGTH+1];
169186
byte bbuf=0;
170187
int ibuf=0;
171-
if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf ))Serial.println(byte(bbuf));
172-
if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH))Serial.println(sbuf);
188+
if (CONFIG::read_byte(EP_WIFI_MODE, &bbuf ))
189+
{
190+
if (byte(bbuf)==CLIENT_MODE) Serial.println("Mode: Station");
191+
else if (byte(bbuf)==AP_MODE) Serial.println("Mode: Access Point");
192+
else Serial.println("Mode: ???");
193+
}
194+
else Serial.println("Error reading mode");
195+
if (CONFIG::read_string(EP_SSID, sbuf , MAX_SSID_LENGTH))
196+
{
197+
Serial.print("SSID: ");
198+
Serial.println(sbuf);
199+
}
200+
else Serial.println("Error reading SSID");
173201
//if (CONFIG::read_string(EP_PASSWORD, sbuf , MAX_PASSWORD_LENGTH))Serial.println(sbuf);
174-
if (CONFIG::read_byte(EP_IP_MODE, &bbuf ))Serial.println(byte(bbuf));
175-
if (CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGTH))Serial.println(wifi_config.ip2str((byte *)sbuf));
176-
if (CONFIG::read_buffer(EP_MASK_VALUE, (byte *)sbuf , IP_LENGTH))Serial.println(wifi_config.ip2str((byte *)sbuf));
177-
if (CONFIG::read_buffer(EP_GATEWAY_VALUE, (byte *)sbuf , IP_LENGTH))Serial.println(wifi_config.ip2str((byte *)sbuf));
178-
if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , INTEGER_LENGTH))Serial.println(ibuf);
179-
if (CONFIG::read_byte(EP_PHY_MODE, &bbuf ))Serial.println(byte(bbuf));
180-
if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf ))Serial.println(byte(bbuf));
181-
if (CONFIG::read_byte(EP_CHANNEL, &bbuf ))Serial.println(byte(bbuf));
182-
if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf ))Serial.println(byte(bbuf));
183-
if (CONFIG::read_byte(EP_SSID_VISIBLE, &bbuf ))Serial.println(byte(bbuf));
184-
if (CONFIG::read_buffer(EP_WEB_PORT, (byte *)&ibuf , INTEGER_LENGTH))Serial.println(ibuf);
185-
if (CONFIG::read_buffer(EP_DATA_PORT, (byte *)&ibuf , INTEGER_LENGTH))Serial.println(ibuf);
186-
if (CONFIG::read_byte(EP_REFRESH_PAGE_TIME, &bbuf ))Serial.println(byte(bbuf));
187-
if (CONFIG::read_string(EP_HOSTNAME, sbuf , MAX_HOSTNAME_LENGTH))Serial.println(sbuf);
188-
if (CONFIG::read_buffer(EP_XY_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))Serial.println(ibuf);
189-
if (CONFIG::read_buffer(EP_Z_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))Serial.println(ibuf);
190-
if (CONFIG::read_buffer(EP_E_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))Serial.println(ibuf);
202+
203+
if (CONFIG::read_byte(EP_IP_MODE, &bbuf ))
204+
{
205+
if (byte(bbuf)==STATIC_IP_MODE) Serial.println("IP Mode: Static");
206+
else if (byte(bbuf)==DHCP_MODE) Serial.println("IP Mode: DHCP");
207+
else Serial.println("IP mode: ???");
208+
}
209+
else Serial.println("Error reading IP mode");
210+
if (CONFIG::read_buffer(EP_IP_VALUE,(byte *)sbuf , IP_LENGTH))
211+
{
212+
Serial.print("IP: ");
213+
Serial.println(wifi_config.ip2str((byte *)sbuf));
214+
}
215+
else Serial.println("Error reading IP");
216+
if (CONFIG::read_buffer(EP_MASK_VALUE, (byte *)sbuf , IP_LENGTH))
217+
{
218+
Serial.print("Subnet: ");
219+
Serial.println(wifi_config.ip2str((byte *)sbuf));
220+
}
221+
else Serial.println("Error reading subnet");
222+
if (CONFIG::read_buffer(EP_GATEWAY_VALUE, (byte *)sbuf , IP_LENGTH))
223+
{
224+
Serial.print("Gateway : ");
225+
Serial.println(wifi_config.ip2str((byte *)sbuf));
226+
}
227+
else Serial.println("Error reading gateway");
228+
if (CONFIG::read_buffer(EP_BAUD_RATE, (byte *)&ibuf , INTEGER_LENGTH))
229+
{
230+
Serial.print("Baud rate : ");
231+
Serial.println(ibuf);
232+
}
233+
else Serial.println("Error reading baud rate");
234+
if (CONFIG::read_byte(EP_PHY_MODE, &bbuf ))
235+
{
236+
Serial.print("Phy mode : ");
237+
if (byte(bbuf)==PHY_MODE_11B)Serial.println("11b");
238+
else if (byte(bbuf)==PHY_MODE_11G)Serial.println("11g");
239+
else if (byte(bbuf)==PHY_MODE_11N)Serial.println("11n");
240+
else Serial.println("???");
241+
}
242+
else Serial.println("Error reading phy mode");
243+
if (CONFIG::read_byte(EP_SLEEP_MODE, &bbuf ))
244+
{
245+
Serial.print("Sleep mode : ");
246+
if (byte(bbuf)==NONE_SLEEP_T)Serial.println("None");
247+
else if (byte(bbuf)==LIGHT_SLEEP_T)Serial.println("Light");
248+
else if (byte(bbuf)==MODEM_SLEEP_T)Serial.println("Modem");
249+
else Serial.println("???");
250+
}
251+
else Serial.println("Error reading sleep mode");
252+
if (CONFIG::read_byte(EP_CHANNEL, &bbuf ))
253+
{
254+
Serial.print("Channel : ");
255+
Serial.println(byte(bbuf));
256+
}
257+
else Serial.println("Error reading channel");
258+
259+
if (CONFIG::read_byte(EP_AUTH_TYPE, &bbuf ))
260+
{
261+
Serial.print("Authentification : ");
262+
if (byte(bbuf)==AUTH_OPEN)Serial.println("None");
263+
else if (byte(bbuf)==AUTH_WEP)Serial.println("WEP");
264+
else if (byte(bbuf)==AUTH_WPA_PSK)Serial.println("WPA");
265+
else if (byte(bbuf)==AUTH_WPA2_PSK)Serial.println("WPA2");
266+
else if (byte(bbuf)==AUTH_WPA_WPA2_PSK)Serial.println("WPA/WPA2");
267+
else if (byte(bbuf)==AUTH_MAX)Serial.println("Max");
268+
else Serial.println("???");
269+
}
270+
else Serial.println("Error reading authentification");
271+
if (CONFIG::read_byte(EP_SSID_VISIBLE, &bbuf ))
272+
{
273+
Serial.print("SSID visibility: ");
274+
if (bbuf==0)Serial.println("Hidden");
275+
else if (bbuf==1)Serial.println("Visible");
276+
else Serial.println(bbuf);
277+
}
278+
else Serial.println("Error reading SSID visibility");
279+
if (CONFIG::read_buffer(EP_WEB_PORT, (byte *)&ibuf , INTEGER_LENGTH))
280+
{
281+
Serial.print("Web port: ");
282+
Serial.println(ibuf);
283+
}
284+
else Serial.println("Error reading web port");
285+
if (CONFIG::read_buffer(EP_DATA_PORT, (byte *)&ibuf , INTEGER_LENGTH))
286+
{
287+
Serial.print("Data port: ");
288+
Serial.println(ibuf);
289+
}
290+
else Serial.println("Error reading data port");
291+
if (CONFIG::read_byte(EP_REFRESH_PAGE_TIME, &bbuf ))
292+
{
293+
Serial.print("Web page refresh time: ");
294+
Serial.println(byte(bbuf));
295+
}
296+
else Serial.println("Error reading refesh page");
297+
if (CONFIG::read_string(EP_HOSTNAME, sbuf , MAX_HOSTNAME_LENGTH))
298+
{
299+
Serial.print("Hostname : ");
300+
Serial.println(sbuf);
301+
}
302+
else Serial.println("Error reading hostname");
303+
304+
if (CONFIG::read_buffer(EP_XY_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))
305+
{
306+
Serial.print("XY feed rate: ");
307+
Serial.println(ibuf);
308+
}
309+
else Serial.println("Error reading XY feed rate");
310+
if (CONFIG::read_buffer(EP_Z_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))
311+
{
312+
Serial.print("Z feed rate: ");
313+
Serial.println(ibuf);
314+
}
315+
else Serial.println("Error reading Z feed rate");
316+
if (CONFIG::read_buffer(EP_E_FEEDRATE, (byte *)&ibuf , INTEGER_LENGTH))
317+
{
318+
Serial.print("E feed rate: ");
319+
Serial.println(ibuf);
320+
}
321+
else Serial.println("Error reading E feed rate");
322+
323+
Serial.print("Captive portal : ");
324+
#ifdef CAPTIVE_PORTAL_FEATURE
325+
Serial.println("Enabled");
326+
#else
327+
Serial.println("Disabled");
328+
#endif
329+
Serial.print("SSDP : ");
330+
#ifdef SSDP_FEATURE
331+
Serial.println("Enabled");
332+
#else
333+
Serial.println("Disabled");
334+
#endif
335+
Serial.print("mDNS : ");
336+
#ifdef MDNS
337+
Serial.println("Enabled");
338+
#else
339+
Serial.println("Disabled");
340+
#endif
191341
}

esp8266/esp8266.ino

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,6 @@ void setup() {
6262
// ESP.wdtDisable();
6363
system_update_cpu_freq(SYS_CPU_160MHZ);
6464
delay(8000);
65-
EEPROM.begin(EEPROM_SIZE);
6665
bool breset_config=false;
6766
//check if reset config is requested
6867
pinMode(RESET_CONFIG_PIN, INPUT);

0 commit comments

Comments
 (0)