Advertisement
Guest User

Untitled

a guest
Mar 25th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.33 KB | None | 0 0
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. #define MQTT_VERSION MQTT_VERSION_3_1_1
  5.  
  6. // Wifi: SSID and password
  7. const char* WIFI_SSID = "ssid";
  8. const char* WIFI_PASSWORD = "password";
  9.  
  10. // MQTT: ID, server IP, port, username and password
  11. const PROGMEM char* MQTT_CLIENT_ID = "growroom_growlight2";
  12. const PROGMEM char* MQTT_SERVER_IP = "192.168.0.116";
  13. const PROGMEM uint16_t MQTT_SERVER_PORT = 1883;
  14. const PROGMEM char* MQTT_USER = "mqtt_admin";
  15. const PROGMEM char* MQTT_PASSWORD = "pass";
  16.  
  17. // MQTT: topics
  18. // brightness
  19. const PROGMEM char* MQTT_LIGHT_BRIGHTNESS_STATE_TOPIC = "growroom/growlight1/brightness";
  20. const PROGMEM char* MQTT_LIGHT_BRIGHTNESS_COMMAND_TOPIC = "growroom/growlight1/brightness/set";
  21.  
  22. // variables used to store the state, the brightness and the color of the light
  23. uint8_t m_rgb_brightness = 100;
  24.  
  25. const PROGMEM uint8_t BRIGHTNESS_PIN = D0;
  26.  
  27. // buffer used to send/receive data with MQTT
  28. const uint8_t MSG_BUFFER_SIZE = 20;
  29. char m_msg_buffer[MSG_BUFFER_SIZE];
  30.  
  31. WiFiClient wifiClient;
  32. PubSubClient client(wifiClient);
  33.  
  34. // function called to publish the brightness of the led (0-100)
  35. void publishRGBBrightness() {
  36. snprintf(m_msg_buffer, MSG_BUFFER_SIZE, "%d", m_rgb_brightness);
  37. client.publish(MQTT_LIGHT_BRIGHTNESS_STATE_TOPIC, m_msg_buffer, true);
  38. }
  39.  
  40. // function called when a MQTT message arrived
  41. void callback(char* p_topic, byte* p_payload, unsigned int p_length) {
  42. // concat the payload into a string
  43. String payload;
  44. for (uint8_t i = 0; i < p_length; i++) {
  45. payload.concat((char)p_payload[i]);
  46. }
  47. Serial.println(payload);
  48. if (String(MQTT_LIGHT_BRIGHTNESS_COMMAND_TOPIC).equals(p_topic)) {
  49. uint8_t brightness = payload.toInt();
  50. m_rgb_brightness = brightness;
  51. if (brightness < 0 || brightness > 255) {
  52. // do nothing...
  53. return;
  54. } else {
  55. analogWrite(BRIGHTNESS_PIN, map(brightness, 0, 255, 1023, 0));
  56. Serial.println("BRIGHTNESS: " + String(map(brightness, 0, 255, 1023, 0)));
  57. publishRGBBrightness();
  58. }
  59. }
  60. }
  61.  
  62. void reconnect() {
  63. // Loop until we're reconnected
  64. while (!client.connected()) {
  65. Serial.println("INFO: Attempting MQTT connection...");
  66. // Attempt to connect
  67. if (client.connect(MQTT_CLIENT_ID, MQTT_USER, MQTT_PASSWORD)) {
  68. Serial.println("INFO: connected");
  69.  
  70. // Once connected, publish an announcement...
  71. // publish the initial values
  72. publishRGBBrightness();
  73.  
  74. // ... and resubscribe
  75. client.subscribe(MQTT_LIGHT_BRIGHTNESS_COMMAND_TOPIC);
  76. } else {
  77. Serial.print("ERROR: failed, rc=");
  78. Serial.print(client.state());
  79. Serial.println("DEBUG: try again in 5 seconds");
  80. // Wait 5 seconds before retrying
  81. delay(5000);
  82. }
  83. }
  84. }
  85.  
  86. void setup() {
  87. // init the serial
  88. Serial.begin(115200);
  89.  
  90. // init the WiFi connection
  91. Serial.println();
  92. Serial.println();
  93. Serial.print("INFO: Connecting to ");
  94. WiFi.mode(WIFI_STA);
  95. Serial.println(WIFI_SSID);
  96. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  97.  
  98. while (WiFi.status() != WL_CONNECTED) {
  99. delay(500);
  100. Serial.print(".");
  101. }
  102.  
  103. Serial.println("");
  104. Serial.println("INFO: WiFi connected");
  105. Serial.print("INFO: IP address: ");
  106. Serial.println(WiFi.localIP());
  107.  
  108. // init the MQTT connection
  109. client.setServer(MQTT_SERVER_IP, MQTT_SERVER_PORT);
  110. client.setCallback(callback);
  111. }
  112.  
  113. void loop() {
  114. if (!client.connected()) {
  115. reconnect();
  116. }
  117. client.loop();
  118. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement