1+ #include < esp_now.h>
2+ #include < WiFi.h>
3+ #include < esp_wifi.h>
4+
5+ // REPLACE WITH YOUR RECEIVER MAC Address
6+ uint8_t broadcastAddress[] = {0xC8 , 0xF0 , 0x9E , 0x19 , 0x88 , 0x80 };
7+
8+ // Structure example to send data
9+ // Must match the receiver structure
10+ typedef struct {
11+ float accMag;
12+ float meanhpfvm;
13+ float peakhpfvm;
14+ float gyrMag;
15+ float tempC;
16+ float batteryLevelVoltage;
17+ int batteryLevelPercentage;
18+ char timeStamp[25 ];
19+ char id[32 ];
20+ } SensorPacket;
21+
22+ uint8_t mac[6 ]; // Mac address
23+ char macStr[18 ]; // Mac address in string format
24+
25+ // Create a struct_message called myData
26+ SensorPacket myData;
27+
28+ esp_now_peer_info_t peerInfo;
29+
30+ uint8_t channel = 0 ;
31+ bool isHubChannelFound = false ;
32+ uint8_t failureCount = 0 ;
33+
34+ // callback when data is sent
35+ void OnDataSent (const uint8_t *mac_addr, esp_now_send_status_t status) {
36+ Serial.print (" \r\n Last Packet Send Status:\t " );
37+ log_i (" Sending status: %s" , status == ESP_NOW_SEND_SUCCESS ? " Delivery Success" : " Delivery Fail" );
38+ // Did sending fail?
39+ if (status == ESP_NOW_SEND_FAIL) {
40+ if (!isHubChannelFound) {
41+ log_i (" Failed with channel %d." , channel);
42+ // Increase channel. If hub is not online this will search forever!
43+ // Consider implementing an increasing waiting time to avoid channel congestion
44+ // whenn all nodes start searching a channel
45+ channel = (channel + 1 ) % 13 ;
46+ esp_wifi_set_promiscuous (true );
47+ esp_wifi_set_channel (channel, WIFI_SECOND_CHAN_NONE);
48+ esp_wifi_set_promiscuous (false );
49+ } else {
50+ // If the hub channel has been found before but we have failures now
51+ // we give the hub some time before we start searching the channel again
52+ // This might avoid all nodes starting the channel search immediatelly
53+ failureCount++;
54+ if (failureCount > 10 ) {
55+ isHubChannelFound = false ;
56+ }
57+ }
58+
59+ } else {
60+ failureCount = 0 ;
61+ isHubChannelFound = true ;
62+ }
63+ }
64+
65+ void setup () {
66+ // Init Serial Monitor
67+ Serial.begin (115200 );
68+
69+ // Set device as a Wi-Fi Station
70+ WiFi.mode (WIFI_STA);
71+
72+ // Init ESP-NOW
73+ if (esp_now_init () != ESP_OK) {
74+ log_i (" Error initializing ESP-NOW" );
75+ return ;
76+ }
77+
78+ esp_err_t ret = esp_wifi_get_mac (WIFI_IF_STA, mac); // Read MAC address (station mode)
79+
80+ if (ret == ESP_OK) {
81+ snprintf (macStr, sizeof (macStr), " %02X:%02X:%02X:%02X:%02X:%02X" ,
82+ mac[0 ], mac[1 ], mac[2 ], mac[3 ], mac[4 ], mac[5 ]); // Convert MAC (unique ID of the device)
83+ } else {
84+
85+ log_i (" Failed to get the MAC!" );
86+ }
87+
88+ log_i (" %s" , macStr);
89+
90+ // Once ESPNow is successfully Init, we will register for Send CB to
91+ // get the status of Trasnmitted packet
92+ esp_now_register_send_cb (OnDataSent);
93+
94+ esp_wifi_set_promiscuous (true );
95+ esp_wifi_set_channel (channel, WIFI_SECOND_CHAN_NONE);
96+ esp_wifi_set_promiscuous (false );
97+
98+ // Register peer
99+ memcpy (peerInfo.peer_addr , broadcastAddress, 6 );
100+ peerInfo.channel = 0 ;
101+ peerInfo.encrypt = false ;
102+
103+ // Add peer
104+ if (esp_now_add_peer (&peerInfo) != ESP_OK){
105+ log_i (" Failed to add peer" );
106+ return ;
107+ }
108+ }
109+
110+ void loop () {
111+
112+ myData.accMag = random (1 ,20 ) / 10 .0f ;
113+ myData.meanhpfvm = random (1 ,20 ) / 10 .0f ;
114+ myData.peakhpfvm = random (1 ,20 ) / 10 .0f ;
115+ myData.gyrMag = random (1 ,20 ) / 10 .0f ;
116+ myData.tempC = random (0 ,450 ) / 10 .0f ;
117+ myData.batteryLevelVoltage = random (1 ,20 ) / 10 .0f ;
118+ myData.batteryLevelPercentage = random (1 ,20 );
119+ // Timestamp will be set on hub
120+ strcpy (myData.timeStamp , " 1970-01-01T00:00:00Z" );
121+ // could also be set on receiver side
122+ strcpy (myData.id ,macStr);
123+
124+ // Send message via ESP-NOW
125+ esp_err_t result = esp_now_send (broadcastAddress, (uint8_t *) &myData, sizeof (myData));
126+
127+ if (result == ESP_OK) {
128+ log_i (" Sent with success" );
129+ } else {
130+ log_i (" Error sending the data" );
131+ }
132+
133+ if (isHubChannelFound) {
134+ delay (2000 );
135+ } else {
136+ delay (100 );
137+ }
138+
139+ }
0 commit comments