-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathESPnow.ino
157 lines (127 loc) · 4.3 KB
/
ESPnow.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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
#include <ESP8266WiFi.h>
#include <espnow.h>
// Prototypes
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus);
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len);
unsigned long lastMillis=0;
//###################### SEND DATA ################################
// Structure to send data
// Must match the receiver structure
typedef struct struct_sensor_data {
float horizonAngle;
float verticalAngle;
bool flipXYAxis;
int temperature;
int sensorSystemStatus;
} struct_sensor_data;
// Create a container for Sensor Data for the Display
struct_sensor_data sensorData;
// Callback when data is sent
void OnDataSent(uint8_t *mac_addr, uint8_t sendStatus) {
if(sendStatus != 0) { // Only log, if Delivery failes
Serial.print("\r\nLast Packet Delivery Status:\t");
Serial.println("Delivery Fail");
Serial.println();
}
}
//###################### RECEIVE DATA ################################
int incomingCommandType;
bool incomingCommandValue;
bool newCommandReceived = false;
// Structure to receive data
// Must match the sender structure
typedef struct struct_sensor_command {
int commandType;
bool commandValue;
} struct_sensor_command;
// Create a container for Sensor Commands from the Display
struct_sensor_command sensorCommand;
// Callback when data is received
void OnDataRecv(uint8_t * mac, uint8_t *incomingData, uint8_t len) {
memcpy(&sensorCommand, incomingData, sizeof(sensorCommand));
Serial.print("Bytes received: ");
Serial.println(len);
incomingCommandType = sensorCommand.commandType;
incomingCommandValue = sensorCommand.commandValue;
newCommandReceived = true;
}
void initEspNow() {
Serial.println("Init ESP-NOW");
Serial.print("ESP8266 Sensor-Board MAC Address: ");
Serial.println(WiFi.macAddress());
// Set device as a Wi-Fi Station
WiFi.mode(WIFI_STA);
WiFi.disconnect();
// Init ESP-NOW
if (esp_now_init() != 0) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Set Role of Sensor to "Controller"
esp_now_set_self_role(ESP_NOW_ROLE_COMBO);
// Register Send Callback Method and get status of transmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
esp_now_add_peer(broadcastAddressEsp32Display, ESP_NOW_ROLE_COMBO, 1, NULL, 0);
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
lastMillis = millis();
}
void sendSensorValues() {
Serial.print(".");
// Get fresh Values from sensor
updateSensor();
// Pack Sensor-Values to datastructure
updateSensorDataStructure();
// Send Data
sendMessage();
// Serial.println(millis());
}
void sendMessage() {
// Send message via ESP-NOW
int result = esp_now_send(broadcastAddressEsp32Display, (uint8_t *) &sensorData, sizeof(sensorData));
if (result != 0) { // Only log, if sending failes
Serial.println("Error sending data");
Serial.print("Horizontal: ");Serial.println(getHorizontalAngleWithOffset());
Serial.print("Vertical: ");Serial.println(getVerticalAngleWithOffset());
Serial.print("flipXYAxis: ");Serial.println(getFlipXY());
Serial.print("temperature: ");Serial.println(getTemperature());
Serial.println();
}
}
void sendSensorSystemStatus(int sensorSystemStatus) {
updateSensorDataStructure();
sensorData.sensorSystemStatus = sensorSystemStatus;
sendMessage();
}
void updateSensorDataStructure() {
sensorData.horizonAngle = getHorizontalAngleWithOffset();
sensorData.verticalAngle = getVerticalAngleWithOffset();
sensorData.flipXYAxis = getFlipXY();
sensorData.temperature = getTemperature();
sensorData.sensorSystemStatus = getSensorSystemStatus();
}
void processCommand() {
switch(incomingCommandType) {
case DISPLAY_COMMAND_CALIBRATE_SENSOR:
Serial.print("DISPLAY_COMMAND_CALIBRATE_SENSOR. Value: ");
setOffsetToNow();
break;
case DISPLAY_COMMAND_FLIP_XY:
Serial.print("DISPLAY_COMMAND_FLIP_XY. Value: ");
setFlipXYToEEPROM(incomingCommandValue);
break;
}
Serial.println(incomingCommandValue ? "True" : "False");
}
void loopEspNow() {
if(millis() - lastMillis > ESPNOW_SEND_RATE) {
lastMillis = millis();
sendSensorValues();
}
if(newCommandReceived == true) {
Serial.println("New Command received!");
processCommand();
newCommandReceived = false;
}
}