diff options
author | Steven <steven.vasilogianis@gmail.com> | 2021-10-06 01:20:51 -0400 |
---|---|---|
committer | Steven <steven.vasilogianis@gmail.com> | 2021-10-06 01:20:51 -0400 |
commit | 9e5442ed88d34ab6a875ff9468decf9f9f126043 (patch) | |
tree | 36958dedffa14511e70a2c0b4700b033ebe52ee4 | |
parent | 4d5c9d93fb633086f3dfe7e3a51c3c23d1c26150 (diff) |
cleaner
-rw-r--r-- | mush.ino | 43 |
1 files changed, 19 insertions, 24 deletions
@@ -37,23 +37,13 @@ void setup(void) | |||
37 | pinMode(PHOTORESISTOR, INPUT); // photo resistor | 37 | pinMode(PHOTORESISTOR, INPUT); // photo resistor |
38 | wific = new WiFiClient(); | 38 | wific = new WiFiClient(); |
39 | 39 | ||
40 | |||
41 | Serial.begin(115200); | 40 | Serial.begin(115200); |
41 | |||
42 | /*Wait for the chip to be initialized completely, and then exit*/ | 42 | /*Wait for the chip to be initialized completely, and then exit*/ |
43 | while (sensor.begin()) { | 43 | while (sensor.begin()) { |
44 | Serial.println("failed to init chip, please check if the chip connection is fine"); | 44 | Serial.println("failed to init chip, please check if the chip connection is fine"); |
45 | delay(1000); | 45 | delay(1000); |
46 | } | 46 | } |
47 | /** | ||
48 | * @brief Set measurement cycle | ||
49 | * @param cycle:in typedef enum{ | ||
50 | * eClosed, //Idle (Measurements are disabled in this mode) | ||
51 | * eCycle_1s, //Constant power mode, IAQ measurement every second | ||
52 | * Ecycle_10s, //Pulse heating mode IAQ measurement every 10 seconds | ||
53 | * eCycle_60s, //Low power pulse heating mode IAQ measurement every 60 seconds | ||
54 | * eCycle_250ms //Constant power mode, sensor measurement every 250ms | ||
55 | * }eCycle_t; | ||
56 | */ | ||
57 | sensor.setMeasCycle(sensor.eCycle_250ms); | 47 | sensor.setMeasCycle(sensor.eCycle_250ms); |
58 | } | 48 | } |
59 | 49 | ||
@@ -65,17 +55,6 @@ void writeBoth(char *buf) | |||
65 | } | 55 | } |
66 | } | 56 | } |
67 | 57 | ||
68 | void log_reading (float temp_c, float humidity, int photons, uint16_t co2, uint16_t tvoc) | ||
69 | { | ||
70 | auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %d TVOC: %d\r\n"; | ||
71 | char buf[500]; | ||
72 | |||
73 | float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); | ||
74 | |||
75 | snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons, co2, tvoc); | ||
76 | writeBoth(buf); | ||
77 | } | ||
78 | |||
79 | struct SensorState | 58 | struct SensorState |
80 | { | 59 | { |
81 | float last_reading; | 60 | float last_reading; |
@@ -94,10 +73,26 @@ PhotoSensor photoSensor; | |||
94 | CO2Sensor cO2Sensor; | 73 | CO2Sensor cO2Sensor; |
95 | TVOCSensor tVOCSensor; | 74 | TVOCSensor tVOCSensor; |
96 | 75 | ||
97 | SensorState *sensors[] = { &temperatureSensor, &humiditySensor, &photoSensor, &cO2Sensor, &tVOCSensor }; | 76 | void log_reading() |
77 | { | ||
78 | float temp_c = temperatureSensor.last_reading; | ||
79 | float humidity = humiditySensor.last_reading; | ||
80 | float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); | ||
81 | int photons = photoSensor.last_reading; | ||
82 | uint32_t co2 = cO2Sensor.last_reading; | ||
83 | uint32_t tvoc = tVOCSensor.last_reading; | ||
84 | |||
85 | auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %u TVOC: %u\r\n"; | ||
86 | char buf[500]; | ||
87 | |||
88 | |||
89 | snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons, co2, tvoc); | ||
90 | writeBoth(buf); | ||
91 | } | ||
98 | 92 | ||
99 | void sensor_loop() | 93 | void sensor_loop() |
100 | { | 94 | { |
95 | SensorState *sensors[] = { &temperatureSensor, &humiditySensor, &photoSensor, &cO2Sensor, &tVOCSensor }; | ||
101 | for (size_t i=0; i<sizeof(sensors)/sizeof(sensors[0]); ++i) | 96 | for (size_t i=0; i<sizeof(sensors)/sizeof(sensors[0]); ++i) |
102 | { | 97 | { |
103 | sensors[i]->sense(); | 98 | sensors[i]->sense(); |
@@ -118,7 +113,7 @@ void loop() | |||
118 | 113 | ||
119 | sensor_loop(); | 114 | sensor_loop(); |
120 | 115 | ||
121 | log_reading(temperatureSensor.last_reading, humiditySensor.last_reading, photoSensor.last_reading, cO2Sensor.last_reading, tVOCSensor.last_reading); | 116 | log_reading(); |
122 | 117 | ||
123 | delay(1000); | 118 | delay(1000); |
124 | } | 119 | } |