About This Project
This Project will check Temperature, Humidity, and Heat Index using an ESP-12f and a DHT11 sensor and will run on 2 AAA batteries (or whatever your 3/3.3v battery preference may be). During transmission the ESP draws around 72mA on average and during sleep it draws 0.05mA. I’m not sure how to calculate how long that will last, but updating only every 10 minutes it should last for quite some time! The Arduino sketch is below and requires the DHT sensor library by Adafruit version 1.2.3 as well as the Cayenne library.
Note:
Pin 16 has to be connected to the reset pin for the ESP to wake up from sleep.
// This example shows how to connect to Cayenne using an ESP8266 and send/receive sample data.
// Make sure you install the ESP8266 Board Package via the Arduino IDE Board Manager and select the correct ESP8266 board before compiling.
//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define DHTTYPE DHT11
#define DHTPIN 14
#include <CayenneMQTTESP8266.h>
#include <DHT.h>
// WiFi network info.
char ssid[] = "";
char wifiPassword[] = "";
// Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
char username[] = "";
char password[] = "";
char clientID[] = "";
//Variables for DHT11 values
float h, t, hif;
//Time to sleep - 10 minutes - delete a zero to set to 1 minute
int SleepTime = 600000000;
//Values sent/error booleans
bool CayenneSent = false;
bool ReadError = false;
DHT dht(DHTPIN, DHTTYPE);
void setup() {
Serial.begin(9600);
Serial.println("Setup");
Cayenne.begin(username, password, clientID, ssid, wifiPassword);
CayenneSent = false;
ReadError = true;
}
void loop() {
//Run Cayenne Functions
Cayenne.loop();
//Only check values if there is a ReadError
if (ReadError){
ReadError = false;
//Check if read failed and try until success
int WhileLoops = 0;
do {
WhileLoops++;
//We can't go too long without calling Cayenne.loop() so exit this loop after 2 seconds and set an error flag
if (WhileLoops >= 4){
Serial.println("Sensor Read Error");
ReadError = true;
break;
}
//Read temperature as Fahrenheit
t = dht.readTemperature(true);
//Read humidity (percent)
h = dht.readHumidity();
//Read temperature as Fahrenheit
t = dht.readTemperature(true);
//Calculate Heat Index as Fahrenheit
hif = dht.computeHeatIndex(t, h);
delay(500);
} while (isnan(t) || isnan(h));
Serial.print("Humidity: ");
Serial.println(h);
Serial.print("Temperature (f): ");
Serial.println(t);
Serial.print("Heat Index (f): ");
Serial.println(hif);
}
else {
//Check if we sent all values and sleep for 10 minutes if we did
if (CayenneSent){
Serial.println("Sent all values - sleeping");
delay(100);
ESP.deepSleep(SleepTime, WAKE_RF_DEFAULT);
delay(100);
}
}
}
// Default function for sending sensor data at intervals to Cayenne.
// You can also use functions for specific channels, e.g CAYENNE_OUT(1) for sending channel 1 data.
CAYENNE_OUT_DEFAULT()
{
//Check if there was a read error and skip sending values if there is
if (!ReadError){
//Send Values
Cayenne.virtualWrite(0, h, "rel_hum", "p");
Cayenne.virtualWrite(1, t, "temp", "f");
Cayenne.virtualWrite(2, hif, "temp", "f");
//Set CayenneSent to true so we know when to sleep
CayenneSent = true;
}
}
To deal with wireless connectivity issues you can modify CayenneMQTTWiFiClient.h in the Arduino\libraries\Cayenne-MQTT-ESP-master\src folder. Below is my solution to the ESP staying awake and draining the batteries when it cannot connect to the configured wifi network. If the ESP fails to connect 30 times it’s safe to say it probably won’t. In that case I have the ESP go in to sleep mode for 5 minutes before it tries again. Keep in mind every time you update the library you will have to make this change as it will be overwritten by the update.
/*
The MIT License(MIT)
Cayenne Arduino Client Library
Copyright (c) 2016 myDevices
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
documentation files(the "Software"), to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense, and / or sell copies of the Software,
and to permit persons to whom the Software is furnished to do so, subject to the following conditions :
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE AUTHORS OR
COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _CAYENNEMQTTWIFICLIENT_h
#define _CAYENNEMQTTWIFICLIENT_h
#include "CayenneArduinoMQTTClient.h"
#ifndef WRITE_CHUNK_SIZE
#define WRITE_CHUNK_SIZE 0 // The chunk size to use when sending data, 0 means data will not be sent in chunks.
#endif // !WRITE_CHUNK_SIZE
class CayenneMQTTWiFiClient : public CayenneArduinoMQTTClient
{
public:
/**
* Begins Cayenne session and in the process establishes a WIFI connection with the supplied ssid and WIFI password
* @param username Cayenne username
* @param password Cayenne password
* @param clientID Cayennne client ID
* @param ssid WiFi network id
* @param wifiPassword WiFi network password
*/
void begin(const char* username, const char* password, const char* clientID, const char* ssid, const char* wifiPassword)
{
int status = WL_IDLE_STATUS;
WiFi.mode(WIFI_STA);
delay(500);
if (WiFi.status() == WL_NO_SHIELD) {
CAYENNE_LOG("WiFi not present");
while (true);
}
CAYENNE_LOG("Connecting to %s", ssid);
if (wifiPassword && strlen(wifiPassword)) {
WiFi.begin(ssid, wifiPassword);
}
else {
WiFi.begin(ssid);
}
int timeoutcounter=0;
while (WiFi.status() != WL_CONNECTED) {
timeoutcounter++;
if (timeoutcounter >= 30){
CAYENNE_LOG("Failed to connect to wireless - sleeping for 5 minutes");
delay(100);
ESP.deepSleep(300000000, WAKE_RF_DEFAULT);
delay(100);
}
delay(500);
}
CAYENNE_LOG("Connected to WiFi");
IPAddress local_ip = WiFi.localIP();
CAYENNE_LOG("IP: %d.%d.%d.%d", local_ip[0], local_ip[1], local_ip[2], local_ip[3]);
CayenneArduinoMQTTClient::begin(_wifiClient, username, password, clientID, WRITE_CHUNK_SIZE);
}
/**
* Begins Cayenne session, assumes that the WIFI is already up and running.
* @param username Cayenne username
* @param password Cayenne password
* @param clientID Cayennne client ID
*/
void begin(const char* username, const char* password, const char* clientID)
{
if (WiFi.status() != WL_CONNECTED) {
CAYENNE_LOG("CayenneMQTTWiFiClient.begin called without WIFI being connected. Either use begin (..., ssid, wifipassword) to establish the connection here. Or setup the WIFI connection manually before calling this variant of the begin function");
}
IPAddress local_ip = WiFi.localIP();
CAYENNE_LOG("IP: %d.%d.%d.%d", local_ip[0], local_ip[1], local_ip[2], local_ip[3]);
CayenneArduinoMQTTClient::begin(_wifiClient, username, password, clientID, WRITE_CHUNK_SIZE);
}
private:
WiFiClient _wifiClient;
};
CayenneMQTTWiFiClient Cayenne;
#endif
What’s Connected
ESP-12f
1000uf Capacitor
PCB Headers
Jumper Wires
Single Sided Perf Board
DHT11
2 AAA battery holder
Triggers & Alerts
I set up a trigger to email me when the temperature drops below 65 (cold) and another trigger to email me when the temperature is above 90 (hot)
Scheduling
Can’t think of any way scheduling would fit in here.