Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define BLYNK_TEMPLATE_ID "TMPLF4ZLfpB8"
- #define BLYNK_TEMPLATE_NAME "Monitoring Suhu"
- #include <WiFi.h>
- #include <WiFiClient.h>
- #include <BlynkSimpleEsp32.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 BUZZER_PIN 27
- #define NUM_SENSORS 2
- #define TEMP_SENSOR 0
- #define HUM_SENSOR 1
- float TEMP_THRESHOLD = 50.0;
- float HUM_THRESHOLD = 80.0;
- char auth[] = "7CeCdQL51Y5AC3OYSrMnTApeAQMXPvfq";
- char ssid[] = "vivo V29";
- char pass[] = "Nabila041185";
- SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
- ModbusMaster node;
- float sensorData[NUM_SENSORS];
- uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
- char humidityStatus[20];
- char status[20];
- const int LED_PINS[] = {13, 12, 14};
- const int V_PINS[] = {V0, V1, V2};
- bool wifiConnected = false;
- bool buzzerStatus = false;
- BlynkTimer timer;
- void preTransmission() {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission() {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- void sendSensorData() {
- Blynk.virtualWrite(V9, humidityStatus);
- Blynk.virtualWrite(V8, sensorData[HUM_SENSOR]);
- Blynk.virtualWrite(V7, sensorData[TEMP_SENSOR]);
- Blynk.virtualWrite(V6, status);
- }
- void checkTemperatureStatus() {
- if (sensorData[TEMP_SENSOR] < 20) {
- strcpy(status, "Suhu Dingin");
- } else if (sensorData[TEMP_SENSOR] >= 20 && sensorData[TEMP_SENSOR] <= TEMP_THRESHOLD) {
- strcpy(status, "Suhu Sedang");
- } else {
- strcpy(status, "Suhu Panas");
- }
- }
- void checkHumidityStatus() {
- if (sensorData[HUM_SENSOR] < 40) {
- strcpy(humidityStatus, "Kelembapan Rendah");
- } else if (sensorData[HUM_SENSOR] >= 40 && sensorData[HUM_SENSOR] <= HUM_THRESHOLD) {
- strcpy(humidityStatus, "Kelembapan Normal");
- } else {
- strcpy(humidityStatus, "Kelembapan Tinggi");
- }
- }
- void buzzerOn() {
- digitalWrite(BUZZER_PIN, HIGH);
- delay(5000);
- digitalWrite(BUZZER_PIN, LOW);
- }
- void setup() {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- Serial.begin(9600);
- RS485Serial.begin(9600);
- node.begin(1, RS485Serial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- for (int i = 0; i < sizeof(LED_PINS) / sizeof(LED_PINS[0]); i++) {
- pinMode(LED_PINS[i], OUTPUT);
- digitalWrite(LED_PINS[i], LOW);
- }
- Blynk.begin(auth, ssid, pass);
- pinMode(BUZZER_PIN, OUTPUT);
- timer.setInterval(5000L, sendSensorData);
- }
- void handleLED(int vPin, int ledIndex, int param) {
- digitalWrite(LED_PINS[ledIndex], param);
- }
- BLYNK_WRITE(V0) { handleLED(V0, 0, param.asInt()); }
- BLYNK_WRITE(V1) { handleLED(V1, 1, param.asInt()); }
- BLYNK_WRITE(V2) { handleLED(V2, 2, param.asInt()); }
- BLYNK_WRITE(V3) { TEMP_THRESHOLD = param.asFloat(); }
- BLYNK_WRITE(V4) { HUM_THRESHOLD = param.asFloat(); }
- BLYNK_WRITE(V5) {
- int pinValue = param.asInt();
- if (pinValue == 1) {
- buzzerOn();
- }
- }
- void readSensors() {
- 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 {
- // Handle error here
- Serial.println("Error reading sensor data");
- return;
- }
- }
- }
- void loop() {
- Blynk.run();
- timer.run();
- if (wifiConnected) {
- readSensors();
- checkTemperatureStatus();
- checkHumidityStatus();
- }
- if (WiFi.status() != WL_CONNECTED) {
- WiFi.begin(ssid, pass);
- } else {
- wifiConnected = true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement