Created
June 8, 2023 16:15
-
-
Save floitsch/18fc1e847a050693cca8451fad7eea4d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| import net | |
| import mqtt | |
| import scd30 show * | |
| import i2c | |
| import gpio | |
| import certificate_roots | |
| import esp32 | |
| import system.storage | |
| ADAFRUIT_IO_USERNAME ::= "foobar" | |
| ADAFRUIT_IO_KEY ::= "foobar" | |
| ADAFRUIT_IO_FEEDNAME_1 ::= "Humidity New" | |
| ADAFRUIT_IO_FEEDNAME_2 ::= "Co2" | |
| HOST ::= "io.adafruit.com" | |
| BUCKET ::= "scd30-state" | |
| co2_level := 0.0 | |
| main: | |
| start_time := Time.now | |
| bus := i2c.Bus | |
| --sda=gpio.Pin 21 | |
| --scl=gpio.Pin 22 | |
| device := bus.device Scd30.I2C_ADDRESS | |
| scd30 := Scd30 device | |
| bucket := storage.Bucket.open --ram BUCKET | |
| state := bucket.get "state" --if_absent=: "powered-down" | |
| if state == "powered-down": | |
| print "Waking up in powered down state." | |
| print "Starting the sensor." | |
| sleep --ms=2_000 | |
| scd30.continuous_mode = true | |
| bucket["state"] = "powered-up" | |
| // Give the sensor 30 seconds to warm up and measure something. | |
| // The duration can be tweaked. | |
| // In the meantime we don't need to stay awake. | |
| print "Sleeping for a bit while the sensor starts measuring." | |
| esp32.deep_sleep (Duration --s=30) | |
| // While the Scd30 is booting up start networking. | |
| // This will take a bit of time anyway. | |
| network := net.open | |
| print "I am trying to connect" | |
| transport := mqtt.TcpTransport.tls network --host=HOST | |
| --root_certificates=[ certificate_roots.DIGICERT_GLOBAL_ROOT_CA ] | |
| 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" | |
| // Make sure the Scd30 is ready to be used. | |
| remaining_sleep_time_ms := 2_000 - (Duration.since start_time).in_ms | |
| sleep --ms=remaining_sleep_time_ms | |
| 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 | |
| client.publish topic_2 "$temperature".to_byte_array | |
| client.close | |
| scd30.continuous_mode = false | |
| device.close | |
| bus.close | |
| bucket["state"] = "powered-down" | |
| jaguar_pin := gpio.Pin 32 --input --pull_up | |
| if jaguar_pin.get == 0: | |
| // The pin is pulled down. | |
| // Don't go to sleep, but let Jaguar handle updates. | |
| exit 0 | |
| print "i am going to deep sleep" | |
| esp32.deep_sleep (Duration --s=90) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment