Building Bluetooth Support – Bluez

I want each of my Raspberry Pi headless servers to scan for Bluetooth Low Energy (BLE) iBeacons. I’ve included iBeacon advertising in my DiyHasMobile smartphone app. The goal is to track the location of my iPhone in my house.

Build Bluez From Source

I read a “learn” blog on Adafruit which recommends building bluez from source. This is standard build process but it will take some time. Create directory to contain source, etc.

mkdir bluetooth
wget http://www.kernel.org/pub/linux/bluetooth/bluez-5.48.tar.xz
tar xvf bluez-5.48.tar.xz
cd bluez-5.48

Ok now start the build process

sudo apt-get update
sudo apt-get install -y libusb-dev libdbus-1-dev libglib2.0-dev 
sudo apt-get install -y libudev-dev libical-dev libreadline-dev

This is optional but I like to sync after major steps.

sudo sync

This will take a little time to complete ( 10 minutes maybe )

./configure
make
sudo sync
sudo make install

Add BLE Support

Modify the bluetooth.service to support BLE services. Use the –experimental option

systemctl status bluetooth
sudo systemctl enable bluetooth
sudo vi /lib/systemd/system/bluetooth.service

This is a single line edit.

ExecStart=/usr/local/libexec/bluetooth/bluetoothd --experimental

Update the daemon and reload

sudo systemctl daemon-reload
sudo systemctl restart bluetooth
systemctl status bluetooth

If all good then reboot and plan your next steps like installing python support for bluez

sudo reboot

Upgrading to Python 3.6.3

I found a great blockchain.py project on Github this week. The first challenge is the requirement for the very latest version of Python 3.6 or higher.

Learn Blockchains by Building One

I found a the instruction and started work on a newly installed headless Raspberry Pi server.

Installing Python 3.6.2 on raspberry pi (raspbian)

Start by upgrading the build environemnt.

sudo apt-get update 
sudo apt-get install build-essential tk-dev libncurses5-dev  
sudo apt-get libncursesw5-dev libreadline6-dev libdb5.3-dev  
sudo apt-get libgdbm-dev libsqlite3-dev libssl-dev libreadline6-dev 
sudo apt-get libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev 
sudo apt-get libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev

Next, download the latest version and make python3.6.3

wget https://www.python.org/ftp/python/3.6.3/Python-3.6.3.tar.xz 
tar xf Python-3.6.3.tar.xz 
cd Python-3.6.3
./configure --enable-optimizations 
make 
sudo make altinstall

Now to test

Raspberry Pi Python Tool Chain

Introduction

I didn’t like Python when I was first introduced to it a couple of years ago. Since that time I’ve learned that it is a great quick-n-dirty language that can be used to build an app or what we used to call a “hack”. The extent of its built-in features and the enormous amount of open source code makes it a natural do-it-yourself language.

Install Tool Chain or Installation Scripts

The first step is to install several tools to facilitate installation of Python modules.

sudo apt-get install -y python-pip git
sudo apt-get install -y build-essential python-dev python-smbus
sudo apt-get install -y i2c-tools

I like to use Flask so install the following for a basic environment.

sudo pip install flask 
sudo pip install requests 
sudo pip install flask-httpauth

My do-it-yourself home automation system uses MQTT for messaging. My Python apps subscribe and publish using the Paho implementation.

sudo pip install paho-mqtt

I like to use Adafruit’s I2C backpack for LED and OLED devices. Their libraries usually require the Python imaging module.

sudo apt-get -y install python-imaging python-pil

Add Adafruit libraries for the most common devices. I like to use the GPIO, LED backpack and OLED displays for almost every project.  Upgrade pip to use setup wheel

sudo python -m pip install --upgrade pip setuptools wheel

Start with the GPIO stuff.

git clone https://github.com/adafruit/Adafruit_Python_GPIO.git
cd Adafruit_Python_GPIO
sudo python setup.py install

Now install the LED backpack.

 

Setting up a systemctl service on a Pi

Each Internet of Things (IOT) device in my Do It Yourself Home Automation System (Diyhas) has three elements.

  1. Headless Raspberry Pi server with Wi-FI and Bluetooth (Pi Zero W for example)
  2. Build and production Python directory structure with my device or subsystem Python modules and a variety of Open Source modules.
  3. Boot configuration using systemctl

The systemctl process is used to manage the IOT application.

Configuring a Basic systemctl Service Configuration File

I’m going to use my basic DiyhasClock.py application as an example. My basic file structure requires to three directories: ../fonts, ../logs and ../systemd. The DiyhasClock service requires a configuration file called DiyhasClock.service. This file has three sections:

  • [Unit] — contains options not dependent on the type of the service. It contains directives like description, service behavior and dependencies with other services.
  • [Service] — categorizes services by their process and daemonizing behavior
  • [Install] — optional and defines the behavior if the service is enabled or disabled

The first step is to create a service configuration file in development directory.

cd /home/pi/systemd
vi DiyhasClock.service

The first section of the configuration file contains a [Unit] with a simple Description and the After directive which states that it requires multi-user run level.

