forked from chrgro/microbit_temperature_mesh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.ts
468 lines (419 loc) · 16.3 KB
/
main.ts
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
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
// DEVICE ID
// CHANGE FOR EVERY NEW DEVICE!
let DEVICE_ID = 15
// Encryption key, must be 19 bytes
let key = pins.createBufferFromArray([0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0])
let AHTX0_I2CADDR = 0x38
// AHT default i2c address
let AHTX0_CMD_CALIBRATE = 0xE1
// Calibration command
let AHTX0_CMD_SOFTRESET = 0xBA
// Soft reset command
let AHTX0_CMD_TRIGGER = 0xAC
let AHTX0_STATUS_BUSY = 0x80
// Status bit for busy
let AHTX0_STATUS_CALIBRATED = 0x08
// Status bit for calibrated
let AHTX0_READ_ATTEMPTS = 5
function ahtx0_get_status(): number {
return pins.i2cReadNumber(AHTX0_I2CADDR, NumberFormat.UInt8LE, false)
}
function ahtx0_init() {
basic.showIcon(IconNames.Surprised)
serial.writeLine("# AHT sensor reset and calibration...")
// Reset AHT
pins.i2cWriteNumber(AHTX0_I2CADDR, AHTX0_CMD_SOFTRESET, NumberFormat.Int8LE, false)
control.waitMicros(20000)
let cmd = Buffer.create(3)
cmd.setUint8(0, AHTX0_CMD_CALIBRATE)
cmd.setUint8(1, 0x08)
cmd.setUint8(2, 0x00)
pins.i2cWriteBuffer(AHTX0_I2CADDR, cmd, false)
while (ahtx0_get_status() & AHTX0_STATUS_BUSY) {
control.waitMicros(10000)
serial.writeLine("# AHT sensor not ready")
}
control.waitMicros(10000)
if (ahtx0_get_status() & AHTX0_STATUS_CALIBRATED) {
serial.writeLine("# AHT sensor calibrated")
basic.showIcon(IconNames.Happy)
} else {
serial.writeLine("# AHT sensor NOT calibrated!!")
basic.showIcon(IconNames.Sad)
}
basic.pause(1000)
}
function ahtx0_get_data(): number[] {
let cmd = Buffer.create(3)
cmd.setNumber(NumberFormat.UInt8LE, 0, AHTX0_CMD_TRIGGER)
cmd.setNumber(NumberFormat.UInt8LE, 1, 0x33)
cmd.setNumber(NumberFormat.UInt8LE, 2, 0x00)
pins.i2cWriteBuffer(AHTX0_I2CADDR, cmd)
control.waitMicros(90000)
for (let attempt = 0; attempt < AHTX0_READ_ATTEMPTS; attempt++) {
if (ahtx0_get_status() & AHTX0_STATUS_BUSY) {
control.waitMicros(20000)
serial.writeLine("# Sensor should not take so long time... attempt " + ("" + attempt) + " of " + ("" + AHTX0_READ_ATTEMPTS))
} else {
break
}
}
let readbuf = pins.i2cReadBuffer(AHTX0_I2CADDR, 6, false)
let h = readbuf.getNumber(NumberFormat.UInt8LE, 1)
h <<= 8
h |= readbuf.getNumber(NumberFormat.UInt8LE, 2)
h <<= 4
h |= readbuf.getNumber(NumberFormat.UInt8LE, 3) >> 4
let humidity = h * 100.0 / 0x100000
serial.writeString("# Humidity: ")
serial.writeNumber(humidity)
let t = readbuf.getNumber(NumberFormat.UInt8LE, 3) & 0x0F
t <<= 8
t |= readbuf.getNumber(NumberFormat.UInt8LE, 4)
t <<= 8
t |= readbuf.getNumber(NumberFormat.UInt8LE, 5)
let temperature = t / 0x100000 * 200.0 - 50
serial.writeString(" Temperature: ")
serial.writeNumber(temperature)
serial.writeLine("")
// Temperature read from i2c can't be 0, let's re-init if we ever see 0
if (t == 0) {
ahtx0_init()
return [-50, -1]
}
return [temperature, humidity]
}
// Function to retrieve the temperature
// In the future, expand this to read from an external set_transmit_power
// instead of the internal microbit sensor
function read_temp_humidity(): number[] {
let raw_value: number;
let temp: number;
let i2c_aht_temp = true
let i2c_temp = false
if (i2c_aht_temp) {
let [temperature, humidity] = ahtx0_get_data()
return [temperature, humidity]
} else if (i2c_temp) {
raw_value = pins.i2cReadNumber(0x48, NumberFormat.Int16BE, false)
// Reduce to just 2 subfractional bits (i.e. 0.25 C resolution)
// truncated_value = raw_value & 0xffC0
temp = raw_value * 0.00390625
// divide by 256
return [temp, -1]
} else {
return [input.temperature(), -1]
}
}
// Encryption is a simple XOR, with keysize equal to datasize,
// 19 bytes. This is equally safe as any other block cipher.
// The block mode is CBC, with an 8 bit block size and thus
// 8 bit initialization vector (IV). It at least reduces the odds
// of repeat messages, but there will be some of them (every 256th
// message with the same plaintext will then have same ciphertext).
// We could use a bigger block size, the code just gets a tiny bit
// more complicated and we lose plaintext capacity.
function encrypt_message(message: Buffer): Buffer {
let char: number;
let ciphertext = control.createBuffer(19)
let iv = randint(0, 255)
let mod_v = iv
for (let i = 0; i < ciphertext.length; i++) {
// Pad message with a slot of IV, and enough spaces
// on the end to always make it at least 19 bytes.
if (i == 0 || i - 1 >= message.length) {
char = 0
} else {
char = message[i - 1]
}
ciphertext[i] = char ^ mod_v ^ key[i]
mod_v = ciphertext[i]
}
return ciphertext
}
function decrypt_message(message: Buffer): Buffer {
let decrypted: number;
// 19 bytes for the buffer
let padded_plain_buffers = control.createBuffer(19)
let mod_v = message[0]
for (let i = 0; i < key.length; i++) {
decrypted = message[i] ^ mod_v ^ key[i]
padded_plain_buffers[i] = decrypted
mod_v = message[i]
}
return padded_plain_buffers.slice(1)
}
// On press button A, force a value send
input.onButtonPressed(Button.A, function on_button_pressed_a() {
let [temp, humidity] = read_temp_humidity()
send_message("t", temp)
if (humidity >= 0) {
send_message("h", humidity)
}
})
// On press button B, change how much is shown on screen
input.onButtonPressed(Button.B, function on_button_pressed_b() {
// 0: Show everything (current temp + radio events)
// 1: Only show radio events, don't show current temp
// 2: Only show current temp, don't show radio events
// 3: Keep the screen clear
verbosity_level = (verbosity_level + 1) % 4
if (verbosity_level == 0) {
basic.showString("SHOW ALL")
} else if (verbosity_level == 1) {
basic.showString("RADIO ONLY")
} else if (verbosity_level == 2) {
basic.showString("TEMP ONLY")
} else if (verbosity_level == 3) {
basic.showString("QUIET")
}
})
// Wrapper function to send messages out on BLE
function send_message(datatype: string, value: number) {
if ([0, 1].indexOf(verbosity_level) >= 0) {
basic.showIcon(IconNames.Duck)
}
// Pack the data
let message_to_send = control.createBuffer(18)
message_to_send.fill(0)
message_to_send[0] = DEVICE_ID
message_to_send[1] = datatype.charCodeAt(0)
message_to_send.setNumber(NumberFormat.Float32LE, 2, value)
radio.sendBuffer(encrypt_message(message_to_send))
// Encrypted
serial.writeLine(buffer_to_json(message_to_send, "sent"))
basic.clearScreen()
}
// For an incoming message with id and type, check if we have reject_seen_recently
// seen a message like it, and update the list as needed
// Return 0 if we should not forward this message, 1 if we should forward.
function check_last_message_time(received_device_id: number, received_value_type: string): number {
let prev_seen_message: Buffer;
let message_received_time: number;
let running_time: number;
let time_since_message: number;
serial.writeLine("# Checking for last recieved time (limit " + ("" + TX_FLOOD_CONTROL_MS) + ") for device_id " + ("" + received_device_id) + " and type " + received_value_type)
for (let i = 0; i < received_messages.length; i++) {
prev_seen_message = received_messages[i]
if (get_message_device_id(prev_seen_message) == received_device_id && get_message_value_type(prev_seen_message) == received_value_type) {
message_received_time = get_message_received_time(prev_seen_message)
running_time = input.runningTime()
time_since_message = running_time - message_received_time
if (0 < time_since_message && time_since_message < TX_FLOOD_CONTROL_MS) {
serial.writeLine("# Very recent match, current time was " + ("" + running_time) + " and time since msg " + ("" + time_since_message))
return 0
} else {
serial.writeLine("# Only an old match, removing it and forwarding")
received_messages.removeAt(i)
return 1
}
}
}
serial.writeLine("# Found no previous match of this id+type")
return 1
}
// Filter bad messages
function is_message_bad(receivedBuffer: Buffer): boolean {
// Reject any non-ASCII messages
let datatype = get_message_value_type(receivedBuffer)
let charcode = datatype.charCodeAt(0)
// Check for valid ascii in buffer type field and last byte equals to 0
if (!(32 <= charcode && charcode <= 126) || receivedBuffer[17] != 0) {
serial.writeLine("# Error, rejecting message due to likely decrypt failure, charcode: " + ("" + charcode))
return true
}
// Reject messages of type different a small group
if (["t", "h", "c", "v", "n", "a", "b", "c"].indexOf(get_message_value_type(receivedBuffer)) < 0) {
serial.writeLine("# Error, rejecting message not having an expected type :" + get_message_value_type(receivedBuffer))
return true
}
// Reject messages that hit the throw condition, i.e. its not a valid number
if (get_message_value(receivedBuffer) == FAILURE_VALUE) {
serial.writeLine("# Error, rejecting message not having a valid floating point ")
return true
}
return false
}
// Append own device ID to forwarded message
function append_forwarded_device_id(messageBuffer: Buffer) {
// Loop all bytes to look for the first free location
for (let i = 6; i < 17; i++) {
if (messageBuffer[i] == 0) {
messageBuffer[i] = DEVICE_ID
return
}
}
// If there are no free slots, just keep the buffer unchanged
return
}
// Callback function on recieved wireless data
function decode_buffer(receivedBuffer: Buffer) {
let received_message_device_id: number;
let received_message_value_type: string;
let last_seen: Buffer;
if ([0, 1].indexOf(verbosity_level) >= 0) {
basic.showIcon(IconNames.SmallDiamond)
}
// # DEBUG PRINT
// serial.write_string("# Decoding buffer:\n# ")
// for i in range(receivedBuffer.length):
// serial.write_string(str(receivedBuffer[i]) + " ")
// serial.write_line("")
if (is_message_bad(receivedBuffer)) {
} else {
// Extract device ID and value type from the incoming data
received_message_device_id = get_message_device_id(receivedBuffer)
received_message_value_type = get_message_value_type(receivedBuffer)
// Check if its our own data coming back to us
if (DEVICE_ID != received_message_device_id) {
// Check whether we've recently seen this data
if (check_last_message_time(received_message_device_id, received_message_value_type) == 1) {
last_seen = control.createBuffer(6)
last_seen.setNumber(NumberFormat.Int8LE, 0, received_message_device_id)
last_seen[1] = received_message_value_type.charCodeAt(0)
last_seen.setNumber(NumberFormat.Float32LE, 2, input.runningTime())
received_messages.push(last_seen)
// Tiny random pause before forwarding, to reduce collision odds
basic.pause(randint(0, 100))
// Append my own device ID to the message
append_forwarded_device_id(receivedBuffer)
// Send off the message again
radio.sendBuffer(encrypt_message(receivedBuffer))
// Encrypted
serial.writeLine(buffer_to_json(receivedBuffer, "forward"))
if ([0, 1].indexOf(verbosity_level) >= 0) {
basic.showIcon(IconNames.Yes)
}
} else {
serial.writeLine(buffer_to_json(receivedBuffer, "reject_seen_recently"))
if ([0, 1].indexOf(verbosity_level) >= 0) {
basic.showIcon(IconNames.No)
}
}
} else {
serial.writeLine(buffer_to_json(receivedBuffer, "reject_own_id"))
if ([0, 1].indexOf(verbosity_level) >= 0) {
basic.showIcon(IconNames.No)
}
}
}
basic.clearScreen()
}
// Buffer layout:
// Byte 0: device id
// Byte 1: Data type
// Byte 2-5: value
// Byte 6-16: device id of forwarding nodes
// Byte 17: Either 0 or ascii > if we ran out of space
function buffer_to_json(buf: Buffer, action: string): string {
let forwarded_device_id: number;
let buffer_device_id = get_message_device_id(buf)
let buffer_type = get_message_value_type(buf)
let buffer_value = buf.getNumber(NumberFormat.Float32LE, 2)
let buffer_sent_via = "[ "
for (let i = 6; i < 18; i++) {
if (buf[i] == 0) {
break
}
forwarded_device_id = buf.getNumber(NumberFormat.Int8LE, i)
buffer_sent_via += "" + forwarded_device_id + ","
}
buffer_sent_via = buffer_sent_via.slice(0, -1) + "]"
let retstr = "{\"device_id\":" + ("" + buffer_device_id) + ","
retstr += "\"type\": \"" + ("" + buffer_type) + "\","
retstr += "\"value\": " + ("" + buffer_value) + ","
retstr += "\"forwarded_via\":" + buffer_sent_via + ","
retstr += "\"action_taken\": \"" + action + "\""
retstr += "}"
return retstr
}
radio.onReceivedBuffer(function on_received_buffer(receivedBuffer: Buffer) {
let decrypted_msg = decrypt_message(receivedBuffer)
decode_buffer(decrypted_msg)
})
// Encrypted
// Split out the type from <id>:<type>:<value>
function get_message_value_type(message: Buffer): string {
let single_byte: Buffer;
let type_str: string;
try {
single_byte = control.createBuffer(1)
single_byte[0] = message[1]
type_str = single_byte.toString()
return type_str
}
catch (_) {
// This is after validation of message types, should
// in theory this should be unreachable
return "bad_type"
}
}
// From a message buffer, extract the
// device id
// Return ID 0 on any errors
// Accept only IDs between 1 and 99
function get_message_device_id(message: Buffer): number {
let t: number;
try {
t = message.getNumber(NumberFormat.Int8LE, 0)
if (!(0 < t && t < 100)) {
return 0
}
return t
}
catch (_) {
return 0
}
}
// Split out the value from <id>:<type>:<value>
function get_message_value(message: Buffer): number {
let v: number;
try {
v = message.getNumber(NumberFormat.Float32LE, 2)
return v
}
catch (_) {
return FAILURE_VALUE
}
}
// Split out the recieved time from the buffer
function get_message_received_time(prev_seen_message: Buffer): number {
return prev_seen_message.getNumber(NumberFormat.Float32LE, 2)
}
// Initial setup and ID print
let FAILURE_VALUE = -999
let verbosity_level = 0
let received_messages : Buffer[] = []
led.setBrightness(128)
radio.setGroup(181)
radio.setTransmitPower(7)
serial.writeLine("# Powered on, with ID: " + ("" + DEVICE_ID))
ahtx0_init()
let TX_INTERVAL_MS = 10 * 60 * 1000
let TX_FLOOD_CONTROL_MS = Math.trunc(TX_INTERVAL_MS * 0.9)
basic.showString("ID " + ("" + DEVICE_ID))
basic.showIcon(IconNames.SmallSquare)
basic.clearScreen()
// Keep printing the current temp
basic.forever(function on_forever_show_screen() {
if ([0, 2].indexOf(verbosity_level) >= 0) {
let [temp, humidity] = read_temp_humidity()
basic.showString("" + Math.roundWithPrecision(temp, 1) + "C")
if (humidity >= 0) {
basic.pause(1000)
basic.showString("" + (Math.round(humidity) + "%"))
}
}
basic.pause(8000)
})
// Keep sending out the temperature
basic.forever(function on_forever_send() {
basic.pause(TX_INTERVAL_MS / 2)
let [temp1, humidity1] = read_temp_humidity()
send_message("t", temp1)
basic.pause(TX_INTERVAL_MS / 2)
let [temp2, humidity2] = read_temp_humidity()
if (humidity2 >= 0) {
send_message("h", humidity2)
}
})