CCCPaste

"ESP32 Mesh" rev_01

/********* Pleasedontcode.com **********

	Pleasedontcode thanks you for automatic code generation! Enjoy your code!

	- Terms and Conditions:
	You have a non-exclusive, revocable, worldwide, royalty-free license
	for personal and commercial use. Attribution is optional; modifications
	are allowed, but you're responsible for code maintenance. We're not
	liable for any loss or damage. For full terms,
	please visit pleasedontcode.com/termsandconditions.

	- Project: "ESP32 Mesh"
	- Source Code NOT compiled for: ESP32 DevKit V1
	- Source Code created on: 2024-06-06 09:36:12

********* Pleasedontcode.com **********/

/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
	/* Use Arduino ID, mirroring of ten digital pins */
	/* between two ESP32 devices using BLE Mesh without */
	/* using any delay() commands and only transmit data */
	/* to receiver when change the pin state, you'll need */
	/* to set up one ESP32 as the sender and the other as */
	/* the */
/****** END SYSTEM REQUIREMENTS *****/

/****** DEFINITION OF LIBRARIES *****/
#include 
#include 
#include 
#include 
#include 

/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void updateOutputs();
void pinStateChanged(void * parameter);

/***** DEFINITION OF DIGITAL PINS *****/
const uint8_t PINS[10] = {2, 4, 5, 12, 13, 14, 15, 16, 17, 18};

/***** DEFINITION OF OUTPUT RAW VARIABLES *****/
/***** used to store raw data *****/
bool pinStates[10] = {0};

/***** BLE Mesh Variables *****/
BLEMesh mesh;
bool isSender = true; // Set this to false for the receiver

void setup(void)
{
    // Initialize serial communication
    Serial.begin(115200);

    // Initialize pin modes
    for (int i = 0; i < 10; i++) {
        pinMode(PINS[i], isSender ? INPUT_PULLUP : OUTPUT);
    }

    // Initialize BLE Mesh
    BLEDevice::init("ESP32_Mesh");
    mesh.begin("ESP32_Mesh_Network");

    if (isSender) {
        // Set up a task to monitor pin state changes
        xTaskCreate(
            pinStateChanged,   // Function to implement the task
            "pinStateChanged", // Name of the task
            10000,             // Stack size in words
            NULL,              // Task input parameter
            1,                 // Priority of the task
            NULL               // Task handle
        );
    }
}

void loop(void)
{
    // put your main code here, to run repeatedly:
    if (!isSender) {
        // Receiver logic to update outputs
        updateOutputs();
    }
}

void updateOutputs()
{
    for (int i = 0; i < 10; i++) {
        digitalWrite(PINS[i], pinStates[i]);
    }
}

void pinStateChanged(void * parameter)
{
    for (;;) {
        for (int i = 0; i < 10; i++) {
            bool currentState = digitalRead(PINS[i]);
            if (currentState != pinStates[i]) {
                pinStates[i] = currentState;
                // Transmit the state change via BLE Mesh
                mesh.send("ESP32_Mesh_Network", (uint8_t*)&pinStates, sizeof(pinStates));
            }
        }
        vTaskDelay(10 / portTICK_PERIOD_MS); // Small delay to prevent task hogging
    }
}