/ FunHouse_Fume_Extractor / code.py
code.py
1 # SPDX-FileCopyrightText: 2021 Liz Clark for Adafruit Industries 2 # 3 # SPDX-License-Identifier: MIT 4 5 import time 6 import board 7 import simpleio 8 import adafruit_sgp30 9 import displayio 10 import adafruit_imageload 11 from adafruit_emc2101 import EMC2101 12 from adafruit_funhouse import FunHouse 13 14 i2c = board.I2C() 15 16 # setup for SGP30 sensor 17 sgp30 = adafruit_sgp30.Adafruit_SGP30(i2c) 18 # setup for fan controller 19 emc = EMC2101(i2c) 20 21 print("SGP30 serial #", [hex(i) for i in sgp30.serial]) 22 23 #SGP30 start-up 24 sgp30.iaq_init() 25 sgp30.set_iaq_baseline(0x8973, 0x8AAE) 26 27 # FunHouse setup 28 funhouse = FunHouse(default_bg=0x0F0F00) 29 # start-up bitmap 30 bitmap, palette = adafruit_imageload.load("/scene1_fume.bmp", 31 bitmap=displayio.Bitmap, 32 palette=displayio.Palette) 33 tile_grid = displayio.TileGrid(bitmap, pixel_shader=palette) 34 # connecting bitmap 35 bitmap2, palette2 = adafruit_imageload.load("/scene2_fume.bmp", 36 bitmap=displayio.Bitmap, 37 palette=displayio.Palette) 38 grid2 = displayio.TileGrid(bitmap2, pixel_shader=palette2) 39 # default background 40 bitmap3, palette3 = adafruit_imageload.load("/scene3_fume.bmp", 41 bitmap=displayio.Bitmap, 42 palette=displayio.Palette) 43 grid3 = displayio.TileGrid(bitmap3, pixel_shader=palette3) 44 # internet connection icon 45 bitmap4, palette4 = adafruit_imageload.load("/connect_icon.bmp", 46 bitmap=displayio.Bitmap, 47 palette=displayio.Palette) 48 icon1 = displayio.TileGrid(bitmap4, pixel_shader=palette4, x = 2, y = 2) 49 # red x icon 50 bitmap5, palette5 = adafruit_imageload.load("/x_icon.bmp", 51 bitmap=displayio.Bitmap, 52 palette=displayio.Palette) 53 icon2 = displayio.TileGrid(bitmap5, pixel_shader=palette5, x = 2, y = 2) 54 # display group 55 group = displayio.Group() 56 # adding start-up bitmap to group 57 group.append(tile_grid) 58 funhouse.splash.append(group) 59 # text for fume data 60 fume_text = funhouse.add_text( 61 text=" ", 62 text_position=(110, 90), 63 text_anchor_point=(0.5, 0.5), 64 text_color=0xf57f20, 65 text_font="fonts/Arial-Bold-24.pcf", 66 ) 67 # text for fan RPM data 68 fan_text = funhouse.add_text( 69 text=" ", 70 text_position=(110, 165), 71 text_anchor_point=(0.5, 0.5), 72 text_color=0x7fffff, 73 text_font="fonts/Arial-Bold-24.pcf", 74 ) 75 # showing graphics 76 funhouse.display.show(funhouse.splash) 77 78 # state machines 79 run = False # state if main code is running 80 connected = False # checks if connected to wifi 81 start_up = False # state for start-up 82 clock = 0 # time.monotonic() device 83 84 # function for sending fume data to adafruit.io 85 def send_fume_data(solder_fumes): 86 funhouse.network.push_to_io("fumes", solder_fumes) 87 88 # function for sending fan rpm to adafruit.io 89 def send_fan_data(fan_rpm): 90 funhouse.network.push_to_io("fan-speed", fan_rpm) 91 92 while True: 93 # if main program has not started 94 if not run: 95 # if you press the down button 96 if funhouse.peripherals.button_down: 97 print("run") 98 # remove start-up bitmap 99 group.remove(tile_grid) 100 # add main bitmap 101 group.append(grid3) 102 # add red x icon to show not connected to internet 103 group.append(icon2) 104 # change state for main program 105 run = True 106 # if you press the middle button 107 if funhouse.peripherals.button_sel: 108 # remove start-up bitmap 109 group.remove(tile_grid) 110 # add connecting... bitmap 111 group.append(grid2) 112 # connect to the network 113 funhouse.network.connect() 114 print("connecting") 115 # change state for network 116 connected = True 117 # start main program 118 start_up = True 119 # start time.monotonic() 120 clock = time.monotonic() 121 # after connecting to the internet 122 if start_up: 123 # remove connecting bitmap 124 group.remove(grid2) 125 # add main bitmap 126 group.append(grid3) 127 # add internet icon 128 group.append(icon1) 129 # start main program 130 run = True 131 # reset start-up state 132 start_up = False 133 134 # run state for main program after selecting whether or not to connect to wifi 135 if run: 136 # print eCO2 and TVOC data to REPL 137 print("eCO2 = %d ppm \t TVOC = %d ppb" % (sgp30.eCO2, sgp30.TVOC)) 138 # 2 second delay 139 time.sleep(2) 140 141 # fumes variable for reading from SGP30 142 # comment out either TVOC or eCO2 depending on data preference 143 fumes = sgp30.TVOC 144 # fumes = sgp30.eCO2 145 146 # mapping fumes data to fan RPM 147 # value for TVOC 148 mapped_val = simpleio.map_range(fumes, 10, 1000, 10, 100) 149 # value for eCO2 150 # mapped_val = simpleio.map_range(fumes, 400, 2500, 10, 100) 151 152 # adding fume text 153 # PPB is for TVOC, PPM is for eCO2 154 funhouse.set_text("%d PPB" % fumes, fume_text) 155 # funhouse.set_text("%d PPM" % fumes, fume_text) 156 157 # adding fan's RPM text 158 funhouse.set_text("%d%s" % (mapped_val, "%"), fan_text) 159 # printing fan's data to the REPL 160 print("fan = ", mapped_val) 161 # setting fan's RPM 162 emc.manual_fan_speed = int(mapped_val) 163 164 # if you're connected to wifi and 15 seconds has passed 165 if connected and ((clock + 15) < time.monotonic()): 166 # send fume data to adafruit.io 167 send_fume_data(fumes) 168 # send fan RPM to adafruit.io 169 send_fan_data(mapped_val) 170 # REPL printout 171 print("data sent") 172 # reset clock 173 clock = time.monotonic() 174 # if you're connected to wifi and you press the up button 175 if connected and funhouse.peripherals.button_up: 176 # the internet icon is removed 177 group.remove(icon1) 178 # the red x icon is added 179 group.append(icon2) 180 # reset connected state - no longer sending data to adafruit.io 181 connected = False 182 # REPL printout 183 print("disconnected") 184 # 1 second delay 185 time.sleep(1) 186 # if you're NOT connected to wifi and you press the up button 187 if not connected and funhouse.peripherals.button_up: 188 # the red x icon is removed 189 group.remove(icon2) 190 # the internet icon is added 191 group.append(icon1) 192 # the connection state is true - start sending data to adafruit.io 193 connected = True 194 # REPL printout 195 print("connected") 196 # 1 second delay 197 time.sleep(1)