Evan Rust
Published © GPL3+

ESP8266 Temperature Monitor

Get an alert if your fridge's temperature rises above 40 F (very dangerous!).

BeginnerFull instructions provided1 hour15,819
ESP8266 Temperature Monitor

Things used in this project

Story

Read more

Schematics

Schematic

Connect as-is

Code

The Code

C/C++
Copy and Paste, enter in your credentials.
#include "DHT.h"

#define DHTPIN 2     // what digital pin we're connected to
#define DHTTYPE DHT11   // DHT 11

DHT dht(DHTPIN, DHTTYPE, 11);

TaskManager taskManager;

#define CAYENNE_PRINT Serial
#include <CayenneMQTTESP8266.h>

char ssid[] = "ssid" //SSID name
char wifiPassword[] = "wifi password"

char username[] = "MQTT_USERNAME";
char password[] = "MQTT_PASSWORD";
char clientID[] = "CLIENT_ID";

void setup() {
  if(taskManager.RestartedFromSleep()){
    //wake
    Serial.begin(9600);
    dht.begin();
    Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  }
  else{
    //normal boot
    Serial.begin(9600);
    dht.begin();
    Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  }
}

void loop() {
  Cayenne.loop();
  // Read temperature as Fahrenheit (isFahrenheit = true)
  float f = dht.readTemperature(true);

  // Check if any reads failed and exit early (to try again).
  if (isnan(f)) {
    Serial.println("Failed to read from DHT sensor!");
    return;
  }
  Cayenne.virtualWrite(0, f);
  taskManager.EnterSleep(1000000 * 60 * 60 * 2); //Wait for 2 hours
}

CAYENNE_IN_DEFAULT()
{
  CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
}

Credits

Evan Rust

Evan Rust

122 projects • 1091 followers
IoT, web, and embedded systems enthusiast. Contact me for product reviews or custom project requests.

Comments