Monitor & Control Your Room

by ofek tavor in Circuits > Arduino

456 Views, 2 Favorites, 0 Comments

Monitor & Control Your Room

DB.png

Do you want to know if someone is entering your room?
do you want to check if you forgot to close your TV and turn it off ?
if so , this guide may be helpful

in this guide we will go over 3 scenarios-

  1. how to check if some one is entering your room, and if so
    play a siren to let them know you know they are in your room
  2. check if the TV is ON and if so turn it OFF
  3. check the temperature in your room and turn on\off the AC

to make all the scenarios come true, i used CPX ( Circuit Playground Express) with WiFi ESP8266 .
the CPX will control all the sensors and will transmit and revive massages from MQTT Broker.

so let's start!

Supplies

you will need the following parts :

Circuit Playground Express connected with WiFi ESP8266

HC-SR04 Ultrasonic Sonar Distance Sensor

2 x 10K resistors

Half-size breadboard

Premium Male/Male Jumper Wires

Small Alligator Clip to Male Jumper Wire Bundle

and also :

install - node-red

have an available user on :
integromat

io.adafruit

Connecting the HC-SR04 to the CPX Board

FBGZ383JLQV51I1.jpg
FC1K3VHJLQV36FR.jpg
FLU87Z8JLQV35CH.jpg
sensors_cpx-hcsr06-breadboard-crocclipse_bb.png

This part is taking from the article :

https://learn.adafruit.com/distance-measurement-ultrasound-hcsr04/connect-the-sensor

I recommend to read it and follow the instructions


here are the main parts:

The common HC-SR04 boards are designed for 5V TTL voltage levels. Since the advent of CMOS many circuits started using 3.3V levels including the Adafruit CPX board, this can create compatibility problems. The guide to Adafruit CPX Pinouts states:
All of the GPIO pads are 3.3V output level, and should not be used with 5V inputs. In general, most 5V devices are OK with 3.3V output though.

This creates a requirement to reduce the voltage from the Echo output of the HC-SR04 rather than simply directly connecting all of the sensor's pins to the CPX board. The easiest way to reduce a single voltage is to use two resistors as a potential divider. The example shown uses two equal resistors to halve the voltage. 2.5V is clearly lower than 3.3V but this is high enough to work. The sum of the two resistors should be above 1 kiloohm to keep within the current limits of the sensor.

In the breadboard example everything is connected directly to the CPX board apart from the Echo output which goes through the two 2.2 kiloohm resistors. The CPX input A1 pin connects in the middle of this pair.

The connections are:

Black: Gnd (row 21) to GND and row 1 is connected to ground too.
Red: Vcc (row 24) to VOUT.Green: Echo (row 22) to potential divider (row 11, a pair of 2k2 resistors across row 6 and row 1).
White: divided voltage (row 6) to A1.
Yellow: Trig (row 23) to A2.

A0 can be used as an output for the sensor's Trig pin if you want the surprise of hearing the trigger pulses. The CPX board has A0 hard wired up to its small speaker.

For comparison: the Raspberry Pi has the same 3.3V limitation on GPIO inputs; many of the Arduino boards like the Uno are 5V tolerant for inputs, allowing direct connection of this sensor.

Check the Sensor Measuring

So after you have connected the sensor
it's time to see it in action,
I used the attached script to check the sensor

now, it is time for some measuring:

  • put the sensor in parallel to your room entrance
  • measure the sensor output - we will call it - Normal state
  • walk in and out of your room
  • go over the sensor output and take an average sample
  • Threshold = Normal state - average sample

we will use the Threshold and the Normal state to detect if someone is entering the room

MQTT Broker Configuration

DB.png

I used the free and awesome MQTT Broker of io.adafruit.
complete the following steps :

  1. registrar to io.adafruit
  2. create the following feeds:
    • temperature - will be used to send messages about temperature
    • alarm - will be used to send messages about the presence of someone in the room
    • TV - will be used to send messages about the status of the TV ( is it On/Off)
    • TvPower - will be used to send command to the CPX to turn the TV on\off
    • ACPower - will be used to send command to the CPX to turn the AC on\off
    • alramPlay - will be used to send command to the CPX to make alarm sound
  3. create a new dashboard, inserts new blocks like in the picture - you will need
    • 3 buttons - for the alarm , ACpower and TVpower
    • 2 indicators - to see if the TV is Off/On and if someone is in the room
    • 1 gauge - to measure your room temperature

Create Integromat Scenario

integromat_flow.png

Since we don't want to check the dashboards every 5 sec to learn if someone is in the room
we will use integromat service to get a message via Telegram if some one is entering the room.


first , create a Telegram bot , add him to a new group
if you don't know how to create a bot - well there is a bot for it https://telegram.me/botfather

now go to integromat , and create a new scenario
in the scenario we will use

  • a webhook
  • Integromat option to send a message via Telegram bot

apply the needed configuration , and run the scenario

Create Node - RED Flow

node-red-flow.png

to check the TV status , and to actives integromat's webhook we will use node red.

Node red will make ping to our TV ever minute to check it status
to check your TV IP - use angry ip scanner

Node -RED flow will do the following

1 .

  • connect to io.adafruit MQTT broker and subscribe to alarm feed
  • when a message received it will make a HTTP request to our integromat webhook

2.

  • Node red will ping your TV IP address - and will send the result to io.adafruit MQTT broker in TV feeds

note - node red ping return a integer value of the TTL ping request if the request succeed -
if not it will return a Boolean value - False
i used the function option to change the result to 1 if the ping request succeed , and 0 if not
the function code:

if(msg.payload == false){
msg.payload = 0;
return msg; }

else{
msg.payload = 1;
return msg; }

when you made all the described configurations just press deploy .

CPX - Code

now is the time to upload some code to CPX.
we have 4 files :
c&c_room - the main sketch file, handles all the connection to the MQTT Broker
alarm - makes the CPX play alarm sounds
dis_check - check the distance using the Ultrasonic Sonar Distance Sensor
power - send IR commands to TV\AC

some things you should edit when using this scripts

c&c_room - you need to edit the following:

  • io.adafruit user name and password

  • WIFI user name and password

dis_check - you need to edit the following:

  • edit the NORMAL_DISTANCE and THRESHOLD_DISTANCE with the number you recorded in step 2

power- you need to edit the following:

  • edit the IR commends for AC and TV

you can use the CPX Infrared_Read.ino Example sketch to discover you AC and TV commends

once you have completed the needed change , upload the sketch and start testing every thing
hope you will enjoy the end result :)