summaryrefslogtreecommitdiff
path: root/actual_board_code/actual_board_code.ino
blob: a080a9b986b0c061c21de94911a7bd3c07bed80b (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

// REQUIRES the following Arduino libraries:
// - DHT Sensor Library: https://github.com/adafruit/DHT-sensor-library
// - Adafruit Unified Sensor Lib: https://github.com/adafruit/Adafruit_Sensor

#include "DHT.h"

// CONFIGURATION

// DHT PINS

#define DHTPIN 4  // Digital pin connected to the DHT sensor
// Feather HUZZAH ESP8266 note: use pins 3, 4, 5, 12, 13 or 14

// Uncomment whatever type you're using
//#define DHTTYPE DHT11   // DHT 11
#define DHTTYPE DHT11   // DHT 22  (AM2302), AM2321
//#define DHTTYPE DHT21   // DHT 21 (AM2301)

// DSM PINS

#define DSM501 18  // Just leave it as is

// END PIN CONFIGURATION SECTION


// SOME VARS

unsigned long duration;
unsigned long starttime;
unsigned long endtime;
unsigned long sampletime_ms = 1000;
unsigned long lowpulseoccupancy = 0;
float ratio = 0;
float concentration = 0;
uint32_t delayMS = 1000; // in loop delay() explicited


// Initialize DHT Sensor. The current DHT reading algorithm adjusts itself to work on faster procs. (rtfm --> docs)
DHT dht(DHTPIN, DHTTYPE);


void setup() { // Setup code that gets run regardless
  Serial.begin(9600);

  Serial.println(F("----- DHT + MQ + DSM Interface -----"));

  dht.begin(); // Begin DHT System at startup
  
  pinMode(DSM501,INPUT); // Initialize DSM501A Sensor
  
  starttime = millis(); 
}

void loop() { // Main code
  // Wait a few seconds between measurements.
  delay(delayMS);

  // vvv DHT MEASUREMENTS SECTION vvv //

  // Reading temperature or humidity takes about 250 milliseconds!
  // Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
  float h = dht.readHumidity();
  // Read temperature as Celsius (the default)
  float t = dht.readTemperature();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(h) || isnan(t) || isnan(f)) {
    Serial.println(F("Failed to read from DHT sensor!"));
    return;
  }

  // Compute heat index in Fahrenheit (the default)
  float hif = dht.computeHeatIndex(f, h);
  // Compute heat index in Celsius (isFahreheit = false)
  float hic = dht.computeHeatIndex(t, h, false);

  Serial.print(F("Humidity: "));
  Serial.print(h);
  Serial.print(F("%  Temperature: "));
  Serial.print(t);
  Serial.print(F("°C "));
  Serial.print(f);
  Serial.print(F("°F  Heat index: "));
  Serial.print(hic);
  Serial.print(F("°C "));
  Serial.print(hif);
  Serial.println(F("°F"));

  // vvv MQ135 MEASUREMENTS SECTION vvv //

  Serial.println(analogRead(15)); // MQ135 Sensor - ppm metrics (raw)
  Serial.println("ppm")

 // vvv DSM501A MEASUREMENTS SECTION vvv //

  duration = pulseIn(DSM501, LOW);
  lowpulseoccupancy += duration;
  endtime = millis();

  ratio = (lowpulseoccupancy-endtime+starttime + sampletime_ms)/(sampletime_ms*10.0);
  concentration = 1.1*pow(ratio,3)-3.8*pow(ratio,2)+520*ratio+0.62; // idk what s going on in here anymore.. it used to work, it doesn t now..
  // concentration = 0.001915 * pow(low_ratio, 2) + 0.09522 * low_ratio - 0.04884;
  Serial.print("lowpulseoccupancy: ");
  Serial.print(lowpulseoccupancy);
  Serial.print("    ratio: ");
  Serial.print(ratio);
  Serial.print("    DSM501A: ");
  Serial.println(concentration);

  lowpulseoccupancy = 0;
  starttime = millis();
}