//#define CAYENNE_DEBUG
#define CAYENNE_PRINT Serial
#define DHTTYPE DHT11
#define DHTPIN 14
#include "CayenneDefines.h"
#include "BlynkSimpleEsp8266_mod.h"
#include "CayenneWiFiClient.h"
#include <DHT.h>
// Cayenne authentication token. This should be obtained from the Cayenne Dashboard.
char token[] = "TOKEN";
// Your network name and password.
char ssid[] = "SSID";
char password[] = "PASSWORD";
//Variables for DHT11 values
float h, t, hif;
bool Humidity = false;
bool Temperature = false;
bool HeatIndex = false;
DHT dht(DHTPIN, DHTTYPE);
void setup()
{
Serial.begin(9600);
Serial.print("Setup");
Cayenne.begin(token, ssid, password);
Humidity = false;
Temperature = false;
HeatIndex = false;
}
void loop()
{
//Run Cayenne Functions
Cayenne.run();
//Check if we got all values we wanted and sleep for 10 minutes if we did
if (Humidity && Temperature && HeatIndex){
Serial.println("Got all values - sleeping");
delay(100);
ESP.deepSleep(60000000, WAKE_RF_DEFAULT);
delay(100);
}
}
CAYENNE_OUT(V0){
Serial.println("Entered Humidity");
//Check if read failed and try until success
do {
//Read humidity (percent)
h = dht.readHumidity();
delay(1000);
} while (isnan(h));
Serial.print("Humidity: ");
Serial.println(h);
//Set Humidity to true so we know when to sleep
Humidity = true;
//Write to Cayenne Dashboard
Cayenne.virtualWrite(V0, h);
}
CAYENNE_OUT(V1){
Serial.println("Entered Temperature");
//Check if read failed and try until success
do {
//Read temperature as Fahrenheit
t = dht.readTemperature(true);
delay(1000);
} while (isnan(t));
Serial.print("Temperature: ");
Serial.println(t);
//Set Temperature to true so we know when to sleep
Temperature = true;
//Write to Cayenne Dashboard
Cayenne.virtualWrite(V1, t);
}
CAYENNE_OUT(V2){
Serial.println("Entered Heat Index");
//Check if read failed and try until success
do {
//Read humidity (percent)
h = dht.readHumidity();
//Read temperature as Fahrenheit
t = dht.readTemperature(true);
//Calculate Heat Index as Fahrenheit
hif = dht.computeHeatIndex(t, h);
} while (isnan(t) || isnan(h));
Serial.print("Heat Index: ");
Serial.println(hif);
//Set HeatIndex to true so we know when to sleep
HeatIndex = true;
//Write to Cayenne Dashboard
Cayenne.virtualWrite(V2, hif);
}
To deal with wireless connectivity issues you can create a copy of BlynkSimpleEsp8266.h in your sketch folder with a modified connectWiFi function. 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. Save the text below to a file called BlynkSimpleEsp8266_mod.h in your sketch folder and instead of importing BlynkSimpleEsp8266.h import BlynkSimpleEsp8266_mod.h.
/**
* @file BlynkSimpleEsp8266.h
* @author Volodymyr Shymanskyy
* @license This project is released under the MIT License (MIT)
* @copyright Copyright (c) 2015 Volodymyr Shymanskyy
* @date Jan 2015
* @brief
*
*/
#ifndef BlynkSimpleEsp8266_h
#define BlynkSimpleEsp8266_h
#ifndef ESP8266
#error This code is intended to run on the ESP8266 platform! Please check your Tools->Board setting.
#endif
#ifndef BLYNK_INFO_DEVICE
#define BLYNK_INFO_DEVICE "ESP8266"
#endif
#include <BlynkApiArduino.h>
#include <Blynk/BlynkProtocol.h>
#include <Adapters/BlynkArduinoClient.h>
#include <ESP8266WiFi.h>
class BlynkWifi
: public BlynkProtocol<BlynkArduinoClient>
{
typedef BlynkProtocol<BlynkArduinoClient> Base;
public:
BlynkWifi(BlynkArduinoClient& transp)
: Base(transp)
{}
void connectWiFi(const char* ssid, const char* pass)
{
int timeoutcounter=0;
BLYNK_LOG("Connecting to %s", ssid);
WiFi.mode(WIFI_STA);
if (pass && strlen(pass)) {
WiFi.begin(ssid, pass);
} else {
WiFi.begin(ssid);
}
while (WiFi.status() != WL_CONNECTED) {
timeoutcounter++;
if (timeoutcounter >= 30){
BLYNK_LOG("Failed to connect to wireless - sleeping for 5 minutes");
delay(100);
ESP.deepSleep(300000000, WAKE_RF_DEFAULT);
delay(100);
}
::delay(500);
}
BLYNK_LOG("Connected to WiFi");
IPAddress myip = WiFi.localIP();
BLYNK_LOG("My IP: %d.%d.%d.%d", myip[0], myip[1], myip[2], myip[3]);
}
void config(const char* auth,
const char* domain = BLYNK_DEFAULT_DOMAIN,
uint16_t port = BLYNK_DEFAULT_PORT)
{
Base::begin(auth);
this->conn.begin(domain, port);
}
void config(const char* auth,
IPAddress ip,
uint16_t port = BLYNK_DEFAULT_PORT)
{
Base::begin(auth);
this->conn.begin(ip, port);
}
void begin(const char* auth,
const char* ssid,
const char* pass,
const char* domain = BLYNK_DEFAULT_DOMAIN,
uint16_t port = BLYNK_DEFAULT_PORT)
{
connectWiFi(ssid, pass);
config(auth, domain, port);
}
void begin(const char* auth,
const char* ssid,
const char* pass,
IPAddress ip,
uint16_t port = BLYNK_DEFAULT_PORT)
{
connectWiFi(ssid, pass);
config(auth, ip, port);
}
};
static WiFiClient _blynkWifiClient;
static BlynkArduinoClient _blynkTransport(_blynkWifiClient);
BlynkWifi Blynk(_blynkTransport);
#include <BlynkWidgets.h>
#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). Triggers and Alerts come standard with Cayenne platform.
UPDATE: This lasted just over 1 month! Continuing to modify and improve!