When the ESP32C3 broadcasts a message, the attached LED on pin 5 turns on for 1 second, then it turns off for 4 seconds, until the ESP32C3 broadcasts again.
When the board is plugged in to my computer, the LED lights up as it's supposed to. However, when I plug the board into a wall charger the LED does not come on. I know the board is getting power because the tiny red Power LED on the board comes on. I've tried several different cables, and several different wall chargers. It always works fine plugged in to my computer, but doesn't work when plugged in to an outlet. What's going on with this? Why won't the LED on pin 5 do what it's supposed to when plugged in to the wall?
Here is my code:
Code: Select all
/*
|| Attempting to write my own ESP_NOW sketch using a hybrid of the 3 examples I have.
||
*/
#include "ESP32_NOW.h"
#include "WiFi.h"
#include <esp_mac.h> // For the MAC2STR and MACSTR macros
/* Definitions */
#define ESPNOW_WIFI_CHANNEL 6
/* Classes */
// Creating a new class that inherits from the ESP_NOW_Peer class is required.
// BROADCAST_PEER CREATION (from Master)
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;
}
};
// PEER CREATION (from Slave)
class ESP_NOW_Peer_Class : public ESP_NOW_Peer {
public:
// Constructor of the class
ESP_NOW_Peer_Class(const uint8_t *mac_addr, uint8_t channel, wifi_interface_t iface, const uint8_t *lmk) : ESP_NOW_Peer(mac_addr, channel, iface, lmk) {}
// Destructor of the class
~ESP_NOW_Peer_Class() {}
// Function to register the master peer
bool add_peer() {
if (!add()) {
log_e("Failed to register the broadcast peer");
return false;
}
return true;
}
// Function to print the received messages from the master
void onReceive(const uint8_t *data, size_t len, bool broadcast) {
Serial.printf("Received a message from master " MACSTR " (%s)\n", MAC2STR(addr()), broadcast ? "broadcast" : "unicast");
Serial.printf(" Message: %s\n", (char *)data);
}
};
/* GLOBAL VARIABLES */
uint32_t msg_count = 0;
// Create a broadcast peer object (from Master)
ESP_NOW_Broadcast_Peer broadcast_peer(ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
// List of all the masters. It will be populated when a new master is registered
std::vector<ESP_NOW_Peer_Class> masters;
uint8_t greenLED = 2;
uint8_t redLED = 5;
/* CALLBACKS */
// Callback called when an unknown peer sends a message (from Slave)
void register_new_master(const esp_now_recv_info_t *info, const uint8_t *data, int len, void *arg) {
if (memcmp(info->des_addr, ESP_NOW.BROADCAST_ADDR, 6) == 0) {
Serial.printf("Unknown peer " MACSTR " sent a broadcast message\n", MAC2STR(info->src_addr));
Serial.println("Registering the peer as a master");
ESP_NOW_Peer_Class new_master(info->src_addr, ESPNOW_WIFI_CHANNEL, WIFI_IF_STA, NULL);
masters.push_back(new_master);
if (!masters.back().add_peer()) {
Serial.println("Failed to register the new master");
return;
}
} else {
// The slave will only receive broadcast messages
log_v("Received a unicast message from " MACSTR, MAC2STR(info->src_addr));
log_v("Igorning the message");
}
}
/* MAIN */
void setup() {
pinMode(greenLED, OUTPUT);
pinMode(redLED, OUTPUT);
//(from Both)
Serial.begin(115200);
while (!Serial) {
delay(10);
}
// Initialize the Wi-Fi module (from Both)
WiFi.mode(WIFI_STA);
WiFi.setChannel(ESPNOW_WIFI_CHANNEL);
while (!WiFi.STA.started()) {
delay(100);
}
// Send a message to Serial Monitor about where program is in execution
Serial.println("ESP-NOW Attempt cobbled together by Brad Freese");
// Initialize the ESP-NOW protocol (from Slave)
if (!ESP_NOW.begin()) {
Serial.println("Failed to initialize ESP-NOW");
Serial.println("Reeboting in 5 seconds...");
delay(5000);
ESP.restart();
}
Serial.println("ESP-NOW Protocol Initalized");
// Register the broadcast peer (from Master)
if (!broadcast_peer.begin()) {
Serial.println("Failed to initialize broadcast peer");
Serial.println("Reebooting in 5 seconds...");
delay(5000);
ESP.restart();
}
Serial.println("broadcast_peer.begin() executed");
Serial.println("Setup complete.");
}
void loop() {
digitalWrite(redLED, HIGH);
// Broadcast a message to all devices within the network
char data[32];
snprintf(data, sizeof(data), "Hello, World! #%lu", msg_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(1000);
digitalWrite(redLED, LOW);
delay(4000);
}