Skip to content

Commit a06f11d

Browse files
author
dingo35
committed
esp32.h, glcd.cpp, network_common.cpp: setup rudimentary AP instead of SmartConfig
1 parent 19cfe07 commit a06f11d

File tree

4 files changed

+75
-184
lines changed

4 files changed

+75
-184
lines changed

SmartEVSE-3/src/esp32.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -98,7 +98,6 @@ extern portMUX_TYPE rtc_spinlock; //TODO: Will be placed in the appropriate po
9898
#define RTC_EXIT_CRITICAL() portEXIT_CRITICAL(&rtc_spinlock)
9999

100100

101-
extern char SmartConfigKey[];
102101
extern struct tm timeinfo;
103102

104103

SmartEVSE-3/src/glcd.cpp

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -87,8 +87,8 @@ uint32_t ScrollTimer = 0;
8787

8888
extern void CheckSwitch(bool force = false);
8989
extern void handleWIFImode(void *s = &Serial);
90-
extern char SmartConfigKey[16];
9190
extern Button ExtSwitch;
91+
extern String APpassword;
9292
unsigned char activeRow;
9393
extern Switch_Phase_t Switching_Phases_C2;
9494
extern uint8_t RCMTestCounter;
@@ -500,19 +500,17 @@ void GLCD(void) {
500500
GLCD_write_buf_str(127,0, Str, GLCD_ALIGN_RIGHT);
501501
} else GLCD_write_buf_str(0,0, "Not connected to WiFi", GLCD_ALIGN_LEFT);
502502

503-
// When Wifi Setup is selected, show AES key for the ESPTouch app
503+
// When Wifi Setup is selected, show password and SSID of the Access Point
504504
} else if (WIFImode == 2) {
505505
if (SubMenu && WiFi.getMode() != WIFI_AP_STA) { // Do not show if AP_STA mode is started
506-
sprintf(Str, "O button starts config");
506+
sprintf(Str, "O button starts portal");
507507
GLCD_write_buf_str(0,0, Str, GLCD_ALIGN_LEFT);
508508
} else {
509-
// Show ESPTouch key
510-
sprintf(Str, "Key:%s", SmartConfigKey);
511-
GLCD_write_buf_str(0, 0, Str, GLCD_ALIGN_LEFT);
512-
GLCD_sendbuf(7, 1);
513-
GLCD_buffer_clr();
514-
sprintf(Str, "Now use EspTouch app ");
515-
GLCD_write_buf_str(0, 0, Str, GLCD_ALIGN_LEFT);
509+
// Show Access Point password
510+
//sprintf(Str, "AP:SmartEVSE-config");
511+
//GLCD_write_buf_str(0,0, Str, GLCD_ALIGN_LEFT);
512+
sprintf(Str, "Portal PW:%s", APpassword.c_str());
513+
GLCD_write_buf_str(0,0, Str, GLCD_ALIGN_LEFT);
516514
}
517515
}
518516
}

SmartEVSE-3/src/network_common.cpp

Lines changed: 61 additions & 169 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@ struct mg_mgr mgr; // Mongoose event manager. Holds all connections
3333
// end of mongoose stuff
3434

3535
String APhostname = "SmartEVSE-" + String( MacId() & 0xffff, 10); // SmartEVSE access point Name = SmartEVSE-xxxxx
36+
String APpassword = "00000000";
3637

3738
#if MQTT && defined(SMARTEVSE_VERSION) // ESP32 only
3839
// MQTT connection info
@@ -62,92 +63,12 @@ uint32_t serialnr;
6263

6364
// The following data will be updated by eeprom/storage data at powerup:
6465
uint8_t WIFImode = WIFI_MODE; // WiFi Mode (0:Disabled / 1:Enabled / 2:Start Portal)
65-
char SmartConfigKey[] = "0123456789abcdef"; // SmartConfig / EspTouch AES key, used to encyrypt the WiFi password.
6666
String TZinfo = ""; // contains POSIX time string
6767

6868
char *downloadUrl = NULL;
6969
int downloadProgress = 0;
7070
int downloadSize = 0;
7171

