Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <WiFi.h>
- #include <painlessMesh.h>
- const char* ssid = "vivo V29";
- const char* password = "Nabila041185";
- #define MESH_PREFIX "yourMeshNetwork"
- #define MESH_PASSWORD "yourMeshPassword"
- #define MESH_PORT 5555
- #define LED_PIN 2
- Scheduler userScheduler;
- painlessMesh mesh;
- void receivedCallback(uint32_t from, String &msg) {
- Serial.printf("Received from %u msg=%s\n", from, msg.c_str());
- Serial.println("Data diterima dari Node Mesh: " + msg); // Tambahkan baris ini untuk mencetak data ke serial monitor
- }
- void newConnectionCallback(uint32_t nodeId) {
- Serial.printf("--> New Connection, nodeId = %u\n", nodeId);
- }
- void changedConnectionCallback() {
- Serial.printf("Changed connections\n");
- if (mesh.getNodeList().size() == 0) {
- digitalWrite(LED_PIN, LOW); // Matikan LED jika tidak ada koneksi
- } else {
- digitalWrite(LED_PIN, HIGH); // Nyalakan LED jika ada koneksi
- }
- }
- void nodeTimeAdjustedCallback(int32_t offset) {
- Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
- }
- void setup() {
- Serial.begin(115200);
- pinMode(LED_PIN, OUTPUT);
- digitalWrite(LED_PIN, LOW); // Matikan LED pada awalnya
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- mesh.setDebugMsgTypes(ERROR | STARTUP);
- mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
- mesh.onReceive(&receivedCallback);
- mesh.onNewConnection(&newConnectionCallback);
- mesh.onChangedConnections(&changedConnectionCallback);
- mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
- }
- void loop() {
- mesh.update();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement