Advertisement
mudhita_triari

ESP32 MQTT - Publisher

Mar 24th, 2024 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.74 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3.  
  4. #define pin_Pot 36
  5.  
  6. // WiFi
  7. const char *ssid = "RobotikID";
  8. const char *password = "123456789";
  9.  
  10. // Broker MQTT
  11. const char *mqtt_broker = "free.mqtt.iyoti.id";
  12. const char *topic_pub = "robotikid/pub";
  13. const char *topic_sub = "robotikid/sub";
  14. const char *mqtt_username = "RobotikID";
  15. const char *mqtt_password = "1234567";
  16. const int mqtt_port = 1883;
  17.  
  18. char client_id[20];
  19. unsigned long lastMillis = 0;
  20. char data_pot_sebelum[10] = "";
  21.  
  22. WiFiClient espClient;
  23. PubSubClient client(espClient);
  24.  
  25. const unsigned long MQTT_RECONNECT_DELAY = 5000; // Delay untuk mencoba kembali koneksi MQTT
  26. const unsigned long WIFI_RECONNECT_DELAY = 5000; // Delay untuk mencoba kembali koneksi WiFi
  27.  
  28. void callback(char *topic, byte *payload, unsigned int length) {
  29.   Serial.print("Pesan diterima [");
  30.   Serial.print(topic);
  31.   Serial.print("] ");
  32.  
  33.   // Tangani pesan yang masuk di sini
  34.   Serial.println("Payload:");
  35.   for (int i = 0; i < length; i++) {
  36.     Serial.print((char)payload[i]);
  37.   }
  38.   Serial.println();
  39. }
  40.  
  41. void setup() {
  42.   Serial.begin(115200);
  43.  
  44.   WiFi.begin(ssid, password);
  45.   while (WiFi.status() != WL_CONNECTED) {
  46.     delay(500);
  47.     Serial.println("Menghubungkan ke WiFi..");
  48.   }
  49.   Serial.println("Terhubung ke WiFi");
  50.  
  51.   // Menghubungkan ke broker MQTT
  52.   client.setServer(mqtt_broker, mqtt_port);
  53.   while (!client.connected()) {
  54.     if (client.connect(client_id, mqtt_username, mqtt_password)) {
  55.       Serial.println("Terhubung ke broker MQTT");
  56.     } else {
  57.       Serial.print("Gagal Terhubung ");
  58.       Serial.print(client.state());
  59.       delay(MQTT_RECONNECT_DELAY);
  60.     }
  61.   }
  62.  
  63.   // Melakukan langganan topik
  64.   client.setCallback(callback);
  65.   client.subscribe(topic_sub);
  66.   client.publish(topic_pub, "Halo, ESP32");
  67.   lastMillis = millis();
  68. }
  69.  
  70. void loop() {
  71.   if (WiFi.status() != WL_CONNECTED) {
  72.     WiFi.begin(ssid, password);
  73.     delay(WIFI_RECONNECT_DELAY);
  74.   } else if (!client.connected()) {
  75.     reconnect();
  76.   }
  77.   client.loop();
  78.  
  79.   int analog_pot = analogRead(pin_Pot);
  80.   char data_pot[10];
  81.   sprintf(data_pot, "%d", analog_pot);
  82.  
  83.   if (millis() - lastMillis >= 1000) {
  84.     client.publish(topic_pub, data_pot);
  85.     Serial.println(data_pot);
  86.  
  87.     lastMillis = millis();
  88.     strncpy(data_pot_sebelum, data_pot, sizeof(data_pot_sebelum));
  89.   }
  90. }
  91.  
  92. void reconnect() {
  93.   while (!client.connected()) {
  94.     Serial.print("Mencoba koneksi MQTT...");
  95.     if (client.connect(client_id, mqtt_username, mqtt_password)) {
  96.       Serial.println("terhubung");
  97.       client.subscribe(topic_sub);
  98.     } else {
  99.       Serial.print("gagal, rc=");
  100.       Serial.print(client.state());
  101.       Serial.println(" coba lagi dalam 5 detik");
  102.       delay(WIFI_RECONNECT_DELAY);
  103.     }
  104.   }
  105. }
  106.  
Tags: #esp32 #MQTT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement