Introduction: HomeKit HomeBridge Siri Enabled Arduino ESP8266 NodeMCU Based Temperature and Ambient Light Room Sensors for HomeKit Automation

About: I'm a lot of things. Galen Wollenberg https://hackaday.io/projects/hacker/246423 https://www.hackster.io/LagunaComputer

HomeKit HomeBridge Siri Enabled Arduino ESP8266 NodeMCU Based Temperature and Ambient Light Room Sensors for HomeKit Automation

by Galen Wollenberg

Build custom room temperature and light sensors for home automation via Siri / HomeKit / HomeBridge using NodeMCU/ESP8266/arduino microcontroller, a DS18b20 digital temp sensor, and a photocell.

I can put one of each of these in each room, and then use the iOs 'Eve' app to publish automatic HomeKit Automation rules and triggers. I.E.: "if it's dark in the bedroom, automatically turn on the Bedroom lights".

Next step, create a PIR motion sensor HomeBridge plugin class and add a motion sensor to each device/room.

Step 1: Parts

Parts:

1x NodeMCU module

1x 10k Photocell

1x DS18B20 temperature sensor

1x 2"x1.5" perf board

1x 10k or 1k resistor or 5k potentiometer (used here)

1x small piece of wire

1x 5v USB charger (i find the iPhone adapters to be the smallest and best)

1x usb cable end with exposed wires

1x HomeBridge installed on a Raspberry Pi and working.

Step 2: Install HomeBridge for HomeKit on a Raspberry Pi

Install HomeBridge for HomeKit on a Raspberry Pi

Test that HomeBridge and HomeKit is working on your iPhone.

See:

https://github.com/nfarina/homebridge

https://github.com/nfarina/homebridge/wiki/Running...

https://www.instructables.com/id/HomeKit-Enabled-Ar...

Ensure these plugin are installed. It may be installed by default in the newer versions:

https://github.com/lagunacomputer/homebridge-CurrentAmbientLightLevel

https://github.com/lucacri/homebridge-http-tempera...

https://www.npmjs.com/package/homebridge-http

Step 3: Edit the HomeBridge /var/homebridge/config.json File on the Raspberry Pi HomeBridge

the file may alternatively be in /home/.homebridge or /root/home/./homebridge. read the docs from the github link

https://github.com/nfarina/homebridge

Ensure this plugin is installed. It may be installed by default in the newer versions:

https://github.com/lucacri/homebridge-http-tempera...

https://github.com/lagunacomputer/homebridge-CurrentAmbientLightLevel

Program the NodeMCU using the Arduino IDE. Upon powering up (5V!) it should be seen on the network.

Try something like http://192.168.1.110/ . You should get a webpage returned. Assuming your NodeMCU pulls a DHCP ip of :192.168.1.110 (you should probably set a DHCP reservation on your router, for each ESP8266 you add) add this code to the config.json file. ( sudo nano /var/homebridge/config.json) etc:

{ 

"accessory": "HttpTemphum",

    "name": "Bedroom Temp",
    "url": "http://192.168.1.100/temp",
    "humidity": "false"
    "http_method": "GET"

},

mind the last comma, you may or may not need it if you have other accessories, or Homebridge is crashing on load.

Step 4: Build It

Build it

Photocell goes to A0 pin.

DS18B20 goes to D4 pin.

See these pinouts for basic schematic help:

USB +5 goes to NodeMCU Vin pin

USB -GND goes to NodeMCU GND

Step 5: Program NodeMCU / ESP8266 / Arduino With Code

