From d5831f9119f2a33bac08598a4ec6fd36a27d910a Mon Sep 17 00:00:00 2001 From: Baggy <38911677+BaggyLala@users.noreply.github.com> Date: Sun, 3 May 2020 17:49:26 +0100 Subject: [PATCH] Addition of a Web interface to OpenBeerFiller This brach is utilising the Arduino UNO for the main machine controller and the parameters will be able to be modified/configured by the addition of a second "Wifi Bridge" controller - NodeMCU ESP8266 --- OBFWeb/OBFWeb.ino | 300 +++++++++++++++++++++++++++++++ OBFWeb/Readme.md | 6 + OBFWeb/data/index.html | 376 +++++++++++++++++++++++++++++++++++++++ OBFWeb/data/wconfig.json | 9 + 4 files changed, 691 insertions(+) create mode 100644 OBFWeb/OBFWeb.ino create mode 100644 OBFWeb/Readme.md create mode 100644 OBFWeb/data/index.html create mode 100644 OBFWeb/data/wconfig.json diff --git a/OBFWeb/OBFWeb.ino b/OBFWeb/OBFWeb.ino new file mode 100644 index 0000000..de56dc4 --- /dev/null +++ b/OBFWeb/OBFWeb.ino @@ -0,0 +1,300 @@ + +#include +#include +#include +#include +#include + +#ifndef APSSID +#define APSSID "OBF_AP" +#define APPSK "vacantgesture" +#endif +const char *APssid = APSSID; +const char *APpassword = APPSK; +int WifiTry =40; + +ESP8266WebServer server; +uint8_t pin_led = 2; +String ssid = "WifiAP"; +String password = "mypassword"; +int t1wait = 0; +int t2wait = 0; +int t3wait = 0; +int t4wait = 0; +int t5wait = 0; +int t6wait = 0; + +//Websockets section +String JSONtxt; +WebSocketsServer webSocket=WebSocketsServer(88); +boolean bEventtrig = false; +#define CONFIG_FILENAME "\wconfig.json" + + + +void setup() +{ + Serial.begin(115200); + + SPIFFS.begin(); + pinMode(pin_led, OUTPUT); + + loadConfig(); + + + WiFi.mode(WIFI_STA); + WiFi.begin(ssid,password); + + + while((WiFi.status()!=WL_CONNECTED) & (WifiTry>0)) + { + WifiTry--; + Serial.print("."); + delay(500); + }; + + if (WifiTry ==0) { + WiFi.mode(WIFI_AP); + WiFi.softAP(APssid, APpassword); + IPAddress myIP = WiFi.softAPIP(); + Serial.print("AP IP address: "); + Serial.println(myIP); + }; + + Serial.println(""); + Serial.print("IP Address: "); + Serial.println(WiFi.localIP()); + + server.on("/",serveIndexFile); + +// server.on("/t1q",t1q); +// server.on("/t2q",t2q); +// server.on("/t3q",t3q); +// server.on("/t4q",t4q); +// server.on("/t5q",t5q); +// server.on("/t6q",t6q); + server.on("/json",whatsmyjsonsay); + server.on("/save",saveConfig); + + server.begin(); + +//Start Websocket Server + webSocket.begin(); + webSocket.onEvent(webSocketEvent); + + + +} + +void loop() +{ + server.handleClient(); + webSocket.loop(); + + JSONtxt="{\"ssid\":\""+ssid+"\","+ // JSON requires double quotes + "\"password\":\""+password+"\","+ + "\"t1wait\":\""+(String)t1wait+"\","+ + "\"t2wait\":\""+(String)t2wait+"\","+ + "\"t3wait\":\""+(String)t3wait+"\","+ + "\"t4wait\":\""+(String)t4wait+"\","+ + "\"t5wait\":\""+(String)t5wait+"\","+ + "\"t6wait\":\""+(String)t6wait+"\","+ + "\"t6wait\":\""+String(t6wait)+"\"}"; + // if there has been a change send back my latest info + if (bEventtrig) {webSocket.broadcastTXT(JSONtxt);bEventtrig=false; Serial.println("sending via websocket");}; + if (bEventtrig) {Serial.println(JSONtxt);}; + if (bEventtrig) {Serial.println("bEventtrig = true");}; + +} + +void serveIndexFile() +{ + File file = SPIFFS.open("/index.html","r"); + server.streamFile(file, "text/html"); + file.close(); + bEventtrig = true; +} + +void whatsmyjsonsay() +{ + File file = SPIFFS.open("/wconfig.json","r"); + server.streamFile(file, "text/html"); + file.close(); +} + + + +void t1q() +{ + server.send(200,"text/plain", String(t1wait)); + Serial.println("t1wait set to "+ String(t1wait)); +} + +void t2q() +{ + server.send(200,"text/plain", String(t2wait)); + Serial.println("t2wait set to "+ String(t2wait)); +} + +void t3q() +{ + server.send(200,"text/plain", String(t3wait)); + Serial.println("t3wait set to "+ String(t3wait)); +} + +void t4q() +{ + server.send(200,"text/plain", String(t4wait)); + Serial.println("t4wait set to "+ String(t4wait)); +} + +void t5q() +{ + server.send(200,"text/plain", String(t5wait)); + Serial.println("t5wait set to "+ String(t5wait)); +} + +void t6q() +{ + server.send(200,"text/plain", String(t6wait)); + Serial.println("t6wait set to "+ String(t6wait)); +} + +bool loadConfig() { + File configFile = SPIFFS.open("/wconfig.json", "r"); + if (!configFile) { + Serial.println("Failed to open config file"); + return false; + } + + size_t size = configFile.size(); + if (size > 1024) { + Serial.println("Config file size is too large"); + return false; + } + + // Allocate a buffer to store contents of the file. + std::unique_ptr buf(new char[size]); + + // We don't use String here because ArduinoJson library requires the input + // buffer to be mutable. If you don't use ArduinoJson, you may as well + // use configFile.readString instead. + configFile.readBytes(buf.get(), size); + + StaticJsonDocument<200> doc; + auto error = deserializeJson(doc, buf.get()); + if (error) { + Serial.println("Failed to parse config file"); + return false; + } + + const char* CfgSsid = doc["CfgSsid"]; + const char* CfgPassword = doc["CfgPassword"]; + const int CfgT1 = doc["CfgT1"]; + const int CfgT2 = doc["CfgT2"]; + const int CfgT3 = doc["CfgT3"]; + const int CfgT4 = doc["CfgT4"]; + const int CfgT5 = doc["CfgT5"]; + const int CfgT6 = doc["CfgT6"]; + + Serial.println(); + Serial.print("Loaded AP name: "); + Serial.println(CfgSsid); + Serial.print("Loaded Password: "); + Serial.println(CfgPassword); + Serial.print("Loaded Timer1: "); + Serial.println(CfgT1); + Serial.print("Loaded Timer2: "); + Serial.println(CfgT2); + Serial.print("Loaded Timer3: "); + Serial.println(CfgT3); + Serial.print("Loaded Timer4: "); + Serial.println(CfgT4); + Serial.print("Loaded Timer5: "); + Serial.println(CfgT5); + Serial.print("Loaded Timer6: "); + Serial.println(CfgT6); + ssid = String(CfgSsid); + password = String(CfgPassword); + t1wait = CfgT1; + t2wait = CfgT2; + t3wait = CfgT3; + t4wait = CfgT4; + t5wait = CfgT5; + t6wait = CfgT6; + return true; +} + +bool saveConfig() { + StaticJsonDocument<200> doc; + + + + doc["CfgSsid"] = ssid; + doc["CfgPassword"] = password; + doc["CfgT1"] = t1wait; + doc["CfgT2"] = t2wait; + doc["CfgT3"] = t3wait; + doc["CfgT4"] = t4wait; + doc["CfgT5"] = t5wait; + doc["CfgT6"] = t6wait; + + Serial.print("Saved AP name: "); + Serial.println(ssid); + Serial.print("Saved Password: "); + Serial.println(password); + Serial.print("Saved Timer1: "); + Serial.println(t1wait); + Serial.print("Saved Timer2: "); + Serial.println(t2wait); + Serial.print("Saved Timer3: "); + Serial.println(t3wait); + Serial.print("Saved Timer4: "); + Serial.println(t4wait); + Serial.print("Saved Timer5: "); + Serial.println(t5wait); + Serial.print("Saved Timer6: "); + Serial.println(t6wait); + + File configFile = SPIFFS.open("/wconfig.json", "w"); + if (!configFile) { + Serial.println("Failed to open config file for writing"); + return false; + } + + serializeJson(doc, configFile); + server.send(200,"text/plain", "Success"); + Serial.println("Saved config file"); + return true; +} + +void webSocketEvent(uint8_t num, WStype_t type, uint8_t * payload, size_t wslength) +{ + String payloadString=(const char *)payload; + Serial.println("payload: '"+payloadString+"', channel: "+(String)num); + if(type==WStype_TEXT){ + byte separator=payloadString.indexOf('='); + String var=payloadString.substring(0,separator); + String val=payloadString.substring(separator+1); + if(var=="ssid"){ + ssid = val; + }else if(var=="password"){ + password = val; + }else if(var=="t1wait"){ + t1wait = val.toInt(); + }else if(var=="t2wait"){ + t2wait = val.toInt(); + }else if(var=="t3wait"){ + t3wait = val.toInt(); + }else if(var=="t4wait"){ + t4wait = val.toInt(); + }else if(var=="t5wait"){ + t5wait = val.toInt(); + }else if(var=="t6wait"){ + t6wait = val.toInt(); + }else if(var=="t6wait"){ + t6wait = val.toInt(); + } + bEventtrig = true; + } +} diff --git a/OBFWeb/Readme.md b/OBFWeb/Readme.md new file mode 100644 index 0000000..12c8579 --- /dev/null +++ b/OBFWeb/Readme.md @@ -0,0 +1,6 @@ +3/5/2020 +This code is initially designed to run on an NodeMCU +acting as a Serial to Wifi bridge to allow the current version of code to run on an Arduino UNO +and have the option of attaching a second controller that will deal with all the Web Config activities + +NodeMCU is not capable of running the 3 Analogue pins required by the code at this time. diff --git a/OBFWeb/data/index.html b/OBFWeb/data/index.html new file mode 100644 index 0000000..2aab7b7 --- /dev/null +++ b/OBFWeb/data/index.html @@ -0,0 +1,376 @@ + + + + + + + + + + OpenBeerFiller Web Front End + + + + + + +
+
+
+ +
+
+
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
+ # + + Timer Description + + Time (ms) + + +
+ 1 + + Can Push + + + + + + + +
+ 2 + + Can Push Retraction Delay + + + + + + + +
+ 3 + + Fill Rail Drop Wait + + + + + + + +
+ 4 + + CO2 Purge + + + + + + + +
+ 5 + + CO2 Blast + + + + + + + + +
+ 6 + + Fill Rail Raise Wait + + + + + + + + +
+


+ +
+
+ +
+ + + +

+
+
+Things still to work out
+1. Sending data to and from OBF +
+ + + + + + + \ No newline at end of file diff --git a/OBFWeb/data/wconfig.json b/OBFWeb/data/wconfig.json new file mode 100644 index 0000000..8627d65 --- /dev/null +++ b/OBFWeb/data/wconfig.json @@ -0,0 +1,9 @@ +{"CfgSsid": "MySsid", + "CfgPassword": "myP455w0rd", + "CfgT1":2500, + "CfgT2":2000, + "CfgT3":2000, + "CfgT4":3000, + "CfgT5":100, + "CfgT6":2000 + } \ No newline at end of file