Advertisement
mudhita_triari

ESP32 MQTT - Mosquitto

Mar 28th, 2024
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.58 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <ModbusMaster.h>
  4. #include <SoftwareSerial.h>
  5.  
  6. #define MAX485_RE_NEG 5
  7. #define MAX485_DE 4
  8. #define SSERIAL_RX_PIN 16
  9. #define SSERIAL_TX_PIN 17
  10.  
  11. #define NUM_SENSORS 2
  12. #define TEMP_SENSOR 0
  13. #define HUM_SENSOR 1
  14.  
  15. const char* ssid = "vivo V29";
  16. const char* password = "Nabila041185";
  17.  
  18. const char* mqtt_server = "test.mosquitto.org";
  19. const char* mqtt_topic_temp = "nabila/suhu";
  20. const char* mqtt_topic_hum = "nabila/kelembapan";
  21. const char* mqtt_topic_pred = "nabila/prediksi";
  22. const char *mqtt_username = "";
  23. const char *mqtt_password = "";
  24. const int mqtt_port = 1883;
  25.  
  26. const int led1Pin = 13;
  27. const int led2Pin = 12;
  28. const int led3Pin = 14;
  29. const int led4Pin = 27;
  30.  
  31. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  32. ModbusMaster node;
  33. WiFiClient espClient;
  34. PubSubClient client(espClient);
  35.  
  36. uint16_t sensorAddresses[NUM_SENSORS] = {0x0001, 0x0002};
  37. float sensorData[NUM_SENSORS];
  38.  
  39. void preTransmission() {
  40. digitalWrite(MAX485_RE_NEG, 1);
  41. digitalWrite(MAX485_DE, 1);
  42. }
  43.  
  44. void postTransmission() {
  45. digitalWrite(MAX485_RE_NEG, 0);
  46. digitalWrite(MAX485_DE, 0);
  47. }
  48.  
  49. void setup() {
  50. pinMode(MAX485_RE_NEG, OUTPUT);
  51. pinMode(MAX485_DE, OUTPUT);
  52. digitalWrite(MAX485_RE_NEG, 0);
  53. digitalWrite(MAX485_DE, 0);
  54.  
  55. Serial.begin(9600);
  56. RS485Serial.begin(9600);
  57.  
  58. node.begin(1, RS485Serial);
  59. node.preTransmission(preTransmission);
  60. node.postTransmission(postTransmission);
  61.  
  62. WiFi.begin(ssid, password);
  63. while (!WiFi.isConnected()) {
  64. delay(500);
  65. Serial.print(".");
  66. }
  67. Serial.println("");
  68. Serial.println("WiFi terhubung");
  69.  
  70. client.setServer(mqtt_server, mqtt_port);
  71. }
  72.  
  73. void loop() {
  74. if (!client.connected()) {
  75. connect_mqtt();
  76. }
  77.  
  78. for (int i = 0; i < NUM_SENSORS; i++) {
  79. uint8_t result = node.readInputRegisters(sensorAddresses[i], 1);
  80.  
  81. if (result == node.ku8MBSuccess) {
  82. sensorData[i] = float(node.getResponseBuffer(0) / 10.00F);
  83. } else {
  84. delay(1000);
  85. return;
  86. }
  87. }
  88.  
  89. send_mqtt(mqtt_topic_temp, sensorData[TEMP_SENSOR]);
  90. send_mqtt(mqtt_topic_hum, sensorData[HUM_SENSOR]);
  91.  
  92. String prediction = analyze_and_predict(sensorData[TEMP_SENSOR], sensorData[HUM_SENSOR]);
  93. client.publish(mqtt_topic_pred, prediction.c_str());
  94.  
  95.  
  96. if (prediction.indexOf("panas") != -1 || prediction.indexOf("hujan") != -1) { // <--- Control LEDs based on prediction
  97. digitalWrite(led1Pin, HIGH);
  98. digitalWrite(led2Pin, HIGH);
  99. digitalWrite(led3Pin, LOW);
  100. digitalWrite(led4Pin, LOW);
  101. } else {
  102. digitalWrite(led1Pin, LOW);
  103. digitalWrite(led2Pin, LOW);
  104. digitalWrite(led3Pin, HIGH);
  105. digitalWrite(led4Pin, HIGH);
  106. }
  107.  
  108. delay(2000);
  109. }
  110.  
  111. void connect_mqtt() {
  112. while (!client.connected()) {
  113. Serial.println("Connecting to MQTT...");
  114. if (client.connect("ESP32Client", mqtt_username, mqtt_password)) {
  115. Serial.println("Connected to MQTT broker");
  116. // Subscribe to topics here
  117. } else {
  118. Serial.print("Failed, rc=");
  119. Serial.print(client.state());
  120. Serial.println(" Retrying in 5 seconds...");
  121. delay(5000);
  122. }
  123. }
  124. }
  125.  
  126. void send_mqtt(const char* topic, float value) {
  127. char buffer[10];
  128. dtostrf(value, 4, 2, buffer);
  129. client.publish(topic, buffer);
  130. }
  131.  
  132. String analyze_and_predict(float temp_value, float hum_value) {
  133. if (temp_value > 30) {
  134. return "Cuaca panas, kemungkinan tidak hujan.";
  135. } else if (hum_value > 70) {
  136. return "Kelembapan tinggi, potensi hujan.";
  137. } else {
  138. return "Cuaca normal, tidak ada perubahan signifikan.";
  139. }
  140. }
Tags: #esp32 #MQTT
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement