5xswitches.ino 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. Home Dynamic System v3
  3. 5-way wall switches
  4. Hardware reference design
  5. Author: tobychui
  6. */
  7. #include <WiFiManager.h>
  8. #include <ArduinoJson.h>
  9. #include <ESP8266WiFi.h>
  10. #include <ESP8266mDNS.h>
  11. #include <ESP8266WebServer.h>
  12. #include <ESP8266HTTPClient.h>
  13. #include "TaskScheduler.h"
  14. #include "LittleFS.h"
  15. // Device Configs, required by HDSv3
  16. #define DEVICE_NAME "hds_5xSwitch" //The name of this IoT device
  17. #define SETUP_WIFI_NAME "hds_5xSwitch"
  18. #define LISTENING_PORT 12110 //The port where this IoT device listen
  19. #define DEBUG true
  20. String deviceUUID = ""; //Device UUID is generated base on devcie MAC address
  21. // Output shift register pints
  22. const int SRCLK_PIN = D7;
  23. const int RCLK_PIN = D1;
  24. const int SER_PIN = D2;
  25. // Input button pins (The last one must be A0 pin)
  26. bool buttonStates[5] = {false, false, false, false, false};
  27. const int buttonPins[] = {D4, D6, D5, D0, A0};
  28. const int numButtons = 5;
  29. //Modify these two control other devices
  30. #define TARGET_DEVICE "hds_4xRelay_48-3F-DA-67-3D-BD.local"
  31. IPAddress target_ip;
  32. uint16_t target_port = 0;
  33. // Status led
  34. #define BLINK_DURATION 100
  35. bool blueLED = false;
  36. bool yellowLED = false;
  37. bool redLED = false;
  38. //Runtimes
  39. Scheduler deviceScheduler;
  40. //WiFi Related
  41. WiFiManager wifiManager;
  42. ESP8266WebServer server(LISTENING_PORT);
  43. void setup() {
  44. //Enable debug Serial
  45. Serial.begin(115200);
  46. //Load device UUID
  47. deviceUUID = WiFi.macAddress();
  48. deviceUUID.replace(":", "-");
  49. // Initialize shift register pins
  50. pinMode(SRCLK_PIN, OUTPUT);
  51. pinMode(RCLK_PIN, OUTPUT);
  52. pinMode(SER_PIN, OUTPUT);
  53. // Initialize button pins
  54. for (int i = 0; i < numButtons; ++i) {
  55. pinMode(buttonPins[i], INPUT_PULLUP);
  56. }
  57. //Reset the state of all LEDs
  58. resetOutputs();
  59. //Start HDSv3 services
  60. initHds3Services();
  61. //Start discovering the target devices ip
  62. resolveTargetRelayIP();
  63. }
  64. void loop() {
  65. //Handle web-ish things
  66. server.handleClient();
  67. MDNS.update();
  68. //Update and Execute Button Events
  69. updateButtonStates();
  70. updateShiftRegister();
  71. //Show yellow warning LED if target not found
  72. if (target_port == 0){
  73. yellowLED = true;
  74. }else{
  75. yellowLED = false;
  76. }
  77. }