Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <WebServer.h>
- #include <ModbusMaster.h>
- #include <SoftwareSerial.h>
- #define MAX485_RE_NEG 5
- #define MAX485_DE 4
- #define SSERIAL_RX_PIN 16
- #define SSERIAL_TX_PIN 17
- #define LED_PIN2 13
- #define LED_PIN3 12
- #define BUZZER_PIN 14
- #define TEMP_THRESHOLD 40.0
- #define HUM_THRESHOLD 80.0
- #define NUM_SENSORS 2
- #define TEMP_SENSOR 0
- #define HUM_SENSOR 1
- SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
- ModbusMaster node;
- float sensorData[NUM_SENSORS];
- uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
- const char* ssid = "vivo V29";
- const char* password = "Nabila041185";
- WebServer server(80);
- bool led2On = false;
- bool led3On = false;
- void preTransmission() {
- digitalWrite(MAX485_RE_NEG, HIGH);
- digitalWrite(MAX485_DE, HIGH);
- }
- void postTransmission() {
- digitalWrite(MAX485_RE_NEG, LOW);
- digitalWrite(MAX485_DE, LOW);
- }
- void setup() {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- pinMode(LED_PIN2, OUTPUT);
- pinMode(LED_PIN3, OUTPUT);
- pinMode(BUZZER_PIN, OUTPUT);
- digitalWrite(MAX485_RE_NEG, LOW);
- digitalWrite(MAX485_DE, LOW);
- digitalWrite(LED_PIN2, LOW);
- digitalWrite(LED_PIN3, LOW);
- digitalWrite(BUZZER_PIN, LOW);
- Serial.begin(9600);
- RS485Serial.begin(9600);
- node.begin(1, RS485Serial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- Serial.println();
- Serial.print("Menghubungkan ke ");
- Serial.println(ssid);
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi terhubung");
- Serial.println("Alamat IP: ");
- Serial.println(WiFi.localIP());
- server.on("/", handleRoot);
- server.on("/led2on", handleLed2On);
- server.on("/led2off", handleLed2Off);
- server.on("/led3on", handleLed3On);
- server.on("/led3off", handleLed3Off);
- server.begin();
- Serial.println("Server dimulai");
- }
- void loop() {
- server.handleClient();
- if (WiFi.status() != WL_CONNECTED) {
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi terhubung");
- }
- // Melakukan pembacaan sensor
- for (int i = 0; i < NUM_SENSORS; i++) {
- uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
- if (result == node.ku8MBSuccess) {
- sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
- } else {
- Serial.println("Gagal membaca data!");
- delay(1000);
- continue;
- }
- }
- // Menampilkan informasi suhu dan kelembaban
- Serial.printf("Suhu: %.2f°C, Kelembaban: %.2f%%\n", sensorData[TEMP_SENSOR], sensorData[HUM_SENSOR]);
- // Memeriksa apakah suhu atau kelembaban melebihi ambang batas
- bool thresholdExceeded = (sensorData[TEMP_SENSOR] > TEMP_THRESHOLD) || (sensorData[HUM_SENSOR] > HUM_THRESHOLD);
- // Menyalakan buzzer jika LED 2 atau LED 3 dalam kondisi hidup, bahkan jika suhu atau kelembaban di bawah ambang batas
- if ((led2On || led3On) && !thresholdExceeded) {
- if (led2On)
- tone(BUZZER_PIN, 1000);
- else if (led3On)
- tone(BUZZER_PIN, 2000);
- }
- // Mematikan buzzer jika suhu atau kelembaban melebihi ambang batas
- else {
- noTone(BUZZER_PIN);
- }
- // Menunda untuk sementara sebelum melanjutkan
- delay(2000);
- }
- void handleRoot() {
- String message = "<!DOCTYPE html><html><head>";
- message += "<link rel='stylesheet' href='https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/css/bootstrap.min.css'>";
- message += "<script>function updateSlider(sliderValue, ledNumber) {window.location.href = sliderValue == 1 ? '/led' + ledNumber + 'on' : '/led' + ledNumber + 'off';}</script>";
- message += "</head><body class='container'>";
- message += "<h1 class='text-center my-4'>Suhu dan Kelembaban</h1>";
- message += "<p class='text-center'>Suhu: " + String(sensorData[TEMP_SENSOR]) + " C</p>";
- message += "<p class='text-center'>Kelembaban: " + String(sensorData[HUM_SENSOR]) + " %</p>";
- message += "<div class='d-flex justify-content-center'>";
- 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>";
- 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>";
- message += "</div></body></html>";
- server.send(200, "text/html", message);
- }
- void handleLed2On() {
- digitalWrite(LED_PIN2, HIGH);
- led2On = true;
- server.send(200, "text/plain", "LED 2 dinyalakan");
- }
- void handleLed3On() {
- digitalWrite(LED_PIN3, HIGH);
- led3On = true;
- server.send(200, "text/plain", "LED 3 dinyalakan");
- }
- void handleLed2Off() {
- digitalWrite(LED_PIN2, LOW);
- led2On = false;
- server.send(200, "text/plain", "LED 2 dimatikan");
- }
- void handleLed3Off() {
- digitalWrite(LED_PIN3, LOW);
- led3On = false;
- server.send(200, "text/plain", "LED 3 dimatikan");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement