Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Daniel Carrasco
- This and more tutorials at https://www.electrosoftcloud.com/
- Example about how to send an int/long variable using ESP_NOW
- */
- #include <esp_now.h>
- #include <WiFi.h>
- // Set the SLAVE MAC Address
- uint8_t slaveAddress[] = {0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF};
- // Callback to have a track of sent messages
- void OnSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
- Serial.print("\r\nSend message status:\t");
- Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Sent Successfully" : "Sent Failed");
- }
- void setup() {
- // Init Serial Monitor
- Serial.begin(115200);
- // Set device as a Wi-Fi Station
- WiFi.mode(WIFI_STA);
- // Init ESP-NOW
- if (esp_now_init() != ESP_OK) {
- Serial.println("There was an error initializing ESP-NOW");
- return;
- }
- // We will register the callback function to respond to the event
- esp_now_register_send_cb(OnSent);
- // Register the slave
- esp_now_peer_info_t slaveInfo;
- memcpy(slaveInfo.peer_addr, slaveAddress, 6);
- slaveInfo.channel = 0;
- slaveInfo.encrypt = false;
- // Add slave
- if (esp_now_add_peer(&slaveInfo) != ESP_OK){
- Serial.println("There was an error registering the slave");
- return;
- }
- }
- void loop() {
- // Preparing the integer to send
- int shortNumber = 123;
- // Is time to send the messsage via ESP-NOW
- esp_err_t result = esp_now_send(slaveAddress, (uint8_t *) &shortNumber, sizeof(int));
- if (result == ESP_OK) {
- Serial.println("The integer was sent sucessfully.");
- }
- else {
- Serial.println("There was an error sending the integer.");
- }
- delay(2000);
- // Set values to send
- // To simplify the code, we will just set two floats and I'll send it
- long longNumber = 34567;
- // Is time to send the messsage via ESP-NOW
- result = esp_now_send(slaveAddress, (uint8_t *) &longNumber, sizeof(long));
- if (result == ESP_OK) {
- Serial.println("The long variable was sent sucessfully.");
- }
- else {
- Serial.println("There was an error sending the long variable.");
- }
- delay(2000);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement