/********************************************************************************* * MIT License * * Copyright (c) 2022 Gregg E. Berman * * https://github.com/HomeSpan/HomeSpan * * 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, sublicense, and/or sell * copies of the Software, and 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. * ********************************************************************************/ //////////////////////////////////////////////////////////// // // // HomeSpan: A HomeKit implementation for the ESP32 // // ------------------------------------------------ // // // // Demonstrates how to implement a Web Server alongside // // of HomeSpan to create a Programmable Hub serving up to // // 16 Configurable Lights. // // // //////////////////////////////////////////////////////////// #include "HomeSpan.h" #include "DEV_LED.h" #include "DEV_Identify.h" #include // include WebServer library WebServer webServer(80); // create WebServer on port 80 #define NLIGHTS 16 // maximum number of Lightbulb Accessories uint8_t pinList[]={0,4,5,12,14,15,16,17,18,19,22,23,25,26,27,32,33}; // list of allowed pins char lightNames[NLIGHTS][9]; // storage for default light names nvs_handle lightNVS; // handle for NVS storage struct { // structure to store pin numbers and dimmable flag uint8_t pin=0; uint8_t dimmable=0; } lightData[NLIGHTS]; //////////////////////////////////////////////////////////// void setup() { Serial.begin(115200); homeSpan.setLogLevel(1); homeSpan.setHostNameSuffix(""); // use null string for suffix (rather than the HomeSpan device ID) homeSpan.setPortNum(1201); // change port number for HomeSpan so we can use port 80 for the Web Server homeSpan.enableOTA(); // enable OTA updates homeSpan.setMaxConnections(5); // reduce max connection to 5 (default is 8) since WebServer and a connecting client will need 2, and OTA needs 1 homeSpan.setWifiCallback(setupWeb); // need to start Web Server after WiFi is established homeSpan.enableWebLog(50,"pool.ntp.org","CST6CDT"); homeSpan.begin(Category::Bridges,"HomeSpan Light Hub","homespanhub"); for(int i=0;i0){ new SpanAccessory(i+2); new DEV_Identify(lightNames[i],"HomeSpan",lightNames[i],lightData[i].dimmable?"Dimmable":"Not Dimmable","1.0",0); new DEV_GenericLED(lightData[i].pin,lightData[i].dimmable); } } } // end of setup() ////////////////////////////////////// void loop(){ homeSpan.poll(); webServer.handleClient(); // need to process webServer once each loop } // end of loop() ////////////////////////////////////// void setupWeb(){ Serial.print("Starting Light Server Hub...\n\n"); webServer.begin(); // Create web routines inline webServer.on("/addForm", []() { String content="

Add New Light Accessory

"; content+="
"; content+=""; content+=""; content+="

Select Light Features:

"; content+=""; content+="
"; content+=""; content+="
"; content+=""; content+="

"; content+=""; content+="

"; content+=""; content+=""; content+="
"; content+=""; webServer.send(200, "text/html", content); }); webServer.on("/addLight", []() { char lightName[32]; uint8_t flags=0; String sColorControl("colorControl"); String sIsDimmable("isDimmable"); String sLightName("lightName"); for(int i=0;iAdd Another"; content+=""; content+=""; webServer.send(200, "text/html", content); }); webServer.on("/", []() { String content = "
HomeSpan Light Server Hub Configuration

"; content += "Select pins and check box if dimmable:

"; for(int i=0;i"; content += String(lightNames[i]) + ": "; content += " "; content += "
"; } content += "

"; webServer.send(200, "text/html", content); }); webServer.on("/configure", []() { for(int i=0;i"; else lightData[i].dimmable=0; content += "
"; content += ""; nvs_set_blob(lightNVS,"LIGHTDATA",&lightData,sizeof(lightData)); // update data nvs_commit(lightNVS); // commit to NVS webServer.send(200, "text/html", content); }); webServer.on("/reboot", []() { String content = "Rebooting! Will return to configuration page in 10 seconds.

"; content += ""; webServer.send(200, "text/html", content); for(int j=0;j