diff options
Diffstat (limited to 'mush.ino')
-rw-r--r-- | mush.ino | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/mush.ino b/mush.ino new file mode 100644 index 0000000..6c2688f --- /dev/null +++ b/mush.ino | |||
@@ -0,0 +1,126 @@ | |||
1 | #include <DHT.h> | ||
2 | #include "ota.h" | ||
3 | #include <WiFiClient.h> | ||
4 | #include "CCS811.h" | ||
5 | |||
6 | #define TEMP_SENSOR_PIN 12 | ||
7 | #define HUMIDITY_SENSOR_PIN 12 | ||
8 | #define FAN_PIN 15 | ||
9 | #define CO2_PIN 16 | ||
10 | #define ULTRASONIC_PIN 12 | ||
11 | |||
12 | #define PHOTORESISTOR 0 // pin 0 is analog | ||
13 | |||
14 | #define CHECK_FREQUENCY 2 // check sensors every 2 seconds | ||
15 | #define HUMIDITY_DESIRED 75 | ||
16 | #define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off | ||
17 | // at (75 + 3 = 78) | ||
18 | WiFiClient *wific = 0; | ||
19 | |||
20 | // Tempature + Humidity Sensor | ||
21 | #define DHTPIN 12 | ||
22 | #define DHTTYPE DHT11 | ||
23 | DHT dht(DHTPIN, DHTTYPE); | ||
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; | ||
30 | |||
31 | void setup(void) | ||
32 | { | ||
33 | setupWifi((char *) hostname); | ||
34 | setupOTA((char *) hostname); | ||
35 | |||
36 | dht.begin(); // temp+humidity sensor | ||
37 | pinMode(PHOTORESISTOR, INPUT); // photo resistor | ||
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()) { | ||
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); | ||
58 | } | ||
59 | |||
60 | void writeBoth(char *buf) | ||
61 | { | ||
62 | Serial.print(buf); | ||
63 | if (wific->connected()) { | ||
64 | wific->write(buf); | ||
65 | } | ||
66 | } | ||
67 | |||
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 | ||
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 | |||