diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/main/main.ino | 109 |
1 files changed, 59 insertions, 50 deletions
diff --git a/src/main/main.ino b/src/main/main.ino index e29a174..6c2688f 100644 --- a/src/main/main.ino +++ b/src/main/main.ino | |||
@@ -40,7 +40,7 @@ void setup(void) | |||
40 | 40 | ||
41 | Serial.begin(115200); | 41 | Serial.begin(115200); |
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() != 0){ | 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 | } |
@@ -57,61 +57,70 @@ void setup(void) | |||
57 | sensor.setMeasCycle(sensor.eCycle_250ms); | 57 | sensor.setMeasCycle(sensor.eCycle_250ms); |
58 | } | 58 | } |
59 | 59 | ||
60 | void log_reading ( float temp_c, float humidity, float heat_index_c, int photons, uint16_t co2, uint16_t tvoc ) { | 60 | void writeBoth(char *buf) |
61 | auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %d TVOC: %d\r\n"; | 61 | { |
62 | char buf[500]; | ||
63 | snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons, co2, tvoc); | ||
64 | //Serial.printf(fmt, temp_c, humidity, heat_index_c); | ||
65 | Serial.print(buf); | 62 | Serial.print(buf); |
66 | if (wific->connected()) { | 63 | if (wific->connected()) { |
67 | wific->write(buf); | 64 | wific->write(buf); |
68 | } | 65 | } |
69 | } | 66 | } |
70 | 67 | ||
71 | void loop() { | 68 | void log_reading (float temp_c, float humidity, int photons, uint16_t co2, uint16_t tvoc) |
72 | uint16_t co2, tvoc; | 69 | { |
70 | auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %d TVOC: %d\r\n"; | ||
71 | char buf[500]; | ||
73 | 72 | ||
74 | delay(1000); | 73 | float heat_index_c = dht.computeHeatIndex(temp_c, humidity, false); |
75 | if(sensor.checkDataReady() == true){ | ||
76 | co2 = sensor.getCO2PPM(); | ||
77 | tvoc = sensor.getTVOCPPB(); | ||
78 | // Serial.print("CO2: "); | ||
79 | // Serial.print(sensor.getCO2PPM()); | ||
80 | // Serial.print("ppm, TVOC: "); | ||
81 | // Serial.print(sensor.getTVOCPPB()); | ||
82 | // Serial.println("ppb"); | ||
83 | } else { | ||
84 | Serial.println("Data is not ready!"); | ||
85 | } | ||
86 | /*! | ||
87 | * @brief Set baseline | ||
88 | * @param get from getBaseline.ino | ||
89 | */ | ||
90 | sensor.writeBaseLine(0x847B); | ||
91 | //delay cannot be less than measurement cycle | ||
92 | //delay(1000); | ||
93 | |||
94 | ArduinoOTA.handle(); | ||
95 | |||
96 | auto ip = IPAddress(192,168,1,2); | ||
97 | auto port = 3141; | ||
98 | if (!wific->connected()) { | ||
99 | Serial.println("Attempting to connect"); | ||
100 | wific->connect(ip, port); | ||
101 | } | ||
102 | |||
103 | // Wait a few seconds between measurements. | ||
104 | |||
105 | float hum = dht.readHumidity(); | ||
106 | float temp = dht.readTemperature(); | ||
107 | // Check if any reads failed and exit early (to try again). | ||
108 | if (isnan(hum) || isnan(temp) ) { | ||
109 | Serial.println(F("Failed to read from DHT sensor!")); | ||
110 | return; | ||
111 | } | ||
112 | float hic = dht.computeHeatIndex(temp, hum, false); | ||
113 | |||
114 | int photons = analogRead(PHOTORESISTOR); | ||
115 | log_reading(temp, hum, hic, photons, co2, tvoc); | ||
116 | 74 | ||
75 | snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons, co2, tvoc); | ||
76 | writeBoth(buf); | ||
117 | } | 77 | } |
78 | |||
79 | struct SensorState | ||
80 | { | ||
81 | float last_reading; | ||
82 | virtual void sense() = 0; | ||
83 | }; | ||
84 | |||
85 | struct TemperatureSensor : public SensorState { virtual void sense() { last_reading = dht.readTemperature(); } }; | ||
86 | struct HumiditySensor : public SensorState { virtual void sense() { last_reading = dht.readHumidity(); } }; | ||
87 | struct PhotoSensor : public SensorState { virtual void sense() { last_reading = analogRead(PHOTORESISTOR); } }; | ||
88 | struct CO2Sensor : public SensorState { virtual void sense() { last_reading = sensor.getCO2PPM(); } }; | ||
89 | struct TVOCSensor : public SensorState { virtual void sense() { last_reading = sensor.getTVOCPPB(); } }; | ||
90 | |||
91 | TemperatureSensor temperatureSensor; | ||
92 | HumiditySensor humiditySensor; | ||
93 | PhotoSensor photoSensor; | ||
94 | CO2Sensor cO2Sensor; | ||
95 | TVOCSensor tVOCSensor; | ||
96 | |||
97 | SensorState *sensors[] = { &temperatureSensor, &humiditySensor, &photoSensor, &cO2Sensor, &tVOCSensor }; | ||
98 | |||
99 | void sensor_loop() | ||
100 | { | ||
101 | for (int i=0; i<sizeof(sensors)/sizeof(sensors[0]); ++i) | ||
102 | { | ||
103 | sensors[i]->sense(); | ||
104 | } | ||
105 | sensor.writeBaseLine(0x847B); | ||
106 | } | ||
107 | |||
108 | void loop() | ||
109 | { | ||
110 | ArduinoOTA.handle(); | ||
111 | |||
112 | auto ip = IPAddress(192,168,1,2); | ||
113 | auto port = 3141; | ||
114 | if (!wific->connected()) { | ||
115 | Serial.println("Attempting to connect"); | ||
116 | wific->connect(ip, port); | ||
117 | } | ||
118 | |||
119 | sensor_loop(); | ||
120 | |||
121 | log_reading(temperatureSensor.last_reading, humiditySensor.last_reading, photoSensor.last_reading, cO2Sensor.last_reading, tVOCSensor.last_reading); | ||
122 | |||
123 | delay(1000); | ||
124 | } | ||
125 | |||
126 | |||