forked from cburgess5294/BurgessWorld
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Basic_Temp_Humidity_Sensor_Thingspeak_example_v1_1.ino
374 lines (343 loc) · 10.3 KB
/
Basic_Temp_Humidity_Sensor_Thingspeak_example_v1_1.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
/* This is version 1.1 of this sketch. It has slightly different timing to conform to new versions of AI Thinker firmware.
* If you can't seem to get the older version to work, try this one.
*
* This example sketch is designed for use with Basic and Advanced Sensor Boards sold by BurgessWorld Custom Electronics
* The sketch uses an ESP8266 wifi module and a DHT temp/humidity sensor to upload temp/humidity data to a
* thingspeak.com channel every 15 minutes.
* It assumes a GREEN LED is attached to pin 5, and a RED LED is attached to pin 13.
* The LEDs can be disabled by commenting out "#define EnableLEDs" in the sketch below
* It is based on the ESP8266 wifi library available here: https://github.com/itead/ITEADLIB_Arduino_WeeESP8266
* The DHT library available here: https://github.com/adafruit/DHT-sensor-library/archive/master.zip
* And the Adadruit sensor library available here: https://github.com/adafruit/Adafruit_Sensor/archive/master.zip
* The sketch takes a sensor reading every 15 minutes and uploads it to the defined thingspeek channel
* After uploading the data, the ESP8266 module is put to sleep until it is time to read the sensor again
* You should define your local WiFi settings, and your thingspeak channel write API key.
* You can add additional sensors and upload additional data by including the appropriate library
* and tweeking the "Build Data Package" section in the sendMessage function
* This sketch is provided AS-IS with no implied warranty for fitness of purpose
* You may modify this sketch to meet your needs
*/
#include "SoftwareSerial.h"
#include "ESP8266.h"
#include <DHT.h>
//comment out the following line to disable status LEDS when running on batteries
#define EnableLEDs
//Define status indicator LED pins
#define GREEN 5
#define RED 13
//define wifi SSID and password
#define SSID "BURGESSWORLD1"
#define PASSWORD "thisistherouterpassword"
//define thingspeak address and port
#define HOST_NAME "api.thingspeak.com"
#define HOST_PORT (80)
//define thingspeek channel write key
#define channelKey "921QR0W64PY0N9BY"
// Define DHT Sensor type - uncomment whatever type you're using
//#define DHTTYPE DHT11 // DHT 11
#define DHTTYPE DHT22 // DHT 22 (AM2302), AM2321
//#define DHTTYPE DHT21 // DHT 21 (AM2301)
//define DHT sensor pin
#define DHTPIN 16
// Define ESP8266 RESET pin
#define ESPRESETPIN 2
//define structure for returning sensor readings
struct reading {
int h;
int f;
} reading_r ;
//define Software Serial instance
SoftwareSerial Serial1(7, 6); // RX, TX
//create ESP8266 instance
ESP8266 wifi(Serial1, 57600);
//initialize DHT
DHT dht(DHTPIN, DHTTYPE);
//initialize other global variables
bool connectStat;
int failCount;
bool espPresent = false;
int sleepTime = 900000; //15 minute sleep time
void setup() {
Serial.begin(57600);
Serial.println(F("Started"));
//set pin mode for status indicator LED pins
pinMode(GREEN, OUTPUT);
pinMode(RED, OUTPUT);
//test LEDs
Serial.println("test LEDs");
flashLED(GREEN, 2);
flashLED(RED, 2);
//set pin mode for DHT22 output pin
pinMode(DHTPIN, INPUT_PULLUP);
//SET ESP RESET PIN TO OUTPUT
pinMode(ESPRESETPIN, OUTPUT);
//flush the serial buffer
flushSerial();
//reset ESP8266
digitalWrite(ESPRESETPIN, LOW);
delay(300);
digitalWrite(ESPRESETPIN, HIGH);
delay(200);
for (int i = 0; i < 200; i++)
{
Serial1.read();
}
//wait for ESP status
if (recFind("ready", 5000)){
Serial.println(F("Ready"));
//set ESP8266 to station mode
if (wifi.setOprToStation()){
Serial.println(F("Station mode enabled"));
Serial.println(F("ESP initialized"));
flushSerial();
flashLED(GREEN, 1);
initEsp();
espPresent = true;
}
}
else{
Serial.println(F("ESP not found"));
Serial.println(F("...check ESP UART Speed"));
Serial.println(F("...verify ESP LED activity"));
flashLED(RED, 5);
}
}
void loop() {
if (espPresent){
//read sensor
reading_r = readSensor();
//send message
sendMessage();
//put ESP to sleep
Serial.println(F("Put ESP to sleep"));
bool espAsleep = false;
while (!espAsleep){
Serial1.println(F("AT+SLEEP=2")); //put the ESP8266 to sleep
if (recFind("OK", 5000)){
Serial.println(F("ESP sleeping"));
espAsleep = true;
}
else {
Serial.println(F("ESP failed to sleep"));
flushSerial();
}
}
delay(sleepTime); //delay up to the value of sleepTime variable
Serial.println(F("Initialize ESP"));
initEsp();
}
}
//function to flush software serial read buffer
void flushSerial(){
while (Serial1.available() >= 1){
Serial1.read();
}
}
//function to flash LEDs a specified number of times
uint8_t flashLED(uint8_t color, uint8_t flashCount){
#ifdef EnableLEDs
for (uint8_t flsh = 0; flsh < flashCount; flsh++ ){
digitalWrite(color, HIGH);
delay(150);
digitalWrite(color, LOW);
delay(100);
}
#endif
}
//Bool function to seach Serial RX buffer for string value
bool recFind(String target, uint32_t timeout)
{
char rdChar;
String rdBuff = "";
unsigned long startMillis = millis();
while (millis() - startMillis < timeout){
while (Serial1.available() >=1){
rdChar = Serial1.read();
rdBuff += rdChar;
if (rdBuff.indexOf(target) != -1) {
break;
}
}
if (rdBuff.indexOf(target) != -1) {
break;
}
}
if (rdBuff.indexOf(target) != -1){
return true;
}
return false;
}
//function to reset ESP
void initEsp(){
bool espReady = false;
bool resetStatus = false;
while (!resetStatus){
//flush the serial buffer
flushSerial();
//reset ESP
Serial.println("reset ESP");
digitalWrite(ESPRESETPIN, LOW);
delay(300);
digitalWrite(ESPRESETPIN, HIGH);
delay(200);
for (int i = 0; i < 200; i++)
{
Serial1.read();
}
//wait for esp ready
if (recFind("ready", 5000)){
Serial.println(F("ESP Ready"));
espReady = true;
flashLED(GREEN, 2);
}
else {
Serial.println(F("ESP not ready"));
flashLED(RED, 2);
}
//check wifi connection
if (espReady){
//wait for wifi connection
bool wifiJoined = false;
if (recFind("GOT IP", 10000)){
wifiJoined = true;
}
if (wifiJoined){
Serial.println(F("Wifi connected"));
flashLED(GREEN, 3);
resetStatus = true;
Serial.print("IP:");
Serial.println(wifi.getLocalIP().c_str());
}
//join wifi if not connected
else {
Serial.println(F("wifi not joined"));
//check to see if esp is responding
if (wifi.kick()){
Serial.println(F("ESP is up"));
Serial1.println(F("AT+CWJAP?"));
if (recFind("No AP", 5000)){
Serial.println(F("No AP configured"));
flushSerial();
//join wifi
if (wifi.joinAP(SSID, PASSWORD)){
Serial.println(F("wifi joined"));
flashLED(GREEN, 3);
resetStatus = true;
Serial.print("IP:");
Serial.println( wifi.getLocalIP().c_str());
}
else {
Serial.println(F("failed to join wifi"));
flashLED(RED, 3);
}
}
else {
Serial.println(F("no response to AT+CWJAP command"));
flashLED(RED, 3);
}
}
else {
Serial.println(F("ESP didn't respond to kick"));
flashLED(RED, 3);
}
}
}
flushSerial();
}
}
struct reading readSensor(){
uint8_t sumReadHUM = 0;
uint8_t sumReadTMP = 0;
uint8_t trashReadHUM = 0;
uint8_t trashReadTMP = 0;
//Enable DHT22 VCC pin
pinMode(DHTPIN, INPUT);
delay(100);
Serial.println(F("read sensor"));
//read sensor and discard first reading
trashReadTMP += dht.readTemperature(true);
trashReadHUM += dht.readHumidity();
delay(2200);
//read sensor 2 more times and average
sumReadTMP += dht.readTemperature(true);
sumReadHUM += dht.readHumidity();
delay(2200);
sumReadTMP += dht.readTemperature(true);
sumReadHUM += dht.readHumidity();
pinMode(DHTPIN, INPUT_PULLUP);
//add readings to struct and return
reading_r.h = (sumReadHUM / 2);
reading_r.f = (sumReadTMP / 2);
Serial.println(reading_r.h);
Serial.println(reading_r.f);
return reading_r;
}
void sendMessage(){
connectStat = 0;
int disableCount;
while (!connectStat){
//open tcp conncection to thingspeek.com
if (wifi.createTCP(HOST_NAME, HOST_PORT)){
Serial.println(F("TCP connected"));
flashLED(GREEN, 4);
//build data package
String outmessage = "GET /update?key=";
outmessage+=channelKey;
outmessage+="&field1=";
outmessage+=reading_r.h;
/* you can create addditional field definitions to upload more data fields by duplicating the following
* two lines of code and incrementing the 'field#' number in the first line and specifying another variable
* to upload in the second line */
outmessage+="&field2=";
outmessage+=reading_r.f;
//add the rest of the data packet
outmessage+=" HTTP/1.0\r\n\r\n";
//find length of outmessage and add 1
int outmessage_len = outmessage.length() + 1;
// Prepare the character array (the buffer)
char outChar[outmessage_len];
// Copy string message to character array for sending
outmessage.toCharArray(outChar, outmessage_len);
//Send data package
if (wifi.send((const uint8_t*)outChar, outmessage_len)){
Serial.println(F("send success!"));
flashLED(GREEN, 5);
flushSerial();
connectStat = 1;
}
else {
Serial.println(F("send failed"));
Serial1.println(F("AT+CIPCLOSE"));
flashLED(RED, 5);
}
}
else {
Serial.println(F("socket error"));
Serial1.println(F("AT+CIPCLOSE"));
flashLED(RED, 4);
}
if (connectStat){
failCount = 0;
disableCount = 0;
}
else {
if (disableCount == 3){
failCount = 0;
disableCount = 0;
connectStat = 1;
}
else {
if (failCount == 3){
flushSerial();
initEsp();
failCount = 0;
disableCount++;
}
else {
failCount++;
}
}
}
flushSerial();
}
}