hendriawan

MQTBasic-2pub-2sub

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