Advertisement
mudhita_triari

koding

Mar 29th, 2024
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.23 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <PubSubClient.h>
  3. #include <ModbusMaster.h>
  4. #include <SoftwareSerial.h>
  5. #include <Adafruit_MQTT.h>
  6. #include <Adafruit_MQTT_Client.h>
  7. #include <AdafruitIO_WiFi.h>
  8.  
  9. #define MAX485_RE_NEG 5
  10. #define MAX485_DE 4
  11. #define SSERIAL_RX_PIN 16
  12. #define SSERIAL_TX_PIN 17
  13.  
  14. #define JUMLAH_SENSOR 2
  15. #define SENSOR_SUHU 0
  16. #define SENSOR_KELEMBABAN 1
  17.  
  18. const char* ssid = "vivo V29";
  19. const char* password = "Nabila041185";
  20. const char* IO_Username = "Mudhita";
  21. const char* IO_Key = "aio_urCG55WHy9b6aOIkD8nmJT8J190q";
  22.  
  23. const char* mqtt_server = "broker.emqx.io";
  24. const char* mqtt_topik_suhu = "wian/esp32/suhu";
  25. const char* mqtt_topik_kelembaban = "wian/esp32/kelembaban";
  26. const char* mqtt_topik_prediksi = "wian/esp32/prediksi";
  27. const char *mqtt_username = "wian";
  28. const char *mqtt_password = "wian";
  29. const int mqtt_port = 1883;
  30.  
  31. const int pin_buzzer = 27;
  32. const int pin_led1 = 13;
  33. const int pin_led2 = 12;
  34. const int pin_led3 = 14;
  35.  
  36. SoftwareSerial RS485Serial(SSERIAL_RX_PIN, SSERIAL_TX_PIN);
  37. ModbusMaster node;
  38. WiFiClient klienWiFi;
  39. PubSubClient klienMQTT(klienWiFi);
  40.  
  41. AdafruitIO_WiFi io( IO_Username, IO_Key, ssid, password);
  42. AdafruitIO_Feed *feed_suhu;
  43. AdafruitIO_Feed *feed_kelembaban;
  44. AdafruitIO_Feed *feed_prediksi;
  45. AdafruitIO_Feed *feed_batas_suhu_bawah;
  46. AdafruitIO_Feed *feed_batas_suhu_atas;
  47.  
  48. uint16_t alamat_sensor[JUMLAH_SENSOR] = {0x0001, 0x0002};
  49. float data_sensor[JUMLAH_SENSOR];
  50.  
  51. void preTransmission() {
  52. digitalWrite(MAX485_RE_NEG, 1);
  53. digitalWrite(MAX485_DE, 1);
  54. }
  55.  
  56. void postTransmission() {
  57. digitalWrite(MAX485_RE_NEG, 0);
  58. digitalWrite(MAX485_DE, 0);
  59. }
  60.  
  61. void setup() {
  62. Serial.begin(9600);
  63. RS485Serial.begin(9600);
  64.  
  65. node.begin(1, RS485Serial);
  66. node.preTransmission(preTransmission);
  67. node.postTransmission(postTransmission);
  68.  
  69. pinMode(MAX485_RE_NEG, OUTPUT);
  70. pinMode(MAX485_DE, OUTPUT);
  71. digitalWrite(MAX485_RE_NEG, 0);
  72. digitalWrite(MAX485_DE, 0);
  73. pinMode(pin_buzzer, OUTPUT);
  74.  
  75. sambungWiFi();
  76. sambungMQTT();
  77. sambungIO();
  78.  
  79. // debug Status Feed IO
  80. Serial.print("Feed IO 'Suhu' status: ");
  81. if (feed_suhu) {
  82. Serial.println("Terhubung");
  83. } else {
  84. Serial.println("Tidak terhubung");
  85. }
  86.  
  87. Serial.print("Feed IO 'Kelembaban' status: ");
  88. if (feed_kelembaban) {
  89. Serial.println("Terhubung");
  90. } else {
  91. Serial.println("Tidak terhubung");
  92. }
  93.  
  94. Serial.print("Feed IO 'Prediksi' status: ");
  95. if (feed_prediksi) {
  96. Serial.println("Terhubung");
  97. } else {
  98. Serial.println("Tidak terhubung");
  99. }
  100.  
  101. Serial.print("Feed IO 'Ambang Batas' status: ");
  102. if (feed_batas_suhu_bawah && feed_batas_suhu_atas) {
  103. Serial.println("Terhubung");
  104. } else {
  105. Serial.println("Tidak terhubung");
  106. }
  107. }
  108.  
  109. void loop() {
  110. if (!klienMQTT.connected()) {
  111. sambungKembaliMQTT();
  112. }
  113.  
  114. bacaDataSensor();
  115. publikasikanDataKeMQTT();
  116. handleLogikaPrediksi();
  117. simpanDataKeIO();
  118.  
  119. delay(2000);
  120. }
  121.  
  122. void sambungWiFi() {
  123. WiFi.begin(ssid, password);
  124. while (WiFi.status() != WL_CONNECTED) {
  125. delay(500);
  126. Serial.print(".");
  127. }
  128. Serial.println("");
  129. Serial.println("WiFi terhubung");
  130. }
  131.  
  132. void sambungMQTT() {
  133. klienMQTT.setServer(mqtt_server, mqtt_port);
  134. }
  135.  
  136. void sambungIO() {
  137. io.connect();
  138. feed_suhu = io.feed("suhu");
  139. feed_kelembaban = io.feed("kelembaban");
  140. feed_prediksi = io.feed("prediksi");
  141. feed_batas_suhu_bawah = io.feed("ambang_batas_bawah");
  142. feed_batas_suhu_atas = io.feed("ambang_batas_atas");
  143. }
  144.  
  145. void sambungKembaliMQTT() {
  146. while (!klienMQTT.connected()) {
  147. Serial.println("Menghubungkan ke MQTT...");
  148. if (klienMQTT.connect("ESP32Client", mqtt_username, mqtt_password)) {
  149. Serial.println("Terhubung ke broker MQTT");
  150. } else {
  151. Serial.print("Gagal, rc=");
  152. Serial.print(klienMQTT.state());
  153. Serial.println(" Coba lagi dalam 5 detik...");
  154. delay(5000);
  155. }
  156. }
  157. }
  158.  
  159. void bacaDataSensor() {
  160. for (int i = 0; i < JUMLAH_SENSOR; i++) {
  161. uint8_t hasil = node.readInputRegisters(alamat_sensor[i], 1);
  162.  
  163. if (hasil == node.ku8MBSuccess) {
  164. data_sensor[i] = float(node.getResponseBuffer(0) / 10.00F);
  165. } else {
  166. delay(1000);
  167. return;
  168. }
  169. }
  170. }
  171.  
  172. void publikasikanDataKeMQTT() {
  173. publishMQTT(mqtt_topik_suhu, data_sensor[SENSOR_SUHU]);
  174. publishMQTT(mqtt_topik_kelembaban, data_sensor[SENSOR_KELEMBABAN]);
  175.  
  176. String prediksi = analisisDanPrediksi(data_sensor[SENSOR_SUHU], data_sensor[SENSOR_KELEMBABAN]);
  177. klienMQTT.publish(mqtt_topik_prediksi, prediksi.c_str());
  178. Serial.print("Publish MQTT: --> " + String(data_sensor[SENSOR_SUHU]) + " || " + String(data_sensor[SENSOR_KELEMBABAN]) + "\n");
  179. }
  180.  
  181. // Membaca nilai ambang batas suhu di Adafruit IO
  182. void handleLogikaPrediksi() {
  183.  
  184. float ambang_batas_suhu_bawah = atof(feed_batas_suhu_bawah->lastValue()->toString().c_str());
  185. float ambang_batas_suhu_atas = atof(feed_batas_suhu_atas->lastValue()->toString().c_str());
  186.  
  187. if (data_sensor[SENSOR_SUHU] > ambang_batas_suhu_atas) {
  188. digitalWrite(pin_buzzer, HIGH);
  189. digitalWrite(pin_led1, HIGH);
  190. digitalWrite(pin_led2, LOW);
  191. digitalWrite(pin_led3, LOW);
  192. } else if (data_sensor[SENSOR_SUHU] < ambang_batas_suhu_bawah) {
  193. digitalWrite(pin_buzzer, LOW);
  194. digitalWrite(pin_led1, LOW);
  195. digitalWrite(pin_led2, HIGH);
  196. digitalWrite(pin_led3, LOW);
  197. } else {
  198. digitalWrite(pin_buzzer, LOW);
  199. digitalWrite(pin_led1, LOW);
  200. digitalWrite(pin_led2, LOW);
  201. digitalWrite(pin_led3, HIGH);
  202. }
  203. }
  204.  
  205. void simpanDataKeIO() {
  206. feed_suhu->save(data_sensor[SENSOR_SUHU]);
  207. feed_kelembaban->save(data_sensor[SENSOR_KELEMBABAN]);
  208. feed_prediksi->save(analisisDanPrediksi(data_sensor[SENSOR_SUHU], data_sensor[SENSOR_KELEMBABAN]));
  209.  
  210. io.run();
  211.  
  212. delay(2000);
  213. }
  214.  
  215. void publishMQTT(const char* topic, float value) {
  216. char buffer[10];
  217. dtostrf(value, 4, 2, buffer);
  218. klienMQTT.publish(topic, buffer);
  219. }
  220.  
  221. String analisisDanPrediksi(float suhu, float kelembaban) {
  222. if (suhu > 30) {
  223. return "Cuaca panas, kemungkinan tidak hujan.";
  224. } else if (kelembaban > 70) {
  225. return "Kelembapan tinggi, potensi hujan.";
  226. } else {
  227. return "Cuaca normal, tidak ada perubahan signifikan.";
  228. }
  229. }
  230.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement