Created
February 2, 2024 22:08
-
-
Save pr8x/ae0e3a193958e8da23074ed79ec269c7 to your computer and use it in GitHub Desktop.
espnow nowis
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <WiFi.h> | |
#include <esp_now.h> | |
#include <ezButton.h> | |
#include <esp_wifi.h> | |
uint8_t globalBroadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; | |
// This is the same message structure as WizMote senders. It's reused here to | |
// ensure a common parsable payload, but everything other than seq and button | |
// aren't used by WLED. The ESP-NOW packet has 512 bytes reserved for the message | |
// anyway, though, so the cruft shouldn't affect anything | |
typedef struct remote_message_struct { | |
uint8_t program; // 0x91 for ON button, 0x81 for all others | |
uint8_t seq[4]; // Incremetal sequence number 32 bit unsigned integer LSB first | |
uint8_t byte5 = 32; // Unknown | |
uint8_t button; // Identifies which button is being pressed | |
uint8_t byte8 = 1; // Unknown, but always 0x01 | |
uint8_t byte9 = 100; // Unnkown, but always 0x64 | |
uint8_t byte10; // Unknown, maybe checksum | |
uint8_t byte11; // Unknown, maybe checksum | |
uint8_t byte12; // Unknown, maybe checksum | |
uint8_t byte13; // Unknown, maybe checksum | |
} remote_message_struct; | |
#define WIZMOTE_BUTTON_ON 1 | |
#define WIZMOTE_BUTTON_OFF 2 | |
#define WIZMOTE_BUTTON_NIGHT 3 | |
#define WIZMOTE_BUTTON_ONE 16 | |
#define WIZMOTE_BUTTON_TWO 17 | |
#define WIZMOTE_BUTTON_THREE 18 | |
#define WIZMOTE_BUTTON_FOUR 19 | |
#define WIZMOTE_BUTTON_BRIGHT_UP 9 | |
#define WIZMOTE_BUTTON_BRIGHT_DOWN 8 | |
remote_message_struct outgoing; | |
uint32_t seq = 1; | |
int mode = 0; | |
ezButton onOffButton(16); | |
ezButton modeButton(17); | |
bool oneDown = false; | |
bool twoDown = false; | |
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) { | |
Serial.print("\r\nLast Packet Send Status:\t"); | |
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail"); | |
} | |
void sendPress(int buttonCode) { | |
Serial.printf("Sending button code: %d\n", buttonCode); | |
// increment message sequence counter | |
seq = seq + 1; | |
// format sequence counter long data in the expected format | |
outgoing.seq[0] = seq; | |
outgoing.seq[1] = seq >> 8; | |
outgoing.seq[2] = seq >> 16; | |
outgoing.seq[3] = seq >> 24; | |
outgoing.button = buttonCode; | |
broadcastAcrossChannels(); | |
} | |
void broadcastAcrossChannels() { | |
for (int i = 1 ; i < 14 ; ++i){ | |
esp_wifi_set_channel(i, WIFI_SECOND_CHAN_NONE); | |
esp_now_send(globalBroadcastAddress, (uint8_t *) &outgoing, sizeof(outgoing)); | |
} | |
} | |
void setup() { | |
// Init Serial Monitor | |
Serial.begin(115200); | |
// Set station mode, though we won't be connecting to another AP | |
WiFi.mode(WIFI_STA); | |
// Init ESP-NOW | |
if (esp_now_init() != 0) { | |
Serial.println("Error initializing ESP-NOW"); | |
return; | |
} | |
// Set ESP NOW to controller (sender) mode | |
//esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER); | |
// Once ESPNow is successfully Init, we will register for Send CB to | |
// get the status of Trasnmitted packet | |
esp_now_register_send_cb(OnDataSent); | |
esp_now_peer_info_t peerInfo; | |
memcpy(peerInfo.peer_addr, globalBroadcastAddress, 6); | |
peerInfo.channel = 0; | |
peerInfo.encrypt = false; | |
// Add peer | |
if (esp_now_add_peer(&peerInfo) != ESP_OK){ | |
Serial.println("Failed to add peer"); | |
return; | |
} | |
} | |
void loop() { | |
onOffButton.loop(); | |
modeButton.loop(); | |
if (onOffButton.isPressed()) { | |
sendPress(WIZMOTE_BUTTON_OFF); | |
} | |
if (modeButton.isPressed()) { | |
sendPress(WIZMOTE_BUTTON_ON); | |
sendPress(WIZMOTE_BUTTON_ONE + mode++); | |
mode %= 4; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment