Commit 9b0530ee authored by Dorian Zedler's avatar Dorian Zedler

added the ability to receive a new wifi configuration over USB (Serial0)

parent faf6465b
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
// - !! CAUTION Use ESP8266 Lib 2.3.0 NOT higher because of memory leak !! - // - !! CAUTION Use ESP8266 Lib 2.3.0 NOT higher because of memory leak !! -
// ------------------------------------------------------------------------- // -------------------------------------------------------------------------
#include <EEPROM.h>
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <ArduinoJson.h> #include <ArduinoJson.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
...@@ -33,10 +34,6 @@ ...@@ -33,10 +34,6 @@
// refresh interval (in ms) // refresh interval (in ms)
#define refreshInterval 300 #define refreshInterval 300
// --- network conf ---
const char* ssid = "ScStwAp";
const char* password = "12345678";
// connection data of the base station // connection data of the base station
const char* host = "192.168.4.1"; const char* host = "192.168.4.1";
const uint16_t port = 3563; const uint16_t port = 3563;
...@@ -60,6 +57,14 @@ typedef struct strSpeedTimerData { ...@@ -60,6 +57,14 @@ typedef struct strSpeedTimerData {
double reactTime; double reactTime;
} SpeedTimerDataT; } SpeedTimerDataT;
// struct to store wifi config
struct WiFiConfigData
{
char WiFiSSID[50]; // STATION /AP Point Name TO cONNECT, if definded
char WiFiPWD[50]; // WiFiPAssword, if definded
char ConfigValid[3]; //If Config is Vaild, Tag "TK" is required"
};
// --- helper vars --- // --- helper vars ---
SpeedTimerDataT timerData; SpeedTimerDataT timerData;
...@@ -69,6 +74,9 @@ int latestCycleTimerId = 0; ...@@ -69,6 +74,9 @@ int latestCycleTimerId = 0;
double timeOfLastCycle = 0; double timeOfLastCycle = 0;
bool sessionInited = false; bool sessionInited = false;
WiFiConfigData localWiFiConf;
String serialBuf = "";
WiFiClient client; WiFiClient client;
// == MAIN == // == MAIN ==
...@@ -92,11 +100,23 @@ void setup() { ...@@ -92,11 +100,23 @@ void setup() {
delay(70); delay(70);
} }
// read the WiFi configuration
Serial.println("Current WiFi config:");
if (loadWiFiConf(&localWiFiConf)) {
Serial.print("| SSID: ");
Serial.println(localWiFiConf.WiFiSSID);
}
else {
Serial.println("| No config found");
// load default conf
writeWiFiConf(&localWiFiConf, "ScStwAp", "12345678");
}
// init wifi // init wifi
WiFi.disconnect(true); WiFi.disconnect(true);
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.persistent(false); WiFi.persistent(false);
WiFi.begin(ssid, password); WiFi.begin(localWiFiConf.WiFiSSID, localWiFiConf.WiFiPWD);
} }
void loop() { void loop() {
...@@ -104,6 +124,8 @@ void loop() { ...@@ -104,6 +124,8 @@ void loop() {
// print the moving loader // print the moving loader
printLoadingScreen(); printLoadingScreen();
handleSerial();
if (WiFi.status() == WL_CONNECTED) { if (WiFi.status() == WL_CONNECTED) {
// wifi is connected -> try to connect to base station socket // wifi is connected -> try to connect to base station socket
Serial.print("connecting to "); Serial.print("connecting to ");
...@@ -123,6 +145,8 @@ void loop() { ...@@ -123,6 +145,8 @@ void loop() {
while (client.connected()) { while (client.connected()) {
// while we are connected to the base station // while we are connected to the base station
handleSerial();
if (!sessionInited) { if (!sessionInited) {
// if the session is not inited yet -> send a session init request // if the session is not inited yet -> send a session init request
if (!initSession()) { if (!initSession()) {
...@@ -133,6 +157,7 @@ void loop() { ...@@ -133,6 +157,7 @@ void loop() {
// wait for an update // wait for an update
while (client.available() == 0 && client.connected()) { while (client.available() == 0 && client.connected()) {
updateDisplay(); updateDisplay();
handleSerial();
} }
if(!client.connected()){ if(!client.connected()){
...@@ -144,6 +169,7 @@ void loop() { ...@@ -144,6 +169,7 @@ void loop() {
while (client.available()) { while (client.available()) {
char ch = static_cast<char>(client.read()); char ch = static_cast<char>(client.read());
response += ch; response += ch;
handleSerial();
} }
// clear buffer // clear buffer
...@@ -533,3 +559,112 @@ void printLoadingScreen() { ...@@ -533,3 +559,112 @@ void printLoadingScreen() {
// apply // apply
matrix.show(); matrix.show();
} }
void handleSerial() {
while (Serial.available()) {
digitalWrite(LED_BUILTIN, HIGH);
// get the new byte:
char inChar = (char)Serial.read();
// add it to the serialBuf:
serialBuf += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
serialBuf.replace("\n", "");
serialBuf.replace("\r", "");
if (serialBuf.startsWith("SetWifiConf=<ssid>") && serialBuf.endsWith("</pwd>") ) {
// wifi conf change was requested, it looks like: "SetWifiConf=<ssid>WiFiSSID</ssid>:<pwd>WiFiPWD</pwd>"
// remove useless stuff
serialBuf.replace("SetWifiConf=<ssid>", "");
serialBuf.replace("</pwd>", "");
// extract pwd
String ssid = serialBuf.substring( 0, serialBuf.indexOf("</ssid>:<pwd>") );
// extract ssid
String pwd = serialBuf.substring( serialBuf.indexOf("</ssid>:<pwd>") + 13, serialBuf.length());
// store to conf
char bufSSID[50];
char bufPWD[50];
pwd.toCharArray(bufPWD, 50);
ssid.toCharArray(bufSSID, 50);
if (strcmp(bufSSID, localWiFiConf.WiFiSSID) != 0 || strcmp(bufPWD, localWiFiConf.WiFiPWD) != 0) {
// if the creds did actually changed
writeWiFiConf(&localWiFiConf, bufSSID, bufPWD);
storeWiFiConf(&localWiFiConf);
Serial.println("OK200");
printDisplay("updte", true);
delay(5000);
ESP.restart();
}
Serial.println("OK302");
serialBuf = "";
break;
}
else {
Serial.println("ERR400");
serialBuf = "";
break;
}
}
}
}
bool loadWiFiConf(WiFiConfigData * targetConf)
{
bool RetValue;
EEPROM.begin(512);
EEPROM.get(0, *targetConf);
EEPROM.end();
if (strcmp(targetConf->ConfigValid, "TK") == 0)
{
RetValue = true;
} else
{
RetValue = false; // WLAN Settings not found.
}
return RetValue;
}
void writeWiFiConf(WiFiConfigData * srcConf, char WiFiSSID[50], char WiFiPWD[50])
{
byte len;
strncpy( srcConf->WiFiSSID, WiFiSSID, sizeof(srcConf->WiFiSSID) );
len = strlen(srcConf->WiFiSSID);
srcConf->WiFiSSID[len + 1] = '\0';
strncpy( srcConf->WiFiPWD, WiFiPWD, sizeof(srcConf->WiFiPWD) ); // no password
len = strlen(srcConf->WiFiPWD);
srcConf->WiFiPWD[len + 1] = '\0';
strncpy( srcConf->ConfigValid, "TK", sizeof(srcConf->ConfigValid) );
len = strlen(srcConf->ConfigValid);
srcConf->ConfigValid[len + 1] = '\0';
}
void storeWiFiConf(WiFiConfigData * srcConf)
{
// write conf to EEPROM
EEPROM.begin(512);
for (int i = 0 ; i < sizeof(*srcConf) ; i++)
{
EEPROM.write(i, 0);
}
strncpy( srcConf->ConfigValid , "TK", sizeof(srcConf->ConfigValid) );
EEPROM.put(0, *srcConf);
EEPROM.commit();
EEPROM.end();
}
...@@ -15,34 +15,47 @@ ...@@ -15,34 +15,47 @@
You should have received a copy of the GNU Affero General Public License You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>. along with this program. If not, see <https://www.gnu.org/licenses/>.
*/ */
#include <ESP8266WiFi.h> #include <ESP8266WiFi.h>
#include <EEPROM.h>
//#define DEBUG //#define DEBUG
#define INDICATOR_LED LED_BUILTIN
// Replace with your network credentials // struct to store wifi config
char* ssid = "ScStwAp"; struct WiFiConfigData
char* password = "12345678"; {
char WiFiSSID[50]; // STATION /AP Point Name TO cONNECT, if definded
char WiFiPWD[50]; // WiFiPAssword, if definded
char ConfigValid[3]; //If Config is Vaild, Tag "TK" is required"
};
// pins // pins
const byte interruptPin = D6; const byte interruptPin = D6;
const byte ledPin = LED_BUILTIN;
const byte laneSetPin = D1; // pin to determine if we are lane A or B (HIGH == A; LOW == B) const byte laneSetPin = D1; // pin to determine if we are lane A or B (HIGH == A; LOW == B)
const byte padTypeSetPin = D5; // pin to determine if we are a top- or startpad (LOW == toppad; HIGH == startpad) const byte padTypeSetPin = D5; // pin to determine if we are a top- or startpad (LOW == toppad; HIGH == startpad)
// enviroment vars // enviroment vars
bool thisIsToppad; bool thisIsToppad;
bool thisIsLaneA; bool thisIsLaneA;
WiFiConfigData localWiFiConf;
// time after trigger during which new triggers are ignored // time after trigger during which new triggers are ignored
volatile unsigned long debounceTime = 200; volatile unsigned long debounceTime = 200;
// to store last trigger time // to store last trigger time
unsigned long lastTriggered = 0; unsigned long lastTriggered = 0;
// buffer for Serial data reading
String serialBuf = "";
// socket server to talk to base station // socket server to talk to base station
WiFiServer server(80); WiFiServer server(80);
void setup() { void setup() {
// turn on LED to indicate no connection // turn on LED to indicate no connection
pinMode(ledPin, OUTPUT); pinMode(INDICATOR_LED, OUTPUT);
digitalWrite(ledPin, LOW); digitalWrite(INDICATOR_LED, LOW);
// init the modes of the pad // init the modes of the pad
pinMode(laneSetPin, INPUT_PULLUP); pinMode(laneSetPin, INPUT_PULLUP);
...@@ -54,10 +67,22 @@ void setup() { ...@@ -54,10 +67,22 @@ void setup() {
// init serial // init serial
Serial.begin(115200); Serial.begin(115200);
Serial.println(); Serial.println();
Serial.println("## ScStw Pad V2.0 - " Serial.println("## ScStw Pad V2.1 - "
+ String(thisIsToppad ? "TOPPAD" : "STARTPAD") + " - " + String(thisIsToppad ? "TOPPAD" : "STARTPAD") + " - "
+ String(thisIsLaneA ? "LaneA" : "LaneB") ); + String(thisIsLaneA ? "LaneA" : "LaneB") );
// read the WiFi configuration
Serial.println("Current WiFi config:");
if (loadWiFiConf(&localWiFiConf)) {
Serial.print("| SSID: ");
Serial.println(localWiFiConf.WiFiSSID);
}
else {
Serial.println("| No config found");
// load default conf
writeWiFiConf(&localWiFiConf, "ScStwAp", "12345678");
}
// configure static ip // configure static ip
IPAddress local_IP(192, 168, 4, (thisIsToppad ? (thisIsLaneA ? 10 : 12) : (thisIsLaneA ? 11 : 13))); IPAddress local_IP(192, 168, 4, (thisIsToppad ? (thisIsLaneA ? 10 : 12) : (thisIsLaneA ? 11 : 13)));
IPAddress gateway(192, 168, 4, 1); IPAddress gateway(192, 168, 4, 1);
...@@ -69,13 +94,14 @@ void setup() { ...@@ -69,13 +94,14 @@ void setup() {
// connect to wifi // connect to wifi
WiFi.mode(WIFI_STA); WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password); WiFi.begin(localWiFiConf.WiFiSSID, localWiFiConf.WiFiPWD);
Serial.print("Connecting to WiFi.."); Serial.println("Connecting to WiFi..");
// wait for wifi // wait for wifi
while (WiFi.status() != WL_CONNECTED) { while (WiFi.status() != WL_CONNECTED) {
delay(1000); handleSerial();
Serial.print("."); // turn LED on to indicate no connection
digitalWrite(INDICATOR_LED, LOW);
} }
Serial.println(""); Serial.println("");
...@@ -96,27 +122,33 @@ void setup() { ...@@ -96,27 +122,33 @@ void setup() {
} }
} }
void loop() { void loop()
{
// listen for clients // listen for clients
WiFiClient client = server.available(); WiFiClient client = server.available();
handleSerial();
if (client) { if (client) {
// turn LED off to indicate active connection // turn LED off to indicate active connection
digitalWrite(ledPin, HIGH); digitalWrite(INDICATOR_LED, HIGH);
String command = ""; String command = "";
Serial.println("Client connected"); Serial.println("Client connected");
while (client.connected()) { while (client.connected()) {
handleSerial();
// wait for incoming data // wait for incoming data
while (client.available() == 0 && client.connected()) { while (client.available() == 0 && client.connected()) {
handleSerial();
delay(10); delay(10);
} }
// read incomming command from client // read incomming command from client
while (client.available() > 0) { while (client.available() > 0) {
handleSerial();
char ch = static_cast<char>(client.read()); char ch = static_cast<char>(client.read());
if (ch >= '0') { if (ch >= '0') {
//ignore all non-valid characters //ignore all non-valid characters
...@@ -152,11 +184,12 @@ void loop() { ...@@ -152,11 +184,12 @@ void loop() {
} }
else { else {
// turn LED on to indicate no connection // turn LED on to indicate no connection
digitalWrite(ledPin, LOW); digitalWrite(INDICATOR_LED, LOW);
} }
} }
void handleInterrupt() { void handleInterrupt()
{
if ((millis() - lastTriggered) > debounceTime) { if ((millis() - lastTriggered) > debounceTime) {
// do nothing during debounce time // do nothing during debounce time
...@@ -168,3 +201,119 @@ void handleInterrupt() { ...@@ -168,3 +201,119 @@ void handleInterrupt() {
#endif #endif
} }
} }
void handleSerial()
{
while (Serial.available()) {
digitalWrite(LED_BUILTIN, HIGH);
// get the new byte:
char inChar = (char)Serial.read();
// add it to the serialBuf:
serialBuf += inChar;
// if the incoming character is a newline, set a flag so the main loop can
// do something about it:
if (inChar == '\n') {
serialBuf.replace("\n", "");
serialBuf.replace("\r", "");
if (serialBuf.startsWith("SetWifiConf=<ssid>") && serialBuf.endsWith("</pwd>") ) {
// wifi conf change was requested, it looks like: "SetWifiConf=<ssid>WiFiSSID</ssid>:<pwd>WiFiPWD</pwd>"
// remove useless stuff
serialBuf.replace("SetWifiConf=<ssid>", "");
serialBuf.replace("</pwd>", "");
// extract pwd
String ssid = serialBuf.substring( 0, serialBuf.indexOf("</ssid>:<pwd>") );
// extract ssid
String pwd = serialBuf.substring( serialBuf.indexOf("</ssid>:<pwd>") + 13, serialBuf.length());
// store to conf
char bufSSID[50];
char bufPWD[50];
pwd.toCharArray(bufPWD, 50);
ssid.toCharArray(bufSSID, 50);
if (strcmp(bufSSID, localWiFiConf.WiFiSSID) != 0 || strcmp(bufPWD, localWiFiConf.WiFiPWD) != 0) {
// if the creds did actually changed
writeWiFiConf(&localWiFiConf, bufSSID, bufPWD);
storeWiFiConf(&localWiFiConf);
Serial.println("OK200");
#ifdef INDICATOR_LED
for (int i = 0; i < 10; i++) {
digitalWrite(INDICATOR_LED, LOW);
delay(100);
digitalWrite(INDICATOR_LED, HIGH);
delay(100);
}
#endif
ESP.restart();
}
Serial.println("OK302");
serialBuf = "";
break;
}
else {
Serial.println("ERR400");
serialBuf = "";
break;
}
}
}
}
bool loadWiFiConf(WiFiConfigData * targetConf)
{
bool RetValue;
EEPROM.begin(512);
EEPROM.get(0, *targetConf);
EEPROM.end();
if (strcmp(targetConf->ConfigValid, "TK") == 0)
{
RetValue = true;
} else
{
RetValue = false; // WLAN Settings not found.
}
return RetValue;
}
void writeWiFiConf(WiFiConfigData * srcConf, char WiFiSSID[50], char WiFiPWD[50])
{
byte len;
strncpy( srcConf->WiFiSSID, WiFiSSID, sizeof(srcConf->WiFiSSID) );
len = strlen(srcConf->WiFiSSID);
srcConf->WiFiSSID[len + 1] = '\0';
strncpy( srcConf->WiFiPWD, WiFiPWD, sizeof(srcConf->WiFiPWD) ); // no password
len = strlen(srcConf->WiFiPWD);
srcConf->WiFiPWD[len + 1] = '\0';
strncpy( srcConf->ConfigValid, "TK", sizeof(srcConf->ConfigValid) );
len = strlen(srcConf->ConfigValid);
srcConf->ConfigValid[len + 1] = '\0';
}
void storeWiFiConf(WiFiConfigData * srcConf)
{
// write conf to EEPROM
EEPROM.begin(512);
for (int i = 0 ; i < sizeof(*srcConf) ; i++)
{
EEPROM.write(i, 0);
}
strncpy( srcConf->ConfigValid , "TK", sizeof(srcConf->ConfigValid) );
EEPROM.put(0, *srcConf);
EEPROM.commit();
EEPROM.end();
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment