IOT

Smart Phone Controlled Home Automation with Raspberry Pi

This tutorial is all about utilizing a Raspberry Pi and Bluetooth Low Energy for home automation. Automating household environment equipment is a component of home automation. To do this, we developed a smart light that can be operated from a distance using a smartphone app.

The project’s goal is to let you use your smartphone to operate a variety of home appliances. This example demonstrates how to remotely operate a Raspberry Pi’s GPIO pin using a smartphone (or another BleuIO Dongle).

Hardware Requirements

Hardware Setup & Connection

Here is the hardware setup circuit for Raspberry Pi-based home automation that can be controlled by a smartphone. The relay can be connected to Raspberry Pi using the wiring shown below.

Raspberry Pi Relay

Beware: When experimenting with AC, be very careful because electrical shock might cause serious injury.

Instructions for bleuio_rpi_switch_example.py

  • Connect your RaspberryPi to the BleuIO Dongle.
  • Change the GPIO pin you want to utilize in the variable “switch” in the script. (You can use the command pinout to get a graphic representation of the board’s GPIO pins.)
  • Finally, just execute the Python script, connect your phone to the BlueIO Dongle, and send messages to turn on and off the GPIO!

Instructions for connecting to the BleuIO from mobile

  • Get a BLE scanning app that can connect to a device and read/write to it. (Such as BLEScanner or nRFConnect)
    Android, IOS
  • Look for the dongle, it will be advertising as ‘BleuIO’.
  • Connect to the BleuIO Dongle.
  • You must first write 0x01 to the Flow Control characteristic in order to make BleuIO capable of receiving commands.(UUID:0783b03e-8535-b5a0-7140-a304d2495cb9)
  • Now you can write to the Server RX Data characteristic (UUID: 0783b03e-8535-b5a0-7140-a304d2495cba) to control the GPIO.
    “SW=1”  for  ON
    “SW=0” for OFF

Source Code/Python Script

Here is the Python code for Smart Phone Controlled Home Automation with Raspberry Pi that helps regulate the light and receives messages via a smartphone app.

#!/usr/bin/python3
# Copyright 2022 Smart Sensor Devices in Sweden AB
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”),
# to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense,
# and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
# The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

import time
import serial.tools.list_ports
import serial
import RPi.GPIO as io


switch = 7 # Edit this to suit your setup! (7 = GPIO 04), use command pinout to graphically show you the GPIO pins for the board
io.setmode(io.BOARD)
io.setup(switch, io.OUT)

master_array = []
index = 1
dongle_port = “”

print(“\nWelcome to BleuIO RaspberryPi Switch Example!\n”)

print(“\nPlease insert dongle…”)
try:
while len(master_array) == 0:
m_ports = serial.tools.list_ports.comports(include_links=False)
for port in m_ports:
if str(port.hwid).__contains__(“VID:PID=2DCF”):
master = port.device + ” ” + port.hwid
if master.__contains__(“VID:PID=2DCF:6002”):
print(“Found dongle in port: %s” % port.device)
master_array.append(master)
dongle_port = port
break

for dongle in master_array:
print(“\nConnecting to BleuIO @ %s\n” % dongle)

time.sleep(0.5)
dongle_conn = serial.Serial(
dongle_port.device,
115200,
timeout=1,
)

if not dongle_conn.is_open:
dongle_conn.open()

print(“Starting Advertising…”)
dongle_conn.write(“AT+GAPDISCONNECTALL\rAT+DUAL\rAT+ADVSTART\rATI\r”.encode())
read_tries = 0
dongle_resp = “”
while read_tries < 20:
dongle_resp = dongle_conn.readline().decode()
if “Not Advertising” in dongle_resp:
dongle_conn.write(“AT+ADVSTART\r”)
if b”Advertising\r\n” in dongle_resp.encode():
break
read_tries += 1
time.sleep(0.01)

if dongle_resp:
print(“BleuIO is %s” % dongle_resp)
else:
print(“ERROR! No response…”)
exit()

print(
“Going into loop, waiting for signal to turn switch on/off…\n(Press Ctrl+C to abort)”
)
while True:
try:
dongle_resp = dongle_conn.readline().decode()
if “SW=0” in dongle_resp:
print(“Turn Switch off!”)
io.output(switch, io.LOW)
if “SW=1” in dongle_resp:
print(“Turn Switch on!”)
io.output(switch, io.HIGH)
except KeyboardInterrupt:
if dongle_conn.is_open:
dongle_conn.write(“AT+GAPDISCONNECTALL\rAT+ADVSTOP\r”.encode())
dongle_conn.close()
io.cleanup()
print(“\nBye!”)
exit()

except Exception as e:
print(“(ERROR: %s)” % (e))

Testing Home Automation Project with Raspberry Pi

We have tested the script by turning on and off the light bulb using the nRFConnect app on both iOS and Android smartphones. The result of this effort is shown here.

Leave a Reply

Your email address will not be published.