-
Notifications
You must be signed in to change notification settings - Fork 0
/
Sender 3.ino
66 lines (46 loc) · 1.3 KB
/
Sender 3.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <ESP8266WiFi.h>
#include <espnow.h>
// C4:5B:BE:63:19:BA Sever IP Address
uint8_t broadcastAddress[] = {0xC4, 0x5B, 0xBE, 0x63, 0x19, 0xBA};
#define BOARD_ID 2
// Structure example to send data
// Must match the receiver structure
typedef struct struct_message {
int id;
int x;
int y;
} struct_message;
struct_message myData;
unsigned long lastTime = 0;
unsigned long timerDelay = 10000;
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
Serial.print("\r\nLast Packet Send Status: ");
if (sendStatus == 0){
Serial.println("Delivery success");
}
else{
Serial.println("Delivery fail");
}
}
void setup() {
Serial.begin(115200);
WiFi.mode(WIFI_STA);
WiFi.disconnect();
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
esp_now_set_self_role(ESP_NOW_ROLE_CONTROLLER);
esp_now_register_send_cb(OnDataSent);
esp_now_add_peer(broadcastAddress, ESP_NOW_ROLE_SLAVE, 1, NULL, 0);
}
void loop() {
if ((millis() - lastTime) > timerDelay) {
// Set values to send
myData.id = BOARD_ID;
myData.x = random(1, 50);
myData.y = random(1, 50);
esp_now_send(0, (uint8_t *) &myData, sizeof(myData));
lastTime = millis();
}
}