hendriawan

02-MQTT PUB AND multiple SUB arduino

Nov 30th, 2020 (edited)
813
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3. #define LED1 2
  4. #define LED2 16
  5. // Update these with values suitable for your network.
  6.  
  7. const char* ssid = "mywifi";
  8. const char* password = "123456789";
  9. //const char* mqtt_server = "broker.mqtt-dashboard.com";
  10. const char* mqtt_server = "192.168.1.6";
  11.  
  12. WiFiClient espClient;
  13. PubSubClient client(espClient);
  14. unsigned long lastMsg = 0;
  15. #define MSG_BUFFER_SIZE (50)
  16. char msg[MSG_BUFFER_SIZE];
  17. int value = 20;
  18.  
  19. void setup_wifi() {
  20.  
  21.   delay(10);
  22.   // We start by connecting to a WiFi network
  23.   Serial.println();
  24.   Serial.print("Connecting to ");
  25.   Serial.println(ssid);
  26.  
  27.   WiFi.mode(WIFI_STA);
  28.   WiFi.begin(ssid, password);
  29.  
  30.   while (WiFi.status() != WL_CONNECTED) {
  31.     delay(500);
  32.     Serial.print(".");
  33.   }
  34.  
  35.   randomSeed(micros());
  36.  
  37.   Serial.println("");
  38.   Serial.println("WiFi connected");
  39.   Serial.println("IP address: ");
  40.   Serial.println(WiFi.localIP());
  41. }
  42.  
  43. void callback(char* topic, byte* payload, unsigned int length) {
  44.   Serial.print("Message arrived [");
  45.   Serial.print(topic);
  46.   Serial.print("] ");
  47.   for (int i = 0; i < length; i++) {
  48.     Serial.print((char)payload[i]);
  49.   }
  50.   Serial.println();
  51.  
  52.   if (strcmp(topic, "room/lamp1") == 0) {
  53.     // Switch on the LED if an 1 was received as first character
  54.     if ((char)payload[0] == '1') {
  55.       digitalWrite(LED2, LOW);   // Turn the LED on (Note that LOW is the voltage level
  56.     } else {
  57.       digitalWrite(LED2, HIGH);  // Turn the LED off by making the voltage HIGH
  58.     }
  59.   }
  60.  
  61.   if (strcmp(topic, "room/lamp2") == 0) {
  62.     // Switch on the LED if an 1 was received as first character
  63.     if ((char)payload[0] == '1') {
  64.       digitalWrite(LED1, LOW);   // Turn the LED on (Note that LOW is the voltage level
  65.     } else {
  66.       digitalWrite(LED1, HIGH);  // Turn the LED off by making the voltage HIGH
  67.     }
  68.   }
  69.  
  70.  
  71. }
  72.  
  73. void reconnect() {
  74.   // Loop until we're reconnected
  75.   while (!client.connected()) {
  76.     Serial.print("Attempting MQTT connection...");
  77.     // Create a random client ID
  78.     String clientId = "ESP8266Client-";
  79.     clientId += String(random(0xffff), HEX);
  80.     // Attempt to connect
  81.     if (client.connect(clientId.c_str())) {
  82.       Serial.println("connected");
  83.       // Once connected, publish an announcement...
  84.       client.publish("outTopic", "hello world");
  85.       // ... and resubscribe
  86.       client.subscribe("room/lamp1");
  87.       client.subscribe("room/lamp2");
  88.     } else {
  89.       Serial.print("failed, rc=");
  90.       Serial.print(client.state());
  91.       Serial.println(" try again in 5 seconds");
  92.       // Wait 5 seconds before retrying
  93.       delay(5000);
  94.     }
  95.   }
  96. }
  97.  
  98. void setup() {
  99.   pinMode(LED1, OUTPUT);  
  100.    pinMode(LED2, OUTPUT);  
  101.   Serial.begin(115200);
  102.   setup_wifi();
  103.   client.setServer(mqtt_server, 1883);
  104.   client.setCallback(callback);
  105. }
  106.  
  107. void loop() {
  108.  
  109.   if (!client.connected()) {
  110.     reconnect();
  111.   }
  112.   client.loop();
  113.  
  114.   unsigned long now = millis();
  115.   if (now - lastMsg > 2000) {
  116.     lastMsg = now;
  117.     ++value;
  118.     if (value > 40) {
  119.       value = 20;
  120.     }
  121.     snprintf (msg, MSG_BUFFER_SIZE,  "%ld C", value);
  122.     Serial.print("Publish message: ");
  123.     Serial.println(msg);
  124.     client.publish("room/temp", msg);
  125.   }
  126. }
Add Comment
Please, Sign In to add comment