[Unit]
Description=DiyhasClock
After=multi-user.target

The [Service] section has two directives. The Type specifies that the service runs after the boot sequence is complete, i.e., idle. The ExecStart is the full command and path to execute. In my case I use Python, the directory of the Python code and a log file.

[Service]
Type=idle
ExecStart=/usr/bin/python /home/pi/systemd/DiyhasClock.py > /home/pi/logs/DiyhasClock.log 2>&1

The remaining section is used to add DiyhasClock.service to the multi-user run level when the Raspberry Pi server is booted.

[Install]
WantedBy=multi-user.target

The next step is to move the completed DiyhasClock.service to the systemctl directory.

Adding the DiyhasClock Service to the Boot Sequence

The next steps are relatively simple. Move the DiyhasClock.service to the systemctl directory and enabling the service.

sudo cp DiyhasClock.service /lib/systemd/system/DiyhasClock.service
sudo chmod 644 /lib/systemd/system/DiyhasClock.service
sudo systemctl daemon-reload
sudo systemctl enable DiyhasClock.service

Reboot and test the installation. For convenience I always create an aliases file to manage my IOT applications.

cd 
vi .bash_aliases

The file contains the following systemctl specific aliases.

alias stop='sudo systemctl stop DiyhasClock.service'
alias status='sudo systemctl -l status DiyhasClock.service'
alias start='sudo systemctl start DiyhasClock.service'
alias restart='sudo systemctl restart DiyhasClock.service'
alias catlog='cat /home/pi/logs/DiyhasClock.log'
alias taillog='tail -f /home/pi/logs/DiyhasClock.log'

That’s how I manage an IOT production device.

Creating a New Headless Pi Server

Introduction

Every time I receive a new Raspberry Pi, I go through the same error prone discovery process of creating my personal development environment. I know its going to be Python based with Flask, Paho MQTT, Adafruit modules, my own Python models and a standard pi user file structure. I also setup a “systemd” init configuration and modify the .bash_aliases file with “status”, “restart”, “stop”, etc. to manage the process started by the boot sequence.

Since I don’t have the space to add a USB keyboard and mouse to my already crowded desk, I’ve settled on headless servers. IMHO a headless Raspberry Pi server is also an ideal Internet of Things host. This “how to guide” is for Mac OS users like myself.

Getting Started

Go to the Raspberry Pi download page, select the latest “lite” image. You will need to unpack or unzip the file.  Raspian Lite Image

Use Etcher to copy the img file to your SD card. It is usually /dev/disk2. Use Etcher to burn the img to the SD card

Ok, now the fun begins. A headless server needs SSH enabled. Simply create an empty file call “ssh” on the boot level.

cd /Volumes/boot
touch ssh

If you can’t see /Volumes/boot try reinserting the SD card.  You should see the boot volume on Finder.

I’ve standardized on Wi-Fi for most of my devices, which is a must for the Raspberry Pi Zero W. The next step is to configure your local Wi-Fi access point.

vi wpa_supplicant.conf

Insert the following into the blank file on the boot level. NO SPACES! Don’t forget to use the correct SSID and PASSWORD.

ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
network={
ssid="SSID"
psk="PASSWORD"
key_mgmt=WPA-PSK
}

Eject the SD card using finder. Insert it into your Raspberry Pi, power it up and cross your fingers!

First Login to Pi Zero

Special instructions for the Raspberry Pi Zero are needed to attach a Wi-Fi dongle to the USB port. First connect your Pi Zero to your network with a RJ45 connector. Boot the Pi Zero and update/upgrade the OS to support USB/Wi-Fi.

sudo rpi-update
sudo apt-get update
sudo apt-get -y upgrade
sudo apt-get -y dist-upgrade

If you get a command not found error on the rpi-update then simply install it with apt-get. Some earlier distributions did not include this command.

sudo apt-get install rpi-update

The latest kernel has changed the /etc/network/interfaces file to use /etc/network/interfaced.d files. What I found that works is to add the following lines to the end of the /etc/network/interfaces file.

cd /etc/network
sudo vi interfaces

Add the following to the end of the file.

allow-hotplug wlan0
iface wlan0 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

allow-hotplug wlan1
iface wlan1 inet manual
    wpa-conf /etc/wpa_supplicant/wpa_supplicant.conf

This may mess up dhcp but I’ve just started working with the Pi Zero USB/Wi-Fi.

First login to Pi Zero W

Login the new server and run raspi-config to set password, hostname and enable I2C.

ssh pi@raspberrypi.local
sudo raspi-config
  • Change the password and hostname
  • Expand memory
  • Enable the camera and I2C bus

Update the operating system with latest.

sudo rpi-update
sudo apt-get update
sudo apt-get -y upgrade 
sudo apt-get -y dist-upgrade

Configure the Apple Bonjour service and Apple networking

sudo apt-get install avahi-daemon
sudo systemctl enable avahi-daemon.service
sudo apt-get -y install netatalk

Edit the .bashrc file to enable ll and other aliases. See systemctl

vi .bashrc

Reboot and test your installation.

 

Hands Free Kitchen Light (IOT)

My new project is a Internet of Things kitchen counter LED light strip. The goal was to implement a 21st century light switch using bright and energy efficient LED lights. This required the replacement the mechanical switch with a hand-motion proximity sensor for on and off. Other features include:

  • ESP-01 microprocessor and web server to control the light – an important maker CPU
  • Motion sensor to turn off the light, when the kitchen is unoccupied for a user defined time interval
  • 12 volt power with MOSFET (Adafruit IRLB8721) switch for the LEDs and a 3.3 volt switching regulator for the system components
  • The user interface was to be a simple Web site for status, clock and system configuration, and remote light control, that could also update the ESP-01’s  EEPROM
  • OLED 128×64 display for system status, debug stuff and Wi-Fi network information

The ESP-01 presents a couple of problems, because it only has two GPIO ports. I needed to use the I2C bus to communicate with the OLED, which used both GPIO. So I decided to use an MCP23008 IC to handle interrupts (INPUT) and to control the MOSFET (OUTPUT). This enabled me to use I2C bus and added 8 more GPIO ports.

I used two two proximity devices on this project. The first is the PIR motion sensor. I really like the Parallax, Inc. PIR Sensor – Rev B, because it can operate on 3.3 volts and has a red LED to indicate detection of motion. The second proximity sensor is the Adafruit, VCNL4010 Proximity/Light sensor. It measures both near proximity and ambient light. The VCNL4010 uses the I2C bus to communicate data and it has a configurable interrupt signal.  I used the MCP23008 to handle both interrupts – PIR and VNCL4010.

IMG_1886

Breadboard prototype

I added a cool on/off switch from Adafruit as the final touch. The Rugged Metal On/Off Switch with Blue LED Ring – 16mm Blue On/Off looks great.

The software features included: (a) NTP time set, (b) AP mode when the ESP-01 could not connect to its predefined SSID/password, (c) defaults in EEPROM, (d) my version of the VNCL4010 library, (e) my MCP23008 library with interrupt handling, and (f)  RESTful interface for the website configuration stuff.

The next step was to fit everything in a small wooden box that I would mount on the wall.

IMG_1908

Beta version in a small wooden box

I had a lot of trouble with the VNCL4010 when it was installed in the box. I need to spend time determining how much of the PCB board needs to be exposed from a hole or mount in the box.  My first attempt blinked on and off after a couple of minutes.

Below is a short video of the light working. I’m planning to move everything to an Adafruit proto-board and finding a smaller box.

Next steps in a couple of weeks.

 

 

Adafruit.IO Prototype

Amazon, Intel, IBM and many other companies have started building Internet of Things platforms and cloud services. These services are exactly what I’ve been looking for as a way to safely and securely gain Internet access to my home automation system. I buy a lot of great products from Adafruit and discovered they have an IOT platform.  Adafruit.IO is a beta version and I started experimenting with their MQTT SDK this week.

My plan was to build a standalone test bed to learn how to publish sensor data to Adafruit.IO and subscribe to switches and triggers. I built the breadboard below with an ESP-1 to handle the MQTT interface and an ATMega328 to process environment sensors and the alarms (flashing LED and loud piezo buzzer).

2016-02-10 07.20.15

Power was one challenge. I needed 3.3 volts for the ESP-1 and the ATMega328 (LM1117).  A 5 volt rail (7805)was needed for the MQ2 and PIR sensors. A 12 volt rail (power supply rated at 1 amp) was needed for the flashing LED and a loud piezo (not shown).

Programming two processors is an added complication but I’ve got the process down and its pretty reliable. The Arduino IDE is not state of the art but its much better than some of the development environments I used to work with in the olden days.

The photo below is my first prototype of a dashboard from Adafruit.IO

AdafruitDashboard1

I’ve created feeds for humidity, temperature and light. The alarm button turns the flashing LED alarm light on and off pretty quickly. I’ll add gas and motion later tonight. I also want to test the trigger capability as well.

Sensors

  • MQ2 gas sensor
  • DHT11 temperature and humidity
  • Photo resistor light
  • PIR motion sensor

That’s it for now and it took me only a couple of days to get it working. The Adafruit.IO forum was also helpful and support was responding almost as fast as I posted questions.

 

Fireplace Mantel Christmas Project

I created an interactive fireplace decoration using an ATmega328, two 8×8 Matrix LED backpacks, a 128×64 OLED, a piezo buzzer and an IR remote sensor. Basic functions include the following

  • 8×8 LED depict snow falling, a flashing light tree and “Merry Christmas” scrolling marque
  • OLED wishes you a Merry Christmas, and has Naughty and Nice lists with names that can change
  • Piezo buzzer plays Jingle Bells
  • IR Remote controls both displays; plays Jingle Bells; and adds people to the Naughty and Nice lists.

I soldered one of the I2C address pads on one of the 8×8 Matrix LED to differentiate displays (0x70 and 0x71). I used an antique wooden glove box as the project box.

Mantel

The video demonstrates the simple functions of this fireplace decoration.