-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathESP32_CANViewer.ino
More file actions
239 lines (205 loc) · 6.36 KB
/
ESP32_CANViewer.ino
File metadata and controls
239 lines (205 loc) · 6.36 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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
/*
* ESP32 CANViewer
* V0.1
* Connect CRX to Pin 4, CTX to Pin 5
*/
#include <SPIFFS.h>
#include <CAN.h>
#include <WiFi.h>
#include <ESPAsyncWebServer.h>
#include <WebSocketsServer.h>
#include <ArduinoJson.h>
const char* ssid = "YOURNETWORK";
const char* password = "YOURPASSWORD";
AsyncWebServer server(80);
WebSocketsServer webSocket = WebSocketsServer(1337);
long CAN_baudRate = 500E3;
long CAN_filterId = -1;
long CAN_filterMask = -1;
void onWebSocketEvent(uint8_t client_num, WStype_t type, uint8_t * payload, size_t length) {
switch(type) {
case WStype_DISCONNECTED:
Serial.printf("[%u] Disconnected!\n", client_num);
break;
case WStype_CONNECTED:
{
IPAddress ip = webSocket.remoteIP(client_num);
Serial.printf("[%u] Connection from ", client_num);
Serial.println(ip.toString());
}
break;
case WStype_TEXT:
{
Serial.printf("[%u] Received text: %s\n", client_num, payload);
StaticJsonDocument<250> jsonobj;
DeserializationError err = deserializeJson(jsonobj, payload);
if(err){
Serial.println("Invalid JSON!");
}else{
const char* command = jsonobj["cmd"];
Serial.print("CMD: ");
Serial.println(command);
// CMD: SETFILTER
if(String(command).equals("SETFILTER")){
const unsigned int filter_id = jsonobj["id"];
const unsigned int filter_mask = jsonobj["mask"];
CAN.filter(filter_id, filter_mask);
CAN_filterId = filter_id;
CAN_filterMask = filter_mask;
Serial.printf("Setting Filter ID: %x ,MASK: %x\n", filter_id, filter_mask);
}
// CMD: SETSPEED
if(String(command).equals("SETSPEED")){
const unsigned int targetspeed = jsonobj["speed"];
CAN_baudRate = targetspeed;
CAN.end();
CAN.begin(CAN_baudRate);
Serial.printf("Setting Speed/Baudrate to: %u\n", targetspeed);
}
// CMD: SEND
if(String(command).equals("SEND")){
const unsigned int send_id = jsonobj["id"];
const unsigned int send_dlc = jsonobj["dlc"];
JsonArray send_arr = jsonobj["data"].as<JsonArray>();
if(send_dlc == send_arr.size()){
if(send_id <= 0x7FF){
CAN.beginPacket(send_id, send_dlc);
}else{
CAN.beginExtendedPacket(send_id, send_dlc);
}
for(byte i=0; i<send_dlc; i++){
CAN.write((byte)send_arr[i]);
}
char can_err = CAN.endPacket();
if(!can_err){
Serial.println("ERR: Sending Frame!");
}
}else{
Serial.println("ERR: Size does not match!");
}
Serial.printf("Sending Frame with ID: %x\n", send_id);
}
}
}
break;
// Ignore all other types
case WStype_BIN:
case WStype_ERROR:
case WStype_FRAGMENT_TEXT_START:
case WStype_FRAGMENT_BIN_START:
case WStype_FRAGMENT:
case WStype_FRAGMENT_FIN:
default:
break;
}
}
void onIndexRequest(AsyncWebServerRequest *request) {
IPAddress remote_ip = request->client()->remoteIP();
Serial.println("["+remote_ip.toString()+"] HTTP GET request of " + request->url());
request->send(SPIFFS, "/index.html", "text/html");
}
void onPageNotFound(AsyncWebServerRequest *request) {
IPAddress remote_ip = request->client()->remoteIP();
Serial.println("["+remote_ip.toString()+"] HTTP GET request of " + request->url());
request->send(404, "text/plain", "Not found");
}
void setup() {
Serial.begin(115200);
while (!Serial);
Serial.println("");
Serial.println("ESP32 CANViewer");
// Test SPIFFS
if(!SPIFFS.begin(true)){
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// Connect to WIFI
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
WiFi.setHostname("ESP32CANViewer");
Serial.print("Connecting to WIFI ...");
byte wifi_counter = 0;
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
wifi_counter++;
if(wifi_counter > 20){
WiFi.disconnect();
WiFi.mode(WIFI_AP);
WiFi.softAP("ESP32CANViewer", NULL);
Serial.print("[Switching to AP Mode]");
break;
}
}
Serial.println(" connected!");
// Show IP Address
Serial.print("IP: ");
Serial.println(WiFi.localIP());
// Setup Webserver
server.on("/", HTTP_GET, onIndexRequest);
server.onNotFound(onPageNotFound);
server.begin();
// Setup Websocket Server
webSocket.begin();
webSocket.onEvent(onWebSocketEvent);
// Start CAN with default Baudrate
if (!CAN.begin(CAN_baudRate)) {
Serial.println("Starting CAN failed!");
while (1);
}else{
Serial.println("CAN started!");
}
}
long prevMillis = 0;
void loop() {
webSocket.loop();
handleCAN_RX();
// Broadcast STATUS message every second
if((millis() - prevMillis) > 1000){
prevMillis = millis();
//Serial.println("Sending status!");
StaticJsonDocument<250> jsonobj;
String output = "";
jsonobj["ts"] = millis();
jsonobj["type"] = "STATUS";
jsonobj["baudrate"] = CAN_baudRate;
jsonobj["filterId"] = CAN_filterId;
jsonobj["filterMask"] = CAN_filterMask;
serializeJson(jsonobj, output);
webSocket.broadcastTXT(output);
}
}
// Handle CAN Input
void handleCAN_RX() {
int packetSize = CAN.parsePacket();
if (packetSize) {
StaticJsonDocument<250> jsonobj;
String output = "";
jsonobj["ts"] = millis();
jsonobj["type"] = "STD";
if (CAN.packetExtended()) {
jsonobj["type"] = "EXT";
}
if (CAN.packetRtr()) {
jsonobj["type"] = "RTR";
if(CAN.packetExtended()) jsonobj["type"] = "ERTR";
}
jsonobj["id"] = CAN.packetId();
if (CAN.packetRtr()) {
// RTR FRAME
jsonobj["dlc"] = CAN.packetDlc();
} else {
// NON RTR FRAME
jsonobj["dlc"] = packetSize;
JsonArray data = jsonobj.createNestedArray("data");
while (CAN.available()) {
char temp = (char)CAN.read();
data.add(temp);
//Serial.printf("%u\n", temp);
}
//Serial.printf("Size: %u\n", data.size());
}
serializeJson(jsonobj, output);
webSocket.broadcastTXT(output); // Broadcast every received packet
}
}