From 6e7acffbc359e760f7c79e13d7cb9bcd2cf6e412 Mon Sep 17 00:00:00 2001 From: Andrew Cady Date: Tue, 12 Oct 2021 09:40:01 -0400 Subject: mosfet toggling --- mush.ino | 78 +++++++++++++++++++++++++++++++++++++++----------------------- wifiinfo.h | 4 ++-- 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 @@ +#include #include #include -#include #include "ota.h" #include #include "CCS811.h" -#define TEMP_SENSOR_PIN 12 -#define HUMIDITY_SENSOR_PIN 12 -#define FAN_PIN 15 -#define CO2_PIN 16 -#define ULTRASONIC_PIN 12 - -#define PHOTORESISTOR 0 // pin 0 is analog +int PHOTORESISTOR = 0; // pin 0 is analog #define CHECK_FREQUENCY 2 // check sensors every 2 seconds -#define HUMIDITY_DESIRED 75 -#define HUMIDITY_VARIATION 3 // ultrasonic turns on at (75 - 3 = 72) and off - // at (75 + 3 = 78) WiFiClient *wific = 0; +int DHTPIN = 2; +int mosfet_pin = 0; +int DHTTYPE = 11; // Tempature + Humidity Sensor -#define DHTPIN 12 -#define DHTTYPE DHT11 DHT dht(DHTPIN, DHTTYPE); -CCS811 sensor; +CCS811 ccs; + +void setupCCS811() +{ + if(!ccs.begin()) { + Serial.println("Failed to start sensor! Please check your wiring."); + return; + } + ccs.setMeasCycle(ccs.eCycle_250ms); +} void setup(void) { + Serial.begin(115200); + Serial.println("\rSerial."); + Serial.println("\rSetupWifi."); setupWifi((char *) hostname); + Serial.println("SetupOTA."); setupOTA((char *) hostname); + Serial.println("SetupCCS811."); + setupCCS811(); + Serial.println("Dht.begin."); dht.begin(); // temp+humidity sensor - pinMode(PHOTORESISTOR, INPUT); // photo resistor + Serial.println("PinMode."); + pinMode(mosfet_pin, OUTPUT); + Serial.println("WifiClient."); wific = new WiFiClient(); - Serial.begin(115200); + setupCCS811(); - /*Wait for the chip to be initialized completely, and then exit*/ - while (sensor.begin()) { - Serial.println("failed to init chip, please check if the chip connection is fine"); - delay(1000); - } - sensor.setMeasCycle(sensor.eCycle_250ms); } -void writeBoth(char *buf) +void writeBoth(const char *buf) { Serial.print(buf); if (wific->connected()) { @@ -59,11 +63,9 @@ const size_t readings_size = seconds_to_average * readings_per_second; struct SensorState { - float last_reading; virtual void sense() = 0; std::list readings; void record(float r) { - last_reading = r; readings.push_back(r); if (readings.size() > readings_size) { @@ -88,8 +90,8 @@ struct SensorState struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature(true)); } }; struct HumiditySensor : public SensorState { virtual void sense() { record(dht.readHumidity()); } }; struct PhotoSensor : public SensorState { virtual void sense() { record(analogRead(PHOTORESISTOR)); } }; -struct CO2Sensor : public SensorState { virtual void sense() { record(sensor.getCO2PPM()); } }; -struct TVOCSensor : public SensorState { virtual void sense() { record(sensor.getTVOCPPB()); } }; +struct CO2Sensor : public SensorState { virtual void sense() { record(ccs.getCO2PPM()); } }; +struct TVOCSensor : public SensorState { virtual void sense() { record(ccs.getTVOCPPB()); } }; TemperatureSensor temperatureSensor; HumiditySensor humiditySensor; @@ -120,14 +122,32 @@ void sensor_loop() { sensors[i]->sense(); } - sensor.writeBaseLine(0x847B); +} + +int fan_counter = 0; + +void fan_iterate() +{ + if (!(fan_counter % 20)) + { + analogWrite(mosfet_pin, 255); + Serial.println("mosfet -> 255"); + } + else if (!(fan_counter % 10)) + { + analogWrite(mosfet_pin, 0); + Serial.println("mosfet -> 0"); + } + ++fan_counter; } void loop() { ArduinoOTA.handle(); - auto ip = IPAddress(192,168,1,2); + fan_iterate(); + + auto ip = IPAddress(192,168,0,1); auto port = 3141; if (!wific->connected()) { //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 @@ #ifndef wifiinfo_h #define wifiinfo_h const char *hostname = "mush"; -const char* ssid = "omni"; -const char* password = "everywhere"; +const char *ssid = "304 Funston"; +const char *password = ""; #endif -- cgit v1.2.3