<p>#include <spi.h><br>//#include <ethernet.h></ethernet.h></spi.h></p><p>byte mac[] = { 0x5C, 0xCF, 0x7F, 0x24, 0x84, 0x3D }; //physical mac address
//byte ip[] = { 192, 168, 1, 120 }; // ip in lan
//byte gateway[] = { 192, 168, 1, 1 }; // internet access via router
//byte subnet[] = { 255, 255, 255, 0 }; //subnet mask
//EthernetServer server(80); //server port</p><p>String readString; </p><p>#include <onewire.h>
#include <dallastemperature.h></dallastemperature.h></onewire.h></p><p>/*-----( Declare Constants )-----*/
#define ONE_WIRE_BUS D4 /*-(Connect to Pin 2 )-*</p><p>*-----( Declare objects )-----*/
/* Set up a oneWire instance to communicate with any OneWire device*/
OneWire ourWire(ONE_WIRE_BUS);</p><p>/* Tell Dallas Temperature Library to use oneWire Library */
DallasTemperature sensors(&ourWire);</p><p>/*-----( Declare Variables )-----*</p><p>/////////////////////</p><p>int photocellPin = 0;     // the cell and 10K pulldown are connected to a0
int photocellReading;     // the analog reading from the analog resistor divider</p><p>#include <esp8266wifi.h></esp8266wifi.h></p><p>IPAddress ip(192, 168, 1, 120); 
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);</p><p>const char* ssid = "yourwifissid";
const char* password = "yourwifipassword";</p><p>// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);</p><p>void setup(){
Serial.begin(115200);
  pinMode(5, OUTPUT); //pin selected to control
  //start Ethernet
  //Ethernet.begin(mac, ip, gateway, gateway, subnet);</p><p>WiFi.begin(ssid, password);
//WiFi.config(ip, gateway, subnet);
while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    //WiFi.begin(ssid, password);
    //Serial.print(".");
  }
  //Serial.println("");
  //Serial.println("WiFi connected");
  
  // Start the server
  server.begin();
  //Serial.println("Server started");</p><p>  // Print the IP address
  Serial.println(WiFi.localIP());</p><p>  
  //server.begin();
  //enable serial data print 
  //Serial.begin(9600); </p><p>sensors.begin();</p><p>  
}</p><p>void loop(){</p><p>readPhotocell();</p><p>float temperatureIndoor;
  float temperatureOutdoor;
  delay(100);
  // Create a client connection
  //EthernetClient client = server.available();
  WiFiClient client = server.available();
  if (client) {
    while (client.connected()) {
      if (client.available()) {
        char c = client.read();</p><p>        //read char by char HTTP request
        if (readString.length() < 100) {</p><p>          //store characters to string 
          readString += c; 
          //Serial.print(c);
        } </p><p>        //if HTTP request has ended
        if (c == '\n') {</p><p>client.println("HTTP/1.1 200 OK");<br>          client.println("Content-Type: application/json;charset=utf-8");
          client.println("Server: Arduino");
          client.println("Connnection: close");
          client.println();
          sensors.requestTemperatures(); // Send the command to get temperatures
          temperatureIndoor = (sensors.getTempCByIndex(0));
          //temperatureOutdoor = (sensors.getTempFByIndex(0));
 //Serial.print(sensors.getTempCByIndex(0));
          client.print("{");
          client.print("\"temperature\":");
          client.print(temperatureIndoor);
          client.print(",");
          //client.print("\"humidity\":");
          //client.print(temperatureOutdoor);
          
          //client.print("}");
          //client.println();
       
          //client.print(",");
          //client.print("{");
          client.print("\"lightval\":");
          client.print(photocellReading);
          client.print("}");
          client.println();</p><p>          //stopping client
          client.stop();</p><p>          ///////////////////// control arduino pin
          if(readString.indexOf("?on") >0)//checks for on
          {
            Serial.println(readString.indexOf("on"));
            
            digitalWrite(5, HIGH);    // set pin 5 high
            Serial.println("Led On");</p><p>          }
          if(readString.indexOf("?off") >0)//checks for off
          {
            Serial.println(readString.indexOf("off"));
            digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("Led Off");
          }
          if(readString.indexOf("?temp") >0)//checks for off
          {
            Serial.println(readString.indexOf("temp"));</p><p>            temperatureIndoor = (sensors.getTempCByIndex(0));
       
            //digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("temp sent");
          }
          if(readString.indexOf("?light") >0)//checks for off
          {
            Serial.println(readString.indexOf("light"));</p><p>       
            //digitalWrite(5, LOW);    // set pin 5 low
            Serial.println("light sent");
          }</p><p>         
          //clearing string for next read
          readString="";</p><p>//sensors.requestTemperatures(); // Send the command to get temperatures
//Serial.print("Device 1 (index 0) = ");
//Serial.print(sensors.getTempFByIndex(0));
//Serial.println(" Degrees F");</p><p>        }                     ///c=n
      }                //client available
    }             //client connected
  }         //if client
}      //loop</p><p>void readPhotocell(){</p><p>photocellReading = analogRead(photocellPin);  
 Serial.print(WiFi.localIP());
  Serial.print(" Analog reading = ");
  Serial.print(photocellReading);     // the raw analog reading</p><p>  // We'll have a few threshholds, qualitatively determined
  if (photocellReading < 10) {
    Serial.println(" - Dark");
  } else if (photocellReading < 200) {
    Serial.println(" - Dim");
  } else if (photocellReading < 500) {
    Serial.println(" - Light");
  } else if (photocellReading < 800) {
    Serial.println(" - Bright");
  } else {
    Serial.println(" - Very bright");
  }
          
}</p>

Step 6: Test the New Accessory in IOS HomeKit App

Test the new Accessory in iOS HomeKit App and Siri

Check out my Light Sensor plugin for HomeBridge / HomeKit !

https://github.com/lagunacomputer/homebridge-Curre...

Step 7: Create HomeKit Automation Rules and Triggers Within the IOS 'Eve' App From the App Store.

Create HomeKit Automation Rules and Triggers within the iOS 'Eve' app from the App Store.

Here I have created a Scene that turns off the Living Room Lamp.

Using the Triggers feature in the Eve iOS App (i assume you can do the same with an Apple TV4) this Scene is triggered when ever:

-the Living Room Light Sensor detects change, and

-the light sensed is below a value of < 589.6 (not real lux! its arduino values 0-1024), and

-the time is after 6:20pm, and

-the time is before 12:20am

....So, the Living Room Light turns on if it is dark in the room, and is between 6:20pm and 12:20am. Else, it is off unless manually commanded to turn on. Sweet!