summaryrefslogtreecommitdiff
path: root/src/main/main.ino
blob: a435473ba101cca21e0b764f970ba9639fa63fcf (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
#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)

#include "ota.h"

#include <WiFiClient.h>
WiFiClient *wific = 0;

// Tempature + Humidity Sensor
#include <DHT.h>
#define DHTPIN 12
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);


void setup () {
  Serial.begin(115200);
  Serial.println(F("Mushing..."));

  setupWifi((char *) hostname);
  setupOTA((char *) hostname);

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

void log_reading ( float temp_c, float humidity, float heat_index_c, int photons ) {
  auto fmt = "T: %.2f H: %.2f HI: %.2f LIGHT: %d\r\n";
  char buf[500];
  snprintf(buf, sizeof(buf), fmt, temp_c, humidity, heat_index_c, photons);
  //Serial.printf(fmt, temp_c, humidity, heat_index_c);
  Serial.print(buf);
  if (wific->connected()) {
    wific->write(buf);
  }
}

/*
Global state variables to hold rolling average humidity, temperature, etc sensor values
Override state variable is set by network command

Need max and min threshhold values for each sensor too

struct SensorState
{
  float rolling_average;
  float threshold_top;
  float threshold_bottom;
  void *top_exceeded_callback();
  float force_value;
}
*/

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);
  }

  // Wait a few seconds between measurements.
  delay(2000);

  float h = dht.readHumidity();
  float t = dht.readTemperature();
  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) ) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }
  float hic = dht.computeHeatIndex(t, h, false);

  int photons = analogRead(PHOTORESISTOR);
  log_reading(t, h, hic, photons);
}