Skip to content

Instantly share code, notes, and snippets.

@benevpi
Created August 27, 2020 13:41
Show Gist options
  • Star 3 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save benevpi/f04a8179963c58ba3e3f9ae1fd1b4e4a to your computer and use it in GitHub Desktop.
Save benevpi/f04a8179963c58ba3e3f9ae1fd1b4e4a to your computer and use it in GitHub Desktop.
A sketch for reading soil moisture levels and using Deep Sleep to preserve battery life on DIY More ESP32 soil boards
#include "DHT.h"
#include "esp_wifi.h"
#include <WiFi.h>
#include <ArduinoJson.h>
#include <HTTPClient.h>
//move to secrets
const char * networkName = "XXX";
const char * networkPswd = "XXX";
String AIOKey = "XXX";
const int led = LED_BUILTIN;
RTC_DATA_ATTR int bootCount = 0;
//for deep sleep
#define uS_TO_S_FACTOR 1000000 /* Conversion factor for micro seconds to seconds */
#define TIME_TO_SLEEP 6000 // not sure why this is coming out wrong
uint64_t sleepus=0;
#define DHTPIN 22
#define DHTTYPE DHT11
DHT dht(DHTPIN, DHTTYPE);
//Other things
float asoilmoist=0.0;
float hum, temp;
void print_wakeup_reason(){
esp_sleep_wakeup_cause_t wakeup_reason;
wakeup_reason = esp_sleep_get_wakeup_cause();
switch(wakeup_reason)
{
case ESP_SLEEP_WAKEUP_EXT0 : Serial.println("Wakeup caused by external signal using RTC_IO"); break;
case ESP_SLEEP_WAKEUP_EXT1 : Serial.println("Wakeup caused by external signal using RTC_CNTL"); break;
case ESP_SLEEP_WAKEUP_TIMER : Serial.println("Wakeup caused by timer"); break;
case ESP_SLEEP_WAKEUP_TOUCHPAD : Serial.println("Wakeup caused by touchpad"); break;
case ESP_SLEEP_WAKEUP_ULP : Serial.println("Wakeup caused by ULP program"); break;
default : Serial.printf("Wakeup was not caused by deep sleep: %d\n",wakeup_reason); break;
}
}
int connectToWiFi(const char * ssid, const char * pwd)
{
#ifdef debug
Serial.println("Connecting to WiFi network: " + String(ssid));
#endif
WiFi.begin(ssid, pwd);
int counter = 0;
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
#ifdef debug
Serial.print(".");
#endif
counter++;
if (counter > 120) { return 2;} // try to connect to the network for one minute.
}
#ifdef debug
Serial.println();
Serial.println("WiFi connected!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
#endif
return 0;
}
void post_data() {
if (connectToWiFi(networkName, networkPswd) == 2) {WiFi.mode(WIFI_OFF); return; } //end the function if can't connect to wifi
HTTPClient http;
//post the humidity
String aio_url = "http://io.adafruit.com/api/v2/ben_ev/feeds/soilmoisture/data";
http.begin(aio_url);
http.addHeader("X-AIO-Key", AIOKey);
http.addHeader("Content-Type", "application/json");
int httpResponseCode = http.POST("{\"value\":\"" + String(asoilmoist) +"\"}");
aio_url = "http://io.adafruit.com/api/v2/ben_ev/feeds/gardentemp/data";
http.begin(aio_url);
http.addHeader("X-AIO-Key", AIOKey);
http.addHeader("Content-Type", "application/json");
httpResponseCode = http.POST("{\"value\":\"" + String(temp) +"\"}");
aio_url = "http://io.adafruit.com/api/v2/ben_ev/feeds/gardenhumid/data";
http.begin(aio_url);
http.addHeader("X-AIO-Key", AIOKey);
http.addHeader("Content-Type", "application/json");
httpResponseCode = http.POST("{\"value\":\"" + String(hum) +"\"}");
// Free resources
http.end();
WiFi.mode(WIFI_OFF);
return;
}
void setup(void) {
//put this right at the start of setup to minimise wasted power
++bootCount;
sleepus = TIME_TO_SLEEP * uS_TO_S_FACTOR;
esp_sleep_enable_timer_wakeup(sleepus);
if(bootCount !=6) { esp_deep_sleep_start(); }
bootCount=0; // this will only run on the 3rd boot.
pinMode(led, OUTPUT);
digitalWrite(led, 0);
Serial.begin(115200);
Serial.println("");
Serial.print("boot count: ");
Serial.println(bootCount);
dht.begin();
//delay for two minutes on boot to allow upload of a new sketch
print_wakeup_reason();
delay(2000);
}
void loop(void) {
asoilmoist=analogRead(32);//exponential smoothing of soil moisture
Serial.print("Moisture: ");
Serial.println(asoilmoist);
// Reading temperature or humidity takes about 250 milliseconds!
// Sensor readings may also be up to 2 seconds 'old' (its a very slow sensor)
hum = dht.readHumidity();
Serial.print("humidity: ");
Serial.println(hum);
// Read temperature as Celsius (the default)
temp = dht.readTemperature();
Serial.print("Temperature: ");
Serial.println(temp);
post_data();
Serial.flush();
delay(2000);
esp_deep_sleep_start();
}
@desalema70
Copy link

I think the reason why you have difficulty with longer sleep times is that your constants default to 32-bit (u)integers, if I understand correctly. Maybe you should try using the ULL suffix to request the compiler to treat them as 64-bit uints? For instance, #define TIME_TO_SLEEP 6000ULL.

Hope this helps!

@NormanDunbar
Copy link

Change this line:

#define TIME_TO_SLEEP 6000 // not sure why this is coming out wrong

To:

#define TIME_TO_SLEEP 6000ULL // not sure why this is coming out wrong

The numbers need to be uint64_t. You might need this as well:

#define uS_TO_S_FACTOR 1000000ULL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment