-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsoil_moisture_wemos.ino
325 lines (257 loc) · 8.53 KB
/
soil_moisture_wemos.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
/************************************************
* soil_moisture_wemos
* gswann - 26 Mar 2021
*
* modified from adafruit example
*
* In HA "Services" mqtt.publish use this to send a message
* to the controller about the desired mode (and set retain flag for this topic)
*
* {"topic": "sm1/modereq","payload":"awake","retain":true}
* or
* {"topic": "sm1/modereq","payload":"sleep","retain":true}
*
* and switch between going to sleep after a report, or
* staying awake indefinitely and continuing to report
* every minute.
*************************************************/
#include "Adafruit_seesaw.h"
#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <PubSubClient.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#include <PubSubClient.h>
#define VERSION "0.96"
#include "secrets.h"
WiFiClient espClient;
PubSubClient client(espClient);
// sleep time is in microseconds
//unsigned long SleepTimer = 300e6; // 300e6 is 300 x 10^6 microseconds (5 minutes)
unsigned long SleepTimer = 1800e6; // 1800e6 is 1800 x 10^6 microseconds (30 minutes)
bool SleepStatus = 0; // 1 = going to sleep 0 = staying awake
const int onesec = 1000;
unsigned long myMillis = 0;
unsigned long oldMillis = 0;
// the 55 allows an early report before sleeping
// rather than waiting a whole 60 seconds
unsigned int mySecs = 55;
unsigned int myMins = 0;
unsigned int cntMR = 0;
Adafruit_seesaw ss;
const char* WiFi_hostname = "soilmoisture1";
// isn't this goofy?
const int led = LED_BUILTIN; // blue LED on the ESP board
const int myLed = LED_BUILTIN; // blue LED on the ESP board
const int warning = LED_BUILTIN; // blue LED on the ESP board
const char* mqtt_server = "192.168.2.6";
//********************************************
void setup() {
Serial.begin(115200);
Serial.println("seesaw Soil Sensor example!");
// turn on the seesaw
pinMode(D6,OUTPUT);
digitalWrite(D6, HIGH);
delay(2000);
if (!ss.begin(0x36)) {
Serial.println("ERROR! seesaw not found");
while (1);
} else {
Serial.print("seesaw started! version: ");
Serial.println(ss.getVersion(), HEX);
}
setupWifi();
setupOTA();
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
// blink three quick for all ready
for (int i = 0 ; i < 3; i++) {
digitalWrite(myLed, LOW);
delay(200);
digitalWrite(myLed, HIGH);
delay(100);
}
client.loop();
Serial.println(F("Program startup complete"));
Serial.println();
Serial.println();
}
//********************************************
void loop() {
ArduinoOTA.handle();
if (!client.connected()) {
reconnect();
}
float tempC = ss.getTemp();
float tempF = ((tempC * 9) / 5) + 32;
int inttempF = int(tempF);
uint16_t capread = ss.touchRead(0);
// Serial.print("Temperature: "); Serial.print(tempC); Serial.println("*C");
// Serial.print("Temperature: "); Serial.print(tempF); Serial.println("*F");
// Serial.print("Capacitive: "); Serial.println(capread);
//delay(1000);
client.loop();
myMillis = millis();
// one second timer
if ((myMillis - oldMillis) >= onesec) {
// oldMillis = myMillis;
oldMillis += onesec;
mySecs += 1;
if (mySecs >= 60) {
mySecs -= 60;
myMins += 1;
Serial.print("Temperature: "); Serial.print(tempF); Serial.println("*F");
Serial.print("Capacitive: "); Serial.println(capread);
digitalWrite(myLed, LOW);
Serial.println("\npublishing...\n");
// always have to convert from int to char array
char mybytes[8] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} ;
unsigned int myFree = ESP.getFreeHeap();
// char mybytes2[8] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} ;
sprintf(mybytes, "%u" , myFree);
client.publish("sm1/memfree", mybytes);
// char mybytes[8] = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0} ;
sprintf(mybytes, "%u" , myMins);
// mybytes[5] = 0x0;
client.publish("sm1/uptime", mybytes);
delay(100);
sprintf(mybytes, "%u" , inttempF);
client.publish("sm1/temp", mybytes);
delay(100);
sprintf(mybytes, "%u" , capread);
client.publish("sm1/moisture", mybytes);
delay(100);
client.publish("sm1/version", VERSION);
digitalWrite(myLed, HIGH);
}
// oldMillis = myMillis;
} // one second timer
// client.loop();
if (SleepStatus == 1 && millis() > 20000) { // was 70000
// turn off the seesaw
digitalWrite(D6, LOW);
client.publish("sm1/status", "sleeping");
client.loop();
Serial.println("\n sleeping...\n");
delay(5000);
WiFi.disconnect();
delay(2000);
// on a test board here, the current drops to about 100 microamps.
ESP.deepSleep(SleepTimer);
}
}
//*************************************************
void setupWifi() {
WiFi.disconnect(); //Prevent connecting to wifi based on previous configuration
IPAddress staticIP(192, 168, 2, MYADDR); //ESP static ip
IPAddress dns(192, 168, 2, 6); //DNS
IPAddress gateway(192, 168, 2, 1); //IP Address of your WiFi Router (Gateway)
IPAddress subnet(255, 255, 255, 0); //Subnet mask
WiFi.mode(WIFI_STA);
WiFi.hostname(WiFi_hostname);
WiFi.config(staticIP, dns, gateway, subnet);
WiFi.begin(SSID, SSIDPWD);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
if (MDNS.begin(WiFi_hostname)) {
Serial.println(F("MDNS responder started"));
MDNS.addService("http", "tcp", 80);
// MDNS.addServiceTxt("arduino","tcp","supports",supportedHost);
}
}
//*************************************************
void callback(char* topic, byte* payload, unsigned int length) {
for (int ii = 0; ii < length; ii++) {
payload[ii] = toupper(payload[ii]);
}
payload[length] = '\0';
char *chrPayload = (char *) payload;
if (strcmp(topic, "sm1/modereq") == 0)
{
if (strncmp(chrPayload, "AWAKE", 5) == 0) {
client.publish("sm1/status", "awake");
SleepStatus = 0;
}
if (strncmp(chrPayload, "SLEEP", 5) == 0) {
client.publish("sm1/status", "sleep pending");
delay(5000);
SleepStatus = 1;
}
if (strncmp(chrPayload, "REBOOT", 6) == 0) {
client.publish("sm1/status", "Rebooting");
ESP.restart();
}
}
} // end callback
//*************************************************
void setupOTA() {
ArduinoOTA.setHostname(WiFi_hostname);
ArduinoOTA.onStart([]() {
digitalWrite(warning, HIGH);
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total) {
// Serial.printf("Progress: %u%%\r", (progress / (total / 100)));
digitalWrite(warning, !digitalRead(warning));
});
ArduinoOTA.onEnd([]() { // do a fancy thing with our board led at end
for (int i = 0; i < 10; i++)
{
digitalWrite(myLed, HIGH);
digitalWrite(warning, HIGH);
delay(100);
digitalWrite(myLed, LOW);
digitalWrite(warning, LOW);
delay(100);
}
digitalWrite(myLed, LOW); // turn off blue LED
// had to add on this computer to make OTA finish properly
ESP.restart();
});
ArduinoOTA.onError([](ota_error_t error) {
(void)error;
ESP.restart();
});
// setup the OTA server
ArduinoOTA.begin();
}
//********************************
void reconnect() {
int attempts = 0;
// Loop until we're reconnected
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Attempt to connect
if (client.connect(mqtt_client, mqtt_user, mqtt_password)) {
Serial.println("connected");
// Once connected, publish an announcement...
cntMR += 1;
client.publish("sm1/status", "up" );
client.subscribe("sm1/#");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println(" try again in 5 seconds");
// blink twice
for (int i = 0 ; i < 2; i++) {
digitalWrite(myLed, LOW);
delay(500);
digitalWrite(myLed, HIGH);
delay(500);
}
attempts += 1;
// Wait 5 seconds before retrying
delay(5000);
if (attempts > 120) {
ESP.restart();
}
}
}
} // end reconnect