1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798 |
- /*
- Home Dynamic System v3
- 5-way wall switches
- Hardware reference design
- Author: tobychui
- */
- #include <WiFiManager.h>
- #include <ArduinoJson.h>
- #include <ESP8266WiFi.h>
- #include <ESP8266mDNS.h>
- #include <ESP8266WebServer.h>
- #include <ESP8266HTTPClient.h>
- #include "TaskScheduler.h"
- #include "LittleFS.h"
- // Device Configs, required by HDSv3
- #define DEVICE_NAME "hds_5xSwitch" //The name of this IoT device
- #define SETUP_WIFI_NAME "hds_5xSwitch"
- #define LISTENING_PORT 12110 //The port where this IoT device listen
- #define DEBUG true
- String deviceUUID = ""; //Device UUID is generated base on devcie MAC address
- // Output shift register pints
- const int SRCLK_PIN = D7;
- const int RCLK_PIN = D1;
- const int SER_PIN = D2;
- // Input button pins (The last one must be A0 pin)
- bool buttonStates[5] = {false, false, false, false, false};
- const int buttonPins[] = {D4, D6, D5, D0, A0};
- const int numButtons = 5;
- //Modify these two control other devices
- #define TARGET_DEVICE "hds_4xRelay_48-3F-DA-67-3D-BD.local"
- IPAddress target_ip;
- uint16_t target_port = 0;
- // Status led
- #define BLINK_DURATION 100
- bool blueLED = false;
- bool yellowLED = false;
- bool redLED = false;
- //Runtimes
- Scheduler deviceScheduler;
- //WiFi Related
- WiFiManager wifiManager;
- ESP8266WebServer server(LISTENING_PORT);
- void setup() {
- //Enable debug Serial
- Serial.begin(115200);
- //Load device UUID
- deviceUUID = WiFi.macAddress();
- deviceUUID.replace(":", "-");
- // Initialize shift register pins
- pinMode(SRCLK_PIN, OUTPUT);
- pinMode(RCLK_PIN, OUTPUT);
- pinMode(SER_PIN, OUTPUT);
- // Initialize button pins
- for (int i = 0; i < numButtons; ++i) {
- pinMode(buttonPins[i], INPUT_PULLUP);
- }
- //Reset the state of all LEDs
- resetOutputs();
- //Start HDSv3 services
- initHds3Services();
- //Start discovering the target devices ip
- resolveTargetRelayIP();
- }
- void loop() {
- //Handle web-ish things
- server.handleClient();
- MDNS.update();
- //Update and Execute Button Events
- updateButtonStates();
- updateShiftRegister();
- //Show yellow warning LED if target not found
- if (target_port == 0){
- yellowLED = true;
- }else{
- yellowLED = false;
- }
- }
|