Created
August 14, 2020 19:57
-
-
Save matthinc/0cfa0f26b2bef03c25c3a6b25fe0ca86 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <Arduino.h> | |
#include <ESP8266WiFi.h> | |
#include <MQTT.h> | |
#include <stdint.h> | |
// Configuration | |
#define WIFI_SSID "" | |
#define WIFI_PASSWORD "" | |
#define MQTT_BROKER "192.168.1.70" | |
#define MQTT_CLIENT_ID "ledstrip1" | |
#define MQTT_USERNAME "" | |
#define MQTT_PASSWORD "" | |
#define MQTT_TOPIC "/leds1" | |
#define LED_TOPIC_1 MQTT_TOPIC "/1" | |
#define LED_TOPIC_2 MQTT_TOPIC "/2" | |
#define LED_TOPIC_3 MQTT_TOPIC "/3" | |
#define CLK_ANIMATE 2 | |
#define CLK_WRITE 10 | |
WiFiClient net; | |
MQTTClient client; | |
// Led status | |
uint16_t led_status[3] = {0, 0, 0}; | |
uint16_t led_target[3] = {0, 0, 0}; | |
// Timing | |
uint32_t last_millis_animate = 0; | |
uint32_t last_millis_write = 0; | |
void connect_mqtt() { | |
// Make sure wifi is connected | |
while (WiFi.status() != WL_CONNECTED) delay(500); | |
// Connect client | |
client.connect(MQTT_BROKER, MQTT_USERNAME, MQTT_PASSWORD); | |
// Subscribe to all led topics | |
client.subscribe(LED_TOPIC_1); | |
client.subscribe(LED_TOPIC_2); | |
client.subscribe(LED_TOPIC_3); | |
// Publish ready message | |
client.publish(MQTT_TOPIC, "Connected."); | |
} | |
static inline void reconnect_if_required() { | |
if (!client.connected()) connect_mqtt(); | |
} | |
void reset_leds() { | |
analogWrite(D5, 0); | |
analogWrite(D6, 0); | |
analogWrite(D7, 0); | |
} | |
void messageReceived(String &topic, String &payload) { | |
uint16_t value = payload.toInt(); | |
if (topic == LED_TOPIC_1) led_target[0] = value; | |
if (topic == LED_TOPIC_2) led_target[1] = value; | |
if (topic == LED_TOPIC_3) led_target[2] = value; | |
} | |
void setup() { | |
// Init LEDs | |
pinMode(D5, OUTPUT); | |
pinMode(D6, OUTPUT); | |
pinMode(D7, OUTPUT); | |
// Connect WIFI | |
WiFi.begin(WIFI_SSID, WIFI_PASSWORD); | |
// Connect MQTT | |
client.begin(MQTT_BROKER, net); | |
client.onMessage(messageReceived); | |
// First connection | |
connect_mqtt(); | |
// Turn all LEDs off | |
reset_leds(); | |
} | |
void loop() { | |
client.loop(); | |
reconnect_if_required(); | |
uint32_t current_time = millis(); | |
if (current_time - last_millis_animate >= CLK_ANIMATE) { | |
// Update status | |
for (uint8_t led = 0; led < 3; led++) { | |
if (led_status[led] < led_target[led]) led_status[led] += 1; | |
if (led_status[led] > led_target[led]) led_status[led] -= 1; | |
} | |
last_millis_animate = current_time; | |
} | |
if (current_time - last_millis_write >= CLK_WRITE) { | |
// Write LED-value | |
analogWrite(D5, led_status[0]); | |
analogWrite(D6, led_status[1]); | |
analogWrite(D7, led_status[2]); | |
last_millis_write = current_time; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment