summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--esp-ledstrip.ino34
1 files changed, 21 insertions, 13 deletions
diff --git a/esp-ledstrip.ino b/esp-ledstrip.ino
index 3404f85..914cdf5 100644
--- a/esp-ledstrip.ino
+++ b/esp-ledstrip.ino
@@ -25,21 +25,23 @@
const char* ssid = "";
const char* password = "";
-#define ME "esplamp1"
+#define ME "esplamp-1"
#ifdef WITH_MQTT
-const char* mqtt_server = "mqtt.example.net";
+const char* mqtt_server = "";
#endif
#ifdef WITH_UDP
const int udpPort = 2391;
+IPAddress multicastAddr(239, 1, 0, 0);
#endif
#ifdef WITH_MQTT
const char* connectTopic = "api/" ME "/connected";
const char* outTopic = "api/" ME "/status/rgb";
const char* inTopic = "api/" ME "/set/rgb";
+const char* updateTopic = "api/" ME "/get/rgb";
#endif
// R G B
-const int leds[] = {2, 0, 3}; // !TX is inverted, not used here
+const int leds[] = {2, 0, 3}; // remember: TX is inverted, not used here
unsigned int state[] = {0, 0, 0};
unsigned char cstate[] = {0, 0, 0};
@@ -66,13 +68,15 @@ void writeJson() {
root.printTo(cJson, sizeof(cJson));
}
-void updateLeds() {
+void updateLeds(bool fast) {
for(int i=0; i<3; i++)
analogWrite(leds[i], state[i]);
- writeJson();
+ if (!fast) {
+ writeJson();
#ifdef WITH_MQTT
- if (mqtt.connected()) mqtt.publish(outTopic, cJson, true);
+ if (mqtt.connected()) mqtt.publish(outTopic, cJson, true);
#endif
+ }
}
#ifdef WITH_HTTP
@@ -85,7 +89,7 @@ void handleRoot() {
if(server.hasArg("blue"))
state[2] = server.arg("blue").toInt();
- updateLeds();
+ updateLeds(false);
server.send(200, "application/json", cJson);
}
#endif
@@ -99,7 +103,10 @@ void handleMqtt(char* topic, byte* payload, unsigned int length) {
state[0] = root["red"];
state[1] = root["green"];
state[2] = root["blue"];
- updateLeds();
+ updateLeds(false);
+ }
+ if (strcmp(topic, updateTopic) == 0) {
+ updateLeds(false); // force update /rgb information
}
}
@@ -109,24 +116,25 @@ void connectMqtt() {
// topic, msg, retain
mqtt.publish(connectTopic, "online", true);
mqtt.subscribe(inTopic);
+ mqtt.subscribe(updateTopic);
}
}
#endif
#ifdef WITH_UDP
/* UDP API */
+// expected to be most time critical
void handleUdp() {
if (!RAWudp)
- RAWudp.begin(udpPort);
+ RAWudp.beginMulticast(WiFi.localIP(), multicastAddr, udpPort);
if (!RAWudp)
return;
-
- int cb = RAWudp.parsePacket();
- if (cb) {
+
+ if (RAWudp.parsePacket()) {
RAWudp.read(cstate, 3);
for(int i=0; i<3; i++)
state[i] = cstate[i] << 2;
- updateLeds();
+ updateLeds(true); // skip publishing
}
}
#endif