Skip to content

Instantly share code, notes, and snippets.

@protongt
Created November 29, 2020 08:25
Show Gist options
  • Save protongt/61970b1513644237504d58910c8e141c to your computer and use it in GitHub Desktop.
Save protongt/61970b1513644237504d58910c8e141c to your computer and use it in GitHub Desktop.
ESP-NOW using WEMOS D1 mini Pro
//=================================================================================================//
// MASTER //
// Board : LOLIN (WEMOS) D1 MINI PRO //
// Upload speed : 92160 //
// CPU frequency : 80 Mhz //
// Flash size : 16MB (FS:14MB OTA:~1019KB //
// Debug port : Disabled //
// Debgu level : None //
// IwIP variant : v2 lower memory //
// VTables : Flash //
// Exception : Legacy //
// Erase flash : Only sketch //
// SSL support : All SSL ciphers //
// visit http://9w2nfe.blogspot.com/ for more projects //
//-------------------------------------------------------------------------------------------------//
#include <ESP8266WiFi.h>
extern "C" {
#include <espnow.h>
}
uint8_t mac[] = {0xF4, 0xCF, 0xA2, 0x6D, 0x20, 0x2C}; //AP MAC MASTER'S ADDRESS
#define WIFI_CHANNEL 4
int prevstate_1 = LOW;
int prevstate_2 = LOW;
// Data structure, must be the same for the slave
struct __attribute__((packed))DataStruct {
char text[32];
};
DataStruct button_1;
DataStruct button_2;
void setup() {
pinMode(D1, INPUT);
pinMode(D2, INPUT);
pinMode(D0, OUTPUT);
pinMode(D5, OUTPUT);
Serial.begin(115200); Serial.println();
Serial.println("Starting ESP-now Master");
WiFi.mode(WIFI_STA); // Station mode for esp-now controller
WiFi.disconnect();
Serial.printf("This mac: %s, ", WiFi.macAddress().c_str());
Serial.printf("slave mac: %02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.printf(", channel: %i\n", WIFI_CHANNEL);
if (esp_now_init() != 0)
{
Serial.println("*** ESP_Now initialization failed");
}
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_add_peer(mac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0);
strcpy(button_1.text, "Button 01 pressed");
strcpy(button_2.text, "Button 02 pressed");
Serial.println("Setup finished");
}
void loop() {
sendData();
}
void sendData() {
int currentstate_1 = digitalRead(D1);
if (prevstate_1 != currentstate_1) {
if (currentstate_1 == LOW) {
uint8_t bs[sizeof(button_1)];
memcpy(bs, &button_1, sizeof(button_1));
esp_now_send(mac, bs, sizeof(button_1));
Serial.println(button_1.text);
digitalWrite(D0, !digitalRead(D0));
}
} prevstate_1 = currentstate_1;
int currentstate_2 = digitalRead(D2);
if (prevstate_2 != currentstate_2) {
if (currentstate_2 == LOW) {
uint8_t bs[sizeof(button_2)];
memcpy(bs, &button_2, sizeof(button_2));
esp_now_send(mac, bs, sizeof(button_2));
Serial.println(button_2.text);
digitalWrite(D5, !digitalRead(D5));
}
} prevstate_2 = currentstate_2;
}
//=================================================================================================//
// SLAVE //
// Board : LOLIN (WEMOS) D1 MINI PRO //
// Upload speed : 92160 //
// CPU frequency : 80 Mhz //
// Flash size : 16MB (FS:14MB OTA:~1019KB //
// Debug port : Disabled //
// Debgu level : None //
// IwIP variant : v2 lower memory //
// VTables : Flash //
// Exception : Legacy //
// Erase flash : Only sketch //
// SSL support : All SSL ciphers //
// visit http://9w2nfe.blogspot.com/ for more projects //
//-------------------------------------------------------------------------------------------------//
#include <ESP8266WiFi.h>
extern "C" {
#include <espnow.h>
#include <user_interface.h>
}
uint8_t mac[] = {0xF4, 0xCF, 0xA2, 0x6D, 0x20, 0x2C}; //AP MAC MASTER'S ADDRESS
int Led1 = D1;
int Led2 = D2;
void initVariant() {
WiFi.mode(WIFI_AP);
wifi_set_macaddr(SOFTAP_IF, &mac[0]);
}
#define WIFI_CHANNEL 4
// Must match the controller struct
struct __attribute__((packed))DataStruct {
char text[32];
unsigned int time;};
DataStruct receivedData;
void setup() {
Serial.begin(115200); Serial.println();
Serial.println("Starting Esp-now Slave");
Serial.print("This node AP mac: "); Serial.println(WiFi.softAPmacAddress());
Serial.print("This node STA mac: "); Serial.println(WiFi.macAddress());
pinMode(Led1, OUTPUT);
pinMode(Led2, OUTPUT);
if (esp_now_init() != 0){
Serial.println("*** ESP_Now initialization failed");
while (true) {};
}
esp_now_set_self_role(ESP_NOW_ROLE_SLAVE);
esp_now_register_recv_cb(receiveCallBackFunction);
Serial.println("Setup finished - waiting for messages");
}
void loop() {
}
void receiveCallBackFunction(uint8_t *senderMac, uint8_t *incomingData, uint8_t len) {
memcpy(&receivedData, incomingData, sizeof(receivedData));
String DataCompare = String(receivedData.text);
if(DataCompare == "Button 01 pressed"){
digitalWrite(Led1, !digitalRead(Led1));
Serial.println(" Message = " + DataCompare);
}
if(DataCompare == "Button 02 pressed"){
digitalWrite(Led2, !digitalRead(Led2));
Serial.println(" Message = " + DataCompare);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment