/* * Home Dynamic System v2 * Designed by tobychui * * This is a basic IoT Switch that support single channel ON / OFF function only * Use this firmware with Wemos D1 R1 board profile */ #include // Include the Wi-Fi library #include // Include the Wi-Fi-Multi library #include // Include the mDNS library #include // Include the WebServer library //Change the properties of the IoT device const String DeviceName = "Sinilink WiFi Relay"; //The name of this IoT device const int ListeningPort = 12110; //The port where this IoT device listen int signalOutputPin = D14; //The pin to activate the mosfet of relay int buttonPin = D6; int refPin = D8; bool poweredOn = true; //The current power state of the switch int val = 0; //Library Objects ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti' ESP8266WebServer server(ListeningPort); //Create an Web Server on the listening port //Change the WiFi Settings void WiFiConfig(){ wifiMulti.addAP("Toby Room Automation", "homedynamicsystem"); //Add more WiFi AP here if nessary //wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2"); //wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3"); } //Inject zeroconf attr into the MDNS respond (For scanning by ArozOS) void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) { //Define the domain of the HDSv2 devices MDNS.addDynamicServiceTxt(p_hService, "domain","hds.arozos.com"); MDNS.addDynamicServiceTxt(p_hService, "protocol","hdsv2"); //Define the OEM written values MDNS.addDynamicServiceTxt(p_hService, "uuid",getMacAddress()); MDNS.addDynamicServiceTxt(p_hService, "model","Switch"); MDNS.addDynamicServiceTxt(p_hService, "vendor","HomeDynamic Project"); MDNS.addDynamicServiceTxt(p_hService, "version_minor","0.00"); MDNS.addDynamicServiceTxt(p_hService, "version_build","0"); } void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) { MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback); } void setup() { //Use 115200 baudrate on serial monitor if you want to see what is happening to the device Serial.begin(115200); delay(10); Serial.println('\n'); //Set output pins as OUTPUT and default it to HIGH pinMode(signalOutputPin, OUTPUT); digitalWrite(signalOutputPin, HIGH); //Button input pin pinMode(buttonPin, INPUT_PULLUP); //Unknown pin that needed to be HIGH pinMode(refPin, OUTPUT); digitalWrite(refPin, HIGH); pinMode(D9, OUTPUT); digitalWrite(D9, HIGH); //Start WiFi Conenction Routines WiFiConfig(); Serial.println("Connecting ..."); while (wifiMulti.run() != WL_CONNECTED) { delay(500); Serial.print('.'); } Serial.println('\n'); Serial.print("Connected to "); Serial.println(WiFi.SSID()); Serial.print("IP address:\t"); Serial.println(WiFi.localIP()); //Startup MDNS Responder MDNS.setHostProbeResultCallback(hostProbeResult); if (!MDNS.begin(DeviceName)) { // Start the mDNS responder for esp8266.local Serial.println("Error setting up MDNS responder!"); } //Advertise the port that you are using MDNS.addService("http", "tcp", ListeningPort); Serial.println("mDNS responder started"); //Startup the Web Server Endpoints delay(100); server.on("/", handle_index); server.on("/status", handle_status); server.on("/eps", handle_endpoints); server.on("/on", handle_on); server.on("/off", handle_off); server.begin(); Serial.println("HTTP server started"); Serial.print("Listening on port: "); Serial.println(ListeningPort); } //Handlers for Web Server void handle_index() { server.send(200, "text/html", ""); } //Handle turning on the switch void handle_on() { Serial.println("Turned ON"); digitalWrite(signalOutputPin, HIGH); poweredOn = true; server.send(200, "text/html", "OK"); } //Handle turning off the switch void handle_off() { Serial.println("Turned OFF"); digitalWrite(signalOutputPin, LOW); poweredOn = false; server.send(200, "text/html", "OK"); } void handle_status() { String powerState = "ON"; if (poweredOn == false){ powerState = "OFF"; } server.send(200, "application/json", "{\"Power\":\"" + powerState + "\"}"); } void handle_endpoints() { server.send(200, "application/json", "[{\ \"Name\": \"ON\",\ \"RelPath\":\"on\",\ \"Desc\":\"Switch on the device attached to the switch\",\ \"Type\":\"none\",\ \"AllowRead\":false,\ \"AllowWrite\":true\ },{\ \"Name\": \"OFF\",\ \"RelPath\":\"off\",\ \"Desc\":\"Switch off the device attached to the switch\",\ \"Type\":\"none\",\ \"AllowRead\":false,\ \"AllowWrite\":true\ }\ ]"); } //Main Loop void loop() { //Do stuffs for handling web interface server.handleClient(); MDNS.update(); //Check if the button is pressed. Toggle the power if yes val = digitalRead(buttonPin); if (val == 0){ //Button is down. Toggle power state if (poweredOn == true){ //Turn if off digitalWrite(signalOutputPin, LOW); poweredOn = false; Serial.println("Turned off using button"); }else{ //Turn it on digitalWrite(signalOutputPin, HIGH); poweredOn = true; Serial.println("Turned on using button"); } //Sleep for 1 second delay(1000); } }