初始化提交

This commit is contained in:
王立帮
2024-07-20 22:09:06 +08:00
commit c247dd07a6
6876 changed files with 2743096 additions and 0 deletions

View File

@@ -0,0 +1,113 @@
/**
* @file
*
* EspNowBroadcast.ino demonstrates how to perform ESP-NOW pseudo broadcast with @c WifiEspNowBroadcast .
* You need two or more ESP8266 or ESP32 devices to run this example.
*
* All devices should run the same program.
* You may need to modify the PIN numbers so that you can observe the effect.
*
* With the program running on several devices:
* @li Press the button to transmit a message.
* @li When a device receives a message, it will toggle its LED between "on" and "off" states.
*/
#include <WifiEspNowBroadcast.h>
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#endif
/**
* @brief PIN number of a button.
*
* The default `0` is the "flash" button on NodeMCU, Witty Cloud, Heltec WiFi_Kit_32, etc.
*/
static const int BUTTON_PIN = 0;
/**
* @brief PIN number of an LED.
*
* The default `2` is the blue LED on ESP-12F.
*/
static const int LED_PIN = 2;
int ledState = HIGH;
void
processRx(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count, void* arg)
{
Serial.printf("Message from %02X:%02X:%02X:%02X:%02X:%02X\n", mac[0], mac[1], mac[2], mac[3],
mac[4], mac[5]);
for (size_t i = 0; i < count; ++i) {
Serial.print(static_cast<char>(buf[i]));
}
Serial.println();
digitalWrite(LED_PIN, ledState);
ledState = 1 - ledState;
}
void
setup()
{
Serial.begin(115200);
Serial.println();
WiFi.persistent(false);
bool ok = WifiEspNowBroadcast.begin("ESPNOW", 3);
if (!ok) {
Serial.println("WifiEspNowBroadcast.begin() failed");
ESP.restart();
}
// WifiEspNowBroadcast.begin() function sets WiFi to AP+STA mode.
// The AP interface is also controlled by WifiEspNowBroadcast.
// You may use the STA interface after calling WifiEspNowBroadcast.begin().
// For best results, ensure all devices are using the same WiFi channel.
WifiEspNowBroadcast.onReceive(processRx, nullptr);
pinMode(BUTTON_PIN, INPUT_PULLUP);
pinMode(LED_PIN, OUTPUT);
digitalWrite(LED_PIN, ledState);
Serial.print("MAC address of this node is ");
Serial.println(WiFi.softAPmacAddress());
Serial.println("Press the button to send a message");
}
void
sendMessage()
{
char msg[60];
int len = snprintf(msg, sizeof(msg), "hello ESP-NOW from %s at %lu",
WiFi.softAPmacAddress().c_str(), millis());
WifiEspNowBroadcast.send(reinterpret_cast<const uint8_t*>(msg), len);
Serial.println("Sending message");
Serial.println(msg);
Serial.print("Recipients:");
const int MAX_PEERS = 20;
WifiEspNowPeerInfo peers[MAX_PEERS];
int nPeers = std::min(WifiEspNow.listPeers(peers, MAX_PEERS), MAX_PEERS);
for (int i = 0; i < nPeers; ++i) {
Serial.printf(" %02X:%02X:%02X:%02X:%02X:%02X", peers[i].mac[0], peers[i].mac[1],
peers[i].mac[2], peers[i].mac[3], peers[i].mac[4], peers[i].mac[5]);
}
Serial.println();
}
void
loop()
{
if (digitalRead(BUTTON_PIN) == LOW) { // button is pressed
sendMessage();
while (digitalRead(BUTTON_PIN) == LOW) // wait for button release
;
}
WifiEspNowBroadcast.loop();
delay(10);
}

View File

@@ -0,0 +1,91 @@
/**
* @file
*
* EspNowUnicast.ino demonstrates how to transmit unicast ESP-NOW messages with @c WifiEspNow .
* You need two ESP8266 or ESP32 devices to run this example.
*
* Unicast communication requires the sender to specify the MAC address of the recipient.
* Thus, you must modify this program for each device.
*
* The recommended workflow is:
* @li 1. Flash the program onto device A.
* @li 2. Run the program on device A, look at serial console for its MAC address.
* @li 3. Copy the MAC address of device A, paste it in the @c PEER variable below.
* @li 4. Flash the program that contains A's MAC address onto device B.
* @li 5. Run the program on device A, look at serial console for its MAC address.
* @li 6. Copy the MAC address of device B, paste it in the @c PEER variable below.
* @li 7. Flash the program that contains B's MAC address onto device A.
*/
#include <WifiEspNow.h>
#if defined(ARDUINO_ARCH_ESP8266)
#include <ESP8266WiFi.h>
#elif defined(ARDUINO_ARCH_ESP32)
#include <WiFi.h>
#endif
// The recipient MAC address. It must be modified for each device.
static uint8_t PEER[]{0x02, 0x00, 0x00, 0x45, 0x53, 0x50};
void
printReceivedMessage(const uint8_t mac[WIFIESPNOW_ALEN], const uint8_t* buf, size_t count,
void* arg)
{
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 < static_cast<int>(count); ++i) {
Serial.print(static_cast<char>(buf[i]));
}
Serial.println();
}
void
setup()
{
Serial.begin(115200);
Serial.println();
WiFi.persistent(false);
WiFi.mode(WIFI_AP);
WiFi.disconnect();
WiFi.softAP("ESPNOW", nullptr, 3);
WiFi.softAPdisconnect(false);
// WiFi must be powered on to use ESP-NOW unicast.
// It could be either AP or STA mode, and does not have to be connected.
// For best results, ensure both devices are using the same WiFi channel.
Serial.print("MAC address of this node is ");
Serial.println(WiFi.softAPmacAddress());
uint8_t mac[6];
WiFi.softAPmacAddress(mac);
Serial.println();
Serial.println("You can paste the following into the program for the other device:");
Serial.printf("static uint8_t PEER[]{0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X, 0x%02X};\n", mac[0],
mac[1], mac[2], mac[3], mac[4], mac[5]);
Serial.println();
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()
{
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(1000);
}