Advertisement
mudhita_triari

Tugas Akhir - ESP32 Gateway v1.0.1

May 25th, 2024 (edited)
738
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.54 KB | Source Code | 0 0
  1. #include <Arduino.h>
  2. #include <painlessMesh.h>
  3. #include <PubSubClient.h>
  4. #include <WiFiClient.h>
  5.  
  6. #define MESH_PREFIX     "INDOBOT FSID"
  7. #define MESH_PASSWORD   "INDOBOTACADEMY"
  8. #define MESH_PORT 5555
  9.  
  10. #define STATION_SSID "vivo V29"
  11. #define STATION_PASSWORD "Nabila041185"
  12.  
  13. #define HOSTNAME "MQTT_Bridge2"
  14.  
  15. // Prototipe
  16. void receivedCallback(const uint32_t &from, const String &msg);
  17. void mqttCallback(char* topic, byte* payload, unsigned int length);
  18. IPAddress getLocalIP();
  19.  
  20. IPAddress myIP(0, 0, 0, 0);
  21. const char* mqttBroker = "broker.emqx.io";
  22. const int mqttPort = 1883;
  23. const char* mqttUser = "emqx";
  24. const char* mqttPassword = "public";
  25.  
  26. painlessMesh mesh;
  27. WiFiClient wifiClient;
  28. PubSubClient mqttClient(wifiClient);
  29.  
  30. void setup() {
  31.   Serial.begin(115200);
  32.  
  33.   mesh.setDebugMsgTypes(ERROR | STARTUP | CONNECTION);              // Set sebelum init() untuk melihat pesan startup
  34.  
  35.   // Inisialisasi jaringan mesh
  36.   mesh.init(MESH_PREFIX, MESH_PASSWORD, MESH_PORT, WIFI_AP_STA);
  37.   mesh.onReceive(&receivedCallback);
  38.  
  39.   // Terhubung ke jaringan Wi-Fi yang ditentukan
  40.   mesh.stationManual(STATION_SSID, STATION_PASSWORD);
  41.   mesh.setHostname(HOSTNAME);
  42.   mesh.setRoot(true);                                               // Tetapkan node ini sebagai root
  43.   mesh.setContainsRoot(true);
  44.  
  45.   mqttClient.setServer(mqttBroker, mqttPort);
  46.   mqttClient.setCallback(mqttCallback);
  47. }
  48.  
  49. void loop() {
  50.   mesh.update();
  51.   mqttClient.loop();
  52.  
  53.   // Periksa dan perbarui alamat IP
  54.   if (myIP != getLocalIP()) {
  55.     myIP = getLocalIP();
  56.     Serial.println("IP saya adalah " + myIP.toString());
  57.  
  58.     if (mqttClient.connect("painlessMeshClient", mqttUser, mqttPassword)) {
  59.       Serial.println("Terhubung ke broker MQTT");
  60.       mqttClient.publish("painlessMesh/from/gateway", "Ready!");
  61.       mqttClient.subscribe("painlessMesh/to/#");
  62.     } else {
  63.       Serial.println("Gagal terhubung ke broker MQTT");
  64.     }
  65.   }
  66. }
  67.  
  68. // Callback untuk pesan yang diterima dari jaringan mesh
  69. void receivedCallback(const uint32_t &from, const String &msg) {
  70.   Serial.printf("bridge: Diterima dari %u msg=%s\n", from, msg.c_str());
  71.   String topic = "all_node3";
  72.   bool result = mqttClient.publish(topic.c_str(), msg.c_str());
  73.   if(result) {
  74.     Serial.println("Pesan berhasil dikirim ke MQTT");
  75.   } else {
  76.     Serial.println("Gagal mengirim pesan ke MQTT");
  77.   }
  78. }
  79.  
  80. // Callback untuk pesan yang diterima dari broker MQTT
  81. void mqttCallback(char* topic, uint8_t* payload, unsigned int length) {
  82.   Serial.print("Pesan diterima dari MQTT, topik: ");
  83.   Serial.println(topic);
  84.   char* cleanPayload = (char*)malloc(length + 1);
  85.   memcpy(cleanPayload, payload, length);
  86.   cleanPayload[length] = '\0';
  87.   String msg = String(cleanPayload);
  88.   free(cleanPayload);
  89.  
  90.   Serial.print("Payload: ");
  91.   Serial.println(msg);
  92.  
  93.   String targetStr = String(topic).substring(16); // Ekstrak ID target dari topik
  94.  
  95.   if (targetStr == "gateway") {
  96.     if (msg == "getNodes") {
  97.       auto nodes = mesh.getNodeList(true);
  98.       String str;
  99.       for (auto &&id : nodes)
  100.         str += String(id) + String(" ");
  101.       mqttClient.publish("painlessMesh/from/gateway", str.c_str());
  102.     }
  103.   } else if (targetStr == "broadcast") {
  104.     mesh.sendBroadcast(msg);
  105.   } else {
  106.     uint32_t target = strtoul(targetStr.c_str(), NULL, 10);
  107.     if (mesh.isConnected(target)) {
  108.       mesh.sendSingle(target, msg);
  109.     } else {
  110.       mqttClient.publish("painlessMesh/from/gateway", "Client not connected!");
  111.     }
  112.   }
  113. }
  114.  
  115. // Fungsi untuk mendapatkan alamat IP lokal node
  116. IPAddress getLocalIP() {
  117.   return IPAddress(mesh.getStationIP());
  118. }
  119.  
Tags: #esp32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement