-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathadafruit.toit
58 lines (43 loc) · 1.69 KB
/
adafruit.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
// Copyright (C) 2022 Toitware ApS.
// Use of this source code is governed by a Zero-Clause BSD license that can
// be found in the EXAMPLES_LICENSE file.
/**
Demonstrates the use of Adafruit's MQTT server.
Adafruit is free, but has some limitations. For example, one can only publish
at most 30 messages per minute.
Before running this program:
- create an account at https://io.adafruit.com/
- create a feed: https://io.adafruit.com/$ADAFRUIT-IO-USERNAME/feeds (replace the $ADAFRUIT-IO-USERNAME with your username)
- update the $ADAFRUIT-IO-USERNAME, $ADAFRUIT-IO-KEY, and $ADAFRUIT-IO-FEEDNAME constants.
*/
import mqtt
import certificate-roots
ADAFRUIT-IO-USERNAME ::= "<YOUR_USERNAME>"
// From io.adafruit.com/$ADAFRUIT_IO_USERNAME/dashboards -> click "My Key"
ADAFRUIT-IO-KEY ::= "<YOUR_KEY>"
ADAFRUIT-IO-FEEDNAME ::= "<YOUR_FEEDNAME>"
HOST ::= "io.adafruit.com"
main:
client := mqtt.Client.tls --host=HOST
--root-certificates=[ certificate-roots.DIGICERT-GLOBAL-ROOT-CA ]
/**
// Alternatively, you can also connect without TLS, by using the
// following client:
client := mqtt.Client --host=HOST
// In that case you can remove the `certificate_roots` import.
*/
options := mqtt.SessionOptions
--client-id = "toit-example-client"
--username = ADAFRUIT-IO-USERNAME
--password = ADAFRUIT-IO-KEY
client.start --options=options
print "Connected to broker"
topic := "$ADAFRUIT-IO-USERNAME/feeds/$ADAFRUIT-IO-FEEDNAME"
// Simulates a temperature sensor.
temperature := 25.0
10.repeat:
temperature += ((random 100) - 50) / 100.0
client.publish topic "$temperature"
// Don't publish too often to avoid rate limiting.
sleep --ms=2_500
client.close