Advertisement
mudhita_triari

Tugas_Blynk_led_Suhu_Buzzer

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