From dfac643c9f9ae9df0dea97fd32a38ea418ddfa11 Mon Sep 17 00:00:00 2001 From: agr Date: Thu, 14 May 2020 18:15:42 +0200 Subject: [PATCH] branch webserver config --- .cproject | 9 +++ .project | 15 ++++ eepromsettings.cpp | 183 +++++++++++++++++++++++++++++++++++++++++++++ eepromsettings.h | 38 ++++++++++ esp32MQTT.ino | 176 ++++++++++++++++++++++++++++++++++++++++++- struct.h | 108 ++++++++++++++++++++++++++ 6 files changed, 528 insertions(+), 1 deletion(-) create mode 100644 eepromsettings.cpp create mode 100644 eepromsettings.h create mode 100644 struct.h diff --git a/.cproject b/.cproject index 9240122..f6c71a3 100644 --- a/.cproject +++ b/.cproject @@ -23,6 +23,9 @@ + + + @@ -32,6 +35,9 @@ + + + @@ -41,6 +47,9 @@ + + + diff --git a/.project b/.project index f910efd..301f100 100644 --- a/.project +++ b/.project @@ -41,11 +41,26 @@ 2 ECLIPSE_HOME/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/variants/esp32 + + libraries/EEPROM + 2 + ECLIPSE_HOME/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/libraries/EEPROM + + + libraries/Ethernet + 2 + ECLIPSE_HOME/arduinoPlugin/libraries/Ethernet/2.0.0 + libraries/PubSubClient 2 ECLIPSE_HOME/arduinoPlugin/libraries/PubSubClient/2.7.0 + + libraries/SPI + 2 + ECLIPSE_HOME/arduinoPlugin/packages/esp32/hardware/esp32/1.0.4/libraries/SPI + libraries/WiFi 2 diff --git a/eepromsettings.cpp b/eepromsettings.cpp new file mode 100644 index 0000000..67b2ceb --- /dev/null +++ b/eepromsettings.cpp @@ -0,0 +1,183 @@ +/* + * eepromsettings.c + * + * Created on: 14.05.2020 + * Author: agr + */ + +#include "eepromsettings.h" +#include "struct.h" + +/** +* read_EEPROM_Settings function +* This function is used to read the EEPROM settings at startup +* +* Overview: +* - Set the PIN for the RESET-button to input and activate pullups +* - Load the stored data from EEPROM into the eeprom_config struct +* - Check if a config is stored or the reset button is pressed. If one of the conditions is ture, set the defaults +*/ +void read_EEPROM_Settings() { + pinMode(RESET_PIN, INPUT); + digitalWrite(RESET_PIN, HIGH); + + // read the current config + EEPROM_readAnything(0, eeprom_config); + + // check if config is present or if reset button is pressed + if (eeprom_config.config_set != 1 || digitalRead(RESET_PIN) == LOW) { + // set default values +#ifdef DEBUG + Serial.println(eeprom_config.config_set); + Serial.println(digitalRead(RESET_PIN)); +#endif + + set_EEPROM_Default(); +#ifdef DEBUG + Serial.println("write default config"); +#endif + // write the config to eeprom + EEPROM_writeAnything(0, eeprom_config); + } +} + +/** +* print_EEPROM_Settings() function +* +* This function is used for debugging the configuration. +* It prints the actual configuration to the serial port. +*/ +#ifdef DEBUG +void print_EEPROM_Settings() { + Serial.print("IP: "); + for(int i = 0; i<4; i++) { + Serial.print(eeprom_config.ip[i]); + if (i<3) { + Serial.print('.'); + } + } + Serial.println(); + + Serial.print("Subnet: "); + for(int i = 0; i<4; i++) { + Serial.print(eeprom_config.subnet[i]); + if (i<3) { + Serial.print('.'); + } + } + Serial.println(); + + Serial.print("Gateway: "); + for(int i = 0; i<4; i++) { + Serial.print(eeprom_config.gateway[i]); + if (i<3) { + Serial.print('.'); + } + } + Serial.println(); + + Serial.print("DNS Server: "); + for(int i = 0; i<4; i++) { + Serial.print(eeprom_config.dns_server[i]); + if (i<3) { + Serial.print('.'); + } + } + Serial.println(); + + Serial.print("MQTT Server: "); + for(int i = 0; i<4; i++) { + Serial.print(eeprom_config.mqtt_server[i]); + if (i<3) { + Serial.print('.'); + } + } + Serial.println(); + + Serial.print("MAC: "); + for (int a=0;a<6;a++) { + Serial.print(eeprom_config.mac[a],HEX); + if(a<5) { + Serial.print(":"); + } + } + Serial.println(); + Serial.print("MQTTserver Port: "); + Serial.println(eeprom_config.mqttserverPort); + + Serial.print("Webserver Port: "); + Serial.println(eeprom_config.webserverPort); + + Serial.print("USE DHCP: "); + Serial.println(eeprom_config.use_dhcp); + + Serial.print(" DHCP renew every "); + Serial.print(eeprom_config.dhcp_refresh_minutes); + Serial.println(" minutes"); + + Serial.print("Config Set: "); + Serial.println(eeprom_config.config_set); + +} +#endif + +// ############################################################################################################################################################# + + +/** +* renewDHCP() function +* Renew the DHCP relase in a given interval. +* +* Overview: +* - Check if interval = 0 and set it to 1 +* - Check if renew interval is reached and renew the lease +*/ +void renewDHCP(int interval) { + unsigned long interval_millis = interval * 60000; + + if (interval == 0 ) { + interval = 1; + } + if (eeprom_config.use_dhcp==1) { + if((millis() - last_dhcp_renew) > interval_millis) { + last_dhcp_renew=millis(); + dhcp_state = Ethernet.maintain(); + } + } +} + + +/** +* setupNetwork() function +* This function is used to setupup the network according to the values stored in the eeprom +* +* Overview: +* - First of all read the EEPROM settings +* - Display a link to the ethernet setup +* - Check if DHCP should be used, if not create instaces of IPAddress for ip, gateway, subnet and dns_server +* - Invoke Ethernet.begin with all parameters if no dhcp is active (Ethernet.begin(mac, ip, dns_server, gateway, subnet);). +* - If DHCP is used invoke only with mac (Ethernet.begin(mac);) and display the ip on the serial console. +*/ +void setupNetwork() { + read_EEPROM_Settings(); + + #ifdef DEBUG + print_EEPROM_Settings(); + #endif + + // byte mac[] = { eeprom_config.mac[0], eeprom_config.mac[1], eeprom_config.mac[2], eeprom_config.mac[3], eeprom_config.mac[4], eeprom_config.mac[5] }; + + if (eeprom_config.use_dhcp != 1) { + IPAddress ip(eeprom_config.ip[0], eeprom_config.ip[1], eeprom_config.ip[2], eeprom_config.ip[3]); + IPAddress gateway (eeprom_config.gateway[0],eeprom_config.gateway[1],eeprom_config.gateway[2],eeprom_config.gateway[3]); + IPAddress subnet (eeprom_config.subnet[0], eeprom_config.subnet[1], eeprom_config.subnet[2], eeprom_config.subnet[3]); + IPAddress dns_server (eeprom_config.dns_server[0], eeprom_config.dns_server[1], eeprom_config.dns_server[2], eeprom_config.dns_server[3]); + Ethernet.begin(eeprom_config.mac, ip, dns_server, gateway, subnet); + } else { + if (Ethernet.begin(eeprom_config.mac) == 0) { + Serial.print("Failed to configure Ethernet using DHCP"); + } + Serial.println(Ethernet.localIP()); + } +} +// END Network section ######################################################################################################################################### diff --git a/eepromsettings.h b/eepromsettings.h new file mode 100644 index 0000000..331bfb3 --- /dev/null +++ b/eepromsettings.h @@ -0,0 +1,38 @@ +/* + * eepromsettings.h + * + * Created on: 14.05.2020 + * Author: agr + */ + +#ifndef EEPROMSETTINGS_H_ +#define EEPROMSETTINGS_H_ + +#include + +#include +#include + +#define RESET_PIN 9 //Connect a button to this PIN. If the button is hold, an the device is turned on the default ethernet settings are restored. + + +void read_EEPROM_Settings(); + +#ifdef DEBUG +void print_EEPROM_Settings(); +#endif + + +/* START Network section ####################################################################################################################################### +* Code for setting up network connection +*/ +unsigned long last_dhcp_renew; +byte dhcp_state; + +void renewDHCP(int interval); + +void setupNetwork() ; + + + +#endif /* EEPROMSETTINGS_H_ */ diff --git a/esp32MQTT.ino b/esp32MQTT.ino index 9ab8927..4e3a493 100644 --- a/esp32MQTT.ino +++ b/esp32MQTT.ino @@ -1,6 +1,8 @@ #include #include +//#include "struct.h" +#include "eepromsettings.h" // Update these with values suitable for your network. const char* ssid = "Andreas-Grabner.NET"; @@ -16,6 +18,76 @@ WiFiClient wifiClient; PubSubClient client(wifiClient); +// Webserver +WiFiServer server(80); +String header; + +// Auxiliar variables to store the current output state +String output26State = "off"; +String output27State = "off"; + +// Assign output variables to GPIO pins +const int output26 = 26; +const int output27 = 27; + +/* Store all string in the FLASH storage to free SRAM. +The P() is a function from Webduino. +*/ +P(Page_start) = "Web_EEPROM_Setup\n"; +P(Page_end) = ""; + +P(Http400) = "HTTP 400 - BAD REQUEST"; +P(Index) = "

