OnOff.ino 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155
  1. /*
  2. * Home Dynamic System v2
  3. * Designed by tobychui
  4. *
  5. * This is a basic IoT Switch that support single channel ON / OFF function only
  6. *
  7. */
  8. #include <ESP8266WiFi.h> // Include the Wi-Fi library
  9. #include <ESP8266WiFiMulti.h> // Include the Wi-Fi-Multi library
  10. #include <ESP8266mDNS.h> // Include the mDNS library
  11. #include <ESP8266WebServer.h> // Include the WebServer library
  12. //Change the properties of the IoT device
  13. const String DeviceName = "Switch"; //The name of this IoT device
  14. const int ListeningPort = 12110; //The port where this IoT device listen
  15. int signalOutputPin = LED_BUILTIN; //The pin to activate during on, default LED pins as demo
  16. bool poweredOn = true; //The current power state of the switch
  17. //Library Objects
  18. ESP8266WiFiMulti wifiMulti; // Create an instance of the ESP8266WiFiMulti class, called 'wifiMulti'
  19. ESP8266WebServer server(ListeningPort); //Create an Web Server on the listening port
  20. //Change the WiFi Settings
  21. void WiFiConfig(){
  22. wifiMulti.addAP("Toby Room Automation", "homedynamicsystem");
  23. //Add more WiFi AP here if nessary
  24. //wifiMulti.addAP("ssid_from_AP_2", "your_password_for_AP_2");
  25. //wifiMulti.addAP("ssid_from_AP_3", "your_password_for_AP_3");
  26. }
  27. //Inject zeroconf attr into the MDNS respond (For scanning by ArozOS)
  28. void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
  29. //Define the domain of the HDSv2 devices
  30. MDNS.addDynamicServiceTxt(p_hService, "domain","hds.arozos.com");
  31. MDNS.addDynamicServiceTxt(p_hService, "protocol","hdsv2");
  32. //Define the OEM written values
  33. MDNS.addDynamicServiceTxt(p_hService, "uuid",getMacAddress());
  34. MDNS.addDynamicServiceTxt(p_hService, "model","Switch");
  35. MDNS.addDynamicServiceTxt(p_hService, "vendor","HomeDynamic Project");
  36. MDNS.addDynamicServiceTxt(p_hService, "version_minor","0.00");
  37. MDNS.addDynamicServiceTxt(p_hService, "version_build","0");
  38. }
  39. void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
  40. MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
  41. }
  42. void setup() {
  43. //Use 115200 baudrate on serial monitor if you want to see what is happening to the device
  44. Serial.begin(115200);
  45. delay(10);
  46. Serial.println('\n');
  47. //Set output pins as OUTPUT and default it to HIGH
  48. pinMode(signalOutputPin, OUTPUT);
  49. digitalWrite(signalOutputPin, HIGH);
  50. //Start WiFi Conenction Routines
  51. WiFiConfig();
  52. Serial.println("Connecting ...");
  53. while (wifiMulti.run() != WL_CONNECTED) {
  54. delay(500);
  55. Serial.print('.');
  56. }
  57. Serial.println('\n');
  58. Serial.print("Connected to ");
  59. Serial.println(WiFi.SSID());
  60. Serial.print("IP address:\t");
  61. Serial.println(WiFi.localIP());
  62. //Startup MDNS Responder
  63. MDNS.setHostProbeResultCallback(hostProbeResult);
  64. if (!MDNS.begin(DeviceName)) { // Start the mDNS responder for esp8266.local
  65. Serial.println("Error setting up MDNS responder!");
  66. }
  67. //Advertise the port that you are using
  68. MDNS.addService("http", "tcp", ListeningPort);
  69. Serial.println("mDNS responder started");
  70. //Startup the Web Server Endpoints
  71. delay(100);
  72. server.on("/", handle_index);
  73. server.on("/status", handle_status);
  74. server.on("/eps", handle_endpoints);
  75. server.on("/on", handle_on);
  76. server.on("/off", handle_off);
  77. server.begin();
  78. Serial.println("HTTP server started");
  79. Serial.print("Listening on port: ");
  80. Serial.println(ListeningPort);
  81. }
  82. //Handlers for Web Server
  83. void handle_index() {
  84. server.send(200, "text/html", "");
  85. }
  86. //Handle turning on the switch
  87. void handle_on() {
  88. Serial.println("Turned ON");
  89. digitalWrite(signalOutputPin, HIGH);
  90. poweredOn = true;
  91. server.send(200, "text/html", "OK");
  92. }
  93. //Handle turning off the switch
  94. void handle_off() {
  95. Serial.println("Turned OFF");
  96. digitalWrite(signalOutputPin, LOW);
  97. poweredOn = false;
  98. server.send(200, "text/html", "OK");
  99. }
  100. void handle_status() {
  101. String powerState = "ON";
  102. if (poweredOn == false){
  103. powerState = "OFF";
  104. }
  105. server.send(200, "application/json", "{\"Power\":\"" + powerState + "\"\}");
  106. }
  107. void handle_endpoints() {
  108. server.send(200, "application/json", "[{\
  109. \"Name\": \"ON\",\
  110. \"RelPath\":\"on\",\
  111. \"Desc\":\"Switch on the device attached to the switch\",\
  112. \"Type\":\"none\",\
  113. \"AllowRead\":false,\
  114. \"AllowWrite\":true\
  115. },{\
  116. \"Name\": \"OFF\",\
  117. \"RelPath\":\"off\",\
  118. \"Desc\":\"Switch off the device attached to the switch\",\
  119. \"Type\":\"none\",\
  120. \"AllowRead\":false,\
  121. \"AllowWrite\":true\
  122. }\
  123. ]");
  124. }
  125. //Main Loop
  126. void loop() {
  127. server.handleClient();
  128. MDNS.update();
  129. }