Hello. I use wifi ESP-NOW library to communicate between remote ESP32 devices.
I dont quite understand how exactly it works.
For example, this is my slave esp32 code:
It is able to receive/reply to a paired devices. Pairing is being done using a MAC address.
What I am struglling to understand is what is the point of using wifi mode: soft access point as the example suggests? Since I am not really using this access point to connect ?
I dont quite understand how exactly it works.
For example, this is my slave esp32 code:
Code:
#include <WifiEspNow.h>
#if defined(ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ESP32)
#include <WiFi.h>
#endif
#define ONBOARD_LED 2
#define BUTTON1 36 //pin D5
int buttonState = 0; // current state of the button
int lastButtonState = 1; // previous state of the button
//MAC address of this node is 10:52:1C:52:0A:C5
//30:AE:A4:97:B4:CD
//static uint8_t PEER[] {0x30, 0xAE, 0xA4, 0x97, 0xDB, 0x05};
//char string_var[10];
static uint8_t PEER[] {0x10, 0x52, 0x1C, 0x52, 0x0A, 0xC5};
void printReceivedMessage(const uint8_t mac[6], const uint8_t* buf, size_t count, void* cbarg) {
char string_var[100];
Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
for (int i = 0; i < count; ++i) {
Serial.println(static_cast<char>(buf[i]));
}
size_t bufflen = sizeof(buf)+1;
memcpy( string_var, buf, bufflen );
string_var[bufflen] = '\0'; // 'str' is now a string
Serial.print("string_var=");
Serial.println(string_var);
strcpy(
//strcat(string_var, (char*)buf);
//Serial.print("string_var=");
//Serial.println(string_var);
if (strncmp(string_var, "ok",2) == 0){
digitalWrite(ONBOARD_LED,HIGH);
}
if (strncmp(string_var, "reset",5) == 0){
digitalWrite(ONBOARD_LED,LOW);
}
Serial.println();
}
void setup() {
pinMode(ONBOARD_LED,OUTPUT);
pinMode(BUTTON1,INPUT);
Serial.begin(115200);
Serial.println();
WiFi.persistent(false);
WiFi.mode(WIFI_AP);
WiFi.softAP("ESPNOW", nullptr, 3);
WiFi.softAPdisconnect(false);
Serial.print("MAC address of this node is ");
Serial.println(WiFi.softAPmacAddress());
bool ok = WifiEspNow.begin();
if (!ok) {
Serial.println("WifiEspNow.begin() failed");
ESP.restart();
}
WifiEspNow.onReceive(printReceivedMessage, nullptr);
ok = WifiEspNow.addPeer(PEER);
if (!ok) {
Serial.println("WifiEspNow.addPeer() failed");
ESP.restart();
}
}
void loop() {
check_button_state(BUTTON1);
//char msg[60];
//int len = snprintf(msg, sizeof(msg), "hello ESP-NOW from %s at %lu", WiFi.softAPmacAddress().c_str(), millis());
//WifiEspNow.send(PEER, reinterpret_cast<const uint8_t*>(msg), len);
delay(100);
}
void check_button_state(int pin){
buttonState = digitalRead(pin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH){
char msg[60];
int len = snprintf(msg, sizeof(msg), "2");
WifiEspNow.send(PEER, reinterpret_cast<const uint8_t*>(msg),len);
// if the current state is HIGH then the button went from off to on:
Serial.println("on");
}
else {
// if the current state is LOW then the button went from on to off:
Serial.println("off");
}
}
lastButtonState = buttonState;
}
What I am struglling to understand is what is the point of using wifi mode: soft access point as the example suggests? Since I am not really using this access point to connect ?