summaryrefslogtreecommitdiff
path: root/mush.ino
blob: c9657a567e4333f90d5c73461d74fbbf3b498755 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
#include <DHT.h>
#include <list>
#include <numeric>
#include "ota.h"
#include <WiFiClient.h>
#include "CCS811.h"

int PHOTORESISTOR = 0;       // pin 0 is analog

#define CHECK_FREQUENCY 2      // check sensors every 2 seconds
WiFiClient *wific = 0;
int DHTPIN = 2;
int mosfet_pin = 0;
int DHTTYPE = 11;

// Tempature + Humidity Sensor
DHT dht(DHTPIN, DHTTYPE);

CCS811 ccs;

void setupCCS811()
{
  switch (ccs.begin())
    {
    case ERR_DATA_BUS:
      Serial.println("ccs: ERR_DATA_BUS");
      return;
    case ERR_IC_VERSION:
      Serial.println("ccs: ERR_IC_VERSION");
      return;
    case ERR_OK:
      Serial.println("ccs: ERR_OK");
      break;
    default:
      Serial.println("ccs: unexpected error");
      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
  Serial.println("PinMode.");
  pinMode(mosfet_pin, OUTPUT);
  pinMode(14, OUTPUT);
  Serial.println("WifiClient.");
  wific = new WiFiClient();

  setupCCS811();

}

void writeBoth(const char *buf)
{
  Serial.print(buf);
  if (wific->connected()) {
    wific->write(buf);
  }
}

const int readings_per_second = 3;
const int seconds_to_average = 5;
const size_t readings_size = seconds_to_average * readings_per_second;

struct SensorState
{
  virtual void sense() = 0;
  std::list<float> readings;
  void record(float r) {
    readings.push_back(r);
    if (readings.size() > readings_size)
      {
        readings.pop_front();
      }
  };
  float avg()
  {
    int n = 0;
    float total = std::accumulate(readings.begin(),                                 // float total = 0;
                                  readings.end(),                                   // for (auto p = readings.begin(); p != readings.end(); ++p)
                                  0.0,                                              //   {
                                  [&n](float sum, int x) {                          //     if (!isnan(*p)) {
                                    if (isnan(x)) return sum; ++n; return sum + x;  //       total += *p;
                                  });                                               //       ++n;
                                                                                    //     }
                                                                                    //   }
    return n ? total / n : NAN;
  };
};

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(ccs.getCO2PPM()); } };
struct TVOCSensor        : public SensorState { virtual void sense() { record(ccs.getTVOCPPB()); } };

TemperatureSensor  temperatureSensor;
HumiditySensor     humiditySensor;
PhotoSensor        photoSensor;
CO2Sensor          cO2Sensor;
TVOCSensor         tVOCSensor;

void log_reading()
{
  float    temp         = temperatureSensor.avg();
  float    humidity     = humiditySensor.avg();
  float    heat_index   = dht.computeHeatIndex(temp, humidity, true);
  int      photons      = photoSensor.avg();
  uint32_t co2          = cO2Sensor.avg();
  uint32_t tvoc         = tVOCSensor.avg();

  auto fmt = "T: %.2f H: %.2f HI: %.2f Light: %d CO2: %u TVOC: %u\r\n";
  char buf[500];

  snprintf(buf, sizeof(buf), fmt, temp, humidity, heat_index, photons, co2, tvoc);
  writeBoth(buf);
}

void sensor_loop()
{
  SensorState *sensors[] = { &temperatureSensor, &humiditySensor, &photoSensor, &cO2Sensor, &tVOCSensor };
  for (size_t i=0; i<sizeof(sensors)/sizeof(sensors[0]); ++i)
  {
    sensors[i]->sense();
  }
}

int seconds = 0;

void timer_switches()
{
  switch (seconds % 10)
    {
    case 0:
      analogWrite(mosfet_pin, 255);
      break;
    case 1:
      analogWrite(mosfet_pin, 0);
      break;
    case 2:
      digitalWrite(14, 1);
      delay(10);
      digitalWrite(14, 0);
      break;
    }
}

void loop()
{
  ArduinoOTA.handle();

  if (++seconds < 0)
    seconds = 0;

  timer_switches();

  auto ip = IPAddress(192,168,0,1);
  auto port = 3141;
  if (!wific->connected()) {
    //Serial.println("Attempting to connect");
    wific->connect(ip, port);
  }

  for (int i=0; i<readings_per_second; ++i)
    {
      sensor_loop();
      if (i == readings_per_second - 1)
        log_reading();
      delay(1000 / readings_per_second);
    }
}