hendriawan

MQTBasic-1pub-1sub

Nov 10th, 2021 (edited)
1,489
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. const char* ssid = "mywifi";
  7. const char* password = "123456789";
  8. // const char* mqtt_server = "broker.mqtt-dashboard.com";
  9. const char* mqtt_server = "192.168.1.4";
  10. WiFiClient espClient;
  11. PubSubClient client(espClient);
  12. unsigned long lastMsg = 0;
  13. #define MSG_BUFFER_SIZE (50)
  14. char msg[MSG_BUFFER_SIZE];
  15. int value = 0;
  16.  
  17. void setup_wifi() {
  18.  
  19.   delay(10);
  20.   // We start by connecting to a WiFi network
  21.   Serial.println();
  22.   Serial.print("Connecting to ");
  23.   Serial.println(ssid);
  24.  
  25.   WiFi.mode(WIFI_STA);
  26.   WiFi.begin(ssid, password);
  27.  
  28.   while (WiFi.status() != WL_CONNECTED) {
  29.     delay(500);
  30.     Serial.print(".");
  31.   }
  32.  
  33.   randomSeed(micros());
  34.  
  35.   Serial.println("");
  36.   Serial.println("WiFi connected");
  37.   Serial.println("IP address: ");
  38.   Serial.println(WiFi.localIP());
  39. }
  40.  
  41. void callback(char* topic, byte* payload, unsigned int length) {
  42.   Serial.print("Message arrived [");
  43.   Serial.print(topic);
  44.   Serial.print("] ");
  45.   for (int i = 0; i < length; i++) {
  46.     Serial.print((char)payload[i]);
  47.   }
  48.   Serial.println();
  49.  
  50.   // Switch on the LED if an 1 was received as first character
  51.   if ((char)payload[0] == '1') {
  52.     digitalWrite(BUILTIN_LED, LOW);   // Turn the LED on (Note that LOW is the voltage level
  53.     // but actually the LED is on; this is because
  54.     // it is active low on the ESP-01)
  55.   } else {
  56.     digitalWrite(BUILTIN_LED, HIGH);  // Turn the LED off by making the voltage HIGH
  57.   }
  58.  
  59. }
  60.  
  61. void reconnect() {
  62.   // Loop until we're reconnected
  63.   while (!client.connected()) {
  64.     Serial.print("Attempting MQTT connection...");
  65.     // Create a random client ID
  66.     String clientId = "ESP8266Client-";
  67.     clientId += String(random(0xffff), HEX);
  68.     // Attempt to connect
  69.     if (client.connect(clientId.c_str())) {
  70.       Serial.println("connected");
  71.       // Once connected, publish an announcement...
  72.       client.publish("ruangan/suhu", "hello world");
  73.       // ... and resubscribe
  74.       client.subscribe("ruangan/lampu");
  75.     } else {
  76.       Serial.print("failed, rc=");
  77.       Serial.print(client.state());
  78.       Serial.println(" try again in 5 seconds");
  79.       // Wait 5 seconds before retrying
  80.       delay(5000);
  81.     }
  82.   }
  83. }
  84.  
  85. void setup() {
  86.   pinMode(BUILTIN_LED, OUTPUT);     // Initialize the BUILTIN_LED pin as an output
  87.   Serial.begin(115200);
  88.   setup_wifi();
  89.   client.setServer(mqtt_server, 1883);
  90.   client.setCallback(callback);
  91. }
  92.  
  93. void loop() {
  94.  
  95.   if (!client.connected()) {
  96.     reconnect();
  97.   }
  98.   client.loop();
  99.  
  100.   unsigned long now = millis();
  101.   if (now - lastMsg > 2000) {
  102.     lastMsg = now;
  103.     ++value;
  104.     if(value==100) {
  105.        value=0;
  106.     }
  107.    
  108.      
  109.     snprintf (msg, MSG_BUFFER_SIZE, "%ld", value);
  110.     Serial.print("Publish message: ");
  111.     Serial.println(msg);
  112.     client.publish("ruangan/suhu", msg);
  113.   }
  114. }
Advertisement
Add Comment
Please, Sign In to add comment