summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAndrew Cady <d@jerkface.net>2021-10-12 09:40:01 -0400
committerAndrew Cady <d@jerkface.net>2021-10-12 09:40:01 -0400
commit6e7acffbc359e760f7c79e13d7cb9bcd2cf6e412 (patch)
tree2fb8f1332b48178757c72005cd17189daafc6fe1
parent3626ad2442bb38fda9fa28492f7f1a3e861fcbd2 (diff)
mosfet toggling
-rw-r--r--mush.ino78
-rw-r--r--wifiinfo.h4
2 files changed, 51 insertions, 31 deletions
diff --git a/mush.ino b/mush.ino
index 4ce6ef9..53e1f1d 100644
--- a/mush.ino
+++ b/mush.ino
@@ -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 8int 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)
20WiFiClient *wific = 0; 11WiFiClient *wific = 0;
12int DHTPIN = 2;
13int mosfet_pin = 0;
14int DHTTYPE = 11;
21 15
22// Tempature + Humidity Sensor 16// Tempature + Humidity Sensor
23#define DHTPIN 12
24#define DHTTYPE DHT11
25DHT dht(DHTPIN, DHTTYPE); 17DHT dht(DHTPIN, DHTTYPE);
26 18
27CCS811 sensor; 19CCS811 ccs;
20
21void 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
29void setup(void) 30void 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
48void writeBoth(char *buf) 52void 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
60struct SensorState 64struct 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
88struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature(true)); } }; 90struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature(true)); } };
89struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } }; 91struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } };
90struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } }; 92struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } };
91struct CO2Sensor : public SensorState { virtual void sense() { record(sensor.getCO2PPM()); } }; 93struct CO2Sensor : public SensorState { virtual void sense() { record(ccs.getCO2PPM()); } };
92struct TVOCSensor : public SensorState { virtual void sense() { record(sensor.getTVOCPPB()); } }; 94struct TVOCSensor : public SensorState { virtual void sense() { record(ccs.getTVOCPPB()); } };
93 95
94TemperatureSensor temperatureSensor; 96TemperatureSensor temperatureSensor;
95HumiditySensor humiditySensor; 97HumiditySensor 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
127int fan_counter = 0;
128
129void 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
126void loop() 144void 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");
diff --git a/wifiinfo.h b/wifiinfo.h
index fb94cdd..ae122b9 100644
--- a/wifiinfo.h
+++ b/wifiinfo.h
@@ -1,6 +1,6 @@
1#ifndef wifiinfo_h 1#ifndef wifiinfo_h
2#define wifiinfo_h 2#define wifiinfo_h
3const char *hostname = "mush"; 3const char *hostname = "mush";
4const char* ssid = "omni"; 4const char *ssid = "304 Funston";
5const char* password = "everywhere"; 5const char *password = "";
6#endif 6#endif