-
Notifications
You must be signed in to change notification settings - Fork 79
/
esp8266.ino
148 lines (137 loc) · 4.19 KB
/
esp8266.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
#include <ESP8266HTTPClient.h>
#include <ESP8266WiFi.h>
#include <DHT.h>
#include "esp8266.secrets.c"
#define PIN_MOISTURE_READ 0 // moisture sensor analog on PIN 0
#define PIN_MOISTURE_POWER 12 // D6
#define PIN_DHT_READ 4 // D2, temp + humidity sensor
#define PIN_DHT_POWER 13 // D7
#define DHT_TYPE DHT22
#define SLEEP_S 900 // how many seconds to sleep between readings
#define DEVICE_ID "plantbuddy1"
#define MAX_READING_RETRIES 10 // maximum number of retries when getting NaN values from sensor
DHT dht(PIN_DHT_READ, DHT_TYPE);
void setup() {
pinMode(PIN_DHT_POWER, OUTPUT);
pinMode(PIN_MOISTURE_POWER, OUTPUT);
Serial.begin(115200);
Serial.println("Starting setup");
delay(100);
digitalWrite(PIN_DHT_POWER, HIGH);
digitalWrite(PIN_MOISTURE_POWER, HIGH);
wifiConnect(WIFI_SSID, WIFI_KEY);
// submit heartbeat
Serial.println("Sending heartbeat..");
int httpBeat = submitHeartbeat(POST_URL, DEVICE_ID);
Serial.print("Heartbeat HTTP Response: ");
Serial.println(httpBeat);
delay(2000); // wait for DHT to power up?
// read sensor values
float moisture = getMoistureLevel(PIN_MOISTURE_READ);
float humidity = dht.readHumidity();
float temp = dht.readTemperature(false);
float heatIndex = dht.computeHeatIndex(temp, humidity, false);
int count = 0;
while (isnan(humidity) || isnan(temp) || isnan(heatIndex)) {
Serial.println("Error while reading sensor values, retrying");
delay(2000);
moisture = getMoistureLevel(PIN_MOISTURE_READ);
humidity = dht.readHumidity();
temp = dht.readTemperature(false);
heatIndex = dht.computeHeatIndex(temp, humidity, false);
count++;
// force ESP into deepsleep so heartbeat gets out in case of permanent sensor damage
if (count > MAX_READING_RETRIES) {
shutdown();
}
}
logSensorStatus(moisture, temp, humidity, heatIndex);
Serial.println("Sending sensor data..");
int httpCode = submitSensorStatus(POST_URL, moisture, temp, humidity, heatIndex);
Serial.print("Sensor data HTTP response: ");
Serial.println(httpCode);
shutdown();
}
void loop() {
}
void shutdown() {
digitalWrite(PIN_DHT_POWER, LOW);
digitalWrite(PIN_MOISTURE_POWER, LOW);
ESP.deepSleep(SLEEP_S * 1000000);
}
float getMoistureLevel(int PIN) {
float moisture;
moisture = analogRead(PIN);
moisture = moisture/10;
return moisture;
}
void logSensorStatus(float moisture, float temp, float humidity, float heatIndex) {
Serial.print("Moisture Level: ");
Serial.print(moisture);
Serial.print("\t");
Serial.print("Temperature: ");
Serial.print(temp);
Serial.print("\t");
Serial.print("Humidity: ");
Serial.print(humidity);
Serial.print("\t");
Serial.print("Heat Index: ");
Serial.print(heatIndex);
Serial.println("");
}
int submitSensorStatus(char* url, float moisture, float temp, float humidity, float heatIndex) {
String json = "{";
json += "\"moisture\": ";
json += moisture;
json += ", \"temp\": ";
json += temp;
json += ", \"humidity\": ";
json += humidity;
json += ", \"id\": \"";
json += DEVICE_ID;
json += "\"";
json += ", \"type\": \"data\"";
json += "}";
int httpCode = httpPost(url, json);
return httpCode;
}
int submitHeartbeat(char* url, char* id) {
String json = "{";
json += "\"msg\": \"Still alive\"";
json += ", \"id\": \"";
json += id;
json += "\"";
json += ", \"type\": \"heartbeat\"";
json += "}";
int httpCode = httpPost(url, json);
return httpCode;
}
// send HTTP post with JSON payload
int httpPost(char* url, String payload) {
HTTPClient http;
http.begin(url);
http.addHeader("Content-Type", "application/json");
if (HEADER_SECRET) {
http.addHeader("SECRET", HEADER_SECRET);
}
int httpCode = http.POST(payload);
String response = http.getString();
if (httpCode < 0) {
Serial.print("Error: ");
Serial.println(http.errorToString(httpCode).c_str());
}
http.end();
return httpCode;
}
// try to connect to given SSID and key, loop until successful
void wifiConnect(char* ssid, char* key) {
WiFi.begin(ssid, key);
Serial.print("Waiting for WiFi connection..");
while (WiFi.status() != WL_CONNECTED) {
Serial.print(".");
delay(500);
}
Serial.println(".");
Serial.print("Successfully connected to ");
Serial.println(WiFi.SSID());
}