|
- #include <WiFi.h>
- #include <PubSubClient.h>
-
- //#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) = "<html><head><title>Web_EEPROM_Setup</title></head><body>\n";
- P(Page_end) = "</body></html>";
-
- P(Http400) = "HTTP 400 - BAD REQUEST";
- P(Index) = "<h1>index.html</h1><br>This is your main site!<br>The code is found in the indexHTML() function.<br>You can add more sites if you need. Please see the well documented source code.<br><br>Use the following link to setup the network.<br><a href=\"setupNet.html\">NETWORK SETUP</a>";
-
- P(Form_eth_start) = "<FORM action=\"setupNet.html\" method=\"get\">";
- P(Form_end) = "<FORM>";
- P(Form_input_send) = "<INPUT type=\"submit\" value=\"Set config\">";
-
- P(Form_input_text_start) = "<input type=\"text\" name=\"";
- P(Form_input_value) = "\" value=\"";
- P(Form_input_size2) = "\" maxlength=\"2\" size=\"2";
- P(Form_input_size3) = "\" maxlength=\"3\" size=\"3";
- P(Form_input_end) = "\">\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) = "<input type=\"radio\" name=\"23\" value=\"";
- P(Form_cb_checked) = " checked ";
- P(Form_cb_on) = ">On";
- P(Form_cb_off) = ">Off";
-
- P(br) = "<br>\n";
-
- P(table_start) = "<table>";
- P(table_tr_start) = "<tr>";
- P(table_tr_end) = "</tr>";
- P(table_td_start) = "<td>";
- P(table_td_end) = "</td>";
- P(table_end) = "</table>";
-
- P(Config_set) = "<font size=\"6\" color=\"red\">New configuration stored! <br>Please turn off and on your Arduino or use the reset button!</font><br>";
-
- 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("<!DOCTYPE html><html>");
- client.println("<head><meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
- client.println("<link rel=\"icon\" href=\"data:,\">");
- // CSS to style the on/off buttons
- // Feel free to change the background-color and font-size attributes to fit your preferences
- client.println("<style>html { font-family: Helvetica; display: inline-block; margin: 0px auto; text-align: center;}");
- client.println(".button { background-color: #4CAF50; border: none; color: white; padding: 16px 40px;");
- client.println("text-decoration: none; font-size: 30px; margin: 2px; cursor: pointer;}");
- client.println(".button2 {background-color: #555555;}</style></head>");
-
- // Web Page Heading
- client.println("<body><h1>ESP32 Web Server</h1>");
-
- // Display current state, and ON/OFF buttons for GPIO 26
- client.println("<p>GPIO 26 - State " + output26State + "</p>");
- // If the output26State is off, it displays the ON button
- if (output26State=="off") {
- client.println("<p><a href=\"/26/on\"><button class=\"button\">ON</button></a></p>");
- } else {
- client.println("<p><a href=\"/26/off\"><button class=\"button button2\">OFF</button></a></p>");
- }
-
- // Display current state, and ON/OFF buttons for GPIO 27
- client.println("<p>GPIO 27 - State " + output27State + "</p>");
- // If the output27State is off, it displays the ON button
- if (output27State=="off") {
- client.println("<p><a href=\"/27/on\"><button class=\"button\">ON</button></a></p>");
- } else {
- client.println("<p><a href=\"/27/off\"><button class=\"button button2\">OFF</button></a></p>");
- }
- client.println("</body></html>");
-
- // 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);
- }
- }
|