diff options
author | Andrew Cady <d@jerkface.net> | 2021-10-12 09:40:01 -0400 |
---|---|---|
committer | Andrew Cady <d@jerkface.net> | 2021-10-12 09:40:01 -0400 |
commit | 6e7acffbc359e760f7c79e13d7cb9bcd2cf6e412 (patch) | |
tree | 2fb8f1332b48178757c72005cd17189daafc6fe1 | |
parent | 3626ad2442bb38fda9fa28492f7f1a3e861fcbd2 (diff) |
mosfet toggling
-rw-r--r-- | mush.ino | 78 | ||||
-rw-r--r-- | wifiinfo.h | 4 |
2 files changed, 51 insertions, 31 deletions
@@ -1,51 +1,55 @@ | |||
1 | #include <DHT.h> | ||
1 | #include <list> | 2 | #include <list> |
2 | #include <numeric> | 3 | #include <numeric> |
3 | #include <DHT.h> | ||
4 | #include "ota.h" | 4 | #include "ota.h" |
5 | #include <WiFiClient.h> | 5 | #include <WiFiClient.h> |
6 | #include "CCS811.h" | 6 | #include "CCS811.h" |
7 | 7 | ||
8 | #define TEMP_SENSOR_PIN 12 | 8 | int PHOTORESISTOR = 0; // pin 0 is analog |
9 | #define HUMIDITY_SENSOR_PIN 12 | ||
10 | #define FAN_PIN 15 | ||
11 | #define CO2_PIN 16 | ||
12 | #define ULTRASONIC_PIN 12 | ||
13 | |||
14 | #define PHOTORESISTOR 0 // pin 0 is analog | ||
15 | 9 | ||
16 | #define CHECK_FREQUENCY 2 // check sensors every 2 seconds | 10 | #define CHECK_FREQUENCY 2 // check sensors every 2 seconds |
17 | #define HUMIDITY_DESIRED 75 | ||
18 | #define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off | ||
19 | // at (75 + 3 = 78) | ||
20 | WiFiClient *wific = 0; | 11 | WiFiClient *wific = 0; |
12 | int DHTPIN = 2; | ||
13 | int mosfet_pin = 0; | ||
14 | int DHTTYPE = 11; | ||
21 | 15 | ||
22 | // Tempature + Humidity Sensor | 16 | // Tempature + Humidity Sensor |
23 | #define DHTPIN 12 | ||
24 | #define DHTTYPE DHT11 | ||
25 | DHT dht(DHTPIN, DHTTYPE); | 17 | DHT dht(DHTPIN, DHTTYPE); |
26 | 18 | ||
27 | CCS811 sensor; | 19 | CCS811 ccs; |
20 | |||
21 | void setupCCS811() | ||
22 | { | ||
23 | if(!ccs.begin()) { | ||
24 | Serial.println("Failed to start sensor! Please check your wiring."); | ||
25 | return; | ||
26 | } | ||
27 | ccs.setMeasCycle(ccs.eCycle_250ms); | ||
28 | } | ||
28 | 29 | ||
29 | void setup(void) | 30 | void setup(void) |
30 | { | 31 | { |
32 | Serial.begin(115200); | ||
33 | Serial.println("\rSerial."); | ||
34 | Serial.println("\rSetupWifi."); | ||
31 | setupWifi((char *) hostname); | 35 | setupWifi((char *) hostname); |
36 | Serial.println("SetupOTA."); | ||
32 | setupOTA((char *) hostname); | 37 | setupOTA((char *) hostname); |
38 | Serial.println("SetupCCS811."); | ||
39 | setupCCS811(); | ||
33 | 40 | ||
41 | Serial.println("Dht.begin."); | ||
34 | dht.begin(); // temp+humidity sensor | 42 | dht.begin(); // temp+humidity sensor |
35 | pinMode(PHOTORESISTOR, INPUT); // photo resistor | 43 | Serial.println("PinMode."); |
44 | pinMode(mosfet_pin, OUTPUT); | ||
45 | Serial.println("WifiClient."); | ||
36 | wific = new WiFiClient(); | 46 | wific = new WiFiClient(); |
37 | 47 | ||
38 | Serial.begin(115200); | 48 | setupCCS811(); |
39 | 49 | ||
40 | /*Wait for the chip to be initialized completely, and then exit*/ | ||
41 | while (sensor.begin()) { | ||
42 | Serial.println("failed to init chip, please check if the chip connection is fine"); | ||
43 | delay(1000); | ||
44 | } | ||
45 | sensor.setMeasCycle(sensor.eCycle_250ms); | ||
46 | } | 50 | } |
47 | 51 | ||
48 | void writeBoth(char *buf) | 52 | void writeBoth(const char *buf) |
49 | { | 53 | { |
50 | Serial.print(buf); | 54 | Serial.print(buf); |
51 | if (wific->connected()) { | 55 | if (wific->connected()) { |
@@ -59,11 +63,9 @@ const size_t readings_size = seconds_to_average * readings_per_second; | |||
59 | 63 | ||
60 | struct SensorState | 64 | struct SensorState |
61 | { | 65 | { |
62 | float last_reading; | ||
63 | virtual void sense() = 0; | 66 | virtual void sense() = 0; |
64 | std::list<float> readings; | 67 | std::list<float> readings; |
65 | void record(float r) { | 68 | void record(float r) { |
66 | last_reading = r; | ||
67 | readings.push_back(r); | 69 | readings.push_back(r); |
68 | if (readings.size() > readings_size) | 70 | if (readings.size() > readings_size) |
69 | { | 71 | { |
@@ -88,8 +90,8 @@ struct SensorState | |||
88 | struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature(true)); } }; | 90 | struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature(true)); } }; |
89 | struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } }; | 91 | struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } }; |
90 | struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } }; | 92 | struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } }; |
91 | struct CO2Sensor : public SensorState { virtual void sense() { record(sensor.getCO2PPM()); } }; | 93 | struct CO2Sensor : public SensorState { virtual void sense() { record(ccs.getCO2PPM()); } }; |
92 | struct TVOCSensor : public SensorState { virtual void sense() { record(sensor.getTVOCPPB()); } }; | 94 | struct TVOCSensor : public SensorState { virtual void sense() { record(ccs.getTVOCPPB()); } }; |
93 | 95 | ||
94 | TemperatureSensor temperatureSensor; | 96 | TemperatureSensor temperatureSensor; |
95 | HumiditySensor humiditySensor; | 97 | HumiditySensor humiditySensor; |
@@ -120,14 +122,32 @@ void sensor_loop() | |||
120 | { | 122 | { |
121 | sensors[i]->sense(); | 123 | sensors[i]->sense(); |
122 | } | 124 | } |
123 | sensor.writeBaseLine(0x847B); | 125 | } |
126 | |||
127 | int fan_counter = 0; | ||
128 | |||
129 | void fan_iterate() | ||
130 | { | ||
131 | if (!(fan_counter % 20)) | ||
132 | { | ||
133 | analogWrite(mosfet_pin, 255); | ||
134 | Serial.println("mosfet -> 255"); | ||
135 | } | ||
136 | else if (!(fan_counter % 10)) | ||
137 | { | ||
138 | analogWrite(mosfet_pin, 0); | ||
139 | Serial.println("mosfet -> 0"); | ||
140 | } | ||
141 | ++fan_counter; | ||
124 | } | 142 | } |
125 | 143 | ||
126 | void loop() | 144 | void loop() |
127 | { | 145 | { |
128 | ArduinoOTA.handle(); | 146 | ArduinoOTA.handle(); |
129 | 147 | ||
130 | auto ip = IPAddress(192,168,1,2); | 148 | fan_iterate(); |
149 | |||
150 | auto ip = IPAddress(192,168,0,1); | ||
131 | auto port = 3141; | 151 | auto port = 3141; |
132 | if (!wific->connected()) { | 152 | if (!wific->connected()) { |
133 | //Serial.println("Attempting to connect"); | 153 | //Serial.println("Attempting to connect"); |
@@ -1,6 +1,6 @@ | |||
1 | #ifndef wifiinfo_h | 1 | #ifndef wifiinfo_h |
2 | #define wifiinfo_h | 2 | #define wifiinfo_h |
3 | const char *hostname = "mush"; | 3 | const char *hostname = "mush"; |
4 | const char* ssid = "omni"; | 4 | const char *ssid = "304 Funston"; |
5 | const char* password = "everywhere"; | 5 | const char *password = ""; |
6 | #endif | 6 | #endif |