-
Notifications
You must be signed in to change notification settings - Fork 0
/
gecko-tank.ino
132 lines (106 loc) · 3.37 KB
/
gecko-tank.ino
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
/*
Gecko tank monitor.
Monitors one dht22 and an LDR (light dependent resistor).
Updates oled temp and humidity display every second.
Posts sensor values to Thingspeak every 5 minutes.
Requirements:
- NodeMCU V2.0
- Thingspeak channel
- dht22 sensor and extension cable to reach from the circuit to inside the terrarium.
- LDR (light dependent resistor) and 10K ohm resistor
- NodeMcu Pinout
D0 <-> DHT22 cool side
D1 <-> DHT22 hot side
D2 <-> oled SDA
D2 <-> oled SCA
A0 <-> light circuit
Light circuit
The following all connect in one row:
Photocell, other end to v3.3 (measurement)
10k resistor to GND (pulldown)
Wire to A0 (signal)
*/
// Community libraries.
#include <ESP8266WiFi.h>
#include "ThingSpeak.h"
// Application header files
#include "communications.h"
#include "dht22.h"
#include "light.h"
#include "oled.h"
// Seconds between data posts to thingSpeak, this is 5 minutes.
unsigned long THINGSPEAK_POST_FREQUENCY_SECONDS = 300;
// For tracking loops between posts.
unsigned long thingspeakCountdown;
// Global variables.
WiFiClient client;
Dht dhtHotSide;
Dht dhtCoolSide;
LightSensor light;
/**
Run once at the beginning of the application.
Initialize the objects and variables, connect to serial monitor and wifi.
*/
void setup() {
Serial.begin(115200);
dhtCoolSide.sensorPin(D0);
dhtHotSide.sensorPin(D1);
light.sensorPin(A0);
ThingSpeak.begin(client);
oled_setup();
drawText("Connecting to Wifi", "");
while (!connect_wifi()) {
Serial.println("Failed to connect to wifi, retrying in 1 second");
delay(1000);
}
thingspeakCountdown = THINGSPEAK_POST_FREQUENCY_SECONDS;
}
/**
Main program loop.
Gather data, update displays, maybe post data, and then delay for a second.
*/
void loop() {
/* Gather and store all sensor values */
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
dhtCoolSide.takeMeasurements();
dhtHotSide.takeMeasurements();
float CoolSide_temp_f = dhtCoolSide.tempF();
float CoolSide_humid_f = dhtCoolSide.humidity();
float HotSide_temp_f = dhtHotSide.tempF();
float HotSide_humid_f = dhtHotSide.humidity();
float rssi = get_rssi();
int lux = light.percent();
/* Send data to the serial monitor */
char serial_update[100];
sprintf(serial_update,"HotSide=%0.1ff %0.1f%% CoolSide=%0.1ff %0.1f%% lux=%d rssi=%0.1f",
HotSide_temp_f,
HotSide_humid_f,
CoolSide_temp_f,
CoolSide_humid_f,
lux,
rssi
);
Serial.println(serial_update);
/* Send data to the oled */
char hot_text[100];
sprintf(hot_text, "H %0.1ff %0.1f%%", HotSide_temp_f, HotSide_humid_f);
char cool_text[100];
sprintf(cool_text, "C %0.1ff %0.1f%%", CoolSide_temp_f, CoolSide_humid_f);
char title[100];
sprintf(title, "lux %d", lux);
drawText(title, hot_text, cool_text);
// Send data to Thingspeak for storage when it is time.
if (0 == thingspeakCountdown--) {
ThingSpeak.setField(1, HotSide_temp_f);
ThingSpeak.setField(2, HotSide_humid_f);
ThingSpeak.setField(3, CoolSide_temp_f);
ThingSpeak.setField(4, CoolSide_humid_f);
ThingSpeak.setField(5, lux);
ThingSpeak.setField(6, rssi);
drawText("Transmitting...", "");
thingspeakPostData();
thingspeakCountdown = THINGSPEAK_POST_FREQUENCY_SECONDS;
}
delay(1000);
}