-
Notifications
You must be signed in to change notification settings - Fork 0
/
gestrure_control_robot.ino
157 lines (131 loc) · 3.94 KB
/
gestrure_control_robot.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 <esp_now.h>
#include <WiFi.h>
int motor1Pin1 = 27;
int motor1Pin2 = 26;
int motor1Pin3 = 21;
int motor1Pin4 = 22;
// REPLACE WITH THE MAC Address of your receiver
uint8_t broadcastAddress[] = {0x08,0x3A,0xF2,0x65,0xD8,0xC4};
// Define variables to store BME280 readings to be sent
float temperature;
float humidity;
// Define variables to store incoming readings
float incomingTemp;
float incomingHum;
// Variable to store if sending data was successful
String success;
typedef struct struct_message {
float temp;
float hum;
} struct_message;
// Create a struct_message called BME280Readings to hold sensor readings
struct_message BME280Readings;
// Create a struct_message to hold incoming sensor readings
struct_message incomingReadings;
esp_now_peer_info_t peerInfo;
// Callback when data is sent
void OnDataSent(const uint8_t *mac_addr, esp_now_send_status_t status) {
Serial.print("\r\nLast Packet Send Status:\t");
Serial.println(status == ESP_NOW_SEND_SUCCESS ? "Delivery Success" : "Delivery Fail");
if (status ==0){
success = "Delivery Success :)";
}
else{
success = "Delivery Fail :(";
}
}
// Callback when data is received
void OnDataRecv(const uint8_t * mac, const uint8_t *incomingData, int len) {
memcpy(&incomingReadings, incomingData, sizeof(incomingReadings));
Serial.print("Bytes received: ");
Serial.println(len);
incomingTemp = incomingReadings.temp;
incomingHum = incomingReadings.hum;
}
void setup() {
Serial.begin(115200);
pinMode(motor1Pin1, OUTPUT);
pinMode(motor1Pin2, OUTPUT);
pinMode(motor1Pin3, OUTPUT);
pinMode(motor1Pin4, OUTPUT);
WiFi.mode(WIFI_STA);
// Init ESP-NOW
if (esp_now_init() != ESP_OK) {
Serial.println("Error initializing ESP-NOW");
return;
}
// Once ESPNow is successfully Init, we will register for Send CB to
// get the status of Trasnmitted packet
esp_now_register_send_cb(OnDataSent);
// Register peer
memcpy(peerInfo.peer_addr, broadcastAddress, 6);
peerInfo.channel = 0;
peerInfo.encrypt = false;
// Add peer
if (esp_now_add_peer(&peerInfo) != ESP_OK){
Serial.println("Failed to add peer");
return;
}
// Register for a callback function that will be called when data is received
esp_now_register_recv_cb(OnDataRecv);
}
void loop() {
BME280Readings.temp = temperature;
BME280Readings.hum = humidity;
// Send message via ESP-NOW
esp_err_t result = esp_now_send(broadcastAddress, (uint8_t *) &BME280Readings, sizeof(BME280Readings));
if (result == ESP_OK) {
Serial.println("Sent with success");
}
else {
Serial.println("Error sending the data");
}
updateDisplay();
delay(1000);
}
void getReadings(){
temperature = random(300);
humidity = random(300);
}
void updateDisplay(){
if(incomingReadings.temp > 0.50)
{
Serial.println("LEFT");
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, HIGH);
}
else if(incomingReadings.temp < -0.50)
{
Serial.println("RIGHT");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor1Pin3, HIGH);
digitalWrite(motor1Pin4, LOW);
}
else if(incomingReadings.hum > 0.5)
{
Serial.println("BACK");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, HIGH);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, HIGH);
}
else if(incomingReadings.hum < -0.5)
{
Serial.println("FRONT");
digitalWrite(motor1Pin1, HIGH);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, HIGH);
digitalWrite(motor1Pin4, LOW);
}
else{
Serial.println("STOP");
digitalWrite(motor1Pin1, LOW);
digitalWrite(motor1Pin2, LOW);
digitalWrite(motor1Pin3, LOW);
digitalWrite(motor1Pin4, LOW);
}
Serial.println();
}