Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <PubSubClient.h>
- #define MQTT_VERSION MQTT_VERSION_3_1_1
- // Wifi: SSID and password
- const char* WIFI_SSID = "ssid";
- const char* WIFI_PASSWORD = "password";
- // MQTT: ID, server IP, port, username and password
- const PROGMEM char* MQTT_CLIENT_ID = "growroom_growlight2";
- const PROGMEM char* MQTT_SERVER_IP = "192.168.0.116";
- const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
- const PROGMEM char* MQTT_USER = "mqtt_admin";
- const PROGMEM char* MQTT_PASSWORD = "pass";
- // MQTT: topics
- // brightness
- const PROGMEM char* MQTT_LIGHT_BRIGHTNESS_STATE_TOPIC = "growroom/growlight1/brightness";
- const PROGMEM char* MQTT_LIGHT_BRIGHTNESS_COMMAND_TOPIC = "growroom/growlight1/brightness/set";
- // variables used to store the state, the brightness and the color of the light
- uint8_t m_rgb_brightness = 100;
- const PROGMEM uint8_t BRIGHTNESS_PIN = D0;
- // buffer used to send/receive data with MQTT
- const uint8_t MSG_BUFFER_SIZE = 20;
- char m_msg_buffer[MSG_BUFFER_SIZE];
- WiFiClient wifiClient;
- PubSubClient client(wifiClient);
- // function called to publish the brightness of the led (0-100)
- void publishRGBBrightness() {
- snprintf(m_msg_buffer, MSG_BUFFER_SIZE, "%d", m_rgb_brightness);
- client.publish(MQTT_LIGHT_BRIGHTNESS_STATE_TOPIC, m_msg_buffer, true);
- }
- // function called when a MQTT message arrived
- void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
- // concat the payload into a string
- String payload;
- for (uint8_t i = 0; i < p_length; i++) {
- payload.concat((char)p_payload[i]);
- }
- Serial.println(payload);
- if (String(MQTT_LIGHT_BRIGHTNESS_COMMAND_TOPIC).equals(p_topic)) {
- uint8_t brightness = payload.toInt();
- m_rgb_brightness = brightness;
- if (brightness < 0 || brightness > 255) {
- // do nothing...
- return;
- } else {
- analogWrite(BRIGHTNESS_PIN, map(brightness, 0, 255, 1023, 0));
- Serial.println("BRIGHTNESS: " + String(map(brightness, 0, 255, 1023, 0)));
- publishRGBBrightness();
- }
- }
- }
- void reconnect() {
- // Loop until we're reconnected
- while (!client.connected()) {
- Serial.println("INFO: Attempting MQTT connection...");
- // Attempt to connect
- if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
- Serial.println("INFO: connected");
- // Once connected, publish an announcement...
- // publish the initial values
- publishRGBBrightness();
- // ... and resubscribe
- client.subscribe(MQTT_LIGHT_BRIGHTNESS_COMMAND_TOPIC);
- } else {
- Serial.print("ERROR: failed, rc=");
- Serial.print(client.state());
- Serial.println("DEBUG: try again in 5 seconds");
- // Wait 5 seconds before retrying
- delay(5000);
- }
- }
- }
- void setup() {
- // init the serial
- Serial.begin(115200);
- // init the WiFi connection
- Serial.println();
- Serial.println();
- Serial.print("INFO: Connecting to ");
- WiFi.mode(WIFI_STA);
- Serial.println(WIFI_SSID);
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("INFO: WiFi connected");
- Serial.print("INFO: IP address: ");
- Serial.println(WiFi.localIP());
- // init the MQTT connection
- client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
- client.setCallback(callback);
- }
- void loop() {
- if (!client.connected()) {
- reconnect();
- }
- client.loop();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement