Humidity Temperature Light and Motion sensor with OpenHab

Last Updated on

This tutorial will describe how to build the sensor and how to connect it to Openhab.
You will need to have Mysensors gateway already installed. For this check my tutorial on how to build Mysensors gateway

What we will need:
Light Sensor BH1750 or
Light sensor LM393
Humidity/Temperature sensor
Arduino Pro Mini 3.3V (You can use the 5V version too. Dont forget Radio module is only 3.3V input)
Radio module
Dupont cable
Capacitor 47uF
USB module
Motion sensor

Connecting it together:
Connecting radio module
Connecting dht22 sensor
Connecting light sensor BH1750 or
Connecting light sensor LM393
Connecting motion sensor

Connecting USB is straight forward except one thing.The USB module give you 5V which is not good for radio module. You have to put the VCC from USB module to RAW on arduino instead of the VCC if you are using 3.3V arduino without regulator on radio module.

Here is the code for the arduino:

/**
 * The MySensors Arduino library handles the wireless radio link and protocol
 * between your home built sensors/actuators and HA controller of choice.
 * The sensors forms a self healing radio network with optional repeaters. Each
 * repeater and gateway builds a routing tables in EEPROM which keeps track of the
 * network topology allowing messages to be routed to nodes.
 *
 * Documentation: http://www.mysensors.org
 * Support Forum: http://forum.mysensors.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation.
 *
 */
#define  MY_DEBUG

#define MY_RADIO_NRF24
#define MY_NODE_ID 4
#include <SPI.h>
#include <MySensors.h>
#include <DHT.h>
#include <BH1750.h>
#include <OneWire.h>

#define CHILD_ID_HUM 0
#define CHILD_ID_TEMP 1
#define CHILD_ID_MOTION 2
#define CHILD_ID_LIGHT 3
#define HUMIDITY_SENSOR_DIGITAL_PIN 4
#define DIGITAL_INPUT_SENSOR 3   // The digital input you attached your motion sensor.  (Only 2 and 3 generates interrupt!)
#define INTERRUPT DIGITAL_INPUT_SENSOR-2 // Usually the interrupt = pin -2 (on uno/nano anyway)

unsigned long SLEEP_TIME = 120000; // Sleep time between reports (in milliseconds)

DHT dht;
BH1750 lightSensor;
uint16_t lastlux;
float lastTemp;
float lastHum;
boolean metric = true; 
MyMessage msgHum(CHILD_ID_HUM, V_HUM);
MyMessage msgTemp(CHILD_ID_TEMP, V_TEMP);
MyMessage msgLight(CHILD_ID_LIGHT, V_LIGHT_LEVEL);
MyMessage msgMotion(CHILD_ID_MOTION, V_TRIPPED);

void setup()  
{
  dht.setup(HUMIDITY_SENSOR_DIGITAL_PIN); 

  // Send the Sketch Version Information to the Gateway
  sendSketchInfo("MotionTemperature", "1.0");  
  pinMode(DIGITAL_INPUT_SENSOR, INPUT);      // sets the motion sensor digital pin as input

  // Register all sensors to gw (they will be created as child devices)
  present(CHILD_ID_MOTION, S_MOTION);
  present(CHILD_ID_HUM, S_HUM);
  present(CHILD_ID_TEMP, S_TEMP);
  present(CHILD_ID_LIGHT, S_LIGHT_LEVEL);
  lightSensor.begin();
  metric = getControllerConfig().isMetric;
}

void loop()      
{ 
  //DHT Humidity Temperature sensor
  //delay(dht.getMinimumSamplingPeriod());
  float temperature = dht.getTemperature();
  if (isnan(temperature)) {
      Serial.println("Failed reading temperature from DHT");
  } else if (temperature != lastTemp) {
    lastTemp = temperature;
    if (!metric) {
      temperature = dht.toFahrenheit(temperature);
    }
    send(msgTemp.set(temperature, 1));
    Serial.print("T: ");
    Serial.println(temperature);
  }
  
  float humidity = dht.getHumidity();
  if (isnan(humidity)) {
    Serial.println("Failed reading humidity from DHT");
  } else if (humidity != lastHum) {
    lastHum = humidity;
    send(msgHum.set(humidity, 1));
    Serial.print("H: ");
    Serial.println(humidity);
  }

  //BHT1750 light sensor
  uint16_t lux = lightSensor.readLightLevel();// Get Lux value
  Serial.print("Light: ");  
  Serial.println(lux);
  if (lux != lastlux) {
      send(msgLight.set(lux));
      lastlux = lux;
  }
  
  // Read digital motion value
  boolean tripped = digitalRead(DIGITAL_INPUT_SENSOR) == HIGH; 
        
  Serial.println(tripped);
  send(msgMotion.set(tripped?"1":"0"));  // Send tripped value to gw 
 
  // Sleep until interrupt comes in on motion sensor. Send update every two minute. 
  sleep(INTERRUPT,CHANGE, SLEEP_TIME);
}

After you turn on the sensor you should be able to simply add it in PaperUI in openhab.
Items example settings in Openhab.
Then you assign the items to discovered Things in Openhab PaperUI

Group   MySen2 "Sensor2" 
Number  MySen2_Humidity        "Humidity [%.1f %%]"              (MySen2)
Number  MySen2_Temperature     "Temperature [%.1f C]"       (MySen2) [ "CurrentTemperature" ]
Number  MySen2_Light           "Light [%s]"                     (MySen2)
Contact MySen2_TrippedStatus   "Motion [%s]"	(MySen2)
DateTime  MySen2_Update  "Updated [%1$td.%1$tm. %1$tH:%1$tM]"              (MySen2)

here is my PCB design you can copy if you wish. works well for case i use.

MotionTemperature_etch_silk_top

MotionTemperature_etch_copper_bottom

Leave a Reply

Your email address will not be published. Required fields are marked *