esp now sender sketch

Solutions on MaxInterview for esp now sender sketch by the best coders in the world

showing results for - "esp now sender sketch"
Manuel
09 Jan 2021
1//=================================================================================================//
2// MASTER                                                                                          //
3// Controlling LEDs with Two ESP8266 Uses ESP-NOW Protocol                                         //
4// Edited and Adapted by: Engineer Jemerson Marques, On: 22.12.2019 - FVM Learning website         //
5// Available at: https://www.fvml.com.br and on Youtube channel                                    //
6// https://www.youtube.com/c/FVMLearning - I hope you have fun - Good luck                         //
7//-------------------------------------------------------------------------------------------------//
8
9#include <ESP8266WiFi.h>
10extern "C" {
11#include <espnow.h>
12}
13// This is the slave MAC Address which receives the data
14 uint8_t mac[] = {0x5C, 0xCF, 0x7F, 0x4C, 0x82, 0x5C}; //AP MAC SLAVE'S ADDRESS
15
16  #define WIFI_CHANNEL 4
17  int prevstate_1 = LOW;
18  int prevstate_2 = LOW;
19
20// Data structure, must be the same for the slave
21
22struct __attribute__((packed))DataStruct {
23  char text[32];
24};
25    DataStruct button_1;
26    DataStruct button_2;
27
28//=====================================================================================================
29void setup() {
30  pinMode(D1, INPUT_PULLUP);
31  pinMode(D4, INPUT_PULLUP);
32  pinMode(D2, OUTPUT);
33  pinMode(D3, OUTPUT);
34
35  Serial.begin(115200); Serial.println();
36  Serial.println("Starting EspnowController.ino");
37  WiFi.mode(WIFI_STA); // Station mode for esp-now controller
38  WiFi.disconnect();
39  Serial.printf("This mac: %s, ", WiFi.macAddress().c_str());
40  Serial.printf("slave mac: %02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
41  Serial.printf(", channel: %i\n", WIFI_CHANNEL);
42  if (esp_now_init() != 0)
43  {
44    Serial.println("*** ESP_Now initialization failed");
45  }
46  esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
47  esp_now_add_peer(mac, ESP_NOW_ROLE_SLAVE, WIFI_CHANNEL, NULL, 0);
48  strcpy(button_1.text, "Button 01 pressed");
49  strcpy(button_2.text, "Button 02 pressed");
50  Serial.println("Setup finished");
51}
52
53//======================================================================================================
54
55void loop() {
56  sendData();
57}
58
59//======================================================================================================
60void sendData() {
61
62  int currentstate_1 = digitalRead(D4);
63  if (prevstate_1 != currentstate_1) {
64    if (currentstate_1 == LOW) {
65      uint8_t bs[sizeof(button_1)];
66      memcpy(bs, &button_1, sizeof(button_1));
67      esp_now_send(mac, bs, sizeof(button_1));
68      Serial.println(button_1.text);
69      digitalWrite(D2, !digitalRead(D2));
70    }
71  } prevstate_1 = currentstate_1;
72
73  int currentstate_2 = digitalRead(D1);
74  if (prevstate_2 != currentstate_2) {
75    if (currentstate_2 == LOW) {
76      uint8_t bs[sizeof(button_2)];
77      memcpy(bs, &button_2, sizeof(button_2));
78      esp_now_send(mac, bs, sizeof(button_2));
79      Serial.println(button_2.text);
80      digitalWrite(D3, !digitalRead(D3));
81    }
82  } prevstate_2 = currentstate_2;
83}
84
85//========================================== www.fvml.com.br ===========================================
86
87
similar questions
queries leading to this page
esp now sender sketch