|
@@ -8,31 +8,21 @@
|
|
|
Author: tobychui
|
|
|
*/
|
|
|
|
|
|
-/*
|
|
|
- *
|
|
|
- * DEVICE SETTINGS
|
|
|
- *
|
|
|
- * Update the configurations below before flashing the firmware to
|
|
|
- * your HDSv3 devices
|
|
|
- *
|
|
|
- */
|
|
|
-
|
|
|
-//Mesh settings (for HDSv3)
|
|
|
-#define MESH_PREFIX "homedynamicv3"
|
|
|
-#define MESH_PASSWORD "verySecurePassword"
|
|
|
-#define MESH_PORT 9640
|
|
|
-
|
|
|
-//Change these to fit your needs
|
|
|
-uint32_t TARGET_RELAY = 3664199101;
|
|
|
-
|
|
|
-/*
|
|
|
- *
|
|
|
- * END OF DEVICE SETTINGS
|
|
|
- *
|
|
|
- */
|
|
|
-
|
|
|
-//Mesh networking
|
|
|
-#include "painlessMesh.h"
|
|
|
+#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;
|
|
@@ -44,30 +34,33 @@ 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;
|
|
|
-painlessMesh mesh;
|
|
|
|
|
|
-//System Tasks
|
|
|
-void updateButtonStates();
|
|
|
-void buttonPressCallbacks();
|
|
|
-void sendMessage();
|
|
|
-void handleSignalLEDBlinks();
|
|
|
-Task taskButtons(10, TASK_FOREVER, &buttonPressCallbacks);
|
|
|
-Task taskSendMessage( TASK_SECOND * 1 , TASK_FOREVER, &sendMessage );
|
|
|
-Task taskLEDsBlink( BLINK_DURATION * TASK_MILLISECOND , 2, &handleSignalLEDBlinks );
|
|
|
+//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);
|
|
@@ -81,25 +74,25 @@ void setup() {
|
|
|
//Reset the state of all LEDs
|
|
|
resetOutputs();
|
|
|
|
|
|
- //Start mesh networking
|
|
|
- mesh.setDebugMsgTypes( ERROR | STARTUP ); // set before init() so that you can see startup messages
|
|
|
-
|
|
|
- mesh.init( MESH_PREFIX, MESH_PASSWORD, &deviceScheduler, MESH_PORT );
|
|
|
- mesh.onReceive(&receivedCallback);
|
|
|
- mesh.onNewConnection(&newConnectionCallback);
|
|
|
- mesh.onChangedConnections(&changedConnectionCallback);
|
|
|
- mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
|
|
|
-
|
|
|
- //Add tasks to scheduler
|
|
|
- deviceScheduler.addTask(taskSendMessage);
|
|
|
- deviceScheduler.addTask(taskButtons);
|
|
|
- deviceScheduler.addTask(taskLEDsBlink);
|
|
|
-
|
|
|
- //Enable tasks
|
|
|
- taskSendMessage.enable();
|
|
|
- taskButtons.enable();
|
|
|
+ //Start HDSv3 services
|
|
|
+ initHds3Services();
|
|
|
+
|
|
|
+ //Start discovering the target devices ip
|
|
|
+ resolveTargetRelayIP();
|
|
|
}
|
|
|
|
|
|
void loop() {
|
|
|
- mesh.update();
|
|
|
+ //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;
|
|
|
+ }
|
|
|
}
|