index.html


This is your main site!
The code is found in the indexHTML() function.
You can add more sites if you need. Please see the well documented source code.

Use the following link to setup the network.
NETWORK SETUP"; + +P(Form_eth_start) = "
"; +P(Form_end) = ""; +P(Form_input_send) = ""; + +P(Form_input_text_start) = "\n"; + +P(MAC) = "MAC address: "; +P(IP) = "IP address: "; +P(SUBNET) = "Subnet: "; +P(GW) = "GW address: "; +P(DNS_SERVER) = "DNS server: "; +P(WEB_PORT) = "Webserver port (1-65535): "; +P(DHCP_ACTIVE) = "Use DHCP: "; +P(DHCP_REFRESH) = "Renew interval for DHCP in minutes (1 - 255): "; +P(MQTT_SERVER) = "MQTT server: "; +P(MQTT_PORT) = "MQTTserver port (1-65535): "; + +P(Form_cb) = "New configuration stored!
Please turn off and on your Arduino or use the reset button!
"; + +P(DHCP_STATE_TIME) = "DHCP last renew timestamp (sec)"; +P(DHCP_STATE) = "DHCP renew return code (sec)"; + + +P(UPTIME) = "Uptime: "; + +#ifdef USE_SYSTEM_LIBRARY +P(RAM_1) = "RAM (byte): "; +P(RAM_2) = " free of "; +#endif + + void setup_wifi() { delay(10); // We start by connecting to a WiFi network @@ -34,6 +106,11 @@ void setup_wifi() { Serial.println(WiFi.localIP()); } +void setup_webserver() { + server.begin(); +} + + void reconnect() { // Loop until we're reconnected while (!client.connected()) { @@ -45,7 +122,7 @@ void reconnect() { if (client.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) { Serial.println("connected"); //Once connected, publish an announcement... - client.publish("icircuit/presence/ESP32/", "hello world"); + client.publish("ESP32/Heizung/Status/restart", "done"); // ... and resubscribe client.subscribe(MQTT_SERIAL_RECEIVER_CH); } else { @@ -71,6 +148,7 @@ void setup() { Serial.begin(115200); Serial.setTimeout(500);// Set time out for setup_wifi(); + setup_webserver(); client.setServer(mqtt_server, mqtt_port); client.setCallback(callback); reconnect(); @@ -82,8 +160,104 @@ void publishSerialData(char *serialData){ } client.publish(MQTT_SERIAL_PUBLISH_CH, serialData); } + +void loop_webserver() { + WiFiClient client = server.available(); // Listen for incoming clients + if (client) { // If a new client connects, + Serial.println("New Client."); // print a message out in the serial port + String currentLine = ""; // make a String to hold incoming data from the client + while (client.connected()) { // loop while the client's connected + if (client.available()) { // if there's bytes to read from the client, + char c = client.read(); // read a byte, then + Serial.write(c); // print it out the serial monitor + header += c; + if (c == '\n') { // if the byte is a newline character + // if the current line is blank, you got two newline characters in a row. + // that's the end of the client HTTP request, so send a response: + if (currentLine.length() == 0) { + // HTTP headers always start with a response code (e.g. HTTP/1.1 200 OK) + // and a content-type so the client knows what's coming, then a blank line: + client.println("HTTP/1.1 200 OK"); + client.println("Content-type:text/html"); + client.println("Connection: close"); + client.println(); + + // turns the GPIOs on and off + if (header.indexOf("GET /26/on") >= 0) { + Serial.println("GPIO 26 on"); + output26State = "on"; + digitalWrite(output26, HIGH); + } else if (header.indexOf("GET /26/off") >= 0) { + Serial.println("GPIO 26 off"); + output26State = "off"; + digitalWrite(output26, LOW); + } else if (header.indexOf("GET /27/on") >= 0) { + Serial.println("GPIO 27 on"); + output27State = "on"; + digitalWrite(output27, HIGH); + } else if (header.indexOf("GET /27/off") >= 0) { + Serial.println("GPIO 27 off"); + output27State = "off"; + digitalWrite(output27, LOW); + } + + // Display the HTML web page + client.println(""); + client.println(""); + client.println(""); + // CSS to style the on/off buttons + // Feel free to change the background-color and font-size attributes to fit your preferences + client.println(""); + + // Web Page Heading + client.println("

ESP32 Web Server

"); + + // Display current state, and ON/OFF buttons for GPIO 26 + client.println("

GPIO 26 - State " + output26State + "

"); + // If the output26State is off, it displays the ON button + if (output26State=="off") { + client.println("

"); + } else { + client.println("

"); + } + + // Display current state, and ON/OFF buttons for GPIO 27 + client.println("

GPIO 27 - State " + output27State + "

"); + // If the output27State is off, it displays the ON button + if (output27State=="off") { + client.println("

"); + } else { + client.println("

"); + } + client.println(""); + + // The HTTP response ends with another blank line + client.println(); + // Break out of the while loop + break; + } else { // if you got a newline, then clear currentLine + currentLine = ""; + } + } else if (c != '\r') { // if you got anything else but a carriage return character, + currentLine += c; // add it to the end of the currentLine + } + } + } + // Clear the header variable + header = ""; + // Close the connection + client.stop(); + Serial.println("Client disconnected."); + Serial.println(""); + } +} + void loop() { client.loop(); + loop_webserver(); if (Serial.available() > 0) { char mun[501]; memset(mun,0, 501); diff --git a/struct.h b/struct.h new file mode 100644 index 0000000..a6a9a12 --- /dev/null +++ b/struct.h @@ -0,0 +1,108 @@ +/* + * struct.h + * + * Created on: 07.04.2020 + * Author: agr + */ + +#ifndef STRUCT_H_ +#define STRUCT_H_ + + + +#include +#include +#include + +struct config_t +{ + byte config_set; + byte use_dhcp; + byte dhcp_refresh_minutes; + byte mac[6]; + byte ip[4]; + byte gateway[4]; + byte subnet[4]; + byte dns_server[4]; + unsigned int webserverPort; + byte mqtt_server[4]; + unsigned int mqttserverPort; +} eeprom_config; + +void set_EEPROM_Default() { + eeprom_config.config_set=1; // dont change! It's used to check if the config is already set + + eeprom_config.use_dhcp=1; // use DHCP per default + eeprom_config.dhcp_refresh_minutes=60; // refresh the DHCP every 60 minutes + + // set the default MAC address. In this case its DE:AD:BE:EF:FE:ED + eeprom_config.mac[0]=0xDE; + eeprom_config.mac[1]=0xAD; + eeprom_config.mac[2]=0xBE; + eeprom_config.mac[3]=0xEF; + eeprom_config.mac[4]=0xFE; + eeprom_config.mac[5]=0xED; + + // set the default IP address for the arduino. In this case its 192.168.1.97 + eeprom_config.ip[0]=192; + eeprom_config.ip[1]=168; + eeprom_config.ip[2]=1; + eeprom_config.ip[3]=97; + + // set the default GATEWAY. In this case its 192.168.1.1 + eeprom_config.gateway[0]=192; + eeprom_config.gateway[1]=168; + eeprom_config.gateway[2]=1; + eeprom_config.gateway[3]=1; + + // set the default SUBNET. In this case its 255.255.255.0 + eeprom_config.subnet[0]=255; + eeprom_config.subnet[1]=255; + eeprom_config.subnet[2]=255; + eeprom_config.subnet[3]=0; + + // set the default DNS SERVER. In this case its 192.168.1.1 + eeprom_config.dns_server[0]=192; + eeprom_config.dns_server[1]=168; + eeprom_config.dns_server[2]=1; + eeprom_config.dns_server[3]=1; + + // set the default Webserver Port. In this case its Port 80 + eeprom_config.webserverPort=80; + + // set the default MQTT SERVER. In this case its 192.168.1.35 + eeprom_config.mqtt_server[0]=192; + eeprom_config.mqtt_server[1]=168; + eeprom_config.mqtt_server[2]=1; + eeprom_config.mqtt_server[3]=35; + + // set the default MqttServer Port. In this case its Port 1883 + eeprom_config.mqttserverPort=1883; + +} + +#define P(name) static const unsigned char name[] + +int EEPROM_readAnything(int ee, config_t& value) +{ + byte* p = (byte*)(void*)&value; + unsigned int i; + for (i = 0; i < sizeof(value); i++) + *p++ = EEPROM.read(ee++); + return i; +} + + int EEPROM_writeAnything(int ee, const config_t& value) +{ + const byte* p = (const byte*)(const void*)&value; + unsigned int i; + for (i = 0; i < sizeof(value); i++) + EEPROM.write(ee++, *p++); + return i; +} + + EthernetClient MQTTethClient; + PubSubClient MQTTclient(MQTTethClient); + + +#endif /* STRUCT_H_ */