hendriawan

MQTBasic-1pub-2sub

Nov 10th, 2021
1,478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <ESP8266WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. // Update these with values suitable for your network.
  5.  
  6. int LED1=2;
  7. int LED2=16;
  8. const char* ssid = "mywifi";
  9. const char* password = "123456789";
  10. // const char* mqtt_server = "broker.mqtt-dashboard.com";
  11. const char* mqtt_server = "192.168.1.4";
  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 = 0;
  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, "ruangan/lampu1") == 0) {
  53.     // Switch on the LED if an 1 was received as first character
  54.     if ((char)payload[0] == '1') {
  55.       digitalWrite(LED1, LOW);   // Turn the LED on (Note that LOW is the voltage level
  56.     } else {
  57.       digitalWrite(LED1, HIGH);  // Turn the LED off by making the voltage HIGH
  58.     }
  59.   }
  60.  
  61.   if (strcmp(topic, "ruangan/lampu2") == 0) {
  62.     // Switch on the LED if an 1 was received as first character
  63.     if ((char)payload[0] == '1') {
  64.       digitalWrite(LED2, LOW);   // Turn the LED on (Note that LOW is the voltage level
  65.     } else {
  66.       digitalWrite(LED2, HIGH);  // Turn the LED off by making the voltage HIGH
  67.     }
  68.   }
  69.  
  70. }
  71.  
  72. void reconnect() {
  73.   // Loop until we're reconnected
  74.   while (!client.connected()) {
  75.     Serial.print("Attempting MQTT connection...");
  76.     // Create a random client ID
  77.     String clientId = "ESP8266Client-";
  78.     clientId += String(random(0xffff), HEX);
  79.     // Attempt to connect
  80.     if (client.connect(clientId.c_str())) {
  81.       Serial.println("connected");
  82.       // Once connected, publish an announcement...
  83.       client.publish("ruangan/suhu", "hello world");
  84.       // ... and resubscribe
  85.       client.subscribe("ruangan/lampu1");
  86.       client.subscribe("ruangan/lampu2");
  87.     } else {
  88.       Serial.print("failed, rc=");
  89.       Serial.print(client.state());
  90.       Serial.println(" try again in 5 seconds");
  91.       // Wait 5 seconds before retrying
  92.       delay(5000);
  93.     }
  94.   }
  95. }
  96.  
  97. void setup() {
  98.   pinMode(LED1, OUTPUT);    
  99.   pinMode(LED2, OUTPUT);  
  100.   Serial.begin(115200);
  101.   setup_wifi();
  102.   client.setServer(mqtt_server, 1883);
  103.   client.setCallback(callback);
  104. }
  105.  
  106. void loop() {
  107.  
  108.   if (!client.connected()) {
  109.     reconnect();
  110.   }
  111.   client.loop();
  112.  
  113.   unsigned long now = millis();
  114.   if (now - lastMsg > 2000) {    
  115.     lastMsg = now;
  116.     ++value;
  117.     if(value==100) {
  118.        value=0;
  119.     }
  120.    
  121.      
  122.     snprintf (msg, MSG_BUFFER_SIZE, "%ld", value);
  123.     Serial.print("Publish message: ");
  124.     Serial.println(msg);
  125.     client.publish("ruangan/suhu", msg);
  126.   }
  127. }
Advertisement
Add Comment
Please, Sign In to add comment