This is part 1 of my project, to Create a Battery Driven Temperature Logger. Introduction to the project and end goal can be found here.

So, the first prototype is now ready….

This is what has been achieved with the first prototype:

  • ESP-12E logs on wifi network
  • DHT-22 reads temperature and humidity
  • ESP-12E sends temperature and humidity to ThingSpeak
  • Reset and Program buttons implemented to ease programming of the ESP-12E module
  • Power so far: USB 3.3V
  • Android app from where you have a live view of the temperature

Program button will set pin GPIO0 to GND while board starts up to enable programming mode.

Reset button connect GPIO16 and RST to GND causing the board to reset. Afterwards CPIO16 and RST will be connected.

Step 1 of 3 – Set up ThingSpeak

So, in order to push data to ThingSpeak, you first need to set up a ThingSpeak account. As soon as you have this, you need to create a “Channel”. Set up the channel like this:

screenshot-from-2017-01-23-10-24-49

After setting up your channel, ThingSpeak have generated API keys for you, one for reading from the channel and one for writing:

Screenshot from 2017-01-23 10-26-32.png

These keys should be used whenever you write the temperature data to ThingSpeak and in the code in this post there’s a line where you need to insert the read key.

Step 2 of 3 – Create your temperature logger

Connections

CP2102 (on 3.3V setting):

  • CP2102 RXD -> ESP-12E TXD0
  • CP2102 TXD -> ESP-12E RXD0
  • CP2102 GND -> Breadboard GND
  • CP2102 3.3V -> Breadboard VCC
  • Breadboard GND ->  220 µF capacitor minus
  • Breadboard VCC ->  220 µF capacitor plus

DHT-22:

  • DHT-22 PIN 1 VCC -> 3.3 VCC
  • DHT-22 PIN 2 -> ESP-12E GPIO4
  • DHT-22 PIN 4 GND -> Breadboard GND

ESP-12E:

  • ESP-12E GPIO4 -> DHT-22 PIN 2
  • ESP-12E GPIO0 -> Top Right Pin on Mini switch (Program button)
  • ESP-12E GPIO2 -> Breadboard VCC
  • ESP-12E GPIO15 -> Breadboard GND
  • ESP-12E GND -> Breadboard GND
  • ESP-12E RST -> Top right pin on Mini switch (Reset button)
  • ESP-12E EN -> Breadboard VCC
  • ESP-12E GPIO16 -> Top right pin on Mini switch (Reset button)
  • ESP-12E VCC -> Breadboard VCC

Switch button (Program button):

  • Top left pin -> 10KOhm resistor and then to GND
  • Top right pin -> GPIO0

Switch button (Reset button):

  • Top left pin -> GND
  • Top right pin -> 10KOhm resistor and then to VCC
  • Top right pin -> ESP-12E CPIO16
  • Top right pin -> ESP-12E RST

Fritzing

esp8266-esp12e-temperature-logger-to-thingspeak-with-reset_bb

Source code

/**
 * Customized with buttons for handling reset and programming.
 * 
 * In order to enable programming mode, the user should hold down the 
 * programming button while briefly pushing the Reset button. 
 * Afterwards both buttons can be released. 
 * This should enable programming mode.
 * 
 * Note that the monitor will start each run with Garbage characters 
 * as initial boot up happens at 9600 Baud and this program uses 
 * Serial port at 115200 Baud
 * 
 * Connections (CP2102 can be replaced by two 1.5V AA batteries):
 * CP2102 RXD       -> ESP-12E TXD0
 * CP2102 TXD       -> ESP-12E RXD0
 * CP2102 GND       -> Breadboard GND
 * CP2102 3.3V      -> Breadboard VCC
 * Breadboard GND -> 220 µF capacitor minus
 * Breadboard VCC -> 220 µF capacitor plus
 *
 * DHT-22 PIN 1 VCC -> 3.3 VCC
 * DHT-22 PIN 2     -> ESP-12E GPIO4
 * DHT-22 PIN 4 GND -> Breadboard GND
 * 
 * ESP-12E GPIO4    -> DHT-22 PIN 2
 * ESP-12E GPIO0    -> Top Right Pin on Mini switch (Program button)
 * ESP-12E GPIO2    -> Breadboard VCC
 * ESP-12E GPIO15   -> Breadboard GND
 * ESP-12E GND      -> Breadboard GND
 * 
 * ESP-12E RST      -> Top right pin on Mini switch (Reset button)
 * ESP-12E EN       -> Breadboard VCC
 * ESP-12E GPIO16   -> Top right pin on Mini switch (Reset button)
 * ESP-12E VCC      -> Breadboard VCC
 * 
 * Switch button (Program button) - Will set pin GPIO0 to GND while 
 * board starts up to enable programming mode.
 * 
 * Top left pin     -> 10KOhm resistor and then to GND
 * Top right pin    -> GPIO0
 * 
 * Switch button (Reset button) - Will connect GPIO16 and RST to GND 
 * causing the board to reset. Afterwards CPIO16 and RST will be 
 * connected.
 * 
 * Top left pin     -> GND
 * Top right pin    -> 10KOhm resistor and then to VCC 
 * Top right pin    -> ESP-12E CPIO16
 * Top right pin    -> ESP-12E RST
 * 
 * Author: Jakob Maaløe
 * https://wackydeveloper.wordpress.com 
 */
 
/**
 * Generic imports
 * 
 */
#define USE_SERIAL Serial // Logging


/**
 * DHT library and variables
 */
#include <DHT.h>
#define DhtPin 4 // Pin is the DHT connected to (4 = GPI04)
DHT dht(DhtPin, DHT22, 15); // Initialize DHT-22

float temperature;
float humidity;


/** 
 * ESP8266 library and variables
 */
#include <ESP8266WiFi.h>
#include <ESP8266WiFiMulti.h>
#include <ESP8266HTTPClient.h>
ESP8266WiFiMulti WiFiMulti;

// Static IP is faster than DHCP
IPAddress ip(192, 168, 1, 232);
IPAddress gateway(192,168,1,1);
IPAddress subnet(255,255,255,0);

const char* WifiSID    = "sid...";      // Wifi SID
const char* WifiPwd     = "password...";    // WiFi password

const int sleepTimeSecs = 300; // Sleep time in seconds - Minimum is 15 secs.

int MaxWifiConnectionAttempts = 50;  // Max attempts to connect to WIFI. If this limit is reached, sleep will be activated for 60 secs.
int WifiAttemptsCounter       = 0;  // Maximum wifi connection attempts before going back to sleep.


/**
 * ThingSpeak configuration
 */
String ThingSpeakKey    = "Key..."; 
String thingSpeakHost   = "api.thingspeak.com";

bool httpReturnCode      = 0; // Did we send the data to ThingSpeak succesfull
bool RequireDataTobeSent = false; // Is it required that we always succeed in the sending to ThingSpeak

void setup() {
  USE_SERIAL.begin(115200);
  USE_SERIAL.setDebugOutput(true);
  USE_SERIAL.println();
  USE_SERIAL.println();
  USE_SERIAL.println();

  delay(10);
  dht.begin();
    
  WiFi.begin(WifiSID, WifiPwd);
  WiFi.config(ip, gateway, subnet);
}

void ReadDHT22Temperature() {
  USE_SERIAL.println("Requesting DHT-22 temperature...");
  humidity = dht.readHumidity();
  temperature = dht.readTemperature();
  USE_SERIAL.print("Read temperature: ");
  USE_SERIAL.print(temperature);
  USE_SERIAL.print(", humidity: ");
  USE_SERIAL.println(humidity);
}

int SendHttpData(String DestIP, int DestPort, String MyData){
   HTTPClient http;
   http.begin(DestIP, DestPort,MyData);
   
   USE_SERIAL.println("Sending HTTP data...");
   
   int httpCode = http.GET();

   USE_SERIAL.println("Returncode: " + String(httpCode));
   return httpCode;
}

void GoToDeepSleep(int SleepDelay) {
  USE_SERIAL.println("Activating sleep mode for " 
    + String(SleepDelay) + " seconds");
  ESP.deepSleep(SleepDelay * 1000000);
}

void loop() {
  // Wait for WiFi connection
  while (WiFi.status() != WL_CONNECTED) {
    delay(100);
    USE_SERIAL.print(".");
  
    if (WifiAttemptsCounter >= MaxWifiConnectionAttempts){  
      USE_SERIAL.println("Could not connect to WIFI, sleeping...");
      GoToDeepSleep(60); // Sleep for 60 secs.
    }
    WifiAttemptsCounter++;
  }

  ReadDHT22Temperature();
  
  String url =  "/update?key=" + ThingSpeakKey + "&field1=" 
    + String(temperature) + "&field2=" + String(humidity); 
  httpReturnCode = SendHttpData(thingSpeakHost, 80, url);
  USE_SERIAL.println("Logging to Thingspeak done!"); 

  if (RequireDataTobeSent) {
    if (httpReturnCode == 200) {
        GoToDeepSleep(sleepTimeSecs); 
    }
    delay(5000); // If data is not sent successful, try again in 5 sec.
  } else {
    GoToDeepSleep(sleepTimeSecs);
  }
}

When uploading the code above, note that you need to choose a different board than if you were working with the Arduino Pro Mini or similar.

Here’s a screenshot that shows what you need to configure (Check board, flash and CPU settings):

Screenshot from 2017-01-24 10-50-22.png

Now you should be able to upload the code to the ESP8266 chip (ESP-01 or ESP-12)

Step 3 of 3 – fire it up and get your temperature data live on your Android mobile

So, connect the Temperature logger to your computers USB port and open up the Arduino IDE so you are ready to upload the code. If you don’t know how to install the Arduino IDE, upload the code or check logging, check out these posts: Installing and understanding the Arduino IDE (4/5) and Uploading your first program! (5/5)

Before you upload hold down the Program button while you press the Reset button. A few seconds after releasing the Reset button, let go of the Program button. Now you can upload the code.

After uploading the code, the Temperature logger should start pushing data to ThingSpeak. Check out your ThingSpeak channel for the data.

This is an example of the data read by my kitchen temperature-logger:

screenshot-from-2017-01-23-10-41-54

Now, if you want to get a live view of the temperature on your Android phone, check out the app “IoT ThingSpeak Monitor Widget”. This is an image of how it looks on my phone:

Screenshot from 2017-01-23 10-49-47.png

Pretty awesome if you ask me. 🙂

The cool thing about this, is that this project just reads the temperature, but in practise, it could read just about anything… so take it away and connect whatever sensors you can think of to ThinkSpeak!

And when you want to take this a step further, change to ESP-01  or connect the data to you Google Home so you can ask “Hey Google, what’s the temperature in my Kitchen?”!

Read on… ThingSpeak battery driven temperature logger – Part 2: Connecting Google Home

Advertisements