-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathBatteryReader.ino
283 lines (215 loc) · 6.79 KB
/
BatteryReader.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
#include <OneWire.h>
#include <WiFi.h>
#include <WiFiMulti.h>
#include <HTTPClient.h>
#include "defines.h"
#include "settings.h"
OneWire ds(DS_PIN);
void taskDS(void *pvParameters);
void taskLED(void *pvParameters);
void setColor(byte color, bool blink);
void appendByte(String &str, uint8_t b);
byte dsDeviceCount();
byte ledColor = COLOR_NONE;
bool ledBlink = false;
void setupWifi() {
WiFiMulti wifiMulti;
WiFi.setHostname("BatteryReader");
WiFi.persistent(false);
WiFi.mode(WIFI_STA);
addWifi(wifiMulti);
Serial.print(F("Attempting WiFi connection ..."));
if (wifiMulti.run(WIFI_TIMEOUT) != WL_CONNECTED) {
Serial.println("error");
setColor(COLOR_RED, true);
while(true) {
}
}
Serial.printf_P(PSTR(" connected to %s, IP address: %s\n"), WiFi.SSID().c_str(), WiFi.localIP().toString().c_str());
}
void setup() {
Serial.begin(115200);
// ds2438.begin();
pinMode(PULLUP_PIN, OUTPUT);
pinMode(LED_GREEN_PIN, OUTPUT);
pinMode(LED_RED_PIN, OUTPUT);
xTaskCreate(&taskLED, "taskLED", 1024, NULL, 1, NULL);
setColor(COLOR_RED, false);
setupWifi();
setColor(COLOR_GREEN, false);
delay(1000);
setColor(COLOR_NONE, false);
digitalWrite(PULLUP_PIN, HIGH);
xTaskCreatePinnedToCore(&taskDS, "taskDS", 8192, NULL, 2, NULL, ARDUINO_RUNNING_CORE);
}
void loop() {
vTaskDelete(NULL);
}
void taskDS(void *pvParameters) {
// while(true) {
// byte addr[8];
// uint8_t bb = 0;
// if (!ds.search(addr))
// {
// Serial.println(" No more addresses.");
// Serial.println();
// ds.reset_search();
// delay(250);
// continue;
// }
// Serial.print(" ROM =");
// for (byte i = 0; i < 8; i++)
// {
// Serial.write(' ');
// Serial.print(addr[i], HEX);
// }
// Serial.println("!");
// }
uint8_t addr[8];
uint8_t DS2433_addr[8];
uint8_t DS2438_addr[8];
String output = "";
loop: while(true) {
vTaskDelay(250 / portTICK_RATE_MS);
setColor(COLOR_NONE, false);
memset(DS2433_addr, 0, sizeof(DS2433_addr));
memset(DS2438_addr, 0, sizeof(DS2438_addr));
output = "";
while(true) { //Search devices
if (!ds.search(addr)) {
ds.reset_search();
vTaskDelay(250 / portTICK_RATE_MS);
break;
}
setColor(COLOR_RED, false);
Serial.print("ROM =");
for (byte i = 0; i < 8; i++) {
Serial.write(' ');
Serial.print(addr[i], HEX);
appendByte(output, addr[i]);
}
output += "/";
Serial.println();
if(addr[0] == DS2433_ID)
memcpy(DS2433_addr, addr, sizeof(addr));
if(addr[0] == DS2438_ID)
memcpy(DS2438_addr, addr, sizeof(addr));
}
if(DS2433_addr[0] == 0x00 || DS2438_addr[0] == 0x00)
continue;
output.remove(output.length() - 1);
output += "_";
setColor(COLOR_ORANGE, false);
vTaskDelay(250 / portTICK_RATE_MS); //Ensure proper seat
// Read DS2438 chip
ds.reset();
ds.select(DS2438_addr);
ds.write(0x44); // Start temperature conversion
vTaskDelay(10 / portTICK_RATE_MS);
ds.reset();
ds.select(DS2438_addr);
ds.write(0xB4); // Start voltage conversion
vTaskDelay(10 / portTICK_RATE_MS);
//Read all pages
for (byte j = 0; j < 8; j++) {
ds.reset();
ds.select(DS2438_addr);
ds.write(0xB8); // Recall memory
ds.write(j);
ds.reset();
ds.select(DS2438_addr);
ds.write(0xBE); // Read memory
ds.write(j);
uint8_t data[9];
for (byte i = 0; i < 9; i++) {
data[i] = ds.read();
appendByte(output, data[i]);
}
if(ds.crc8(data, 8) != data[8]) {
Serial.print("Invalid CRC for PAGE ");
Serial.println(j);
goto loop;
}
}
output += "_";
// Read DS2433 chip
ds.reset();
ds.select(DS2433_addr);
ds.write(0xF0); // Read memory
ds.write(0x00); // Offset
ds.write(0x00); // Offset
uint8_t data[512];
for (int i = 0; i < 512; i++) //TODO: CRC?
data[i] = ds.read();
//Verify
ds.reset();
ds.select(DS2433_addr);
ds.write(0xF0); // Read memory
ds.write(0x00); // Offset
ds.write(0x00); // Offset
for (int i = 0; i < 512; i++) {
if (data[i] == ds.read())
appendByte(output, data[i]);
else {
Serial.print("Invalid data byte ");
Serial.println(i);
goto loop;
}
}
setColor(COLOR_GREEN, true);
Serial.println(output);
HTTPClient http;
String serverPath = SERVER_URL + "?token=" + SERVER_TOKEN + "&data=" + output;
http.begin(serverPath.c_str(), le_root_ca_crt);
int httpResponseCode = http.GET();
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
http.end();
if (httpResponseCode == 200) {
setColor(COLOR_GREEN, false);
dsDeviceCount(); // Idk why, buy first time it is always returns 0
while(true) {
vTaskDelay(250 / portTICK_RATE_MS);
if(dsDeviceCount() == 0)
break;
}
} else {
setColor(COLOR_RED, true);
}
vTaskDelay(1000 / portTICK_RATE_MS);
}
}
void taskLED(void *pvParameters) {
while(true) {
digitalWrite(LED_GREEN_PIN, (ledColor == COLOR_ORANGE || ledColor == COLOR_GREEN) ? HIGH : LOW);
digitalWrite(LED_RED_PIN, (ledColor == COLOR_ORANGE || ledColor == COLOR_RED) ? HIGH : LOW);
vTaskDelay(250 / portTICK_RATE_MS);
if(ledColor != COLOR_NONE && ledBlink) {
digitalWrite(LED_GREEN_PIN, LOW);
digitalWrite(LED_RED_PIN, LOW);
vTaskDelay(250 / portTICK_RATE_MS);
}
}
}
byte dsDeviceCount() {
byte ret = 0;
uint8_t addr[8];
while(true) {
if (!ds.search(addr)) {
ds.reset_search();
break;
}
ret++;
}
return ret;
}
void setColor(byte color, bool blink) {
ledColor = color;
ledBlink = blink;
}
void appendByte(String &str, uint8_t b) {
str += "0123456789ABCDEF"[b / 16];
str += "0123456789ABCDEF"[b % 16];
}