Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <AdafruitIO_WiFi.h>
- #include <ModbusMaster.h>
- #include <SoftwareSerial.h>
- #include <PubSubClient.h>
- #include <WiFi.h>
- // Difinisi Pin
- #define MAX485_RE_NEG 5
- #define MAX485_DE 4
- #define SSERIAL_RX_PIN 16
- #define SSERIAL_TX_PIN 17
- #define PIN_LED_1 13
- #define PIN_LED_2 12
- #define PIN_LED_3 14
- #define PIN_BUZZER 27
- // Difinisi Sensor dan Address
- #define NUM_SENSORS 2
- #define TEMP_SENSOR 0
- #define HUM_SENSOR 1
- uint8_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
- // Konfigurasi WiFi dan Adafruit IO
- #define WIFI_SSID "vivo V29"
- #define WIFI_PASS "Nabila041185"
- #define IO_USERNAME "Mudhita"
- #define IO_KEY "aio_urCG55WHy9b6aOIkD8nmJT8J190q"
- // Konfigurasi MQTT
- const char* mqtt_server = "broker.emqx.io";
- const char* mqtt_topik_suhu = "mudhita/esp32/suhu";
- const char* mqtt_topik_kelembaban = "mudhita/esp32/kelembaban";
- const int mqtt_port = 1883;
- const char* mqtt_username = "";
- const char* mqtt_password = "";
- // Variabel Umum
- float sensorData[NUM_SENSORS];
- float batasAtasSuhu = 0.0;
- float batasBawahSuhu = 0.0;
- unsigned long waktuUpdateTerakhir = 0;
- const unsigned long intervalUpdate = 5000;
- // Object Declarations
- SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
- ModbusMaster node;
- WiFiClient wifiClient;
- PubSubClient client(wifiClient);
- AdafruitIO_WiFi io(IO_USERNAME, IO_KEY, WIFI_SSID, WIFI_PASS);
- AdafruitIO_Feed *feedUpper = io.feed("suhuAtas");
- AdafruitIO_Feed *feedLower = io.feed("suhuBawah");
- AdafruitIO_Feed *feedTemp = io.feed("suhu");
- AdafruitIO_Feed *feedHumid = io.feed("kelembaban");
- // Fungsi Unit
- void setup_wifi();
- void setup_MQTT();
- void setup_AdafruitIO();
- void setupActuators();
- void readSensor();
- void publishData_MQTT_dan_AdafruitIO();
- void handleSensorReadingErrors();
- void reconnectMQTT();
- void setup() {
- pinMode(MAX485_RE_NEG, OUTPUT);
- pinMode(MAX485_DE, OUTPUT);
- Serial.begin(9600);
- RS485Serial.begin(9600);
- node.begin(1, RS485Serial);
- node.preTransmission([](){
- digitalWrite(MAX485_RE_NEG, HIGH);
- digitalWrite(MAX485_DE, HIGH);
- });
- node.postTransmission([](){
- digitalWrite(MAX485_RE_NEG, LOW);
- digitalWrite(MAX485_DE, LOW);
- });
- setup_wifi();
- setup_MQTT();
- setup_AdafruitIO();
- setupActuators();
- }
- void loop() {
- io.run();
- unsigned long millisSekarang = millis();
- if (millisSekarang - waktuUpdateTerakhir >= intervalUpdate) {
- readSensor();
- publishData_MQTT_dan_AdafruitIO();
- waktuUpdateTerakhir = millisSekarang;
- }
- }
- void setup_wifi() {
- Serial.println("Connecting to WiFi...");
- WiFi.begin(WIFI_SSID, WIFI_PASS);
- int attempts = 0;
- while (WiFi.status() != WL_CONNECTED) {
- delay(500);
- Serial.print(".");
- attempts++;
- if(attempts > 20) {
- Serial.println("WiFi connection failed, restarting...");
- ESP.restart();
- }
- }
- Serial.println("");
- Serial.println("WiFi connected");
- Serial.println("IP address: ");
- Serial.println(WiFi.localIP());
- }
- void setup_MQTT() {
- client.setServer(mqtt_server, mqtt_port);
- }
- void setup_AdafruitIO() {
- io.connect();
- feedUpper->onMessage([](AdafruitIO_Data *data) {
- if (strcmp(data->feedName(), "suhuAtas") == 0) {
- batasAtasSuhu = atof(data->value());
- } else if (strcmp(data->feedName(), "suhuBawah") == 0) {
- batasBawahSuhu = atof(data->value());
- }
- });
- feedLower->onMessage([](AdafruitIO_Data *data) {
- if (strcmp(data->feedName(), "suhuAtas") == 0) {
- batasAtasSuhu = atof(data->value());
- } else if (strcmp(data->feedName(), "suhuBawah") == 0) {
- batasBawahSuhu = atof(data->value());
- }
- });
- feedUpper->get();
- feedLower->get();
- }
- void setupActuators() {
- pinMode(PIN_LED_1, OUTPUT);
- pinMode(PIN_LED_2, OUTPUT);
- pinMode(PIN_LED_3, OUTPUT);
- pinMode(PIN_BUZZER, OUTPUT);
- }
- void readSensor() {
- 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("Failed to read data!");
- handleSensorReadingErrors();
- continue;
- }
- }
- }
- void handleSensorReadingErrors() {
- Serial.println("Segera Hubungi Pihak Berwenang...");
- }
- void publishData_MQTT_dan_AdafruitIO() {
- if (!client.connected()) {
- reconnectMQTT();
- }
- char tempStr[8];
- char humidStr[8];
- dtostrf(sensorData[TEMP_SENSOR], 1, 2, tempStr);
- dtostrf(sensorData[HUM_SENSOR], 1, 2, humidStr);
- if (client.connected()) {
- client.publish(mqtt_topik_suhu, tempStr);
- client.publish(mqtt_topik_kelembaban, humidStr);
- } else {
- Serial.println("MQTT client not connected!");
- }
- if (!isnan(sensorData[TEMP_SENSOR]) && !isnan(sensorData[HUM_SENSOR])) {
- feedTemp->save(sensorData[TEMP_SENSOR]);
- feedHumid->save(sensorData[HUM_SENSOR]);
- }
- if (sensorData[TEMP_SENSOR] > batasAtasSuhu) {
- digitalWrite(PIN_BUZZER, HIGH);
- digitalWrite(PIN_LED_1, HIGH);
- } else {
- digitalWrite(PIN_BUZZER, LOW);
- digitalWrite(PIN_LED_1, LOW);
- }
- if (sensorData[TEMP_SENSOR] >= batasBawahSuhu && sensorData[TEMP_SENSOR] <= batasAtasSuhu) {
- digitalWrite(PIN_LED_2, HIGH);
- } else {
- digitalWrite(PIN_LED_2, LOW);
- }
- if (sensorData[TEMP_SENSOR] < batasBawahSuhu) {
- digitalWrite(PIN_LED_3, HIGH);
- } else {
- digitalWrite(PIN_LED_3, LOW);
- }
- }
- void reconnectMQTT() {
- while (!client.connected()) {
- Serial.println("Connecting to MQTT...");
- if (client.connect("ESP32Client", mqtt_username, mqtt_password)) {
- Serial.println("Connected to MQTT");
- } else {
- Serial.print("Failed to connect to MQTT. State: ");
- Serial.println(client.state());
- delay(5000);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement