Advertisement
mudhita_triari

Praktek ESP32 - Memonitoring suhu & kelembaban - Mengatur led & Buzzer via MQTT dan AdafruitI Server

Apr 1st, 2024
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.82 KB | Source Code | 0 0
  1. #include <AdafruitIO_WiFi.h>
  2. #include <ModbusMaster.h>
  3. #include <SoftwareSerial.h>
  4. #include <PubSubClient.h>
  5. #include <WiFi.h>
  6.  
  7. // Difinisi Pin
  8. #define MAX485_RE_NEG   5
  9. #define MAX485_DE       4
  10. #define SSERIAL_RX_PIN  16
  11. #define SSERIAL_TX_PIN  17
  12. #define PIN_LED_1       13  
  13. #define PIN_LED_2       12
  14. #define PIN_LED_3       14
  15. #define PIN_BUZZER      27
  16.  
  17. // Difinisi Sensor dan Address
  18. #define NUM_SENSORS     2
  19. #define TEMP_SENSOR     0
  20. #define HUM_SENSOR      1
  21. uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
  22.  
  23. // Konfigurasi WiFi dan Adafruit IO
  24. #define WIFI_SSID       "vivo V29"
  25. #define WIFI_PASS       "Nabila041185"
  26. #define IO_USERNAME     "Mudhita"
  27. #define IO_KEY          "aio_urCG55WHy9b6aOIkD8nmJT8J190q"
  28.  
  29. // Konfigurasi MQTT
  30. const char* mqtt_server = "broker.emqx.io";
  31. const char* mqtt_topik_suhu = "mudhita/esp32/suhu";
  32. const char* mqtt_topik_kelembaban = "mudhita/esp32/kelembaban";
  33. const int mqtt_port = 1883;
  34. const char* mqtt_username = "";
  35. const char* mqtt_password = "";
  36.  
  37. // Variabel Umum
  38. float sensorData[NUM_SENSORS];
  39. float batasAtasSuhu = 0.0;
  40. float batasBawahSuhu = 0.0;
  41. unsigned long waktuUpdateTerakhir = 0;
  42. const unsigned long intervalUpdate = 5000;
  43.  
  44. // Object Declarations
  45. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  46. ModbusMaster node;
  47. WiFiClient wifiClient;
  48. PubSubClient client(wifiClient);
  49. AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
  50. AdafruitIO_Feed *feedUpper = io.feed("suhuAtas");
  51. AdafruitIO_Feed *feedLower = io.feed("suhuBawah");
  52. AdafruitIO_Feed *feedTemp = io.feed("suhu");
  53. AdafruitIO_Feed *feedHumid = io.feed("kelembaban");
  54.  
  55. // Fungsi Unit
  56. void setup_wifi();
  57. void setup_MQTT();
  58. void setup_AdafruitIO();
  59. void setupActuators();
  60. void readSensor();
  61. void publishData_MQTT_dan_AdafruitIO();
  62. void handleSensorReadingErrors();
  63. void reconnectMQTT();
  64.  
  65. void setup() {
  66.   pinMode(MAX485_RE_NEG, OUTPUT);
  67.   pinMode(MAX485_DE, OUTPUT);
  68.  
  69.   Serial.begin(9600);
  70.   RS485Serial.begin(9600);
  71.  
  72.   node.begin(1, RS485Serial);
  73.   node.preTransmission([](){
  74.     digitalWrite(MAX485_RE_NEG, HIGH);
  75.     digitalWrite(MAX485_DE, HIGH);
  76.   });
  77.   node.postTransmission([](){
  78.     digitalWrite(MAX485_RE_NEG, LOW);
  79.     digitalWrite(MAX485_DE, LOW);
  80.   });
  81.  
  82.   setup_wifi();
  83.   setup_MQTT();
  84.   setup_AdafruitIO();
  85.   setupActuators();
  86. }
  87.  
  88. void loop() {
  89.   io.run();
  90.  
  91.   unsigned long millisSekarang = millis();
  92.   if (millisSekarang - waktuUpdateTerakhir >= intervalUpdate) {
  93.   readSensor();
  94.   publishData_MQTT_dan_AdafruitIO();
  95.   waktuUpdateTerakhir = millisSekarang;
  96.   }
  97. }
  98.  
  99. void setup_wifi() {
  100.   Serial.println("Connecting to WiFi...");
  101.   WiFi.begin(WIFI_SSID, WIFI_PASS);
  102.   int attempts = 0;
  103.   while (WiFi.status() != WL_CONNECTED) {
  104.     delay(500);
  105.     Serial.print(".");
  106.     attempts++;
  107.     if(attempts > 20) {
  108.       Serial.println("WiFi connection failed, restarting...");
  109.       ESP.restart();
  110.     }
  111.   }
  112.   Serial.println("");
  113.   Serial.println("WiFi connected");
  114.   Serial.println("IP address: ");
  115.   Serial.println(WiFi.localIP());
  116. }
  117.  
  118. void setup_MQTT() {
  119.   client.setServer(mqtt_server, mqtt_port);
  120. }
  121.  
  122. void setup_AdafruitIO() {
  123.   io.connect();
  124.   feedUpper->onMessage([](AdafruitIO_Data *data) {
  125.     if (strcmp(data->feedName(), "suhuAtas") == 0) {
  126.       batasAtasSuhu = atof(data->value());
  127.     } else if (strcmp(data->feedName(), "suhuBawah") == 0) {
  128.       batasBawahSuhu = atof(data->value());
  129.     }
  130.   });
  131.   feedLower->onMessage([](AdafruitIO_Data *data) {
  132.     if (strcmp(data->feedName(), "suhuAtas") == 0) {
  133.       batasAtasSuhu = atof(data->value());
  134.     } else if (strcmp(data->feedName(), "suhuBawah") == 0) {
  135.       batasBawahSuhu = atof(data->value());
  136.     }
  137.   });
  138.   feedUpper->get();
  139.   feedLower->get();
  140. }
  141.  
  142. void setupActuators() {
  143.   pinMode(PIN_LED_1, OUTPUT);
  144.   pinMode(PIN_LED_2, OUTPUT);
  145.   pinMode(PIN_LED_3, OUTPUT);
  146.   pinMode(PIN_BUZZER, OUTPUT);
  147. }
  148.  
  149. void readSensor() {
  150.   for (int i = 0; i < NUM_SENSORS; i++) {
  151.     uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
  152.  
  153.     if (result == node.ku8MBSuccess) {
  154.       sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
  155.     } else {
  156.       Serial.println("Failed to read data!");
  157.       handleSensorReadingErrors();
  158.       continue;
  159.     }
  160.   }
  161. }
  162.  
  163. void handleSensorReadingErrors() {
  164.   Serial.println("Segera Hubungi Pihak Berwenang...");
  165. }
  166.  
  167. void publishData_MQTT_dan_AdafruitIO() {
  168.   if (!client.connected()) {
  169.     reconnectMQTT();
  170.   }
  171.  
  172.   char tempStr[8];
  173.   char humidStr[8];
  174.  
  175.   dtostrf(sensorData[TEMP_SENSOR], 1, 2, tempStr);
  176.   dtostrf(sensorData[HUM_SENSOR], 1, 2, humidStr);
  177.  
  178.   if (client.connected()) {
  179.     client.publish(mqtt_topik_suhu, tempStr);
  180.     client.publish(mqtt_topik_kelembaban, humidStr);
  181.   } else {
  182.     Serial.println("MQTT client not connected!");
  183.   }
  184.  
  185.   if (!isnan(sensorData[TEMP_SENSOR]) && !isnan(sensorData[HUM_SENSOR])) {
  186.     feedTemp->save(sensorData[TEMP_SENSOR]);
  187.     feedHumid->save(sensorData[HUM_SENSOR]);
  188.   }
  189.  
  190.   if (sensorData[TEMP_SENSOR] > batasAtasSuhu) {
  191.     digitalWrite(PIN_BUZZER, HIGH);
  192.     digitalWrite(PIN_LED_1, HIGH);
  193.   } else {
  194.     digitalWrite(PIN_BUZZER, LOW);
  195.     digitalWrite(PIN_LED_1, LOW);
  196.   }
  197.  
  198.   if (sensorData[TEMP_SENSOR] >= batasBawahSuhu && sensorData[TEMP_SENSOR] <= batasAtasSuhu) {
  199.     digitalWrite(PIN_LED_2, HIGH);
  200.   } else {
  201.     digitalWrite(PIN_LED_2, LOW);
  202.   }
  203.  
  204.   if (sensorData[TEMP_SENSOR] < batasBawahSuhu) {
  205.     digitalWrite(PIN_LED_3, HIGH);
  206.   } else {
  207.     digitalWrite(PIN_LED_3, LOW);
  208.   }
  209. }
  210.  
  211. void reconnectMQTT() {
  212.   while (!client.connected()) {
  213.     Serial.println("Connecting to MQTT...");
  214.     if (client.connect("ESP32Client", mqtt_username, mqtt_password)) {
  215.       Serial.println("Connected to MQTT");
  216.     } else {
  217.       Serial.print("Failed to connect to MQTT. State: ");
  218.       Serial.println(client.state());
  219.       delay(5000);
  220.     }
  221.   }
  222. }
  223.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement