#include #include //#include "struct.h" #include "eepromsettings.h" // Update these with values suitable for your network. const char* ssid = "Andreas-Grabner.NET"; const char* password = "born2win"; const char* mqtt_server = "192.168.11.35"; #define mqtt_port 1883 #define MQTT_USER "loxberry" #define MQTT_PASSWORD "OSVL0AMqISFXgr5g" #define MQTT_SERIAL_PUBLISH_CH "ESP32/Heizung/status" #define MQTT_SERIAL_RECEIVER_CH "ESP32/Heizung/stufe" 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 Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } randomSeed(micros()); Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); } void setup_webserver() { server.begin(); } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Create a random client ID String clientId = "ESP32Client-"; clientId += String(random(0xffff), HEX); // Attempt to connect if (client.connect(clientId.c_str(),MQTT_USER,MQTT_PASSWORD)) { Serial.println("connected"); //Once connected, publish an announcement... client.publish("ESP32/Heizung/Status/restart", "done"); // ... and resubscribe client.subscribe(MQTT_SERIAL_RECEIVER_CH); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void callback(char* topic, byte *payload, unsigned int length) { Serial.println("-------new message from broker-----"); Serial.print("channel:"); Serial.println(topic); Serial.print("data:"); Serial.write(payload, length); Serial.println(); } 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(); } void publishSerialData(char *serialData){ if (!client.connected()) { reconnect(); } 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); Serial.readBytesUntil( '\n',mun,500); publishSerialData(mun); } }