-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEmbedded_FinalCode_Accelerometer.ino
More file actions
197 lines (158 loc) · 4.99 KB
/
Copy pathEmbedded_FinalCode_Accelerometer.ino
File metadata and controls
197 lines (158 loc) · 4.99 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
#include <Wire.h>
#include <WiFi.h>
#include <WebServer.h>
#define STM32_ADDR 0x42 //I2C address of STM32
//define variable for the values we retrieve from the STM32
volatile uint16_t front_distance = 0;
volatile uint16_t rear_distance = 0;
volatile int16_t accel_x = 0;
volatile int16_t accel_y = 0;
volatile int16_t accel_z = 0;
volatile uint16_t accel_mag = 0;
volatile uint8_t Last_Cmd = 'S'; // default = Stop
//Function used for getting distace and data values from the STM32
void Receive_Event(int Sent_Bytes)
{
//Three bytes are sent for the ID to distinguish what is sent
//and to take two bytes for the value of the data sent
if (Sent_Bytes == 3) {
uint8_t ID = Wire.read();
uint8_t High = Wire.read();
uint8_t Low = Wire.read();
uint16_t value = ((uint16_t)High << 8) | Low;
switch (ID) {
case 0x01: //front distance
front_distance = value;
break;
case 0x02: //rear distance
rear_distance = value;
break;
case 0x03: //accel X
accel_x = (int16_t)value;
break;
case 0x04: //accel Y
accel_y = (int16_t)value;
break;
case 0x05: //accel Z
accel_z = (int16_t)value;
break;
case 0x06: //accel magnitude
accel_mag = value;
break;
default:
// Unknown ID
break;
}
} else {
while (Wire.available()) Wire.read();
}
}
// This is called whenever the STM32 wants to receive data
void Request_Event()
{
// Send the latest command as a single byte
Wire.write(Last_Cmd);
}
//WiFi Name and Password
const char* ssid = "ESP32-RC-Car";
const char* password = "12345678";
WebServer server(80);
//Code for building the webpage used to control motor and see live data
String Build_WebPage() {
String html =
"<html>"
"<head>"
"<style>"
"body { font-family: Arial; text-align:center; }"
"h1 { color:#003366; }"
"button { width:120px; height:50px; font-size:20px; margin:10px; }"
"div { font-size:24px; margin-top:20px; }"
"</style>"
"<script>"
"function sendCmd(cmd) {"
"fetch('/cmd?val=' + cmd);"
"}"
"function updateDistances() {"
"fetch('/dist').then(response => response.json()).then(data => {"
"document.getElementById('front').innerHTML = data.front;"
"document.getElementById('rear').innerHTML = data.rear;"
"document.getElementById('accel').innerHTML = "
"'X=' + data.ax + ', Y=' + data.ay + ', Z=' + data.az + ', |Mag|=' + data.amag;"
"});"
"}"
"setInterval(updateDistances, 250);" // update every 250 ms
"</script>"
"</head>"
"<body>"
"<h1>RC Car Controller</h1>"
"<div>"
"<button onclick=\"sendCmd('F')\">Forward</button>"
"</div>"
"<div>"
"<button onclick=\"sendCmd('L')\">Left</button>"
"<button onclick=\"sendCmd('S')\">Stop</button>"
"<button onclick=\"sendCmd('R')\">Right</button>"
"</div>"
"<div>"
"<button onclick=\"sendCmd('B')\">Reverse</button>"
"</div>"
"<div>Front Distance: <span id='front'>--</span> cm</div>"
"<div>Rear Distance: <span id='rear'>--</span> cm</div>"
"<div>Accel (g): <span id='accel'>X=--, Y=--, Z=--, |Mag|=--</span></div>"
"</body>"
"</html>";
return html;
}
// Send webpage
void Handle_Web_Root() {
server.send(200, "text/html", Build_WebPage());
}
//sends values for distances
void Handle_Distance_Vals() {
String json = "{";
json += "\"front\":" + String(front_distance) + ",";
json += "\"rear\":" + String(rear_distance) + ",";
json += "\"ax\":" + String(accel_x) + ",";
json += "\"ay\":" + String(accel_y) + ",";
json += "\"az\":" + String(accel_z) + ",";
json += "\"amag\":" + String(accel_mag);
json += "}";
server.send(200, "application/json", json);
}
// Receive command from browser and update Last_Cmd
void Handle_Command() {
if (!server.hasArg("val")) {
server.send(400, "text/plain", "Missing val param");
return;
}
String cmd = server.arg("val");
if (cmd.length() > 0) {
Last_Cmd = (uint8_t)cmd[0]; // store as single char for STM32
}
Serial.print("Received CMD: ");
Serial.println(cmd);
server.send(200, "text/plain", "OK");
}
void setup() {
Serial.begin(115200);
delay(500);
Serial.println("Starting I2C + WiFi AP...");
// ESP32 acts as I2C receiver at address 0x42
Wire.begin(STM32_ADDR);
Wire.onReceive(Receive_Event); // STM32 writes the distances + accel
Wire.onRequest(Request_Event); // STM32 reads Last_CMd
//for creating the WiFi AP
WiFi.softAP(ssid, password);
Serial.print("AP IP: ");
Serial.println(WiFi.softAPIP());
//for updating the webpage depending on commands and live data
server.on("/", Handle_Web_Root);
server.on("/dist", Handle_Distance_Vals);
server.on("/cmd", Handle_Command);
server.begin();
Serial.println("Web server started!");
}
void loop() {
server.handleClient();
delay(1);
}