#define pinCLK 2 // CLK pin connected to GPIO 1 #define pinDT 3 // DT pin connected to GPIO 2 #define pinSW 4 // SW pin connected to GPIO 3 #include #include "ESP32_NOW.h" #include "WiFi.h" #include // For the MAC2STR and MACSTR macros /* Definitions */ #define ESPNOW_WIFI_CHANNEL 6 int count = 0; // Var to know how many clicks occured (increment clockwise and decrement counter_clockwise) int previousStateSW; // Var to memorize previous SW state to compare it with actual state int previousStateCLK; // Var to memorize previous CL state to compare it with actual state /* Classes */ // Creating a new class that inherits from the ESP_NOW_Peer class is required. class ESP_NOW_Broadcast_Peer : public ESP_NOW_Peer { public: // Constructor of the class using the broadcast address ESP_NOW_Broadcast_Peer(uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) : ESP_NOW_Peer(ESP_NOW.BROADCAST_ADDR, channel, iface, lmk) {} // Destructor of the class ~ESP_NOW_Broadcast_Peer() { remove(); } // Function to properly initialize the ESP-NOW and register the broadcast peer bool begin() { if (!ESP_NOW.begin() || !add()) { log_e("Failed to initialize ESP-NOW or register the broadcast peer"); return false; } return true; } // Function to send a message to all devices within the network bool send_message(const uint8_t *data, size_t len) { if (!send(data, len)) { log_e("Failed to broadcast message"); return false; } return true; } }; // Create a boradcast peer object ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL); void setup() { Serial.begin(115200); // Initialize the Wi-Fi module WiFi.mode(WIFI_STA); WiFi.setChannel(ESPNOW_WIFI_CHANNEL); while(!WiFi.STA.started()) delay(100); Serial.println("ESP-NOW Example - Broadcast Master"); Serial.println("Wi-Fi parameters:"); Serial.println(" Mode: STA"); Serial.println(" MAC Address: " + WiFi.macAddress()); Serial.printf(" Channel: %d\n", ESPNOW_WIFI_CHANNEL); // Register the broadcast peer if (!broadcast_peer.begin()) { Serial.println("Failed to initialize broadcast peer"); Serial.println("Reebooting in 5 seconds..."); delay(5000); ESP.restart(); } Serial.println("Setup complete. Broadcasting messages every 5 seconds."); pinMode(pinSW, INPUT_PULLUP); // INPUT_PULLUP to avoid 'floating' pinMode(pinDT, INPUT); pinMode(pinCLK, INPUT); previousStateSW = digitalRead(pinSW); // initial value for SW previousStateCLK = digitalRead(pinCLK); // initial value for CLK delay(200); // delay to stabilize signal before the loop } void loop() { int actualStateCLK = digitalRead(pinCLK); // reading CLK value int actualStateDT = digitalRead(pinDT); // reading DT value int actualStateSW = digitalRead(pinSW); // reading SW value // ************************* // Push Button verification // ************************* if (actualStateSW != previousStateSW) { // Checking if SW state has changed previousStateSW = actualStateSW; // Save new state if (actualStateSW == LOW) { Serial.println(F("SW Button pushed")); // Display State on serial monitor char data[32]; snprintf(data, sizeof(data), "SW Button pushed"); Serial.printf("Broadcasting message: %s\n", data); if (!broadcast_peer.send_message((uint8_t *)data, sizeof(data))) { Serial.println("Failed to broadcast message"); } } else { Serial.println(F("SW Button released")); char data[32]; snprintf(data, sizeof(data), "SW Button released"); Serial.printf("Broadcasting message: %s\n", data); if (!broadcast_peer.send_message((uint8_t *)data, sizeof(data))) { Serial.println("Failed to broadcast message"); } delay(10); // Delay to avoid rebounce } } // *************************** // Rotary encoder verification // *************************** if (actualStateCLK != previousStateCLK) { // Checking if SW state has changed previousStateCLK = actualStateCLK; // Save new state if (actualStateCLK == LOW) { if (actualStateCLK != actualStateDT) { // comparing CLK and DT, if they're different, direction is counter-clockwise count--; // decrement counter Serial.print(F("Direction = counter-clockwise | Counter value = ")); // Display value on Serial Monitor Serial.println(count); char data[32]; snprintf(data, sizeof(data), "counterclockwise | counter : %s\n", count); snprintf(data, sizeof(data), "Counter value", count); Serial.printf("Broadcasting message: %s\n", data); if (!broadcast_peer.send_message((uint8_t *)data, sizeof(data))) { Serial.println("Failed to broadcast message"); } } } else { // when CLK and DT are similar, direction is clockwise count++; // increment counter Serial.print(F("Direction = clockwise | Counter value = ")); // Display value on Serial Monitor Serial.println(count); char data[32]; snprintf(data, sizeof(data), "clockwise | counter : %s\n", count); Serial.printf("Broadcasting message: %s\n", data); if (!broadcast_peer.send_message((uint8_t *)data, sizeof(data))) { Serial.println("Failed to broadcast message"); } } delay(10); // delay to avoid CLK rebounce } }