|Something's missing ... yes the USB bluetooth dongle|
Nowadays I use the iterative development method. Following Part 1 and Part 2, we used the Raspberry Pi Zero W as an IoT Bluetooth Gateway daily for about six months. It was more secure than using the smartphone to directly pair to the autogate bluetooth module HC-06 - the PIN is only 4 digits and easily brute-forced.. Using a WiFi repeater, its range was much better. We could now reliably operate the autogate without getting out of the car.
In addition the Pi Zero W gateway hogged the HC-06 all the time, and prevented any intruder from pairing to it. To gain access the intruder would now have to brute-force the WPA2 passphrase in the WiFi repeater, a much tougher opponent.
It also got turned on and off a lot to avoid being damaged by lightning, and sometimes it would fail to start up properly. This is usually due to HC-06 connectivity issues- perhaps it was not unpaired properly or was later that usual being discovered. This allowed the startup to be adjusted using my bash script in /etc/rc.d/rc.local.
Now that it was stable enough it was time to improve it further. From Part 2, Slackware 14.2 did not recognize the Raspberry Pi Zero W's builtin bluetooth module. Perhaps the Raspbian kernel I used was not current enough or perhaps my Slackware installation lacked something, but having to use an RM32 bluetooth dongle with an RM42.20 Raspberry Pi Zero W rankled.
I downloaded the Raspberry Pi NOOBS, and following the installation guide, installed the latest Raspbian OS into by Pi Zero W. Make sure to set up the WiFi connection to your broadband. The builtin bluetooth controller worked first time and had no trouble working with the HC-06 in the autogate. I repeated the procedure in Part 1, and upgraded the firmware/BIOS in the Slackware 14.1 image, but the builtin bluetooth controller did not come up.
Now I could have gone further and upgraded the Linux kernel as well, but perhaps it is time to work with Raspbian/Debian for a while. At least I would not have to keep up with the updates.
To use Raspbian for the IoT Bluetooth Gateway, I needed pymodbus, apache, and php. In Slackware, pymodbus needed upgrades to python 2.7, pip and pysetuptools. Checked in Raspbian:
root@raspi-0-w-2:/root# pip --version
pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7)
root@raspi-0-w-2:/root# python --version
Now that was a pleasant surprise: no upgrades necessary. Now for pysetuptools: Make sure your WiFi connection is working before you try this:
root@raspi-0-w-2:/root# apt-get upgrade python-setuptools
Reading package lists... Done
Building dependency tree
Reading state information... Done
python-setuptools is already the newest version (33.1.1-1).
python-setuptools set to manually installed.
Calculating upgrade... Done
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Again, no upgrades necessary. Might as well go ahead with pymodbus:
root@raspi-0-w-2:/root# pip install -U pymodbus
Installed successfully, no fuss. Now my Modbus server program needed twisted, so:
root@raspi-0-w-2:/root# pip install twisted
No problems there. Also required is cryptography:
root@raspi-0-w-2:/root# pip install cryptography
Now for apache. Before that just to be sure I checked for upgrades to Raspbian:
root@raspi-0-w-2:/root# apt-get update
No upgrades necessary- I am beginning to like Debian. Now for apache:
root@raspi-0-w-2:/root# apt-get install apache2 -y
No fuss, apache ran right out of the box (check using http://localhost/index.html):
root@raspi-0-w-2:/root# find /var -name index.html
root@raspi-0-w-2:/root# ps -ef
root 20567 2 0 14:14 ? 00:00:00 [kworker/0:3]
root 21396 1 0 14:16 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 21398 21396 0 14:16 ? 00:00:00 /usr/sbin/apache2 -k start
www-data 21399 21396 0 14:16 ? 00:00:00 /usr/sbin/apache2 -k start
root 21723 1705 0 14:18 pts/1 00:00:00 ps -ef
Similarly, php just worked, striaght from the box:
root@raspi-0-w-2:/root# apt-get install libapache2-mod-php php
The web server was linked to pymodbus using the same method as before. Now Raspbian defaults to using dynamic IP, and ssh disabled, which is not very useful if you are using the Pi in "headless" (ie without monitor, mouse or keyboard). ssh is easily enabled via the Raspbian desktop or using raspi-config. To get it to use static IP simply add the following lines to /etc/dhcpcd.conf:
Lastly Raspbian uses the much improved bluetoothctl, which is an interactive program and not easily included in a bash script like bluez. Luckily Linux has just the thing we need: 'expect' can be used to fool bluetoothctl into thinking it is interacting with a human via the keyboard.
root@raspi-0-w-2:/root# apt-get install expect
Put it in a little bash script and you have a much more robust startup script than simple-agent:
root@raspi-0-w-2:/root# cat ./hc-06.sh
set prompt "#"
set address [lindex $argv 0]
spawn sudo bluetoothctl -a
expect -re $prompt
send "remove $address\r"
expect -re $prompt
send "scan on\r"
send_user "\nDone waiting for controller\r"
send_user "\nSleeping ... waiting for autogate\r"
send_user "\nDone waiting for autogate\r"
send "scan off\r"
send "trust $address\r"
send "pair $address\r"
send_user "\nShould be paired now.\r"
Note: replace '0000' with your password and launch the script thus:
root@raspi-0-w-2:/root# ./hc-06.sh 12:34:56:78:9A:BC
Lastly bind to your bluetooth device to get the serial port /dev/rfcomm0:
rfcomm bind 0 12:34:56:78:9A:BC 1
And you are ready for the pymodbus program, which I launched in the background and captured its output to a log file:
root@raspi-0-w-2:/root# python ./autogate_server.py >> ./autogate.log 2>> ./autogate.log &
Raspbian turned out to be quite pleasant to use, but then it is Linux, so we expect nothing less.