Advertisement
mudhita_triari

Tugas Akhir - ESP32 Gateway

May 23rd, 2024
626
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.74 KB | Source Code | 0 0
  1. #include <WiFi.h>
  2. #include <painlessMesh.h>
  3.  
  4. const char* ssid =      "vivo V29";
  5. const char* password =  "Nabila041185";
  6.  
  7. #define MESH_PREFIX     "yourMeshNetwork"
  8. #define MESH_PASSWORD   "yourMeshPassword"
  9. #define MESH_PORT       5555
  10.  
  11. #define LED_PIN 2
  12.  
  13. Scheduler userScheduler;
  14. painlessMesh mesh;
  15.  
  16. void receivedCallback(uint32_t from, String &msg) {
  17.   Serial.printf("Received from %u msg=%s\n", from, msg.c_str());
  18.   Serial.println("Data diterima dari Node Mesh: " + msg);                 // Tambahkan baris ini untuk mencetak data ke serial monitor
  19. }
  20.  
  21. void newConnectionCallback(uint32_t nodeId) {
  22.   Serial.printf("--> New Connection, nodeId = %u\n", nodeId);
  23. }
  24.  
  25. void changedConnectionCallback() {
  26.   Serial.printf("Changed connections\n");
  27.   if (mesh.getNodeList().size() == 0) {
  28.     digitalWrite(LED_PIN, LOW); // Matikan LED jika tidak ada koneksi
  29.   } else {
  30.     digitalWrite(LED_PIN, HIGH); // Nyalakan LED jika ada koneksi
  31.   }
  32. }
  33.  
  34. void nodeTimeAdjustedCallback(int32_t offset) {
  35.   Serial.printf("Adjusted time %u. Offset = %d\n", mesh.getNodeTime(), offset);
  36. }
  37.  
  38. void setup() {
  39.   Serial.begin(115200);
  40.  
  41.   pinMode(LED_PIN, OUTPUT);
  42.   digitalWrite(LED_PIN, LOW); // Matikan LED pada awalnya
  43.  
  44.   WiFi.begin(ssid, password);
  45.   while (WiFi.status() != WL_CONNECTED) {
  46.     delay(1000);
  47.     Serial.println("Connecting to WiFi...");
  48.   }
  49.   Serial.println("Connected to WiFi");
  50.  
  51.   mesh.setDebugMsgTypes(ERROR | STARTUP);
  52.   mesh.init(MESH_PREFIX, MESH_PASSWORD, &userScheduler, MESH_PORT);
  53.   mesh.onReceive(&receivedCallback);
  54.   mesh.onNewConnection(&newConnectionCallback);
  55.   mesh.onChangedConnections(&changedConnectionCallback);
  56.   mesh.onNodeTimeAdjusted(&nodeTimeAdjustedCallback);
  57. }
  58.  
  59. void loop() {
  60.   mesh.update();
  61. }
  62.  
Tags: #esp32
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement