Forráskód Böngészése

Added API server implementation

Toby Chui 1 hónapja
szülő
commit
bd6a319e09

+ 6 - 6
firmware/rgbfill/buttons.ino

@@ -118,10 +118,10 @@ void HandleAddButtonPress() {
   if (currentMode == 0) {
     //White Color Mode
     if (adjustingCatergory == 0) {
-      //add 500 to K value
+      //add to K value
       values[0] = values[0] + 100;
-      if (values[0] > 10000) {
-        values[0] = 10000;
+      if (values[0] > WHITE_MAX_TEMP) {
+        values[0] = WHITE_MAX_TEMP;
         blinkUpperLimit();
       }
       Serial.print("Updating color temperature to ");
@@ -149,10 +149,10 @@ void HandleMinusButtonPress() {
   if (currentMode == 0) {
     //White Color Mode
     if (adjustingCatergory == 0) {
-      //reduce 500 from K value
+      //reduce from K value
       values[0] = values[0] - 100;
-      if (values[0] < 2500) {
-        values[0] = 2500;
+      if (values[0] < WHITE_MIN_TEMP) {
+        values[0] = WHITE_MIN_TEMP;
         blinkLowerLimit();
       }
 

+ 161 - 0
firmware/rgbfill/discovery.ino

@@ -0,0 +1,161 @@
+//Inject zeroconf attr into the MDNS respond (For scanning by ArozOS)
+void MDNSDynamicServiceTxtCallback(const MDNSResponder::hMDNSService p_hService) {
+  //Define the domain of the HDSv2 devices
+  MDNS.addDynamicServiceTxt(p_hService, "domain", "fl1010-rgb.hds.imuslab.com");
+  MDNS.addDynamicServiceTxt(p_hService, "protocol", "hdsv3");
+
+  //Define the OEM written values
+  MDNS.addDynamicServiceTxt(p_hService, "uuid", deviceUUID.c_str());
+  MDNS.addDynamicServiceTxt(p_hService, "name", (String(MDNS_NAME) + "_" + deviceUUID).c_str());
+  MDNS.addDynamicServiceTxt(p_hService, "model", DEVICE_NAME);
+  MDNS.addDynamicServiceTxt(p_hService, "vendor", "imuslab");
+  MDNS.addDynamicServiceTxt(p_hService, "version", "1.00");
+}
+
+void hostProbeResult(String p_pcDomainName, bool p_bProbeResult) {
+  MDNS.setDynamicServiceTxtCallback(MDNSDynamicServiceTxtCallback);
+}
+
+//Handlers for Web Server
+void handle_index() {
+  server.send(200, "text/html", "[OK] RGB Fill Light - WiFi Mode API Server");
+}
+
+
+void handle_status() {
+  DynamicJsonDocument doc(256);
+  doc["r"] = values[0];
+  doc["g"] = values[1];
+  doc["b"] = values[2];
+
+  String json;
+  serializeJson(doc, json);
+  server.send(200, "application/json", json.c_str());
+}
+
+void handle_uuid() {
+  server.send(200, "application/json", "\"" + deviceUUID + "\"");
+}
+
+void handle_setRGB() {
+  String r = server.arg("r");
+  String g = server.arg("g");
+  String b = server.arg("b");
+
+  if (r.length() > 0) {
+    int intR = r.toInt();
+    if (intR < 0 || intR > 255) {
+      server.send(200, "application/json", "{\"error\":\"invalid r (red) value given\"}");
+      return;
+    }
+
+    values[0] = intR;
+  }
+
+  if (g.length() > 0) {
+    int intG = g.toInt();
+    if (intG < 0 || intG > 255) {
+      server.send(200, "application/json", "{\"error\":\"invalid g (green) value given\"}");
+      return;
+    }
+
+    values[1] = intG;
+  }
+
+  if (b.length() > 0) {
+    int intB = b.toInt();
+    if (intB < 0 || intB > 255) {
+      server.send(200, "application/json", "{\"error\":\"invalid b (blue) value given\"}");
+      return;
+    }
+
+    values[2] = intB;
+  }
+  //Update light color
+  setLightColor(values[0], values[1], values[2]);
+
+  //Return ok
+  server.send(200, "application/json", "\"ok\"");
+}
+
+void handle_setColorTemp() {
+  String k = server.arg("k");
+  String b = server.arg("b");
+
+  int intK = 5000;
+  if (k.length() > 0) {
+    intK = k.toInt();
+    if (intK < WHITE_MIN_TEMP) {
+      server.send(200, "application/json", "{\"error\":\"color temperature too low\"}");
+      return;
+    } else if (intK > WHITE_MAX_TEMP) {
+      server.send(200, "application/json", "{\"error\":\"color temperature too high\"}");
+      return;
+    }
+  } else {
+    server.send(200, "application/json", "{\"error\":\"k (color temperature) is not set\"}");
+    return;
+  }
+
+  int brightness = 128; //50%
+  if (b.length() > 0) {
+    int intB = b.toInt();
+    if (intB < 0 || intB > 255) {
+      server.send(200, "application/json", "{\"error\":\"invalid b (brightness) value given\"}");
+      return;
+    }
+
+    brightness = intB;
+  }
+
+  //Set color temperature
+  setColorTemperature(intK, brightness);
+
+  //Return ok
+  server.send(200, "application/json", "\"ok\"");
+}
+
+void handle_off() {
+  //Set RGB to 0
+  //Update light color
+  values[0] = 0;
+  values[1] = 0;
+  values[2] = 0;
+  setLightColor(values[0], values[1], values[2]);
+
+  //Return ok
+  server.send(200, "application/json", "\"ok\"");
+}
+
+void registerAPI() {
+  // Connect to Wi-Fi
+  Serial.print("Connected to ");
+  Serial.println(WiFi.SSID());
+  Serial.print("IP address:\t");
+  Serial.println(WiFi.localIP());
+
+
+  //Startup MDNS Responder
+  MDNS.setHostProbeResultCallback(hostProbeResult);
+  if (!MDNS.begin((String(MDNS_NAME) + "_" + deviceUUID).c_str())) {
+    Serial.println("Error setting up MDNS responder!");
+  }
+
+  //Advertise the port that you are using
+  MDNS.addService("http", "tcp", LISTENING_PORT);
+  Serial.println("mDNS responder started");
+
+  //Restful API
+  server.on("/", handle_index);
+  server.on("/status", handle_status);
+  server.on("/uuid", handle_uuid);
+  server.on("/api/rgb", handle_setRGB);
+  server.on("/api/temp", handle_setColorTemp);
+  server.on("/api/off", handle_off);
+
+  delay(100);
+  server.begin();
+  Serial.println("Restful API server started");
+  Serial.print("Listening on port: ");
+  Serial.println(LISTENING_PORT);
+}

+ 67 - 9
firmware/rgbfill/rgbfill.ino

@@ -8,9 +8,20 @@
 
     Recommend compiling with following board profiles
     - Wemos D1 Mini
+    - CPU Speed: 160Mhz
 
-
+    CopyRight tobychui, All Right Reserved
 */
+
+/* WiFi and Discovery */
+#include <ESP8266WiFi.h>
+#include <WiFiManager.h>
+#include <ESP8266mDNS.h>
+#include <ESP8266WebServer.h>
+#include <ArduinoJson.h>
+#include "LittleFS.h"
+
+/* Hardware & Controls */
 #include <Adafruit_NeoPixel.h>
 
 // Hardware Configs
@@ -21,12 +32,23 @@
 #define BUTTON_COLOR D5
 #define BUTTON_MINUS D6
 
-#define BUTTON_AUTOINC_DELAY 300 //Delay before auto increments
-#define BUTTON_HOLD_DELAY 50 //Auto-increment delays
-#define BUTTON_DEBOUNCE 50 //Naive debounce delay in ms
+#define BUTTON_AUTOINC_DELAY 500 //Delay before auto increments
+#define BUTTON_HOLD_DELAY 70 //Auto-increment delays
+#define BUTTON_DEBOUNCE 70 //Naive debounce delay in ms
 #define MAX_BRIGHTNESS 256 //Make sure the battery you using can output the current required by LEDs
 #define MAX_CTRLBRIGHTNESS 32 //Max brightness for signaling LED (the one above the button)
+#define WHITE_MIN_TEMP 2500 //Min color temperature in white mode
+#define WHITE_MAX_TEMP 10000 //Max color temperature in white mode
+
 //Runtimes
+/* Network Settings */
+#define DEVICE_NAME "FL1010-RGB"
+#define MDNS_NAME "fl1010-rgb"
+#define LISTENING_PORT 80
+WiFiManager wifiManager;
+ESP8266WebServer server(LISTENING_PORT);
+String deviceUUID = "";
+
 /*
    Current Mode
    0 = White Mode (+/- Change the K value of the light)
@@ -67,12 +89,14 @@ int adjustingCatergory = 0;
 
 */
 int values[] = {0, 0, 0};
-int currentCtrlLedRgb[] = {0,0,0};
+int currentCtrlLedRgb[] = {0, 0, 0};
+
+bool wifiControlMode = false;
 
 Adafruit_NeoPixel strip = Adafruit_NeoPixel(NUM_LEDS, PIN, NEO_GRB + NEO_KHZ800);
 
 //Set the current control LED color
-void setControlLEDColor(int r, int g, int b){
+void setControlLEDColor(int r, int g, int b) {
   currentCtrlLedRgb[0] = r;
   currentCtrlLedRgb[1] = g;
   currentCtrlLedRgb[2] = b;
@@ -88,6 +112,7 @@ void setLightColor(int r, int g, int b) {
   strip.show();
 }
 void setup() {
+  //Start Serial
   Serial.begin(115200);
 
   //Set the buttons to input pin
@@ -96,11 +121,44 @@ void setup() {
   pinMode(BUTTON_COLOR, INPUT);
   pinMode(BUTTON_MINUS, INPUT);
   strip.begin();
+  delay(300);
+  setLightColor(0, 0, 0);
+
+  //Check if mode button is hold. If yes, enter WiFi mode
+  int tmp = digitalRead(BUTTON_MODE);
+  if (tmp == LOW) {
+    //Load device UUID
+    deviceUUID = WiFi.macAddress();
+    deviceUUID.replace(":", "-");
+
+    //Setup WiFi
+    setControlLEDColor(255, 255, 0); //Yellow
+    Serial.println("Entering WiFi Mode");
+    wifiManager.autoConnect(DEVICE_NAME);
+    if (WiFi.status() == WL_CONNECTED) {
+      Serial.println("WiFi Connected");
+      setControlLEDColor(0, 32, 0); //Green
+      wifiControlMode = true;
+
+      //Start API service over WiFi
+      registerAPI();
+    } else {
+      Serial.println("Network error. Running in offline mode");
+      setControlLEDColor(255, 0, 0); //Red
+    }
+  } else {
+    // Initialize all pixels to natural warm white light
+    loadWhiteModeDefault();
+  }
+
 
-  // Initialize all pixels to natural warm white light
-  loadWhiteModeDefault();
 }
 
 void loop() {
-  handleButtonLogic();
+  if (wifiControlMode){
+    server.handleClient();
+    MDNS.update();
+  }else{
+    handleButtonLogic();
+  }
 }

BIN
model/v7/lockfile.lck


BIN
model/v7/phone-mount.ipt


BIN
model/v7/phone-mount.stl