72-
static uint8_t CliState = 0;
73-
#ifdef SENSORBOX_VERSION
74-
void ProvisionCli(HardwareSerial &s) {
75-
//void ProvisionCli(HardwareSerial &s = &Serial) {
76-
#else
77-
#if SMARTEVSE_VERSION >=30 && SMARTEVSE_VERSION < 40
78-
void ProvisionCli(void) {
79-
HardwareSerial &s = Serial;
80-
#else
81-
void ProvisionCli(HWCDC &s = Serial) {
82-
#endif
83-
#endif
84-
// SSID and PW for your Router
85-
static String Router_SSID, Router_Pass;
86-
static char CliBuffer[64];
87-
static uint8_t idx = 0;
88-
static bool entered = false;
89-
char ch;
90-
91-
if (CliState == 0) {
92-
s.println("Enter WiFi access point name:");
93-
CliState++;
94-
95-
} else if (CliState == 1 && entered) {
96-
Router_SSID = String(CliBuffer);
97-
Router_SSID.trim(); //SSID has no limitations on special characters, so dnt check them
98-
CliState++; // All OK, now request password.
99-
idx = 0;
100-
entered = false;
101-
102-
} else if (CliState == 2) {
103-
s.println("Enter WiFi password:");
104-
CliState++;
105-
106-
} else if (CliState == 3 && entered) {
107-
Router_Pass = String(CliBuffer);
108-
Router_Pass.trim();
109-
if (idx < 8) {
110-
s.println("Password should be min 8 characters.");
111-
Router_Pass = "";
112-
CliState = 2;
113-
} else CliState++; // All OK
114-
idx = 0;
115-
entered = false;
116-
117-
} else if (CliState == 4) {
118-
s.println("WiFi credentials stored.");
119-
WiFi.mode(WIFI_STA); // Set Station Mode
120-
WiFi.begin(Router_SSID, Router_Pass); // Configure Wifi with credentials
121-
CliState++;
122-
}
123-
124-
125-
// read input, and store in buffer until we read a \n
126-
while (s.available()) {
127-
ch = s.read();
128-
129-
// When entering a password, replace last character with a *
130-
if (CliState == 3 && idx) s.printf("\b*");
131-
s.print(ch);
132-
133-
// check for CR/LF, and make sure the contents of the buffer is atleast 1 character
134-
if (ch == '\n' || ch == '\r') {
135-
if (idx) {
136-
CliBuffer[idx] = 0; // null terminate
137-
entered = true;
138-
} else if (CliState == 1 || CliState == 3) CliState--; // Reprint the last message
139-
} else if (idx < 63) { // Store in buffer
140-
if (ch == '\b' && idx) {
141-
idx--;
142-
s.print(" \b"); // erase character from terminal
143-
} else {
144-
CliBuffer[idx++] = ch;
145-
}
146-
}
147-
}
148-
}
149-
150-
15172
#if MQTT && defined(SMARTEVSE_VERSION) // ESP32 only
15273
#if MQTT_ESP == 1
15374
/*
@@ -966,6 +887,26 @@ static void timer_fn(void *arg) {
966887
}
967888
#endif
968889

890+
891+
// HTML web form for entering WIFI credentials in AP setup portal
892+
static const char *html_form = R"EOF(
893+
<!DOCTYPE html><html><head><title>WiFi Setup</title>
894+
<script>
895+
function togglePassword(){
896+
var x = document.getElementById('password');
897+
x.type = x.type === 'password' ? 'text' : 'password';
898+
}
899+
</script></head><body>
900+
<h2>WiFi Configuration</h2>
901+
<form action="/save" method="POST">
902+
SSID:<br><input type="text" name="ssid"><br>
903+
Password:<br><input type="password" name="password" id="password"><br>
904+
<input type="checkbox" onclick="togglePassword()"> Show Password<br><br>
905+
<input type="submit" value="Save">
906+
</form></body></html>
907+
)EOF";
908+
909+
969910
// Connection event handler function
970911
// indenting lower level two spaces to stay compatible with old StartWebServer
971912
// We use the same event handler function for HTTP and HTTPS connections
@@ -1002,6 +943,25 @@ static void fn_http_server(struct mg_connection *c, int ev, void *ev_data) {
1002943
}
1003944
shouldReboot = true;
1004945
mg_http_reply(c, 200, "Content-Type: text/plain\r\n", "Erasing settings, rebooting");
946+
} else if (mg_http_match_uri(hm, "/") && WIFImode == 2) { // serve AP page to fill in WIFI credentials
947+
mg_http_reply(c, 200, "Content-Type: text/html\r\n", "%s", html_form);
948+
} else if (mg_http_match_uri(hm, "/save")) {
949+
char ssid[64], password[64];
950+
bool has_ssid = mg_http_get_var(&hm->body, "ssid", ssid, sizeof(ssid)) > 0;
951+
bool has_pass = mg_http_get_var(&hm->body, "password", password, sizeof(password)) > 0;
952+
if (has_ssid && has_pass) {
953+
mg_http_reply(c, 200, "Content-Type: text/html\r\n", "<html><body><h2>Saved! Rebooting...</h2></body></html>");
954+
delay(2000);
955+
_LOG_A("Connecting to wifi network.\n");
956+
WiFi.mode(WIFI_STA); // Set Station Mode
957+
WiFi.begin(ssid, password); // Configure Wifi with credentials
958+
WIFImode = 1; // we are already connected so don't call handleWIFImode
959+
write_settings();
960+
delay(2000);
961+
ESP.restart();
962+
} else {
963+
mg_http_reply(c, 400, "", "Missing SSID or password");
964+
}
1005965
} else if (mg_http_match_uri(hm, "/autoupdate")) {
1006966
char owner[40];
1007967
char buf[8];
@@ -1382,88 +1342,29 @@ void onWifiEvent(WiFiEvent_t event, WiFiEventInfo_t info) {
13821342
}
13831343

13841344

1385-
void SetupPortalTask(void * parameter) {
1386-
#ifdef SENSORBOX_VERSION
1387-
HardwareSerial *s1 = *((HardwareSerial **)parameter);
1388-
HardwareSerial &s = *s1;
1389-
#endif
1390-
_LOG_A("Start Portal...\n");
1391-
WiFi.disconnect(true);
1392-
1393-
// Close Mongoose HTTP Server
1394-
if (HttpListener80) {
1395-
HttpListener80->is_closing = 1;
1396-
}
1397-
if (HttpListener443) {
1398-
HttpListener443->is_closing = 1;
1399-
}
1400-
1401-
while (HttpListener80 || HttpListener443) {
1402-
vTaskDelay(1000 / portTICK_PERIOD_MS);
1403-
_LOG_A("Waiting for Mongoose Server to terminate\n");
1404-
}
1405-
1406-
//Init WiFi as Station, start SmartConfig
1407-
WiFi.mode(WIFI_AP_STA);
1408-
WiFi.beginSmartConfig(SC_TYPE_ESPTOUCH_V2, SmartConfigKey);
1409-
1410-
//Wait for SmartConfig packet from mobile.
1411-
_LOG_V("Waiting for SmartConfig.\n");
1412-
#ifdef SENSORBOX_VERSION
1413-
s.end();
1414-
s.begin(115200, SERIAL_8N1, PIN_RXD, PIN_TXD, false); // Input from TX of PIC, and debug output to USB
1415-
#endif
1416-
unsigned long configTimer = millis();
1417-
while (!WiFi.smartConfigDone() && (WIFImode == 2) && (WiFi.status() != WL_CONNECTED) && millis() - configTimer < 180000) {
1418-
// Also start Serial CLI for entering AP and password.
1419-
#ifdef SENSORBOX_VERSION
1420-
ProvisionCli(s);
1421-
#else
1422-
ProvisionCli();
1423-
#endif
1424-
delay(100);
1425-
} // loop until connected or Wifi setup menu is exited.
1426-
delay(2000); // give smartConfig time to send provision status back to the users phone.
1427-
1428-
if (WiFi.status() == WL_CONNECTED) {
1429-
_LOG_V("\nWiFi Connected, IP Address:%s.\n", WiFi.localIP().toString().c_str());
1430-
WIFImode = 1; // we are already connected so don't call handleWIFImode
1431-
} else {
1432-
_LOG_V("\nCould not connect to WiFi, giving up.\n");
1433-
WIFImode = 0;
1434-
handleWIFImode();
1435-
}
1436-
write_settings();
1437-
CliState= 0;
1438-
#ifndef SENSORBOX_VERSION //so we are not on a sensorbox but on a smartevse
1439-
#if SMARTEVSE_VERSION >=30
1440-
LCDNav = 0;
1441-
#endif
1442-
#else
1443-
s.end();
1444-
if (s == Serial2) {
1445-
Serial2.setRxBufferSize(2048); // Important! first increase buffer, then setup Uart2
1446-
Serial2.begin(115200, SERIAL_8N1, PIN_RX, -1, true);
1447-
Serial.begin(115200, SERIAL_8N1, PIN_PGD, PIN_TXD, false);
1448-
} else
1449-
s.begin(115200, SERIAL_8N1, PIN_PGD, PIN_TXD, false); // Input from TX of PIC, and debug output to USB
1450-
#endif
1451-
WiFi.stopSmartConfig(); // this makes sure repeated SmartConfig calls are succesfull
1452-
vTaskDelete(NULL); //end this task so it will not take up resources
1453-
}
1454-
1455-
14561345
void handleWIFImode(void *s) {
14571346
if (WIFImode == 2 && WiFi.getMode() != WIFI_AP_STA) {
1458-
//now start the portal in the background, so other tasks keep running
1459-
xTaskCreate(
1460-
SetupPortalTask, // Function that should be called
1461-
"SetupPortalTask", // Name of the task (for debugging)
1462-
10000, // Stack size (bytes) // printf needs atleast 1kb
1463-
(void *) &s, // Parameter: which serial interface to use for ProvisionCli
1464-
3, // Task priority - medium
1465-
NULL // Task handleCTReceive
1466-
);
1347+
_LOG_A("Start Portal...\n");
1348+
1349+
// set random AP password
1350+
uint8_t i, c;
1351+
for (i=0; i<8 ;i++) {
1352+
c = random(16) + '0';
1353+
if (c > '9') c += 'a'-'9'-1;
1354+
APpassword[i] = c;
1355+
}
1356+
1357+
// Start WiFi as AP
1358+
WiFi.softAP("SmartEVSE-config", APpassword);
1359+
IPAddress IP = WiFi.softAPIP();
1360+
1361+
if (!HttpListener80) {
1362+
HttpListener80 = mg_http_listen(&mgr, "http://0.0.0.0:80", fn_http_server, NULL); // Setup listener
1363+
}
1364+
if (!HttpListener443) {
1365+
HttpListener443 = mg_http_listen(&mgr, "http://0.0.0.0:443", fn_http_server, (void *) 1); // Setup listener
1366+
}
1367+
_LOG_A("HTTP server started\n");
14671368
}
14681369

14691370
if (WIFImode == 1 && WiFi.getMode() == WIFI_OFF) {
@@ -1525,15 +1426,6 @@ void WiFiSetup(void) {
15251426
//WiFi.persistent(true);
15261427
WiFi.onEvent(onWifiEvent);
15271428

1528-
1529-
// Set random AES Key for SmartConfig provisioning, first 8 positions are 0
1530-
// This key is displayed on the LCD, and should be entered when using the EspTouch app.
1531-
#ifndef SENSORBOX_VERSION
1532-
for (uint8_t i=0; i<8 ;i++) {
1533-
SmartConfigKey[i+8] = random(9) + '1';
1534-
}
1535-
#endif
1536-
15371429
if (preferences.begin("settings", false) ) {
15381430
TZinfo = preferences.getString("TimezoneInfo","");
15391431
if (TZinfo != "") {

docs/configuration.md

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -133,9 +133,9 @@ Enable Wifi connection to your network.
133133

134134
- **Disabled**: Wifi connection is disabled.
135135
- **SetupWifi**
136-
- v3.6.3 or older:
137-
The SmartEVSE presents itself as a Wifi Acces Point with SSID "smartevse-xxxx". Connect with your phone to that access point, go to [http://192.168.4.1/](http://192.168.4.1/) and configure your Wifi SSID and key (password).
138-
- v.3.6.4 and newer:
136+
- v3.9.0 or newer:
137+
The SmartEVSE presents itself as a Wifi Acces Point with SSID "SmartEVSE-config"; the password is displayed at the top line of your LCD display. Connect with your phone to that access point, go to [http://192.168.4.1/](http://192.168.4.1/) and configure your Wifi SSID and password.
138+
- v3.6.4 until v3.8.x:
139139
- Connect your smartphone to the wifi network you want your SmartEVSE connected to.
140140
Note: If you have a multi AP setup, with the same SSID, you need to be connected to the desired AP, as the configuration is based on the BSSID, so it will choos the specific AP your phone is connected to.
141141
- Download and run the ESPTouch app from your favorite app store [Android](https://play.google.com/store/apps/details?id=com.fyent.esptouch.android&hl=en_US:) (please ignore the strange Author name) or [Apple](https://apps.apple.com/us/app/espressif-esptouch/id1071176700) or [Github](https://github.com/EspressifApp/EsptouchForAndroid) (for source code).
@@ -150,7 +150,7 @@ Note: If you have a multi AP setup, with the same SSID, you need to be connected
150150
- Press **Confirm**, within 30 seconds the app will confirm a MAC address and an IP address.
151151
- You are connected now. If you want special stuff (static IP address, special DNS address), configure them on your AP/router.
152152

153-
- v3.6.4 and newer BACKUP PROCEDURE: if you don't get it to work with the ESPTouch app, there is a backup procedure:
153+
- v3.6.4 and until v3.8.x: BACKUP PROCEDURE: if you don't get it to work with the ESPTouch app, there is a backup procedure:
154154
- connect your SmartEVSE with a USB cable to your PC
155155
- install the USB driver (Windows) or not (Linux) for ESP32 chipset
156156
- connect your favorite serial terminal to the appropriate port,
@@ -163,6 +163,8 @@ Note: If you have a multi AP setup, with the same SSID, you need to be connected
163163
- press the middle button to start the configuration procedure
164164
- on your terminal window you should see a request to enter your WiFi access point SSID and password.
165165
- the controller should now connect to WiFi.
166+
- v3.6.3 or older:
167+
The SmartEVSE presents itself as a Wifi Acces Point with SSID "smartevse-xxxx". Connect with your phone to that access point, go to [http://192.168.4.1/](http://192.168.4.1/) and configure your Wifi SSID and key (password).
166168
- **Enabled**: Connect to your network via Wifi.
167169

168170
## AUTOUPDAT

0 commit comments

Comments
 (0)