Skip to content

Commit 709b6fa

Browse files
committed
add code for trinkey enviro gadget
1 parent 32b326a commit 709b6fa

7 files changed

Lines changed: 245 additions & 0 deletions

File tree

Trinkey_QT2040_Enviro_Gadget/arduino/.test.only.qt2040_trinkey

Whitespace-only changes.
Lines changed: 111 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,111 @@
1+
// SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
2+
//
3+
// SPDX-License-Identifier: MIT
4+
5+
#include <Adafruit_NeoPixel.h>
6+
#include <Adafruit_BME280.h>
7+
#include <SensirionI2CScd4x.h>
8+
#include <Wire.h>
9+
10+
//--| User Config |-----------------------------------------------
11+
#define DATA_FORMAT 0 // 0=CSV, 1=JSON
12+
#define DATA_RATE 5000 // generate new number ever X ms
13+
#define BEAT_COLOR 0xADAF00 // neopixel heart beat color
14+
#define BEAT_RATE 1000 // neopixel heart beat rate in ms, 0=none
15+
//----------------------------------------------------------------
16+
17+
Adafruit_BME280 bme;
18+
SensirionI2CScd4x scd4x;
19+
Adafruit_NeoPixel neopixel(1, PIN_NEOPIXEL, NEO_GRB + NEO_KHZ800);
20+
21+
uint16_t CO2, data_ready;
22+
float scd4x_temp, scd4x_humid;
23+
float temperature, humidity, pressure;
24+
int current_time, last_data, last_beat;
25+
26+
void setup() {
27+
Serial.begin(115200);
28+
29+
// init status neopixel
30+
neopixel.begin();
31+
neopixel.fill(0);
32+
neopixel.show();
33+
34+
// init BME280 first, this calls Wire.begin()
35+
if (!bme.begin()) {
36+
Serial.println("Failed to initialize BME280.");
37+
neoPanic();
38+
}
39+
40+
// init SCD40
41+
scd4x.begin(Wire);
42+
scd4x.stopPeriodicMeasurement();
43+
if (scd4x.startPeriodicMeasurement()) {
44+
Serial.println("Failed to start SCD40.");
45+
neoPanic();
46+
}
47+
48+
// init time tracking
49+
last_data = last_beat = millis();
50+
}
51+
52+
void loop() {
53+
current_time = millis();
54+
55+
//-----------
56+
// Send Data
57+
//-----------
58+
if (current_time - last_data > DATA_RATE) {
59+
temperature = bme.readTemperature();
60+
pressure = bme.readPressure() / 100;
61+
humidity = bme.readHumidity();
62+
scd4x.setAmbientPressure(uint16_t(pressure));
63+
scd4x.readMeasurement(CO2, scd4x_temp, scd4x_humid);
64+
switch (DATA_FORMAT) {
65+
case 0:
66+
sendCSV(); break;
67+
case 1:
68+
sendJSON(); break;
69+
default:
70+
Serial.print("Unknown data format: "); Serial.println(DATA_FORMAT);
71+
neoPanic();
72+
}
73+
last_data = current_time;
74+
}
75+
76+
//------------
77+
// Heart Beat
78+
//------------
79+
if ((BEAT_RATE) && (current_time - last_beat > BEAT_RATE)) {
80+
if (neopixel.getPixelColor(0)) {
81+
neopixel.fill(0);
82+
} else {
83+
neopixel.fill(BEAT_COLOR);
84+
}
85+
neopixel.show();
86+
last_beat = current_time;
87+
}
88+
}
89+
90+
void sendCSV() {
91+
Serial.print(CO2); Serial.print(", ");
92+
Serial.print(pressure); Serial.print(", ");
93+
Serial.print(temperature); Serial.print(", ");
94+
Serial.println(humidity);
95+
}
96+
97+
void sendJSON() {
98+
Serial.print("{");
99+
Serial.print("\"CO2\" : "); Serial.print(CO2); Serial.print(", ");
100+
Serial.print("\"pressure\" : "); Serial.print(pressure); Serial.print(", ");
101+
Serial.print("\"temperature\" : "); Serial.print(temperature); Serial.print(", ");
102+
Serial.print("\"humidity\" : "); Serial.print(humidity);
103+
Serial.println("}");
104+
}
105+
106+
void neoPanic() {
107+
while (1) {
108+
neopixel.fill(0xFF0000); neopixel.show(); delay(100);
109+
neopixel.fill(0x000000); neopixel.show(); delay(100);
110+
}
111+
}
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
import usb_cdc
2+
usb_cdc.enable(data=True)
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import usb_cdc
8+
import adafruit_scd4x
9+
from adafruit_bme280 import basic as adafruit_bme280
10+
import neopixel
11+
12+
#--| User Config |-----------------------------------
13+
DATA_FORMAT = "JSON" # data format, CSV or JSON
14+
DATA_RATE = 5 # data read rate in secs
15+
BEAT_COLOR = 0xADAF00 # neopixel heart beat color
16+
BEAT_RATE = 1 # neopixel heart beat rate in secs, 0=none
17+
#----------------------------------------------------
18+
19+
# check that USB CDC data has been enabled
20+
if usb_cdc.data is None:
21+
print("Need to enable USB CDC serial data in boot.py.")
22+
while True:
23+
pass
24+
25+
# setup stuff
26+
scd = adafruit_scd4x.SCD4X(board.I2C())
27+
scd.start_periodic_measurement()
28+
bme = adafruit_bme280.Adafruit_BME280_I2C(board.I2C())
29+
pixel = neopixel.NeoPixel(board.NEOPIXEL, 1)
30+
31+
# CSV output
32+
def send_csv_data(data):
33+
usb_cdc.data.write("{}, {}, {}, {}\n".format(*data).encode())
34+
35+
# JSON output
36+
def send_json_data(data):
37+
usb_cdc.data.write('{'.encode())
38+
usb_cdc.data.write('"CO2" : {},'.format(data[0]).encode())
39+
usb_cdc.data.write('"pressure" : {},'.format(data[1]).encode())
40+
usb_cdc.data.write('"temperature" : {},'.format(data[2]).encode())
41+
usb_cdc.data.write('"humidity" : {}'.format(data[3]).encode())
42+
usb_cdc.data.write('}\n'.encode())
43+
44+
# init time tracking
45+
last_data = last_beat = time.monotonic()
46+
47+
# loop forever!
48+
while True:
49+
current_time = time.monotonic()
50+
51+
# data
52+
if current_time - last_data > DATA_RATE:
53+
data = (scd.CO2, bme.pressure, bme.temperature, bme.humidity)
54+
usb_cdc.data.reset_output_buffer()
55+
if DATA_FORMAT == "CSV":
56+
send_csv_data(data)
57+
elif DATA_FORMAT == "JSON":
58+
send_json_data(data)
59+
else:
60+
usb_cdc.data.write(b"Unknown data format.\n")
61+
last_data = current_time
62+
63+
# heart beat
64+
if BEAT_RATE and current_time - last_beat > BEAT_RATE:
65+
if (pixel[0][0]):
66+
pixel.fill(0)
67+
else:
68+
pixel.fill(BEAT_COLOR)
69+
last_beat = current_time
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import serial
6+
7+
# open serial port (NOTE: change location as needed)
8+
ss = serial.Serial("/dev/ttyACM0")
9+
10+
# read string
11+
_ = ss.readline() # first read may be incomplete, just toss it
12+
raw_string = ss.readline().strip().decode()
13+
14+
# create list of floats
15+
data = [float(x) for x in raw_string.split(',')]
16+
17+
# print them
18+
print("CO2 =", data[0])
19+
print("pressure =", data[1])
20+
print("temperature =", data[2])
21+
print("humidity =", data[3])
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import json
6+
import serial
7+
8+
# open serial port (NOTE: change location as needed)
9+
ss = serial.Serial("/dev/ttyACM0")
10+
11+
# read string
12+
_ = ss.readline() # first read may be incomplete, just toss it
13+
raw_string = ss.readline().strip().decode()
14+
15+
# load JSON
16+
data = json.loads(raw_string)
17+
18+
# print data
19+
print("CO2 =", data['CO2'])
20+
print("pressure =", data['pressure'])
21+
print("temperature =", data['temperature'])
22+
print("humidity =", data['humidity'])
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# SPDX-FileCopyrightText: 2021 Carter Nelson for Adafruit Industries
2+
#
3+
# SPDX-License-Identifier: MIT
4+
5+
import time
6+
import board
7+
import adafruit_scd4x
8+
from adafruit_bme280 import basic as adafruit_bme280
9+
10+
scd = adafruit_scd4x.SCD4X(board.I2C())
11+
scd.start_periodic_measurement()
12+
13+
bme = adafruit_bme280.Adafruit_BME280_I2C(board.I2C())
14+
15+
while True:
16+
time.sleep(5)
17+
print("CO2 =", scd.CO2)
18+
print("Pressure = {:.1f} hPa".format(bme.pressure))
19+
print("Temperature = {:.1f} degC".format(bme.temperature))
20+
print("Humidity = {:.1f}%".format(bme.humidity))

0 commit comments

Comments
 (0)