-
Notifications
You must be signed in to change notification settings - Fork 39
/
DeepSleepDHT22.ino
218 lines (155 loc) · 4.24 KB
/
DeepSleepDHT22.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
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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <DNSServer.h>
#include <ESP8266WebServer.h>
#include <WiFiManager.h> //https://github.com/tzapu/WiFiManager
#include <PietteTech_DHT.h> //https://github.com/chaeplin/PietteTech_DHT-8266
//DHT22 config
#define DHTPIN 2 // what pin DHT is connected to
#define DHTTYPE DHT22 // DHT 11
//declaration
void dht_wrapper(); // must be declared before the lib initialization
// Lib instantiate
PietteTech_DHT DHT(DHTPIN, DHTTYPE, dht_wrapper);
// globals
bool bDHTstarted; // flag to indicate we started acquisition
// This wrapper is in charge of calling
// must be defined like this for the lib work
void dht_wrapper() {
DHT.isrCallback();
}
#define DEBUGPRINT
#ifdef DEBUGPRINT
#define DEBUG_PRINT(x) Serial.println (x)
#else
#define DEBUG_PRINT(x)
#endif
//#include <SimpleTimer.h> //http://playground.arduino.cc/Code/SimpleTimer || https://github.com/jfturcot/SimpleTimer
char* topic = "nodes/battery/temp";
char* hellotopic = "nodes/register";
//IPAddress server(192, 168, 10, 10);
//SimpleTimer timer;
String clientName;
WiFiClient wifiClient;
//void callback(const MQTT::Publish& pub) {
// handle message arrived
//}
//PubSubClient client(wifiClient, "iot.eclipse.org");
//emoncoms
const char* host = "emoncms.org";
const char* nodeId = "21";
//either define key here of create a config.h key and add it there
//const char* privateKey = "YOUR_EMONCMS_KEY";
#import "config.h"
ADC_MODE(ADC_VCC);
float h = 0;
float t = 0;
int vcc;
long startMills;
void sendData();
void setup(void)
{
//ESP.eraseConfig();
startMills = millis();
// start serial port
Serial.begin(115200);
//-------------------
DEBUG_PRINT();
WiFiManager wifi;
//wifi.resetSettings();
wifi.setTimeout(120); //so if it restarts and router is not yet online, it keeps rebooting and retrying to connect
if (!wifi.autoConnect("Cashula")) {
DEBUG_PRINT("timeout - going to sleep");
DEBUG_PRINT(millis() - startMills);
delay(200);
//sleep and try again
ESP.deepSleep(10 * 60 * 1000 * 1000);
delay(1000);
}
DEBUG_PRINT(millis() - startMills);
//-------
//setup hardware
}
void loop(void)
{
vcc = ESP.getVcc();//readvdd33();
// original loop
DEBUG_PRINT(millis() - startMills);
DEBUG_PRINT("Requesting temperatures...");
int acquireresult;
//read twice as the first result is cached from last time. suggested by @chaeplin
delay(2000);
DHT.acquireAndWait(100);
delay(2000);
acquireresult = DHT.acquireAndWait(100);
if ( acquireresult == 0 ) {
t = DHT.getCelsius();
h = DHT.getHumidity();
delay(10);
sendData();
DEBUG_PRINT("DONE");
} else {
t = h = 0;
DEBUG_PRINT("Failed");
}
/*h = dht.readHumidity();
t = dht.readTemperature();
if (!isnan(h) && !isnan(t)) {
delay(10);
sendData();
}*/
DEBUG_PRINT(millis() - startMills);
DEBUG_PRINT("Going to sleep");
delay(250);
ESP.deepSleep(5 * 60 * 1000 * 1000);
//ESP.deepSleep(60 * 1000 * 1000);
delay(1000);
}
void sendData() {
DEBUG_PRINT("connecting to ");
DEBUG_PRINT(host);
WiFiClient emoClient;
const int httpPort = 80;
if (!emoClient.connect(host, httpPort)) {
DEBUG_PRINT("connection failed");
return;
}
String json = "{temperature:";
json += t;
json += ",humidity:";
json += h;
json += ",vcc:";
json += vcc;
json += "}";
String url = "/input/post.json?node=";
url += nodeId;
url += "&apikey=";
url += privateKey;
url += "&json=";
url += json;
DEBUG_PRINT("Requesting URL: ");
DEBUG_PRINT(url);
// This will send the request to the server
emoClient.print(String("GET ") + url + " HTTP/1.1\r\n" +
"Host: " + host + "\r\n" +
"Connection: close\r\n\r\n");
delay(10);
// Read all the lines of the reply from server and print them to Serial
while (emoClient.available()) {
String line = emoClient.readStringUntil('\r');
DEBUG_PRINT(line);
}
DEBUG_PRINT();
DEBUG_PRINT("closing connection");
/*
if (client.connected()) {
DEBUG_PRINT("Sending mqtt: ");
//DEBUG_PRINT(json);
if (client.publish(topic, (char*) json.c_str())) {
DEBUG_PRINT("Publish ok");
}
else {
DEBUG_PRINT("Publish failed");
}
}*/
}