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>
- #define MAX485_RE_NEG 5
- #define MAX485_DE 4
- #define SSERIAL_RX_PIN 16
- #define SSERIAL_TX_PIN 17
- #define NUM_SENSORS 2
- #define TEMP_SENSOR 0
- #define HUM_SENSOR 1
- const char* ssid = "vivo V29";
- const char* password = "Nabila041185";
- const char* mqtt_server = "test.mosquitto.org";
- const char* mqtt_topic_temp = "nabila/suhu";
- const char* mqtt_topic_hum = "nabila/kelembapan";
- const char* mqtt_topic_pred = "nabila/prediksi";
- const char *mqtt_username = "";
- const char *mqtt_password = "";
- const int mqtt_port = 1883;
- const int led1Pin = 13;
- const int led2Pin = 12;
- const int led3Pin = 14;
- const int led4Pin = 27;
- SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
- ModbusMaster node;
- WiFiClient espClient;
- PubSubClient client(espClient);
- uint16_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
- float sensorData[NUM_SENSORS];
- void preTransmission() {
- digitalWrite(MAX485_RE_NEG, 1);
- digitalWrite(MAX485_DE, 1);
- }
- void postTransmission() {
- digitalWrite(MAX485_RE_NEG, 0);
- digitalWrite(MAX485_DE, 0);
- }
- 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);
- WiFi.begin(ssid, password);
- while (!WiFi.isConnected()) {
- delay(500);
- Serial.print(".");
- }
- Serial.println("");
- Serial.println("WiFi terhubung");
- client.setServer(mqtt_server, mqtt_port);
- }
- void loop() {
- if (!client.connected()) {
- connect_mqtt();
- }
- 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 {
- delay(1000);
- return;
- }
- }
- send_mqtt(mqtt_topic_temp, sensorData[TEMP_SENSOR]);
- send_mqtt(mqtt_topic_hum, sensorData[HUM_SENSOR]);
- String prediction = analyze_and_predict(sensorData[TEMP_SENSOR], sensorData[HUM_SENSOR]);
- client.publish(mqtt_topic_pred, prediction.c_str());
- if (prediction.indexOf("panas") != -1 || prediction.indexOf("hujan") != -1) { // <--- Control LEDs based on prediction
- digitalWrite(led1Pin, HIGH);
- digitalWrite(led2Pin, HIGH);
- digitalWrite(led3Pin, LOW);
- digitalWrite(led4Pin, LOW);
- } else {
- digitalWrite(led1Pin, LOW);
- digitalWrite(led2Pin, LOW);
- digitalWrite(led3Pin, HIGH);
- digitalWrite(led4Pin, HIGH);
- }
- delay(2000);
- }
- void connect_mqtt() {
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect("ESP32Client", mqtt_username, mqtt_password)) {
- Serial.println("Connected to MQTT broker");
- // Subscribe to topics here
- } else {
- Serial.print("Failed, rc=");
- Serial.print(client.state());
- Serial.println(" Retrying in 5 seconds...");
- delay(5000);
- }
- }
- }
- void send_mqtt(const char* topic, float value) {
- char buffer[10];
- dtostrf(value, 4, 2, buffer);
- client.publish(topic, buffer);
- }
- String analyze_and_predict(float temp_value, float hum_value) {
- if (temp_value > 30) {
- return "Cuaca panas, kemungkinan tidak hujan.";
- } else if (hum_value > 70) {
- return "Kelembapan tinggi, potensi hujan.";
- } else {
- return "Cuaca normal, tidak ada perubahan signifikan.";
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement