summaryrefslogtreecommitdiff
path: root/mush.ino
blob: 440042059f28bbcfeefd772302b288bec2927424 (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
#include <list>
#include <numeric>
#include <DHT.h>
#include "ota.h"
#include <WiFiClient.h>
#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

#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;

// Tempature + Humidity Sensor
#define DHTPIN 12
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);

/*
 * IIC address default 0x5A, the address becomes 0x5B if the ADDR_SEL is soldered.
 */
//CCS811 sensor(&Wire, /*IIC_ADDRESS=*/0x5A);
CCS811 sensor;

void setup(void)
{
  setupWifi((char *) hostname);
  setupOTA((char *) hostname);

  dht.begin();                    // temp+humidity sensor
  pinMode(PHOTORESISTOR, INPUT);  // photo resistor
  wific = new WiFiClient();

  Serial.begin(115200);

  /*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)
{
  Serial.print(buf);
  if (wific->connected()) {
    wific->write(buf);
  }
}

struct SensorState
{
  float last_reading;
  virtual void sense() = 0;
  std::list<float> readings;
  void record(float r) {
    last_reading = r;
    readings.push_back(r);
    if (readings.size() > 12)
      {
        readings.pop_front();
      }
  };
  float avg()
  {
    return std::accumulate(readings.begin(), readings.end(), 0.0) / readings.size();
  }

};

struct TemperatureSensor : public SensorState { virtual void sense() { record(dht.readTemperature()); } };
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()); } };

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

void log_reading()
{
  float    temp_c       = temperatureSensor.last_reading;
  float    humidity     = humiditySensor.last_reading;
  float    heat_index_c = dht.computeHeatIndex(temp_c, humidity, false);
  int      photons      = photoSensor.avg();
  uint32_t co2          = cO2Sensor.last_reading;
  uint32_t tvoc         = tVOCSensor.last_reading;

  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_c, humidity, heat_index_c, 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();
  }
  sensor.writeBaseLine(0x847B);
}

void loop()
{
  ArduinoOTA.handle();

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

  sensor_loop();

  log_reading();

  delay(1000);
}