Shadi 06/08/2023 03:46 PMADAFRUIT_IO_FEEDNAME_1 ::= "Humidity New"
ADAFRUIT_IO_FEEDNAME_2 ::= "Co2"
HOST ::= "io.adafruit.com"
co2_level := 0.0
main:
network := net.open
print "I am trying to connect"
transport := mqtt.TcpTransport.tls network --host=HOST
--root_certificates=[ certificate_roots.DIGICERT_GLOBAL_ROOT_CA ]
/**
// Alternatively, you can also connect without TLS, by using the
// following transport:
transport := mqtt.TcpTransport network --host=HOST
// In that case you can remove the `certificate_roots` import.
*/
client := mqtt.Client --transport=transport
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_1 := "$ADAFRUIT_IO_USERNAME/feeds/$ADAFRUIT_IO_FEEDNAME_2"
topic_2 := "$ADAFRUIT_IO_USERNAME/feeds/$ADAFRUIT_IO_FEEDNAME_1"
bus := i2c.Bus
--sda=gpio.Pin 21
--scl=gpio.Pin 22
device := bus.device Scd30.I2C_ADDRESS
scd30 := Scd30 device
scd30.continuous_mode = true
sleep --ms=5000
//seconds := #[0x00, 0x10]
//command := #[0x46, 0x00] + seconds + #[scd30.compute_crc8_ seconds]
//scd30.device_.write command
while true:
reading := scd30.read
if reading.co2 > 2000:
print "Open your window: $(reading.co2.to_int)ppm"
else:
print "CO2 level is healthy: $(reading.co2.to_int)ppm"
print "Temperature: $(%.1f reading.temperature)ΒΊC"
print "Humidity: $(%.1f reading.humidity)%"
temperature := reading.co2.to_int
client.publish topic_1 "$temperature".to_byte_array
sleep --ms=5_500
// temperature := 25
client.publish topic_2 "$temperature".to_byte_array
print "i am going to deep sleep %"
sleep --ms=5_500
scd30.continuous_mode = false
device.close
bus.close
esp32.deep_sleep (Duration --s=90)
print "I am awake"
client.close
(edited)