-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathM5TempMon.ino
112 lines (107 loc) · 2.18 KB
/
M5TempMon.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
#include "M5StickCPlus.h"
#include "BluetoothSerial.h"
#include "ELMduino.h"
int alarmTemp = 99;
int cycle = 0;
int temp = 0;
int prevTemp = 0;
int prevBgColor = BLACK;
int bgColor = BLACK;
BluetoothSerial SerialBT;
ELM327 myELM327;
void setup(){
M5.begin();
Serial.begin(115200);
Serial.println("Starting up...");
// Connect to the bluetooth device
SerialBT.begin("M5TempMon", true);
if (!SerialBT.connect("OBDII"))
{
Serial.println("Couldn't connect to OBD scanner - Phase 1");
while(1);
}
// Handshake with with the ELM
if (!myELM327.begin(SerialBT, true, 2000))
{
Serial.println("Couldn't connect to OBD scanner - Phase 2");
while (1);
}
Serial.println("Connected to ELM327");
}
void loop() {
M5.update(); // Read the press state of the key
float tempFloat = myELM327.engineCoolantTemp();
// We got a reading
if (myELM327.nb_rx_state == ELM_SUCCESS)
{
temp = (int)tempFloat;
Serial.print("Temp: ");
Serial.println(temp);
// Over temperature
if (temp >= alarmTemp)
{
M5.Beep.tone(3000);
if (cycle % 2)
{
bgColor = BLACK;
M5.Lcd.setTextColor(RED);
}
else
{
bgColor = RED;
M5.Lcd.setTextColor(BLACK);
}
cycle++;
}
// Normal temperature
else
{
bgColor = BLACK;
M5.Lcd.setTextColor(WHITE);
M5.Beep.mute();
}
}
// No response available, try again
else if (myELM327.nb_rx_state == ELM_GETTING_MSG) {
return;
}
// Something went wrong
else
{
Serial.println("Couldn't get coolant temp");
M5.Beep.mute();
if (cycle % 2)
{
bgColor = BLACK;
M5.Lcd.setTextColor(YELLOW);
}
else
{
bgColor = YELLOW;
M5.Lcd.setTextColor(BLACK);
}
cycle++;
}
// Refresh the LCD if needed
if (prevBgColor != bgColor)
{
M5.Lcd.fillScreen(bgColor);
prevBgColor = bgColor;
}
// Also refresh if our temp changed
if (prevTemp != temp)
{
M5.Lcd.fillScreen(bgColor);
prevTemp = temp;
}
if (temp > 99)
{
M5.Lcd.setTextSize(5);
}
else
{
M5.Lcd.setTextSize(7);
}
M5.Lcd.drawString(String(temp), 16, 60, 2);
delay(500);
}