Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #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[] = "Beli kuota dong";
- 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[15];
- char status[15];
- const int LED_PINS[] = {13, 12, 14};
- bool wifiConnected = false;
- bool buzzerControl = false;
- unsigned long previousMillis = 0;
- const long interval = 1000;
- 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");
- buzzerControl = true;
- }
- }
- 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");
- buzzerControl = true;
- }
- }
- void setup() {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- pinMode(BUZZER_PIN, 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);
- timer.setInterval(5000L, sendSensorData);
- }
- BLYNK_WRITE(V0) {
- digitalWrite(LED_PINS[0], param.asInt());
- }
- BLYNK_WRITE(V1) {
- digitalWrite(LED_PINS[1], param.asInt());
- }
- BLYNK_WRITE(V2) {
- digitalWrite(LED_PINS[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) {
- buzzerControl = true;
- } else {
- buzzerControl = false;
- }
- }
- void checkWiFiConnection() {
- if (WiFi.status() != WL_CONNECTED) {
- wifiConnected = false;
- WiFi.begin(ssid, pass);
- } else {
- wifiConnected = true;
- }
- }
- 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 {
- return;
- }
- }
- }
- void controlBuzzer() {
- if (buzzerControl) {
- digitalWrite(BUZZER_PIN, HIGH);
- } else {
- digitalWrite(BUZZER_PIN, LOW);
- }
- }
- void loop() {
- Blynk.run();
- timer.run();
- unsigned long currentMillis = millis();
- if (currentMillis - previousMillis >= interval) {
- previousMillis = currentMillis;
- checkWiFiConnection();
- if (wifiConnected) {
- readSensors();
- checkTemperatureStatus();
- checkHumidityStatus();
- controlBuzzer();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement