4xrelay.ino 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. Home Dynamic System v3
  3. 4-way relays
  4. Hardware reference design
  5. Author: tobychui
  6. Compile with Wemos D1 mini board manager
  7. This project require LittleFS to store the device configuration
  8. json file. Install the LittleFS Uploader plugin here
  9. https://github.com/earlephilhower/arduino-esp8266littlefs-plugin
  10. */
  11. #include <WiFiManager.h>
  12. #include <ArduinoJson.h>
  13. #include <ESP8266WiFi.h>
  14. #include <ESP8266mDNS.h>
  15. #include <ESP8266WebServer.h>
  16. #include "LittleFS.h"
  17. // Device Configs, required by HDSv3
  18. #define DEVICE_NAME "hds_4xRelay" //The name of this IoT device
  19. #define LISTENING_PORT 12110 //The port where this IoT device listen
  20. String deviceUUID = ""; //Device UUID is generated base on devcie MAC address
  21. // Output shift register pints
  22. const int RELAY_1 = D0;
  23. const int RELAY_2 = D5;
  24. const int RELAY_3 = D6;
  25. const int RELAY_4 = D7;
  26. bool relayStatus[4] = {false, false, false, false};
  27. //WiFi Related
  28. WiFiManager wifiManager;
  29. ESP8266WebServer server(LISTENING_PORT);
  30. //Update relay status update the relay physical state base on relay status array
  31. void updateRelayStatus(){
  32. if (relayStatus[0]){
  33. digitalWrite(RELAY_1, HIGH);
  34. }else{
  35. digitalWrite(RELAY_1, LOW);
  36. }
  37. if (relayStatus[1]){
  38. digitalWrite(RELAY_2, HIGH);
  39. }else{
  40. digitalWrite(RELAY_2, LOW);
  41. }
  42. if (relayStatus[2]){
  43. digitalWrite(RELAY_3, HIGH);
  44. }else{
  45. digitalWrite(RELAY_3, LOW);
  46. }
  47. if (relayStatus[3]){
  48. digitalWrite(RELAY_4, HIGH);
  49. }else{
  50. digitalWrite(RELAY_4, LOW);
  51. }
  52. }
  53. void setup() {
  54. Serial.begin(115200);
  55. //Load device UUID
  56. deviceUUID = WiFi.macAddress();
  57. deviceUUID.replace(":", "-");
  58. //Initialize the pins. Set all to low
  59. pinMode(RELAY_1, OUTPUT);
  60. digitalWrite(RELAY_1, LOW);
  61. pinMode(RELAY_2, OUTPUT);
  62. digitalWrite(RELAY_2, LOW);
  63. pinMode(RELAY_3, OUTPUT);
  64. digitalWrite(RELAY_3, LOW);
  65. pinMode(RELAY_4, OUTPUT);
  66. digitalWrite(RELAY_4, LOW);
  67. //Start HDSv2 services
  68. initHds3Services();
  69. }
  70. void loop() {
  71. server.handleClient();
  72. MDNS.update();
  73. }