Advertisement
mudhita_triari

ESP32 MQTT - Emqx & io.adafruit

Mar 28th, 2024 (edited)
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.97 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <ModbusMaster.h>
  4. #include <SoftwareSerial.h>
  5. #include <Adafruit_MQTT.h>
  6. #include <Adafruit_MQTT_Client.h>
  7. #include <AdafruitIO_WiFi.h>
  8.  
  9. #define MAX485_RE_NEG     5
  10. #define MAX485_DE         4
  11. #define SSERIAL_RX_PIN    16
  12. #define SSERIAL_TX_PIN    17
  13.  
  14. #define JUMLAH_SENSOR     2
  15. #define SENSOR_SUHU       0
  16. #define SENSOR_KELEMBABAN 1
  17.  
  18. const char* ssid =          "vivo V29";
  19. const char* password =      "Nabila041185";
  20. const char* IO_Username =   "Mudhita";
  21. const char* IO_Key =        "aio_urCG55WHy9b6aOIkD8nmJT8J190q";
  22.  
  23. const char* mqtt_server =           "broker.emqx.io";
  24. const char* mqtt_topik_suhu =       "wian/esp32/suhu";
  25. const char* mqtt_topik_kelembaban = "wian/esp32/kelembaban";
  26. const char* mqtt_topik_prediksi =   "wian/esp32/prediksi";
  27. const char *mqtt_username =         "wian";
  28. const char *mqtt_password =         "wian";
  29. const int mqtt_port = 1883;
  30.  
  31. const int pin_buzzer = 27;
  32. const int pin_led1 = 13;
  33. const int pin_led2 = 12;
  34. const int pin_led3 = 14;
  35.  
  36. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  37. ModbusMaster node;
  38. WiFiClient klienWiFi;
  39. PubSubClient klienMQTT(klienWiFi);
  40.  
  41. AdafruitIO_WiFi io( IO_Username, IO_Key, ssid, password);
  42. AdafruitIO_Feed *feed_suhu;
  43. AdafruitIO_Feed *feed_kelembaban;
  44.  
  45. uint16_t alamat_sensor[JUMLAH_SENSOR] = {0x0001, 0x0002};
  46. float data_sensor[JUMLAH_SENSOR];
  47.  
  48. void preTransmission() {
  49.   digitalWrite(MAX485_RE_NEG, 1);
  50.   digitalWrite(MAX485_DE, 1);
  51. }
  52.  
  53. void postTransmission() {
  54.   digitalWrite(MAX485_RE_NEG, 0);
  55.   digitalWrite(MAX485_DE, 0);
  56. }
  57.  
  58. void setup() {
  59.   Serial.begin(9600);
  60.   RS485Serial.begin(9600);
  61.  
  62.   node.begin(1, RS485Serial);
  63.   node.preTransmission(preTransmission);
  64.   node.postTransmission(postTransmission);
  65.  
  66.   inisialisasiPerangkat();
  67.   sambungWiFi();
  68.   sambungMQTT();
  69.   sambungIO();
  70. }
  71.  
  72. void loop() {
  73.   if (!klienMQTT.connected()) {
  74.     sambungKembaliMQTT();
  75.   }
  76.  
  77.   bacaDataSensor();
  78.   publikasikanDataKeMQTT();
  79.   handleLogikaPrediksi();
  80.   simpanDataKeIO();
  81.  
  82.   delay(2000);
  83. }
  84.  
  85. void inisialisasiPerangkat() {
  86.   pinMode(MAX485_RE_NEG, OUTPUT);
  87.   pinMode(MAX485_DE, OUTPUT);
  88.   digitalWrite(MAX485_RE_NEG, 0);
  89.   digitalWrite(MAX485_DE, 0);
  90.   pinMode(pin_buzzer, OUTPUT);
  91. }
  92.  
  93. void sambungWiFi() {
  94.   WiFi.begin(ssid, password);
  95.   while (WiFi.status() != WL_CONNECTED) {
  96.     delay(500);
  97.     Serial.print(".");
  98.   }
  99.   Serial.println("");
  100.   Serial.println("WiFi terhubung");
  101. }
  102.  
  103. void sambungMQTT() {
  104.   klienMQTT.setServer(mqtt_server, mqtt_port);
  105. }
  106.  
  107. void sambungIO() {
  108.   io.connect();
  109.   feed_suhu = io.feed("suhu");
  110.   feed_kelembaban = io.feed("kelembaban");
  111. }
  112.  
  113. void sambungKembaliMQTT() {
  114.   while (!klienMQTT.connected()) {
  115.     Serial.println("Menghubungkan ke MQTT...");
  116.     if (klienMQTT.connect("ESP32Client", mqtt_username, mqtt_password)) {
  117.       Serial.println("Terhubung ke broker MQTT");
  118.     } else {
  119.       Serial.print("Gagal, rc=");
  120.       Serial.print(klienMQTT.state());
  121.       Serial.println(" Coba lagi dalam 5 detik...");
  122.       delay(5000);
  123.     }
  124.   }
  125. }
  126.  
  127. void bacaDataSensor() {
  128.   for (int i = 0; i < JUMLAH_SENSOR; i++) {
  129.     uint8_t hasil = node.readInputRegisters(alamat_sensor[i], 1);
  130.  
  131.     if (hasil == node.ku8MBSuccess) {
  132.       data_sensor[i] = float(node.getResponseBuffer(0) / 10.00F);
  133.     } else {
  134.       delay(1000);
  135.       return;
  136.     }
  137.   }
  138. }
  139.  
  140. void publikasikanDataKeMQTT() {
  141.   publishMQTT(mqtt_topik_suhu, data_sensor[SENSOR_SUHU]);
  142.   publishMQTT(mqtt_topik_kelembaban, data_sensor[SENSOR_KELEMBABAN]);
  143.  
  144.   String prediksi = analisisDanPrediksi(data_sensor[SENSOR_SUHU], data_sensor[SENSOR_KELEMBABAN]);
  145.   klienMQTT.publish(mqtt_topik_prediksi, prediksi.c_str());
  146.   Serial.print("Publish MQTT: --> " + String(data_sensor[SENSOR_SUHU]) + " || " + String(data_sensor[SENSOR_KELEMBABAN]) + "\n");
  147. }
  148.  
  149. void handleLogikaPrediksi() {
  150.   if (data_sensor[SENSOR_SUHU] > 30) {
  151.     digitalWrite(pin_buzzer, HIGH);
  152.     digitalWrite(pin_led1, HIGH);
  153.     digitalWrite(pin_led2, LOW);
  154.     digitalWrite(pin_led3, LOW);
  155.   } else if (data_sensor[SENSOR_SUHU] > 20 && data_sensor[SENSOR_SUHU] <= 30) {
  156.     digitalWrite(pin_buzzer, LOW);
  157.     digitalWrite(pin_led1, LOW);
  158.     digitalWrite(pin_led2, HIGH);
  159.     digitalWrite(pin_led3, LOW);
  160.   } else {
  161.     digitalWrite(pin_buzzer, LOW);
  162.     digitalWrite(pin_led1, LOW);
  163.     digitalWrite(pin_led2, LOW);
  164.     digitalWrite(pin_led3, HIGH);
  165.   }
  166. }
  167.  
  168. void simpanDataKeIO() {
  169.   feed_suhu->save(data_sensor[SENSOR_SUHU]);
  170.   feed_kelembaban->save(data_sensor[SENSOR_KELEMBABAN]);
  171.  
  172.   io.run();
  173.  
  174.   delay(2000);
  175.  
  176. }
  177.  
  178. void publishMQTT(const char* topic, float value) {
  179.   char buffer[10];
  180.   dtostrf(value, 4, 2, buffer);
  181.   klienMQTT.publish(topic, buffer);
  182. }
  183.  
  184. String analisisDanPrediksi(float suhu, float kelembaban) {
  185.   if (suhu > 30) {
  186.     return "Cuaca panas, kemungkinan tidak hujan.";
  187.   } else if (kelembaban > 70) {
  188.     return "Kelembapan tinggi, potensi hujan.";
  189.   } else {
  190.     return "Cuaca normal, tidak ada perubahan signifikan.";
  191.   }
  192. }
  193.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement