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