Skip to content

Commit e4af195

Browse files
author
Luc
committed
Allow to disable TCP data feature
minor fixes: -Correct logic for web ports due to refactoring -Duplicate data read from serial if multiple clients -Improve reliability to reconnect to AP after a restart -Typo issues
1 parent 535f26c commit e4af195

File tree

7 files changed

+56
-14
lines changed

7 files changed

+56
-14
lines changed

esp8266/config.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -375,14 +375,16 @@ void CONFIG::print_config()
375375
} else {
376376
Serial.println(F("Error reading web port"));
377377
}
378-
378+
Serial.print(F("Data port: "));
379+
#ifdef TCP_IP_DATA_FEATURE
379380
if (CONFIG::read_buffer(EP_DATA_PORT, (byte *)&ibuf , INTEGER_LENGTH)) {
380-
Serial.print(F("Data port: "));
381381
Serial.println(ibuf);
382382
} else {
383383
Serial.println(F("Error reading data port"));
384384
}
385-
385+
#else
386+
Serial.println(F("Disabled"));
387+
#endif
386388
if (CONFIG::read_byte(EP_REFRESH_PAGE_TIME, &bbuf )) {
387389
Serial.print(F("Web page refresh time: "));
388390
Serial.println(byte(bbuf));

esp8266/config.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@
3838
//SERIAL_COMMAND_FEATURE: allow to send command by serial
3939
#define SERIAL_COMMAND_FEATURE
4040

41+
//TCP_IP_DATA_FEATURE: allow to connect serial from TCP/IP
42+
#define TCP_IP_DATA_FEATURE
43+
4144
#ifndef CONFIG_h
4245
#define CONFIG_h
4346

esp8266/data/system.tpl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ $SLEEP_MODE_OPTIONS_LIST$
2626
</select></div>
2727
<div class="form-group $WEB_PORT_STATUS$"><label class="control-label" for="CONFIG3">Web port:</label><br>
2828
<input type="number" class="form-control" id="CONFIG3" name="WEBPORT" min="1" max="65000" step="1" placeholder="1~65000" value="$WEB_PORT$" style="width: auto;"></div>
29-
<div class="form-group $DATA_PORT_STATUS$"><label class="control-label" for="CONFIG4">Data port:</label><br>
29+
<div class="form-group $DATA_PORT_STATUS$" style="$DATA_PORT_VISIBILITY$"><label class="control-label" for="CONFIG4">Data port:</label><br>
3030
<input type="number" class="form-control" id="CONFIG4" name="DATAPORT" min="1" max="65000" step="1" placeholder="1~65000" value="$DATA_PORT$" style="width: auto;"></div>
3131
<div class="alert alert-danger" role="alert" style="$ERROR_MSG_VISIBILITY$" >
3232
$ERROR_MSG$

esp8266/esp8266.ino

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ void setup()
6060
// init:
6161
web_interface = NULL;
6262
data_server = NULL;
63-
// ESP.wdtDisable();
63+
WiFi.disconnect();
6464
system_update_cpu_freq(SYS_CPU_160MHZ);
6565
delay(8000);
6666
bool breset_config=false;
@@ -117,17 +117,20 @@ void setup()
117117
wifi_config.Safe_Setup();
118118
}
119119
delay(1000);
120-
//start interfaces
120+
//start web interface
121121
web_interface = new WEBINTERFACE_CLASS(wifi_config.iweb_port);
122-
data_server = new WiFiServer (wifi_config.idata_port);
123122
//here the list of headers to be recorded
124123
const char * headerkeys[] = {"Cookie"} ;
125124
size_t headerkeyssize = sizeof(headerkeys)/sizeof(char*);
126125
//ask server to track these headers
127126
web_interface->WebServer.collectHeaders(headerkeys, headerkeyssize );
128127
web_interface->WebServer.begin();
128+
#ifdef TCP_IP_DATA_FEATURE
129+
//start TCP/IP interface
130+
data_server = new WiFiServer (wifi_config.idata_port);
129131
data_server->begin();
130132
data_server->setNoDelay(true);
133+
#endif
131134

