Advertisement
pleasedontcode

"ESP32 Mesh" rev_01

Jun 6th, 2024
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: "ESP32 Mesh"
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-06-06 09:36:12
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Use Arduino ID, mirroring of ten digital pins */
  21.     /* between two ESP32 devices using BLE Mesh without */
  22.     /* using any delay() commands and only transmit data */
  23.     /* to receiver when change the pin state, you'll need */
  24.     /* to set up one ESP32 as the sender and the other as */
  25.     /* the */
  26. /****** END SYSTEM REQUIREMENTS *****/
  27.  
  28. /****** DEFINITION OF LIBRARIES *****/
  29. #include <BLEDevice.h>
  30. #include <BLEUtils.h>
  31. #include <BLEServer.h>
  32. #include <BLE2902.h>
  33. #include <BLEMesh.h>
  34.  
  35. /****** FUNCTION PROTOTYPES *****/
  36. void setup(void);
  37. void loop(void);
  38. void updateOutputs();
  39. void pinStateChanged(void * parameter);
  40.  
  41. /***** DEFINITION OF DIGITAL PINS *****/
  42. const uint8_t PINS[10] = {2, 4, 5, 12, 13, 14, 15, 16, 17, 18};
  43.  
  44. /***** DEFINITION OF OUTPUT RAW VARIABLES *****/
  45. /***** used to store raw data *****/
  46. bool pinStates[10] = {0};
  47.  
  48. /***** BLE Mesh Variables *****/
  49. BLEMesh mesh;
  50. bool isSender = true; // Set this to false for the receiver
  51.  
  52. void setup(void)
  53. {
  54.     // Initialize serial communication
  55.     Serial.begin(115200);
  56.  
  57.     // Initialize pin modes
  58.     for (int i = 0; i < 10; i++) {
  59.         pinMode(PINS[i], isSender ? INPUT_PULLUP : OUTPUT);
  60.     }
  61.  
  62.     // Initialize BLE Mesh
  63.     BLEDevice::init("ESP32_Mesh");
  64.     mesh.begin("ESP32_Mesh_Network");
  65.  
  66.     if (isSender) {
  67.         // Set up a task to monitor pin state changes
  68.         xTaskCreate(
  69.             pinStateChanged,   // Function to implement the task
  70.             "pinStateChanged", // Name of the task
  71.             10000,             // Stack size in words
  72.             NULL,              // Task input parameter
  73.             1,                 // Priority of the task
  74.             NULL               // Task handle
  75.         );
  76.     }
  77. }
  78.  
  79. void loop(void)
  80. {
  81.     // put your main code here, to run repeatedly:
  82.     if (!isSender) {
  83.         // Receiver logic to update outputs
  84.         updateOutputs();
  85.     }
  86. }
  87.  
  88. void updateOutputs()
  89. {
  90.     for (int i = 0; i < 10; i++) {
  91.         digitalWrite(PINS[i], pinStates[i]);
  92.     }
  93. }
  94.  
  95. void pinStateChanged(void * parameter)
  96. {
  97.     for (;;) {
  98.         for (int i = 0; i < 10; i++) {
  99.             bool currentState = digitalRead(PINS[i]);
  100.             if (currentState != pinStates[i]) {
  101.                 pinStates[i] = currentState;
  102.                 // Transmit the state change via BLE Mesh
  103.                 mesh.send("ESP32_Mesh_Network", (uint8_t*)&pinStates, sizeof(pinStates));
  104.             }
  105.         }
  106.         vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay to prevent task hogging
  107.     }
  108. }
  109.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement