-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.toit
97 lines (83 loc) · 2 KB
/
main.toit
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
// Copyright 2023 Ekorau LLC
import gpio
import mqtt
import net
import monitor show Mutex
import .vindriktning
import .credentials show ADAFRUIT-IO-USERNAME ADAFRUIT-IO-KEY ADAFRUIT-IO-FEEDNAME CLIENT-ID
data := Deque
mutex := Mutex
RED-LED ::= 13
rled := gpio.Pin RED-LED --output
main:
task:: data-collect
task:: data-print
task:: data-send
data-collect:
vin := Vindriktning 21
while vin.next:
mutex.do:
dot-red
if data.size >4: data.remove-first
data.add vin.air-quality
data-print:
sum := 0
msg := ""
val := 0
while true:
an-exception := catch:
(Duration --m=1).periodic:
mutex.do:
sum = data.reduce --initial=0 : | a b |
a + b
val = data.size > 0? (sum / data.size): 0 // For .periodic at t=0.
msg = "$val"
print "$Time.now ppm: $val"
if an-exception:
print "At $Time.now got $an-exception"
data-send:
HOST ::= "io.adafruit.com"
TOPIC ::= "$ADAFRUIT-IO-USERNAME/feeds/$ADAFRUIT-IO-FEEDNAME"
sum := 0
val := -1
msg := ""
network := net.open
transport := mqtt.TcpTransport network --host=HOST
client := mqtt.Client --transport=transport
options := mqtt.SessionOptions
--client-id = CLIENT-ID
--username = ADAFRUIT-IO-USERNAME
--password = ADAFRUIT-IO-KEY
client.start --options=options
mq-exception := catch:
(Duration --m=1).periodic:
mutex.do:
sum = data.reduce --initial=0: | a b |
a + b
val = data.size > 0? (sum / data.size): 0 // For .periodic at t=0.
msg = "$val"
print "ppm: $val"
client.publish TOPIC msg.to-byte-array
dash-red
if mq-exception:
fail 250 750
red-on -> none:
rled.set 0
red-off -> none:
rled.set 1
fail on off:
while true:
red-on
sleep --ms=on
red-off
sleep --ms=off
dash-red --on=950 --off=50 -> none:
red-on
sleep --ms=on
red-off
sleep --ms=off
dot-red --on=50 --off=950 -> none:
red-on
sleep --ms=on
red-off
sleep --ms=off