132135
#ifdef MDNS_FEATURE
133136
// Check for any mDNS queries and send responses
@@ -177,6 +180,7 @@ void loop()
177180
web_interface->WebServer.handleClient();
178181
//TODO use a method to handle serial also in class and call it instead of this one
179182
uint8_t i,data;
183+
#ifdef TCP_IP_DATA_FEATURE
180184
//check if there are any new clients
181185
if (data_server->hasClient()) {
182186
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
@@ -206,19 +210,23 @@ void loop()
206210
}
207211
}
208212
}
213+
#endif
209214
//check UART for data
210215
if(Serial.available()) {
211216
size_t len = Serial.available();
212217
uint8_t sbuf[len];
213218
Serial.readBytes(sbuf, len);
219+
#ifdef TCP_IP_DATA_FEATURE
214220
//push UART data to all connected tcp clients
215221
for(i = 0; i < MAX_SRV_CLIENTS; i++) {
216222
if (serverClients[i] && serverClients[i].connected()) {
217223
serverClients[i].write(sbuf, len);
218224
delay(1);
219225
}
220-
COMMAND::read_buffer_serial(sbuf, len);
221226
}
227+
#endif
228+
//process data if any
229+
COMMAND::read_buffer_serial(sbuf, len);
222230
}
223231
if (web_interface->restartmodule) {
224232
Serial.flush();

esp8266/webinterface.cpp

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,7 @@ const char MISSING_DATA [] PROGMEM = "Error: Missing data";
206206
const char EEPROM_NOWRITE [] PROGMEM = "Error: Cannot write to EEPROM";
207207
const char KEY_WEB_UPDATE [] PROGMEM = "$WEB_UPDATE_VISIBILITY$";
208208
const char KEY_STA_SIGNAL [] PROGMEM = "$STA_SIGNAL$";
209+
const char KEY_DATA_PORT_VISIBILITY [] PROGMEM = "$DATA_PORT_VISIBILITY$";
209210

210211
bool WEBINTERFACE_CLASS::isHostnameValid(const char * hostname)
211212
{
@@ -594,7 +595,14 @@ void GetPorts(STORESTRINGS_CLASS & KeysList, STORESTRINGS_CLASS & ValuesList)
594595
ValuesList.add(intTostr(wifi_config.iweb_port));
595596
//Data port
596597
KeysList.add(FPSTR(KEY_DATA_PORT));
598+
KeysList.add(FPSTR(KEY_DATA_PORT_VISIBILITY));
599+
#ifdef TCP_IP_DATA_FEATURE
597600
ValuesList.add(intTostr(wifi_config.idata_port));
601+
ValuesList.add(FPSTR(VALUE_ITEM_VISIBLE));
602+
#else
603+
ValuesList.add(FPSTR(VALUE_NONE));
604+
ValuesList.add(FPSTR(VALUE_ITEM_HIDDEN));
605+
#endif
598606
}
599607
// -----------------------------------------------------------------------------
600608
// Helper for Page properties
@@ -1060,11 +1068,18 @@ void handle_web_interface_configSys()
10601068
//check is it is a submission or a display
10611069
if (web_interface->WebServer.hasArg("SUBMIT")) {
10621070
//is there a correct list of values?
1063-
if (web_interface->WebServer.hasArg("BAUD_RATE") && web_interface->WebServer.hasArg("SLEEP_MODE")&& web_interface->WebServer.hasArg("DATAPORT")&& web_interface->WebServer.hasArg("WEBPORT")) {
1071+
if (web_interface->WebServer.hasArg("BAUD_RATE")
1072+
&& web_interface->WebServer.hasArg("SLEEP_MODE")
1073+
#ifdef TCP_IP_DATA_FEATURE
1074+
&& web_interface->WebServer.hasArg("DATAPORT")
1075+
#endif
1076+
&& web_interface->WebServer.hasArg("WEBPORT")) {
10641077
//is each value correct ?
10651078
ibaud = web_interface->WebServer.arg("BAUD_RATE").toInt();
10661079
iweb_port = web_interface->WebServer.arg("WEBPORT").toInt();
1080+
#ifdef TCP_IP_DATA_FEATURE
10671081
idata_port = web_interface->WebServer.arg("DATAPORT").toInt();
1082+
#endif
10681083
bsleepmode = web_interface->WebServer.arg("SLEEP_MODE").toInt();
10691084

10701085
if (!(iweb_port>0 && iweb_port<65001)) {
@@ -1073,12 +1088,14 @@ void handle_web_interface_configSys()
10731088
KeysList.add(FPSTR(KEY_WEB_PORT_STATUS));
10741089
ValuesList.add(FPSTR(VALUE_HAS_ERROR));
10751090
}
1091+
#ifdef TCP_IP_DATA_FEATURE
10761092
if (!(idata_port>0 && idata_port<65001)) {
10771093
msg_alert_error=true;
10781094
smsg.concat("Error: invalid port value for data port<BR>");
10791095
KeysList.add(FPSTR(KEY_DATA_PORT_STATUS));
10801096
ValuesList.add(FPSTR(VALUE_HAS_ERROR));
10811097
}
1098+
#endif
10821099
if (iweb_port== idata_port) {
10831100
msg_alert_error=true;
10841101
smsg.concat("Error: web port and data port cannot be identical<BR>");
@@ -1105,12 +1122,19 @@ void handle_web_interface_configSys()
11051122
}
11061123
//if no error apply the changes
11071124
if (msg_alert_error!=true) {
1108-
if(!CONFIG::write_buffer(EP_BAUD_RATE,(const byte *)&ibaud,INTEGER_LENGTH)||!CONFIG::write_buffer(EP_WEB_PORT,(const byte *)&iweb_port,INTEGER_LENGTH)||!CONFIG::write_buffer(EP_DATA_PORT,(const byte *)&idata_port,INTEGER_LENGTH)||!CONFIG::write_byte(EP_SLEEP_MODE,bsleepmode)) {
1125+
if(!CONFIG::write_buffer(EP_BAUD_RATE,(const byte *)&ibaud,INTEGER_LENGTH)
1126+
||!CONFIG::write_buffer(EP_WEB_PORT,(const byte *)&iweb_port,INTEGER_LENGTH)
1127+
#ifdef TCP_IP_DATA_FEATURE
1128+
||!CONFIG::write_buffer(EP_DATA_PORT,(const byte *)&idata_port,INTEGER_LENGTH)
1129+
#endif
1130+
||!CONFIG::write_byte(EP_SLEEP_MODE,bsleepmode)) {
11091131
msg_alert_error=true;
11101132
smsg = FPSTR(EEPROM_NOWRITE);
11111133
} else {
11121134
msg_alert_success=true;
1135+
#ifdef TCP_IP_DATA_FEATURE
11131136
wifi_config.iweb_port=iweb_port;
1137+
#endif
11141138
wifi_config.idata_port=idata_port;
11151139
smsg = F("Changes saved to EEPROM, restarting....");
11161140
}
@@ -1125,10 +1149,12 @@ void handle_web_interface_configSys()
11251149
if (!CONFIG::read_buffer(EP_WEB_PORT, (byte *)&iweb_port , INTEGER_LENGTH)) {
11261150
iweb_port=DEFAULT_WEB_PORT;
11271151
}
1152+
wifi_config.iweb_port=iweb_port;
11281153
if (!CONFIG::read_buffer(EP_DATA_PORT, (byte *)&idata_port , INTEGER_LENGTH)) {
11291154
idata_port=DEFAULT_DATA_PORT;
11301155
}
1131-
}
1156+
wifi_config.idata_port=idata_port;
1157+
};
11321158
//Baud rate list
11331159
istatus = 0;
11341160
stmp="";

esp8266/wifi.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ bool WIFI_CONFIG::Setup()
215215
return false;
216216
}
217217
wifi_set_phy_mode((phy_mode)bflag);
218+
delay(500);
218219
byte i=0;
219220
//try to connect
220221
while (WiFi.status() != WL_CONNECTED && i<40) {

keywords.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -17,18 +17,19 @@ $CHIP_ID$ : Chip ID
1717
$CPU_FREQ$ : CPU Frequency
1818
$FREE_MEM$ : Free memory on heap
1919
$SDK_VER$ : SDK version
20-
$MDNS_VISIBLE$: set to hidden is no MDNS and visible if present
20+
$MDNS_VISIBLE$: set to hidden if no MDNS and visible if present
2121
$MDNS_NAME$ : mDNS name if enabled or "Not enabled" if not enabled
22-
$SSDP_VISIBLE$ : set to hidden is no MDNS and visible if present
22+
$SSDP_VISIBLE$ : set to hidden if no MDNS and visible if present
2323
$SSDP_STATUS$ : set to Enabled / Not enabled according compilation settings
24-
$CAPTIVE_PORTAL_VISIBLE$: set to hidden is no Captive portal and visible if present
24+
$CAPTIVE_PORTAL_VISIBLE$: set to hidden if no Captive portal and visible if present
2525
$CAPTIVE_PORTAL_STATUS$: set to Enabled / Not enabled according compilation settings
2626
$NET_PHY$ : Network type (b/g/n)
2727
$SLEEP_MODE$ : Sleep Mode
2828
$BOOT_VER$ : Boot version
2929
$BAUD_RATE$ : Baud rates for serial communication
3030
$WEB_PORT$ : Port for web access
3131
$DATA_PORT$ : Port for tcp ip connection
32+
$DATA_PORT_VISIBILITY$ : set to hidden if no enabled and visible if enabled
3233

3334
$AP_STATUS_ENABLED$ : is Access Point enabled or disabled
3435
$AP_VISIBILITY$ : if Access Point is enabled set visible, else set to hidden
@@ -72,6 +73,7 @@ $SLEEP_MODE_OPTIONS_LIST$ : Sleep mode list
7273
$POLLING_OPTIONS_LIST$ : Refresh delay list
7374
$WEB_PORT$ : Port for web access
7475
$DATA_PORT$ : Port for tcp ip connection
76+
$DATA_PORT_VISIBILITY$ : set to hidden if no enabled and visible if enabled
7577
$ERROR_MSG$ : Error message if any
7678
$SUCCESS_MSG$ : Success message announcing restart
7779
$ERROR_MSG_VISIBILITY$ : Show/Hide Error message

0 commit comments

Comments
 (0)