-
Notifications
You must be signed in to change notification settings - Fork 0
/
weather_station.toit
executable file
·48 lines (37 loc) · 1.09 KB
/
weather_station.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
import net
import mqtt
import gpio
import i2c
import bme280
import encoding.json
CLIENT_ID ::= "toit-node"
HOST ::= "192.168.80.74"
PORT ::= 1883
TOPIC ::= "test"
main:
bus := i2c.Bus
--sda=gpio.Pin 21
--scl=gpio.Pin 22
device := bus.device 0x76
driver := bme280.Driver device
socket := net.open.tcp_connect HOST PORT
client := mqtt.Client
CLIENT_ID
mqtt.TcpTransport socket
print "Setup complete. Connected to MQTT Broker @ $HOST:$PORT"
while true:
print "Temperature: $driver.read_temperature C"
print "Humidity: $driver.read_humidity %"
print "Pressure: $driver.read_pressure Pa"
publish client driver.read_temperature driver.read_humidity driver.read_pressure
sleep --ms=10000
publish client/mqtt.Client payload1/float payload2/float payload3/float:
client.publish
TOPIC
json.encode {
"Temperature (C)": payload1,
"Humidity (%)": payload2,
"Pressure (Pa)": payload3,
"Timestamp": Time.now.local.to_iso8601_string
}
print "Published message new sensor reading on '$TOPIC', speeling for 10s"