hendriawan

MQTBasic-2pub-2sub-adafruitIO

Nov 16th, 2021 (edited)
1,310
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 -u hendriawan -P IO_KEY -h io.adafruit.com -t hendriawan/feeds/kamar-slash-lampu1 -m 0
  4. mosquitto_pub -u hendriawan -P IO_KEY -h io.adafruit.com -t hendriawan/feeds/kamar-slash-lampu1 -m 1
  5. mosquitto_pub -u hendriawan -P IO_KEY -h io.adafruit.com -t hendriawan/feeds/kamar-slash-lampu2 -m 0
  6. mosquitto_pub -u hendriawan -P IO_KEY -h io.adafruit.com -t hendriawan/feeds/kamar-slash-lampu2 -m 1
  7. mosquitto_sub -u hendriawan -P IO_KEY -h io.adafruit.com -t "hendriawan/feeds/kamar-slash-suhu"
  8. mosquitto_sub -u hendriawan -P IO_KEY -h io.adafruit.com -t hendriawan/feeds/kamar-slash-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 = "io.adafruit.com";
  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, "hendriawan/feeds/kamar-slash-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, "hendriawan/feeds/kamar-slash-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.     if (client.connect(clientId.c_str(), "hendriawan", "aio_PoNc11FV6eoQ90FJEotpfTPZ1QcE")) {
  92.  
  93.       Serial.println("connected");
  94.       // Once connected, publish an announcement...
  95.       client.publish("hendriawan/feeds/kamar-slash-suhu", "hello world");
  96.       client.publish("hendriawan/feeds/kamar-slash-kelembapan", "hello world");
  97.       // ... and resubscribe
  98.       client.subscribe("hendriawan/feeds/kamar-slash-lampu1");
  99.       client.subscribe("hendriawan/feeds/kamar-slash-lampu2");
  100.     } else {
  101.       Serial.print("failed, rc=");
  102.       Serial.print(client.state());
  103.       Serial.println(" try again in 5 seconds");
  104.       // Wait 5 seconds before retrying
  105.       delay(5000);
  106.     }
  107.   }
  108. }
  109.  
  110. void setup() {
  111.   pinMode(LED1, OUTPUT);    
  112.   pinMode(LED2, OUTPUT);  
  113.   Serial.begin(115200);
  114.   setup_wifi();
  115.   client.setServer(mqtt_server, 1883);
  116.   client.setCallback(callback);
  117. }
  118.  
  119. void loop() {
  120.  
  121.   if (!client.connected()) {
  122.     reconnect();
  123.   }
  124.   client.loop();
  125.  
  126.   unsigned long now = millis();
  127.   if (now - lastMsg > 5000) {    
  128.     lastMsg = now;
  129.     ++value;
  130.     if(value==100) {
  131.        value=0;
  132.     }
  133.    
  134.     //publish suhu  
  135.     snprintf (msg, MSG_BUFFER_SIZE, "%ld", value);
  136.     Serial.print("Publish message:  suhu:  kelembaban   ");
  137.     Serial.print(msg);
  138.     Serial.print(": ");
  139.     client.publish("hendriawan/feeds/kamar-slash-suhu", msg);
  140.  
  141.     // publish kelembapan
  142.     int dataA0=analogRead(PIN_A0);
  143.     itoa(dataA0,msg,10);
  144.     // snprintf (msg, MSG_BUFFER_SIZE, "%ld", dataA0);
  145.     Serial.println(msg);
  146.     client.publish("hendriawan/feeds/kamar-slash-kelembapan", msg);
  147.  
  148.  
  149.   }
  150. }
Advertisement
Add Comment
Please, Sign In to add comment