Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <PubSubClient.h>
- #include <ModbusMaster.h>
- #include <SoftwareSerial.h>
- #include <Adafruit_MQTT.h>
- #include <Adafruit_MQTT_Client.h>
- #include <AdafruitIO_WiFi.h>
- #define MAX485_RE_NEG 5
- #define MAX485_DE 4
- #define SSERIAL_RX_PIN 16
- #define SSERIAL_TX_PIN 17
- #define JUMLAH_SENSOR 2
- #define SENSOR_SUHU 0
- #define SENSOR_KELEMBABAN 1
- const char* ssid = "vivo V29";
- const char* password = "Nabila041185";
- const char* IO_Username = "Mudhita";
- const char* IO_Key = "aio_urCG55WHy9b6aOIkD8nmJT8J190q";
- const char* mqtt_server = "broker.emqx.io";
- const char* mqtt_topik_suhu = "wian/esp32/suhu";
- const char* mqtt_topik_kelembaban = "wian/esp32/kelembaban";
- const char* mqtt_topik_prediksi = "wian/esp32/prediksi";
- const char *mqtt_username = "wian";
- const char *mqtt_password = "wian";
- const int mqtt_port = 1883;
- const int pin_buzzer = 27;
- const int pin_led1 = 13;
- const int pin_led2 = 12;
- const int pin_led3 = 14;
- SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
- ModbusMaster node;
- WiFiClient klienWiFi;
- PubSubClient klienMQTT(klienWiFi);
- AdafruitIO_WiFi io( IO_Username, IO_Key, ssid, password);
- AdafruitIO_Feed *feed_suhu;
- AdafruitIO_Feed *feed_kelembaban;
- uint16_t alamat_sensor[JUMLAH_SENSOR] = {0x0001, 0x0002};
- float data_sensor[JUMLAH_SENSOR];
- void preTransmission() {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission() {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- void setup() {
- Serial.begin(9600);
- RS485Serial.begin(9600);
- node.begin(1, RS485Serial);
- node.preTransmission(preTransmission);
- node.postTransmission(postTransmission);
- inisialisasiPerangkat();
- sambungWiFi();
- sambungMQTT();
- sambungIO();
- }
- void loop() {
- if (!klienMQTT.connected()) {
- sambungKembaliMQTT();
- }
- bacaDataSensor();
- publikasikanDataKeMQTT();
- handleLogikaPrediksi();
- simpanDataKeIO();
- delay(2000);
- }
- void inisialisasiPerangkat() {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- pinMode(pin_buzzer, OUTPUT);
- }
- void sambungWiFi() {
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi terhubung");
- }
- void sambungMQTT() {
- klienMQTT.setServer(mqtt_server, mqtt_port);
- }
- void sambungIO() {
- io.connect();
- feed_suhu = io.feed("suhu");
- feed_kelembaban = io.feed("kelembaban");
- }
- void sambungKembaliMQTT() {
- while (!klienMQTT.connected()) {
- Serial.println("Menghubungkan ke MQTT...");
- if (klienMQTT.connect("ESP32Client", mqtt_username, mqtt_password)) {
- Serial.println("Terhubung ke broker MQTT");
- } else {
- Serial.print("Gagal, rc=");
- Serial.print(klienMQTT.state());
- Serial.println(" Coba lagi dalam 5 detik...");
- delay(5000);
- }
- }
- }
- void bacaDataSensor() {
- for (int i = 0; i < JUMLAH_SENSOR; i++) {
- uint8_t hasil = node.readInputRegisters(alamat_sensor[i], 1);
- if (hasil == node.ku8MBSuccess) {
- data_sensor[i] = float(node.getResponseBuffer(0) / 10.00F);
- } else {
- delay(1000);
- return;
- }
- }
- }
- void publikasikanDataKeMQTT() {
- publishMQTT(mqtt_topik_suhu, data_sensor[SENSOR_SUHU]);
- publishMQTT(mqtt_topik_kelembaban, data_sensor[SENSOR_KELEMBABAN]);
- String prediksi = analisisDanPrediksi(data_sensor[SENSOR_SUHU], data_sensor[SENSOR_KELEMBABAN]);
- klienMQTT.publish(mqtt_topik_prediksi, prediksi.c_str());
- Serial.print("Publish MQTT: --> " + String(data_sensor[SENSOR_SUHU]) + " || " + String(data_sensor[SENSOR_KELEMBABAN]) + "\n");
- }
- void handleLogikaPrediksi() {
- if (data_sensor[SENSOR_SUHU] > 30) {
- digitalWrite(pin_buzzer, HIGH);
- digitalWrite(pin_led1, HIGH);
- digitalWrite(pin_led2, LOW);
- digitalWrite(pin_led3, LOW);
- } else if (data_sensor[SENSOR_SUHU] > 20 && data_sensor[SENSOR_SUHU] <= 30) {
- digitalWrite(pin_buzzer, LOW);
- digitalWrite(pin_led1, LOW);
- digitalWrite(pin_led2, HIGH);
- digitalWrite(pin_led3, LOW);
- } else {
- digitalWrite(pin_buzzer, LOW);
- digitalWrite(pin_led1, LOW);
- digitalWrite(pin_led2, LOW);
- digitalWrite(pin_led3, HIGH);
- }
- }
- void simpanDataKeIO() {
- feed_suhu->save(data_sensor[SENSOR_SUHU]);
- feed_kelembaban->save(data_sensor[SENSOR_KELEMBABAN]);
- io.run();
- delay(2000);
- }
- void publishMQTT(const char* topic, float value) {
- char buffer[10];
- dtostrf(value, 4, 2, buffer);
- klienMQTT.publish(topic, buffer);
- }
- String analisisDanPrediksi(float suhu, float kelembaban) {
- if (suhu > 30) {
- return "Cuaca panas, kemungkinan tidak hujan.";
- } else if (kelembaban > 70) {
- return "Kelembapan tinggi, potensi hujan.";
- } else {
- return "Cuaca normal, tidak ada perubahan signifikan.";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement