Advertisement
mudhita_triari

Praktek ESP32 - Memonitoring suhu & kelembaban serta mengendalikan Led menggunakan Web Server HTTP

Mar 24th, 2024
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.17 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <WebServer.h>
  3. #include <ModbusMaster.h>
  4. #include <SoftwareSerial.h>
  5.  
  6. #define MAX485_RE_NEG  5
  7. #define MAX485_DE      4
  8. #define SSERIAL_RX_PIN 16
  9. #define SSERIAL_TX_PIN 17
  10. #define LED_PIN2       13
  11. #define LED_PIN3       12
  12. #define BUZZER_PIN     14
  13.  
  14. #define TEMP_THRESHOLD 40.0
  15. #define HUM_THRESHOLD  80.0
  16.  
  17. #define NUM_SENSORS 2
  18. #define TEMP_SENSOR 0
  19. #define HUM_SENSOR 1
  20.  
  21. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  22. ModbusMaster node;
  23.  
  24. float sensorData[NUM_SENSORS];
  25. uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
  26.  
  27. const char* ssid = "vivo V29";
  28. const char* password = "Nabila041185";
  29.  
  30. WebServer server(80);
  31.  
  32. bool led2On = false;
  33. bool led3On = false;
  34.  
  35. void preTransmission() {
  36.   digitalWrite(MAX485_RE_NEG, HIGH);
  37.   digitalWrite(MAX485_DE, HIGH);
  38. }
  39.  
  40. void postTransmission() {
  41.   digitalWrite(MAX485_RE_NEG, LOW);
  42.   digitalWrite(MAX485_DE, LOW);
  43. }
  44.  
  45. void setup() {
  46.   pinMode(MAX485_RE_NEG, OUTPUT);
  47.   pinMode(MAX485_DE, OUTPUT);
  48.   pinMode(LED_PIN2, OUTPUT);
  49.   pinMode(LED_PIN3, OUTPUT);
  50.   pinMode(BUZZER_PIN, OUTPUT);
  51.  
  52.   digitalWrite(MAX485_RE_NEG, LOW);
  53.   digitalWrite(MAX485_DE, LOW);
  54.   digitalWrite(LED_PIN2, LOW);
  55.   digitalWrite(LED_PIN3, LOW);
  56.   digitalWrite(BUZZER_PIN, LOW);
  57.  
  58.   Serial.begin(9600);
  59.   RS485Serial.begin(9600);
  60.  
  61.   node.begin(1, RS485Serial);
  62.  
  63.   node.preTransmission(preTransmission);
  64.   node.postTransmission(postTransmission);
  65.  
  66.   Serial.println();
  67.   Serial.print("Menghubungkan ke ");
  68.   Serial.println(ssid);
  69.  
  70.   WiFi.begin(ssid, password);
  71.  
  72.   while (WiFi.status() != WL_CONNECTED) {
  73.     delay(500);
  74.     Serial.print(".");
  75.   }
  76.  
  77.   Serial.println("");
  78.   Serial.println("WiFi terhubung");
  79.   Serial.println("Alamat IP: ");
  80.   Serial.println(WiFi.localIP());
  81.  
  82.   server.on("/", handleRoot);
  83.   server.on("/led2on", handleLed2On);
  84.   server.on("/led2off", handleLed2Off);
  85.   server.on("/led3on", handleLed3On);
  86.   server.on("/led3off", handleLed3Off);
  87.  
  88.   server.begin();
  89.   Serial.println("Server dimulai");
  90. }
  91.  
  92. void loop() {
  93.   server.handleClient();
  94.  
  95.   if (WiFi.status() != WL_CONNECTED) {
  96.     WiFi.begin(ssid, password);
  97.     while (WiFi.status() != WL_CONNECTED) {
  98.       delay(500);
  99.       Serial.print(".");
  100.     }
  101.     Serial.println("");
  102.     Serial.println("WiFi terhubung");
  103.   }
  104.  
  105.   // Melakukan pembacaan sensor
  106.   for (int i = 0; i < NUM_SENSORS; i++) {
  107.     uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
  108.  
  109.     if (result == node.ku8MBSuccess) {
  110.       sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
  111.     } else {
  112.       Serial.println("Gagal membaca data!");
  113.       delay(1000);
  114.       continue;
  115.     }
  116.   }
  117.  
  118.   // Menampilkan informasi suhu dan kelembaban
  119.   Serial.printf("Suhu: %.2f°C, Kelembaban: %.2f%%\n", sensorData[TEMP_SENSOR], sensorData[HUM_SENSOR]);
  120.  
  121.   // Memeriksa apakah suhu atau kelembaban melebihi ambang batas
  122.   bool thresholdExceeded = (sensorData[TEMP_SENSOR] > TEMP_THRESHOLD) || (sensorData[HUM_SENSOR] > HUM_THRESHOLD);
  123.  
  124.   // Menyalakan buzzer jika LED 2 atau LED 3 dalam kondisi hidup, bahkan jika suhu atau kelembaban di bawah ambang batas
  125.   if ((led2On || led3On) && !thresholdExceeded) {
  126.     if (led2On)
  127.       tone(BUZZER_PIN, 1000);
  128.     else if (led3On)
  129.       tone(BUZZER_PIN, 2000);
  130.   }
  131.   // Mematikan buzzer jika suhu atau kelembaban melebihi ambang batas
  132.   else {
  133.     noTone(BUZZER_PIN);
  134.   }
  135.  
  136.   // Menunda untuk sementara sebelum melanjutkan
  137.   delay(2000);
  138. }
  139.  
  140. void handleRoot() {
  141.   String message = "<!DOCTYPE html><html><head>";
  142.   message += "<link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'>";
  143.   message += "<script>function updateSlider(sliderValue, ledNumber) {window.location.href = sliderValue == 1 ? '/led' + ledNumber + 'on' : '/led' + ledNumber + 'off';}</script>";
  144.   message += "</head><body class='container'>";
  145.   message += "<h1 class='text-center my-4'>Suhu dan Kelembaban</h1>";
  146.   message += "<p class='text-center'>Suhu: " + String(sensorData[TEMP_SENSOR]) + " C</p>";
  147.   message += "<p class='text-center'>Kelembaban: " + String(sensorData[HUM_SENSOR]) + " %</p>";
  148.   message += "<div class='d-flex justify-content-center'>";
  149.   message += "<div class='mx-2'><input type='range' min='0' max='1' value='0' class='slider' id='led2' onchange='updateSlider(this.value, 2)'><label for='led2' class='text-center d-block'>LED 2</label></div>";
  150.   message += "<div class='mx-2'><input type='range' min='0' max='1' value='0' class='slider' id='led3' onchange='updateSlider(this.value, 3)'><label for='led3' class='text-center d-block'>LED 3</label></div>";
  151.   message += "</div></body></html>";
  152.   server.send(200, "text/html", message);
  153. }
  154.  
  155. void handleLed2On() {
  156.   digitalWrite(LED_PIN2, HIGH);
  157.   led2On = true;
  158.   server.send(200, "text/plain", "LED 2 dinyalakan");
  159. }
  160.  
  161. void handleLed3On() {
  162.   digitalWrite(LED_PIN3, HIGH);
  163.   led3On = true;
  164.   server.send(200, "text/plain", "LED 3 dinyalakan");
  165. }
  166.  
  167. void handleLed2Off() {
  168.   digitalWrite(LED_PIN2, LOW);
  169.   led2On = false;
  170.   server.send(200, "text/plain", "LED 2 dimatikan");
  171. }
  172.  
  173. void handleLed3Off() {
  174.   digitalWrite(LED_PIN3, LOW);
  175.   led3On = false;
  176.   server.send(200, "text/plain", "LED 3 dimatikan");
  177. }
  178.  
Tags: #esp32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement