ESP8266 ESP-12E Arduino LUX Monitor/Controller w/ IFTTT API :: Student Project

What started out as a simple LDR project has turned into a neat multi-part masterpiece from John Hart! A little background to this project: John has a SmartThings home automation hub that controls some of the indoor house RGB bulbs and things via Z-wave and TCP.

His wife mentioned that it would be much Cooler if those lights came on automatically based on the amount of light in the room, instead of using a specific time or the outside brightness. He decided to see what he could come up with… ‘Arduino Style’!

Parts and Apps used:

  • NODEMCU 0.9 board with ESP8266-12E
  • LDR and 100k resistor
  • LED and 220k resistor
  • HC-06 Bluetooth module
  • Arduino IDE
  • Ardutooth Android App
  • IFTTT.COM account
  • IFTTT applet
  • SmartThings Home Automation System
The current hardware

The current hardware

The current project has the hardware setup relatively simply on a breadboard with the NODEMCU 0.9, HC-06 Bluetooth module, LDR+resistor, LED, and a few jumper wires for connections. The ESP8266 ESP-12E NodeMCU is not strictly an Arduino, but you can use the Arduino IDE to program it.

This is what happens when the light in the room is above the set lux threshold:

  1. Bluetooth starts  
  2. WiFi is put to sleep immediately
  3. It read LDR output values
  4. LDR output values are sent to BTserial port to the HC-06 for broadcast to the Ardutooth App on a phone or tablet
“Phone displaying lux values taken by the Arduino and sent via Bluetooth”

Phone displaying lux values taken by the Arduino and sent via Bluetooth

The Bluetooth connection is not absolutely necessary, but it allows the ESP/LDR to be placed out of the way on a shelf, not tethered to a laptop. Since there is only one dedicated Analog pin on the NODE boards, it is used it for the LDR, and SoftwareSerial is used for the TX/RX Bluetooth pins.

The LDR measures the brightness and if it stays above the threshold, all the Node  does is continue  broadcast the brightness value via Bluetooth,nothing else.

When the lux value drops below the threshold, the magic begins!

  1. A message is sent via Bluetooth “It’s too dark in here…”
  2. The LED turns on, to visually indicate that the threshold has been exceeded
  3. The WiFi wakes up and connects to the router, with Bluetooth status update
  4. Connects to the IFTTT site, with Bluetooth status update
  5. Sends a POST request to IFTTT which contains creds, API, applet_name, etc.
  6. Disconnects from IFTTT
  7. The LED turns off
  8. The WiFi sleeps
  9. A 22 hour delay begins, with Bluetooth status update (We don’t expect dark enough conditions until the next afternoon/evening)
  10. That IFTTT applet_name contains ‘what to do’ with which SmartThings device(s)
  11. IFTTT calls the SmartThings website’s backend, which has been granted permissions
  12. SmartThings tells the house hub via internet/TCP to do what the IFTTT applet_name said to do. (Turn family room lights on)
  13. The SmartThings Hub continues control the lights for 22 hours, until tomorrow evening when the delay expires on the Node, and the LDR above threshold loop starts again

 

Youtube demonstration of the project in action…

There isn’t too much to ‘show-off’ as far as eye-candy goes, but integrating Arduino, Bluetooth, Wi-Fi, IFTTT, an Android app, and SmartThings into one project is really neat. John’s next step is to simplify the setup using a ESP12F Witty Dev Board that has Arduino, Wi-Fi, LDR, and LED on one board. By testing with this current 

Bluetooth setup, he already has a range of threshold values established and can set those in the sketch on the new board, and can eliminate the breadboards, jumpers, LDR, resistor, and BT board altogether.

ESP8266 programmed with Arduino IDE

The next step John will take is to use one ESP12F board to replace all this

John saysALL THANKS TO YOU and your online goodness that I started learning last summer!”; but Cooler status was achieved by you, John Hart and your great work!

 

Arduino Code:

#include <ESP8266WiFi.h>
#include "WiFiClientSecure.h"
#include <SoftwareSerial.h>
SoftwareSerial BTserial(D6, D5); // TX | RX

const char* ssid = "Your_Network";
const char* password = "Your_Password";
const char* host = "maker.ifttt.com";
const char* apiKey = "Your_IFTTT_API_Key";
unsigned long seconds = 1000L; 
unsigned long minutes = seconds * 60;
unsigned long hours = minutes * 60;
unsigned long myDelay = hours * 22;
unsigned long myTestdelay = minutes * 2;
unsigned long mySecondsdelay = seconds * 10;

int sensorPin = A0;   
int sensorValue = 0;  
int ledPin = D3;
int myThreshold = 500;
int myTestthreshold = 700;
const char* ldr_state = "tripped";

void setup() {
  pinMode(ledPin, OUTPUT);
  BTserial.begin(9600);
  //  serial.begin(9600);
  delay(100);
  BTserial.println("Preparing the LDR IFTTT/SmartThings Monitor project...");
  BTserial.print(";");

  pinMode(sensorPin, INPUT);
}
void wifiConnect() {
  WiFi.forceSleepWake();
  BTserial.println();
  BTserial.println();
  BTserial.print("Connecting to ");
  BTserial.print(";");
  BTserial.println(ssid);
  BTserial.print(";");
  delay(1000);

  WiFi.begin(ssid, password);
  delay(1000);

  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    BTserial.print(".");
    BTserial.print(";");
  }

  BTserial.println("");
  BTserial.print(";");
  BTserial.println("WiFi connected");
  BTserial.print(";");
  BTserial.println("IP address: ");
  BTserial.print(";");
  BTserial.println(WiFi.localIP());
  BTserial.print(";");
}

void stopWiFiAndSleep() {
  WiFi.disconnect();
  WiFi.mode(WIFI_OFF);
  WiFi.forceSleepBegin();
  delay(1);
}

void loop() {
  // read the value from the sensor:
  stopWiFiAndSleep();
  delay(1000);
  sensorValue = analogRead(sensorPin);

  BTserial.print(sensorValue);
  BTserial.print(";");

  delay(20);

  if (sensorValue < myThreshold) {
    BTserial.print("It's too dark in here, turning on Family Room Lights! ");
    BTserial.print(";");
 
    digitalWrite(ledPin, HIGH);
    delay(5000);
    wifiConnect();

    BTserial.print("connecting to ");
    BTserial.print(";");
 
    BTserial.println(host);
    BTserial.print(";");

    WiFiClient client;
    const int httpPort = 80;
    if (!client.connect(host, httpPort)) {
      BTserial.println("connection failed");
      BTserial.print(";");
      return;
    }

    String url = "/trigger/esp_darkness/with/key/";
    url += apiKey;

    BTserial.print("Requesting URL: ");
    BTserial.print(";");
    BTserial.println(url);
    BTserial.print(";");
    client.print(String("POST ") + url + " HTTP/1.1\r\n" +
                 "Host: " + host + "\r\n" +
                 "Content-Type: application/x-www-form-urlencoded\r\n" +
                 "Content-Length: 13\r\n\r\n" +
                 "value1=" + ldr_state + "\r\n");


    digitalWrite(ledPin, LOW);
    BTserial.print("Sleeping for 22 Hours");
    BTserial.print(";");
    stopWiFiAndSleep();
    delay(myDelay);
    digitalWrite(ledPin, HIGH);
    delay(minutes);
    digitalWrite(ledPin, LOW);
  }

}

 

installing Arduino libraries

Installing Arduino Libraries | Beginners Guide

IoT sewage project

Pumping poo! An IoT sewage project

ESP32 webOTA updates

How to update ESP32 firmware using web OTA [Guide + Code]

error message Brackets Thumbnail V1

expected declaration before ‘}’ token [SOLVED]

Compilation SOLVED | 1

Compilation error: expected ‘;’ before [SOLVED]

Learn how to structure your code

Leave a Comment