-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMXDiag.ino
363 lines (294 loc) · 7.74 KB
/
MXDiag.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
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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>
#include "jquery.h"
#include "index.h"
#include "svg.h"
#include "javascript.h"
#include "manifest.h"
#include "icon.h"
#include "dtc.h"
// Current state and previous state of the pin
int oldpin = 1;
int currentpin = 1;
// timenow of current state change, and previous
unsigned long timenow;
unsigned long statestart;
unsigned int diagcount = 0;
int firstpulsedone = 0;
int serialin = 0;
// Current diagnostic stage
int diagstate = -1;
String diagstring = "";
String dtcoutput = "";
String statoutput = "Booting...";
const char *ssid = "MX5Diag";
const char *password = "password";
int waitmilis = 0;
ESP8266WebServer server(80);
void handle_jq_js() {
server.sendHeader("Content-Encoding","gzip");
server.send_P(200, "text/javascript", jquery, sizeof(jquery));
}
void handle_mx_js() {
server.sendHeader("Content-Encoding","gzip");
server.send_P(200, "text/javascript", mxjs, sizeof(mxjs));
}
void handle_manifest_json() {
server.sendHeader("Content-Encoding","gzip");
server.send_P(200, "text/json", manifestjson, sizeof(manifestjson));
}
void handle_icon_png() {
server.sendHeader("Content-Encoding","gzip");
server.send_P(200, "image/png", iconpng, sizeof(iconpng));
}
void handle_index() {
server.sendHeader("Content-Encoding","gzip");
server.send_P(200, "text/html", indexhtml, sizeof(indexhtml));
}
void handle_go_svg() {
server.sendHeader("Content-Encoding","gzip");
server.send_P(200, "image/svg+xml", gosvg, sizeof(gosvg));
}
void handle_mz_svg() {
server.sendHeader("Content-Encoding","gzip");
server.send_P(200, "image/svg+xml", mzsvg, sizeof(mzsvg));
}
void handle_dtc() {
server.send(200, "text/plain", dtcoutput);
}
void handle_stat() {
server.send(200, "text/plain", statoutput);
}
void handle_run() {
server.send(200, "text/plain", "");
begindiag();
}
String identifydtc(String dtc)
{
int found = -1;
for(unsigned int i=0; i<(sizeof(dtc_index)/sizeof(dtc_index[0]));i++)
{
if(dtc_index[i] == dtc.toInt())
{
found = i;
}
}
if(found == -1)
return "Unknown";
else
return dtc_data[found];
}
void physicalsetup()
{
pinMode(D1, INPUT); // Signal from ECU
pinMode(D3, OUTPUT); // Diagnosis enable bin
pinMode(LED_BUILTIN, OUTPUT); // Status light
// Don't turn pull-down transistor on yet
digitalWrite(D3, LOW);
// Set up serial
Serial.begin(115200);
Serial.println();
}
void wifisetup()
{
WiFi.softAP(ssid/*, password*/);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);
// Files served - all pre-gzipped to save flash
server.on("/jq.js", handle_jq_js);
server.on("/", handle_index);
server.on("/go.svg", handle_go_svg);
server.on("/mz.svg", handle_mz_svg);
server.on("/mx.js", handle_mx_js);
// Make Android web app work nicely
server.on("/manifest.json", handle_manifest_json);
server.on("/icon.png", handle_icon_png);
// Dynamic 'files'
server.on("/dtc", handle_dtc);
server.on("/stat", handle_stat);
server.on("/run", handle_run);
server.begin();
}
void setup()
{
physicalsetup();
wifisetup();
statoutput = "MXDiag Online";
Serial.println(statoutput);
}
void begindiag()
{
statoutput = "Turn off ignition";
Serial.println(statoutput);
waitmilis = millis() + 6000;
}
void diag()
{
// Read the pin once here
currentpin = digitalRead(D1);
if (diagstate == 0) // Default state on boot, waiting for pulse from ECU
{
// Give a little flash each cycle to show we're OK
digitalWrite(LED_BUILTIN, LOW);
delay(5);
digitalWrite(LED_BUILTIN, HIGH);
delay(200);
// If the pin drops
if (currentpin == 0)
{
// Record when it happens
statestart = millis();
// Move us to the next state
diagstate = 1;
// Mirror the ECU output
digitalWrite(LED_BUILTIN, LOW);
}
}
else if (diagstate == 1) // This state is during the ECU greeting pulse
{
// If the pin comes back up, the pulse is over
if (currentpin == 1)
{
// Mirror ECU ouptut
digitalWrite(LED_BUILTIN, HIGH);
// Record 'now'
timenow = millis();
// Default to returning to the wait (may not be needed)
diagstate = 0;
// Calculate the length of the pulse
int pulselength = (timenow - statestart);
// If it's too short or too long
if ((pulselength < 1000) || (pulselength > 5000))
{
// Go back to wait state
diagstate = 0;
} else {
// If it's our 3000ms goldilocks pulse, move to the next stage
statoutput = "ECU Detected";
Serial.println(statoutput);
diagstate = 2;
}
}
}
else if (diagstate == 2) // Waiting for a DTC pule
{
// If pin goes low we're starting on a pulse
if (currentpin == 0)
{
// Mirror to LED
digitalWrite(LED_BUILTIN, LOW);
// Record when it happens and calculate the length of the silence beforehand
timenow = millis();
int pulselength = (timenow - statestart);
// If it's a sensible length
if (pulselength > 100)
{
// If it's gurt then probably next DTC
if (pulselength > 3500)
{
if (firstpulsedone == 1)
{
// Divider for easy debug
diagstring = diagstring + diagcount;
diagcount = 0;
} else {
firstpulsedone = 1;
Serial.println("STATUS: Begining DTC read");
}
}
if (pulselength > 7500)
{
// Divider for easy debug
Serial.println(diagstring);
dtcoutput = dtcoutput + "<h2>P" + diagstring + "</h2>" + identifydtc(diagstring) + "<br/>";
diagstring = "";
}
}
// Record when state change occured
statestart = millis();
// Move to 3 (waiting for pulse end)
diagstate = 3;
}
}
else if (diagstate == 3) // Waits for pulse end
{
if (currentpin == 1)
{
// Mirror to LED
digitalWrite(LED_BUILTIN, HIGH);
// Record when it happened
timenow = millis();
// Default to waiting for next pulse
diagstate = 2;
// Calculate pulse length
int pulselength = (timenow - statestart);
// If it's too short or too long
if ((pulselength < 300) || (pulselength > 1400))
{
// Wait for another one
diagstate = 2;
} else {
// 0.4 is short, 1.2 is long so lets split the difference
if (pulselength > 800)
{
// On late Mk1 / Mk2 all codes are 4 digit so we ignore the longs
}
else
{
diagcount++;
}
// Record for silence calculation
statestart = millis();
}
}
}
else
{
// An error state that stops all comms and shouts lots
statoutput = "Diagnostic failure";
Serial.println(statoutput);
}
}
void loop()
{
server.handleClient();
// Look for instructions
if (Serial.available() > 0)
{
serialin = Serial.read();
// D for DTC
if (serialin == 68)
begindiag();
// F for fan (5 seconds)
//if (serialin == 70)
// fancycle();// TODO
// P for fuel pump (5 seconds)
//if (serialin == 80)
// fuelprime();// TODO
}
int left = (waitmilis - millis());
if( left > 0)
{
left = left / 1000;
statoutput = "Turn off ignition: " + String(left);
Serial.println(statoutput);
}
else
{
if(waitmilis != 0)
{
// Turn on diag pin
digitalWrite(D3, HIGH);
statoutput = "Turn on ignition (waiting for ECU)";
Serial.println(statoutput);
// Put us in default 'ready to go' mode
diagstate = 0;
waitmilis = 0;
}
}
if(waitmilis == 0)
if(diagstate >= 0)
diag();
}