/* General Purpose Webserver adjusted for Olimex ESP32-PoE board. It can control Olimex module MOD-IO. This sketch requires Arduino for ESP32 - how to install it read here: https://github.com/espressif/arduino-esp32 This project is based on the one created by David Bird. The original can be found and downloaded here: https://github.com/G6EJD/ESP32-General-Purpose-Webserver The MIT License (MIT) Copyright (c) 2017 by David Bird. ### The formulation and calculation method of an IAQ - Internal Air Quality index ### ### The provision of a general purpose webserver ### Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, but not to use it commercially for profit making or to sub-license and/or to sell copies of the Software or to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. See more at http://dsbird.org.uk */ //################# LIBRARIES ################ #include #include #include #include #include //https://github.com/Pedroalbuquerque/ESP32WebServer download and place in your Libraries folder //################ VARIABLES ################ const char* ssid = "YOUR_SSID"; // WiFi SSID const char* password = "YOUR_PASSWORD"; // WiFi Password String siteheading = "ESP32-PoE with MOD-IO"; // Site's Main Title String sitetitle = "ESP32-PoE Webserver"; // Appears on the tabe of a Web Browser String yourfootnote = "Olimex demo for ESP32-PoE"; // A foot note e.g. "My Web Site" String siteversion = "v1.0"; // Version of your Website #define sitewidth 1024 // Adjust site page width in pixels as required String webpage = ""; // General purpose variable to hold HTML code ESP32WebServer server(80); // Start server on port 80 (default for a web-browser, change to your requirements, e.g. 8080 perhaps, if your Router uses port 80 // To access server from outside of a WiFi (LAN) network e.g. on port 8080 add a rule on your Router that forwards a connection request // to http://your_network_WAN_address:8080 to http://your_network_LAN_address:8080 and then you can view your ESP server from anywhere. // Example http://yourhome.ip:8080 and your ESP Server is at 192.168.0.40, then the request will be directed to http://192.168.0.40:8080 #define BUTTON_PRESSED() (!digitalRead (34)) #define INDICATOR(X)\ {\ if (X)\ webpage += " ";\ else\ webpage += " ";\ } void MOD_IO_SetRelay (int Value) { Wire.beginTransmission(0x58); Wire.write(0x10); Wire.write(Value); Wire.endTransmission(); } void setup() { Serial.begin(115200); // initialize serial communications Wire.begin (13, 16); // SPP Serial.println("Connect your I2C sensors to the default SDA, SCL pins for your board shown here:"); Serial.println("I2C SDA pin = " + String(SDA)); Serial.println("I2C SCL pin = " + String(SCL)); // Connect I2C sensors to the default SDA and SCL pins! Check Serial port for details StartWiFi(ssid, password); StartTime(); //---------------------------------------------------------------------- Serial.println("Use this URL to connect: http://" + WiFi.localIP().toString() + "/"); // Print the IP address server.on("/", PoE_Demo); // If the user types at their browser http://192.168.0.100/ control is passed here and then to user_input, you get values for your program... server.on("/PoE_Demo", PoE_Demo); // // SPP server.onNotFound(handleNotFound); // If the user types something that is not supported, say so server.begin(); Serial.println(F("Webserver started...")); // Start the webserver pinMode (34, INPUT); } void loop () { server.handleClient(); } void handleNotFound() { String message = "The request entered could not be found, please try again with a different option\n"; server.send(404, "text/plain", message); } void PoE_Demo () { String MOD_IO_CheckBoxChoice = ""; webpage = ""; // don't delete this command, it ensures the server works reliably! append_HTML_header(); String IPaddress = WiFi.localIP().toString(); webpage += "
"; webpage += ""; webpage += ""; webpage += ""; webpage += "


"; // And so-on // MOD-IO webpage += "
MOD-IO: "; webpage += " "; webpage += " "; webpage += " "; webpage += " "; webpage += "
"; // And so-on webpage += "
"; append_HTML_footer(); server.send(200, "text/html", webpage); // Send a response to the client to enter their inputs, if needed, Enter=defaults if (server.args() > 0 ) { // Arguments were received for ( uint8_t i = 0; i < server.args(); i++ ) { static int Temp=0, RelayValue = 0; String Argument_Name = server.argName(i); String client_response = server.arg(i); if (Argument_Name == "MOD_IO_CheckBox") { MOD_IO_CheckBoxChoice = client_response; // Checking for more than one check-box being selected too, 'a' if more than one Temp = MOD_IO_CheckBoxChoice.toInt () - 1; // SPP RelayValue = RelayValue | (1 << Temp); Serial.println (RelayValue); MOD_IO_SetRelay (RelayValue); } if (Argument_Name == "MOD_IO_Relay") { Serial.print ("Value: "); Serial.println (RelayValue); MOD_IO_SetRelay (RelayValue); RelayValue = 0; } } } } void StartWiFi(const char* ssid, const char* password) { int connAttempts = 0; Serial.print(F("\r\nConnecting to: ")); Serial.println(String(ssid)); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED ) { delay(500); Serial.print("."); if (connAttempts > 20) { Serial.println("Failed to connect to WiFi"); } connAttempts++; } Serial.print(F("WiFi connected at: ")); Serial.println(WiFi.localIP()); } void StartTime() { configTime(0, 0, "0.uk.pool.ntp.org", "time.nist.gov"); setenv("TZ", "GMT0BST,M3.5.0/01,M10.5.0/02", 1); // Set for your locale delay(200); GetTime(); } String GetTime() { struct tm timeinfo; while (!getLocalTime(&timeinfo)) { Serial.println("Failed to obtain time"); StartTime(); } //See http://www.cplusplus.com/reference/ctime/strftime/ //Serial.println(&timeinfo, "%a %b %d %Y %H:%M:%S"); // Displays: Saturday, June 24 2017 14:05:49 char output[50]; strftime(output, 50, "%d/%m/%y %H:%M:%S", &timeinfo); // Format needed for Google Charts is "11/12/17 22:01:00"; //dd/mm/yy hh:hh:ss return output; } void append_HTML_header() { webpage = ""; webpage += ""; webpage += ""; // 5-min refresh time, test needed to prevent auto updates repeating some commands webpage += ""; webpage += ""; webpage += ""; webpage += "
" + sitetitle + "
"; webpage += "

" + siteheading + " " + siteversion + "

"; } void append_HTML_footer() { webpage += ""; webpage += "
"; }