feature-ota-update #5
@ -6,6 +6,7 @@ LiquidCrystal_I2C lcd(0x3F,16,2); //0x3F = Adresse des Displays
|
||||
|
||||
#include "Truhe.h"
|
||||
#include "Settings.h"
|
||||
#include "Webserver.h"
|
||||
|
||||
static unsigned long last_lcd_time = 0;
|
||||
static unsigned long last_mess_time = 0;
|
||||
@ -35,6 +36,7 @@ void setup() {
|
||||
truhen[i].setup();
|
||||
}
|
||||
wdt_enable(WDTO_4S); // Watchdog auf 4 s stellen
|
||||
WifiSetup();
|
||||
delay(3000);
|
||||
Serial.println("Setup fi");
|
||||
}
|
||||
@ -102,5 +104,6 @@ void loop() {
|
||||
}
|
||||
}
|
||||
}
|
||||
server.handleClient();
|
||||
wdt_reset();
|
||||
}
|
||||
|
182
Webserver.h
Normal file
182
Webserver.h
Normal file
@ -0,0 +1,182 @@
|
||||
// Load Wi-Fi library
|
||||
#include <ESP8266WiFi.h>
|
||||
#include <ESP8266WebServer.h>
|
||||
#include <Preferences.h>
|
||||
Preferences preferences;
|
||||
ESP8266WebServer server(80);
|
||||
String header = "<!DOCTYPE html> <html>\n"
|
||||
"<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1.0, user-scalable=no\">\n"
|
||||
"<title>Truhensteuerung</title>\n"
|
||||
"<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}\n"
|
||||
"body{margin-top: 50px;} h1 {color: #444444;margin: 50px auto 30px;} h3 {color: #444444;margin-bottom: 50px;}\n"
|
||||
"</style>\n"
|
||||
"</head>\n"
|
||||
"<body>\n"
|
||||
"<h1>Truhensteuerung Web Server</h1>\n";
|
||||
String ssid = "REPLACE_WITH_YOUR_SSID";
|
||||
String password = "REPLACE_WITH_YOUR_PASSWORD";
|
||||
unsigned long currentTime = millis();
|
||||
unsigned long previousTime= 0;
|
||||
//const long timeoutTime= 2000;
|
||||
|
||||
void WifiConnect(String _ssid, String _password){
|
||||
// Connect to Wi-Fi network with SSID and password
|
||||
Serial.print("Connecting to ");
|
||||
Serial.println(_ssid);
|
||||
WiFi.begin(_ssid, _password);
|
||||
if(WiFi.waitForConnectResult() == WL_CONNECTED)
|
||||
{
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected.");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("WiFi Connection failed! Restarting in SoftAP mode instead");
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP("Truhensteuerung", "12345678");
|
||||
Serial.print("AP IP address: ");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
}
|
||||
// Print local IP address and start web server
|
||||
}
|
||||
|
||||
void handleWifiSetup(){
|
||||
String ptr = header;
|
||||
// ptr += "URI: ";
|
||||
// ptr += server.uri();
|
||||
// ptr += "<br>\nMethod: ";
|
||||
// ptr += (server.method() == HTTP_GET) ? "GET" : "POST";
|
||||
// ptr += "<br>\nArguments: ";
|
||||
// ptr += server.args();
|
||||
// ptr += "<br>\n";
|
||||
for (uint8_t i = 0; i < server.args(); i++) { ptr += " " + server.argName(i) + ": " + server.arg(i) + "<br>\n"; }
|
||||
if(server.method() == HTTP_GET){
|
||||
int n = WiFi.scanNetworks(false, true);
|
||||
String ssid;
|
||||
uint8_t encryptionType;
|
||||
int32_t RSSI;
|
||||
uint8_t* BSSID;
|
||||
int32_t channel;
|
||||
bool isHidden;
|
||||
ptr += "<a href=\"/\">Home</a>";
|
||||
ptr += "<form action=\"/setup\" method=\"post\">";
|
||||
ptr +="<table style=\"width: 100%;\">\n<tr style=\"border-bottom: 5px solid #444444;\"><td></td><td>ID</td><td>SSID</td><td>Channel</td><td>RSSI</td><td></td></tr>\n";
|
||||
for (int i = 0; i < n; i++)
|
||||
{
|
||||
WiFi.getNetworkInfo(i, ssid, encryptionType, RSSI, BSSID, channel, isHidden);
|
||||
ptr +="<tr><td><input type=\"radio\" id=\"ssid\" name=\"ssid\" value=\""+String(ssid.c_str())+"\"></td><td>"+String(i + 1)+"</td><td>"+String(ssid.c_str())+"</td><td>"+String(channel)+"</td><td>"+String(RSSI)+"dBm</td><td>"+String(encryptionType == ENC_TYPE_NONE ? "open" : "")+""+String(isHidden ? "hidden" : "")+"</td></tr>\n";
|
||||
}
|
||||
|
||||
ptr += "</table>\n";
|
||||
ptr += "<input type=\"input\" id=\"password\" name=\"password\" placeholder=\"Kennwort\" >";
|
||||
ptr += "<input type=\"submit\" id=\"submit\" name=\"submit\" >";
|
||||
ptr += "</form>";
|
||||
}else{
|
||||
ptr += "<p>\n";
|
||||
if(server.args() != 4){
|
||||
ptr += "Missing Arguments<br>\n";
|
||||
ptr += "<a href=\"/setup\">zurück</a>\n";
|
||||
}else{
|
||||
String ssid;
|
||||
String password;
|
||||
for (uint8_t i = 0; i < server.args(); i++) {
|
||||
if(server.argName(i) == "ssid"){
|
||||
ssid = server.arg(i);
|
||||
}
|
||||
if(server.argName(i) == "password"){
|
||||
password = server.arg(i);
|
||||
}
|
||||
}
|
||||
ptr += "Connecting to "+ssid+"<br>\n";
|
||||
Serial.println("Connecting to "+ssid+", "+password+"");
|
||||
WiFi.begin(ssid, password);
|
||||
if(WiFi.waitForConnectResult() == WL_CONNECTED)
|
||||
{
|
||||
Serial.println("");
|
||||
Serial.println("WiFi connected.");
|
||||
Serial.println("IP address: ");
|
||||
Serial.println(WiFi.localIP());
|
||||
preferences.begin("my-app", false);
|
||||
preferences.putString("ssid", ssid);
|
||||
preferences.putString("password", password);
|
||||
preferences.end();
|
||||
ptr += "WiFi connected.<br>";
|
||||
// ptr += "IP address: ";
|
||||
// ptr += WiFi.localIP();
|
||||
ptr += "<br>\n";
|
||||
}
|
||||
else
|
||||
{
|
||||
Serial.println("WiFi Connection failed! Restarting in SoftAP mode instead");
|
||||
ptr += "WiFi Connection failed! Restarting in SoftAP mode instead<br>\n";
|
||||
WiFi.mode(WIFI_AP);
|
||||
WiFi.softAP("Truhensteuerung", "12345678");
|
||||
Serial.print("AP IP address: ");
|
||||
Serial.println(WiFi.softAPIP());
|
||||
//ptr += "AP IP address: ";
|
||||
//ptr += WiFi.softAPIP();
|
||||
ptr += "<br>\n";
|
||||
}
|
||||
// Print local IP address and start web server
|
||||
ptr += "</p>\n";
|
||||
}
|
||||
}
|
||||
server.send(200, "text/html", ptr);
|
||||
|
||||
}
|
||||
void handleRoot(){
|
||||
String ptr = header;
|
||||
ptr += "<a href=\"/setup\">Setup Wifi</a>";
|
||||
ptr +="<table style=\"width: 100%;\">\n<tr style=\"border-bottom: 5px solid #444444;\"><td>Name</td><td>Temp</td><td>Status</td></tr>\n";
|
||||
for (int i = 0; i < (sizeof(truhen) / sizeof(truhen[0])); i++) {
|
||||
ptr +="<tr>\n<td>";
|
||||
ptr +=truhen[i].getName();
|
||||
ptr +="</td>\n<td>";
|
||||
ptr +=String(truhen[i].getCurTemp())+"℃";
|
||||
ptr +="</td>\n<td>";
|
||||
if (truhen[i].getStat() == -1) {
|
||||
ptr +="Nicht bekannt";
|
||||
}
|
||||
else if (truhen[i].getStat() == 1) {
|
||||
ptr +="An";
|
||||
}
|
||||
else if (truhen[i].getStat() == 0) {
|
||||
ptr +="Aus";
|
||||
}
|
||||
else if (truhen[i].getStat() == 2) {
|
||||
ptr +="Error";
|
||||
}
|
||||
ptr += "</td>\n</tr>\n";
|
||||
}
|
||||
ptr += "</table>\n";
|
||||
ptr +="</body>\n";
|
||||
ptr +="</html>\n";
|
||||
server.send(200, "text/html", ptr);
|
||||
}
|
||||
void handleNotFound() {
|
||||
String message = "File Not Found\n\n";
|
||||
message += "URI: ";
|
||||
message += server.uri();
|
||||
message += "\nMethod: ";
|
||||
message += (server.method() == HTTP_GET) ? "GET" : "POST";
|
||||
message += "\nArguments: ";
|
||||
message += server.args();
|
||||
message += "\n";
|
||||
for (uint8_t i = 0; i < server.args(); i++) { message += " " + server.argName(i) + ": " + server.arg(i) + "\n"; }
|
||||
server.send(404, "text/plain", message);
|
||||
}
|
||||
|
||||
void WifiSetup() {
|
||||
preferences.begin("my-app", false);
|
||||
ssid = preferences.getString("ssid", ssid);
|
||||
password = preferences.getString("password", password);
|
||||
preferences.end();
|
||||
|
||||
WifiConnect(ssid, password);
|
||||
server.on("/setup",handleWifiSetup);
|
||||
server.on("/",handleRoot);
|
||||
|
||||
server.begin();
|
||||
};
|
Loading…
x
Reference in New Issue
Block a user