Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <Arduino.h>
- #include <painlessMesh.h>
- #include <PubSubClient.h>
- #include <WiFiClient.h>
- #define MESH_PREFIX "INDOBOT FSID"
- #define MESH_PASSWORD "INDOBOTACADEMY"
- #define MESH_PORT 5555
- #define STATION_SSID "vivo V29"
- #define STATION_PASSWORD "Nabila041185"
- #define HOSTNAME "MQTT_Bridge2"
- // Prototipe
- void receivedCallback(const uint32_t &from, const String &msg);
- void mqttCallback(char* topic, byte* payload, unsigned int length);
- IPAddress getLocalIP();
- IPAddress myIP(0, 0, 0, 0);
- const char* mqttBroker = "broker.emqx.io";
- const int mqttPort = 1883;
- const char* mqttUser = "emqx";
- const char* mqttPassword = "public";
- painlessMesh mesh;
- WiFiClient wifiClient;
- PubSubClient mqttClient(wifiClient);
- void setup() {
- Serial.begin(115200);
- mesh.setDebugMsgTypes(ERROR | STARTUP | CONNECTION); // Set sebelum init() untuk melihat pesan startup
- // Inisialisasi jaringan mesh
- mesh.init(MESH_PREFIX, MESH_PASSWORD, MESH_PORT, WIFI_AP_STA);
- mesh.onReceive(&receivedCallback);
- // Terhubung ke jaringan Wi-Fi yang ditentukan
- mesh.stationManual(STATION_SSID, STATION_PASSWORD);
- mesh.setHostname(HOSTNAME);
- mesh.setRoot(true); // Tetapkan node ini sebagai root
- mesh.setContainsRoot(true);
- mqttClient.setServer(mqttBroker, mqttPort);
- mqttClient.setCallback(mqttCallback);
- }
- void loop() {
- mesh.update();
- mqttClient.loop();
- // Periksa dan perbarui alamat IP
- if (myIP != getLocalIP()) {
- myIP = getLocalIP();
- Serial.println("IP saya adalah " + myIP.toString());
- if (mqttClient.connect("painlessMeshClient", mqttUser, mqttPassword)) {
- Serial.println("Terhubung ke broker MQTT");
- mqttClient.publish("painlessMesh/from/gateway", "Ready!");
- mqttClient.subscribe("painlessMesh/to/#");
- } else {
- Serial.println("Gagal terhubung ke broker MQTT");
- }
- }
- }
- // Callback untuk pesan yang diterima dari jaringan mesh
- void receivedCallback(const uint32_t &from, const String &msg) {
- Serial.printf("bridge: Diterima dari %u msg=%s\n", from, msg.c_str());
- String topic = "all_node3";
- bool result = mqttClient.publish(topic.c_str(), msg.c_str());
- if(result) {
- Serial.println("Pesan berhasil dikirim ke MQTT");
- } else {
- Serial.println("Gagal mengirim pesan ke MQTT");
- }
- }
- // Callback untuk pesan yang diterima dari broker MQTT
- void mqttCallback(char* topic, uint8_t* payload, unsigned int length) {
- Serial.print("Pesan diterima dari MQTT, topik: ");
- Serial.println(topic);
- char* cleanPayload = (char*)malloc(length + 1);
- memcpy(cleanPayload, payload, length);
- cleanPayload[length] = '\0';
- String msg = String(cleanPayload);
- free(cleanPayload);
- Serial.print("Payload: ");
- Serial.println(msg);
- String targetStr = String(topic).substring(16); // Ekstrak ID target dari topik
- if (targetStr == "gateway") {
- if (msg == "getNodes") {
- auto nodes = mesh.getNodeList(true);
- String str;
- for (auto &&id : nodes)
- str += String(id) + String(" ");
- mqttClient.publish("painlessMesh/from/gateway", str.c_str());
- }
- } else if (targetStr == "broadcast") {
- mesh.sendBroadcast(msg);
- } else {
- uint32_t target = strtoul(targetStr.c_str(), NULL, 10);
- if (mesh.isConnected(target)) {
- mesh.sendSingle(target, msg);
- } else {
- mqttClient.publish("painlessMesh/from/gateway", "Client not connected!");
- }
- }
- }
- // Fungsi untuk mendapatkan alamat IP lokal node
- IPAddress getLocalIP() {
- return IPAddress(mesh.getStationIP());
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement