diff options
Diffstat (limited to 'src/main/main.ino')
-rw-r--r-- | src/main/main.ino | 130 |
1 files changed, 78 insertions, 52 deletions
diff --git a/src/main/main.ino b/src/main/main.ino index a435473..e29a174 100644 --- a/src/main/main.ino +++ b/src/main/main.ino | |||
@@ -1,3 +1,8 @@ | |||
1 | #include <DHT.h> | ||
2 | #include "ota.h" | ||
3 | #include <WiFiClient.h> | ||
4 | #include "CCS811.h" | ||
5 | |||
1 | #define TEMP_SENSOR_PIN 12 | 6 | #define TEMP_SENSOR_PIN 12 |
2 | #define HUMIDITY_SENSOR_PIN 12 | 7 | #define HUMIDITY_SENSOR_PIN 12 |
3 | #define FAN_PIN 15 | 8 | #define FAN_PIN 15 |
@@ -10,35 +15,52 @@ | |||
10 | #define HUMIDITY_DESIRED 75 | 15 | #define HUMIDITY_DESIRED 75 |
11 | #define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off | 16 | #define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off |
12 | // at (75 + 3 = 78) | 17 | // at (75 + 3 = 78) |
13 | |||
14 | #include "ota.h" | ||
15 | |||
16 | #include <WiFiClient.h> | ||
17 | WiFiClient *wific = 0; | 18 | WiFiClient *wific = 0; |
18 | 19 | ||
19 | // Tempature + Humidity Sensor | 20 | // Tempature + Humidity Sensor |
20 | #include <DHT.h> | ||
21 | #define DHTPIN 12 | 21 | #define DHTPIN 12 |
22 | #define DHTTYPE DHT11 | 22 | #define DHTTYPE DHT11 |
23 | DHT dht(DHTPIN, DHTTYPE); | 23 | DHT dht(DHTPIN, DHTTYPE); |
24 | 24 | ||
25 | /* | ||
26 | * IIC address default 0x5A, the address becomes 0x5B if the ADDR_SEL is soldered. | ||
27 | */ | ||
28 | //CCS811 sensor(&Wire, /*IIC_ADDRESS=*/0x5A); | ||
29 | CCS811 sensor; | ||
25 | 30 | ||
26 | void setup () { | 31 | void setup(void) |
27 | Serial.begin(115200); | 32 | { |
28 | Serial.println(F("Mushing...")); | ||
29 | |||
30 | setupWifi((char *) hostname); | 33 | setupWifi((char *) hostname); |
31 | setupOTA((char *) hostname); | 34 | setupOTA((char *) hostname); |
32 | 35 | ||
33 | dht.begin(); // temp+humidity sensor | 36 | dht.begin(); // temp+humidity sensor |
34 | pinMode(PHOTORESISTOR, INPUT); // photo resistor | 37 | pinMode(PHOTORESISTOR, INPUT); // photo resistor |
35 | wific = new WiFiClient(); | 38 | wific = new WiFiClient(); |
39 | |||
40 | |||
41 | Serial.begin(115200); | ||
42 | /*Wait for the chip to be initialized completely, and then exit*/ | ||
43 | while(sensor.begin() != 0){ | ||
44 | Serial.println("failed to init chip, please check if the chip connection is fine"); | ||
45 | delay(1000); | ||
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); | ||
36 | } | 58 | } |
37 | 59 | ||
38 | void log_reading ( float temp_c, float humidity, float heat_index_c, int photons ) { | 60 | void log_reading ( float temp_c, float humidity, float heat_index_c, int photons, uint16_t co2, uint16_t tvoc ) { |
39 | auto fmt = "T: %.2f H: %.2f HI: %.2f LIGHT: %d\r\n"; | 61 | auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %d TVOC: %d\r\n"; |
40 | char buf[500]; | 62 | char buf[500]; |
41 | snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons); | 63 | snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons, co2, tvoc); |
42 | //Serial.printf(fmt, temp_c, humidity, heat_index_c); | 64 | //Serial.printf(fmt, temp_c, humidity, heat_index_c); |
43 | Serial.print(buf); | 65 | Serial.print(buf); |
44 | if (wific->connected()) { | 66 | if (wific->connected()) { |
@@ -46,46 +68,50 @@ void log_reading ( float temp_c, float humidity, float heat_index_c, int photons | |||
46 | } | 68 | } |
47 | } | 69 | } |
48 | 70 | ||
49 | /* | ||
50 | Global state variables to hold rolling average humidity, temperature, etc sensor values | ||
51 | Override state variable is set by network command | ||
52 | |||
53 | Need max and min threshhold values for each sensor too | ||
54 | |||
55 | struct SensorState | ||
56 | { | ||
57 | float rolling_average; | ||
58 | float threshold_top; | ||
59 | float threshold_bottom; | ||
60 | void *top_exceeded_callback(); | ||
61 | float force_value; | ||
62 | } | ||
63 | */ | ||
64 | |||
65 | void loop() { | 71 | void loop() { |
66 | ArduinoOTA.handle(); | 72 | uint16_t co2, tvoc; |
73 | |||
74 | delay(1000); | ||
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); | ||
67 | 116 | ||
68 | |||
69 | auto ip = IPAddress(192,168,1,2); | ||
70 | auto port = 3141; | ||
71 | if (!wific->connected()) { | ||
72 | Serial.println("Attempting to connect"); | ||
73 | wific->connect(ip, port); | ||
74 | } | ||
75 | |||
76 | // Wait a few seconds between measurements. | ||
77 | delay(2000); | ||
78 | |||
79 | float h = dht.readHumidity(); | ||
80 | float t = dht.readTemperature(); | ||
81 | // Check if any reads failed and exit early (to try again). | ||
82 | if (isnan(h) || isnan(t) ) { | ||
83 | Serial.println(F("Failed to read from DHT sensor!")); | ||
84 | return; | ||
85 | } | ||
86 | float hic = dht.computeHeatIndex(t, h, false); | ||
87 | |||
88 | int photons = analogRead(PHOTORESISTOR); | ||
89 | log_reading(t, h, hic, photons); | ||
90 | } | 117 | } |
91 | |||