Advertisement
mudhita_triari

Praktek ESP32 - Memonitoring suhu & kelembaban - Mengatur led & Buzzer via Blynk.cloud Server

Mar 24th, 2024
145
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.75 KB | Source Code | 0 0
  1. #define BLYNK_TEMPLATE_ID "TMPLF4ZLfpB8"
  2. #define BLYNK_TEMPLATE_NAME "Monitoring Suhu"
  3.  
  4. #include <WiFi.h>
  5. #include <WiFiClient.h>
  6. #include <BlynkSimpleEsp32.h>
  7. #include <ModbusMaster.h>
  8. #include <SoftwareSerial.h>
  9.  
  10. #define MAX485_RE_NEG   5
  11. #define MAX485_DE       4
  12. #define SSERIAL_RX_PIN  16
  13. #define SSERIAL_TX_PIN  17
  14. #define BUZZER_PIN      27
  15. #define NUM_SENSORS     2
  16. #define TEMP_SENSOR     0
  17. #define HUM_SENSOR      1
  18.  
  19. float TEMP_THRESHOLD = 50.0;
  20. float HUM_THRESHOLD = 80.0;
  21.  
  22. char auth[] = "7CeCdQL51Y5AC3OYSrMnTApeAQMXPvfq";
  23. char ssid[] = "vivo V29";
  24. char pass[] = "Nabila041185";
  25.  
  26. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  27. ModbusMaster node;
  28.  
  29. float sensorData[NUM_SENSORS];
  30. uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
  31.  
  32. char humidityStatus[20];
  33. char status[20];
  34.  
  35. const int LED_PINS[] = {13, 12, 14};
  36. const int V_PINS[] = {V0, V1, V2};
  37.  
  38. bool wifiConnected = false;
  39. bool buzzerStatus = false;
  40.  
  41. BlynkTimer timer;
  42.  
  43. void preTransmission() {
  44.   digitalWrite(MAX485_RE_NEG, 1);
  45.   digitalWrite(MAX485_DE, 1);
  46. }
  47.  
  48. void postTransmission() {
  49.   digitalWrite(MAX485_RE_NEG, 0);
  50.   digitalWrite(MAX485_DE, 0);
  51. }
  52.  
  53. void sendSensorData() {
  54.   Blynk.virtualWrite(V9, humidityStatus);
  55.   Blynk.virtualWrite(V8, sensorData[HUM_SENSOR]);
  56.   Blynk.virtualWrite(V7, sensorData[TEMP_SENSOR]);
  57.   Blynk.virtualWrite(V6, status);
  58. }
  59.  
  60. void checkTemperatureStatus() {
  61.   if (sensorData[TEMP_SENSOR] < 20) {
  62.     strcpy(status, "Suhu Dingin");
  63.   } else if (sensorData[TEMP_SENSOR] >= 20 && sensorData[TEMP_SENSOR] <= TEMP_THRESHOLD) {
  64.     strcpy(status, "Suhu Sedang");
  65.   } else {
  66.     strcpy(status, "Suhu Panas");
  67.   }
  68. }
  69.  
  70. void checkHumidityStatus() {
  71.   if (sensorData[HUM_SENSOR] < 40) {
  72.     strcpy(humidityStatus, "Kelembapan Rendah");
  73.   } else if (sensorData[HUM_SENSOR] >= 40 && sensorData[HUM_SENSOR] <= HUM_THRESHOLD) {
  74.     strcpy(humidityStatus, "Kelembapan Normal");
  75.   } else {
  76.     strcpy(humidityStatus, "Kelembapan Tinggi");
  77.   }
  78. }
  79.  
  80. void buzzerOn() {
  81.   digitalWrite(BUZZER_PIN, HIGH);
  82.   delay(5000);
  83.   digitalWrite(BUZZER_PIN, LOW);
  84. }
  85.  
  86. void setup() {
  87.   pinMode(MAX485_RE_NEG, OUTPUT);
  88.   pinMode(MAX485_DE, OUTPUT);
  89.   digitalWrite(MAX485_RE_NEG, 0);
  90.   digitalWrite(MAX485_DE, 0);
  91.  
  92.   Serial.begin(9600);
  93.   RS485Serial.begin(9600);
  94.  
  95.   node.begin(1, RS485Serial);
  96.   node.preTransmission(preTransmission);
  97.   node.postTransmission(postTransmission);
  98.  
  99.   for (int i = 0; i < sizeof(LED_PINS) / sizeof(LED_PINS[0]); i++) {
  100.     pinMode(LED_PINS[i], OUTPUT);
  101.     digitalWrite(LED_PINS[i], LOW);
  102.   }
  103.  
  104.   Blynk.begin(auth, ssid, pass);
  105.   pinMode(BUZZER_PIN, OUTPUT);
  106.   timer.setInterval(5000L, sendSensorData);
  107. }
  108.  
  109. void handleLED(int vPin, int ledIndex, int param) {
  110.   digitalWrite(LED_PINS[ledIndex], param);
  111. }
  112.  
  113. BLYNK_WRITE(V0) { handleLED(V0, 0, param.asInt()); }
  114. BLYNK_WRITE(V1) { handleLED(V1, 1, param.asInt()); }
  115. BLYNK_WRITE(V2) { handleLED(V2, 2, param.asInt()); }
  116. BLYNK_WRITE(V3) { TEMP_THRESHOLD = param.asFloat(); }
  117. BLYNK_WRITE(V4) { HUM_THRESHOLD = param.asFloat(); }
  118. BLYNK_WRITE(V5) {
  119.   int pinValue = param.asInt();
  120.   if (pinValue == 1) {
  121.     buzzerOn();
  122.   }
  123. }
  124.  
  125. void readSensors() {
  126.   for (int i = 0; i < NUM_SENSORS; i++) {
  127.     uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
  128.  
  129.     if (result == node.ku8MBSuccess) {
  130.       sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
  131.     } else {
  132.       // Handle error here
  133.       Serial.println("Error reading sensor data");
  134.       return;
  135.     }
  136.   }
  137. }
  138.  
  139. void loop() {
  140.   Blynk.run();
  141.   timer.run();
  142.  
  143.   if (wifiConnected) {
  144.     readSensors();
  145.     checkTemperatureStatus();
  146.     checkHumidityStatus();
  147.   }
  148.  
  149.   if (WiFi.status() != WL_CONNECTED) {
  150.     WiFi.begin(ssid, pass);
  151.   } else {
  152.     wifiConnected = true;
  153.   }
  154. }
  155.  
Tags: #esp32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement