Last active
October 17, 2021 10:15
-
-
Save SimedruF/2e45028e8ce1c299602ab217911d6ec9 to your computer and use it in GitHub Desktop.
ESP32_DEVKIT_sender_esp_now.ino
This file contains hidden or 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
/* | |
Florin Simedru | |
Complete project details at https://automatic-house.blogspot.com | |
Permission is hereby granted, free of charge, to any person obtaining a copy | |
of this software and associated documentation files. | |
The above copyright notice and this permission notice shall be included in all | |
copies or substantial portions of the Software. | |
*/ | |
/******************************************************************************************************************** | |
* Preferences--> Aditional boards Manager URLs : https://dl.espressif.com/dl/package_esp32_index.json, http://arduino.esp8266.com/stable/package_esp8266com_index.json | |
* Board Settings: | |
* Board: "ESP32 Wrover Module" | |
* Upload Speed: "921600" | |
* Flash Frequency: "80MHz" | |
* Flash Mode: "QIO" | |
* Partition Scheme: "Hue APP (3MB No OTA/1MB SPIFFS)" | |
* Core Debug Level: "None" | |
* COM Port: Depends *On Your System* | |
* | |
*/ | |
#include <esp_now.h> | |
#include <esp_wifi.h> | |
#include <WiFi.h> | |
// REPLACE WITH YOUR RECEIVER MAC Address | |
uint8_t broadcastAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF}; | |
// Insert your SSID | |
constexpr char WIFI_SSID[] = "YOURWIFI_SSID"; | |
// Structure example to send data | |
// Must match the receiver structure | |
typedef struct struct_message { | |
char a[32]; | |
int b; | |
float c; | |
bool d; | |
} struct_message; | |
// Create a struct_message called myData | |
struct_message myData; | |
// callback when data is sent | |
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"); | |
} | |
int32_t getWiFiChannel(const char *ssid) { | |
if (int32_t n = WiFi.scanNetworks()) { | |
for (uint8_t i=0; i<n; i++) { | |
if (!strcmp(ssid, WiFi.SSID(i).c_str())) { | |
return WiFi.channel(i); | |
} | |
} | |
} | |
return 0; | |
} | |
void setup() { | |
// Init Serial Monitor | |
Serial.begin(115200); | |
// Set device as a Wi-Fi Station | |
WiFi.mode(WIFI_MODE_STA); | |
Serial.println(WiFi.macAddress()); | |
int32_t channel = getWiFiChannel(WIFI_SSID); | |
esp_wifi_set_channel(channel, WIFI_SECOND_CHAN_NONE); | |
// Init ESP-NOW | |
if (esp_now_init() != ESP_OK) { | |
Serial.println("Error initializing ESP-NOW"); | |
return; | |
} | |
// Once ESPNow is successfully Init, we will register for Send CB to | |
// get the status of Trasnmitted packet | |
esp_now_register_send_cb(OnDataSent); | |
// Register peer | |
esp_now_peer_info_t peerInfo; | |
memcpy(peerInfo.peer_addr, broadcastAddress, 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() { | |
// Set values to send | |
strcpy(myData.a, "THIS IS A CHAR"); | |
myData.b = random(1,20); | |
myData.c = 1.2; | |
myData.d = false; | |
// Send message via ESP-NOW | |
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &myData, sizeof(myData)); | |
if (result == ESP_OK) { | |
Serial.println("Sent with success"); | |
} | |
else { | |
Serial.println("Error sending the data"); | |
} | |
delay(2000); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment