In this series, we will get the environment data of SensorTag from Raspberry Pi 3. After that, I will write Python code for wind analysis with Spark Streaming via Kafka. There are many samples on how to set up the Raspberry Pi 3. Here, we will prepare to easily acquire the environment data of SensorTag.
The Raspberry Pi 3 has built-in Wi-Fi and Bluetooth. Dongles are no longer needed and less items are required. In this example, an old MacBook Pro (macOS Sierra 10.12.6) with a USB-A port is used as the development terminal. Prepare a peripheral device that connects to the Raspberry Pi 3 via Ethernet.
Texas Instruments SensorTag can be connected to Raspberry Pi 3 via Bluetooth to easily acquire environmental data such as temperature and humidity.
Follow the official Installing operating system images on Mac OS procedure. Check the device name of the SD card and unmount it. In this example it is / dev / disk2.
$ diskutil list
...
/dev/disk2 (internal, physical):
#: TYPE NAME SIZE IDENTIFIER
0: FDisk_partition_scheme *15.6 GB disk2
1: Windows_FAT_32 NO NAME 15.6 GB disk2s1
$ diskutil unmountDisk /dev/disk2
[Download] Raspbian Jessie Lite (https://www.raspberrypi.org/downloads/raspbian/) and burn it to an SD card. Don't forget to enable SSH as well.
$ cd ~/Downloads
$ wget http://director.downloads.raspberrypi.org/raspbian_lite/images/raspbian_lite-2017-07-05/2017-07-05-raspbian-jessie-lite.zip
$ unzip 2017-07-05-raspbian-jessie-lite.zip
$ sudo dd bs=1m if=2017-07-05-raspbian-jessie-lite.img of=/dev/rdisk2
$ touch /Volumes/boot/ssh
$ diskutil unmountDisk /dev/disk2
macOS can be easily SSHed to the Raspberry Pi 3 with an Ethernet cable connection. By default, the following users are set.
$ ssh [email protected]
Copy the macOS public key to `~ / .ssh / authorized_keys' on your Raspberry Pi 3.
$ mkdir -p -m 700 ~/.ssh
$ cat << EOF > ~/.ssh/authorized_keys
ssh-rsa AAAA...
EOF
$ chmod 600 ~/.ssh/authorized_keys
Please log in again.
$ exit
Set up your wireless LAN access point (ESSID) and restart your network. Check your internet connection from ping
.
$ sudo sh -c 'wpa_passphrase [ESSID] [password] >> /etc/wpa_supplicant/wpa_supplicant.conf'
$ sudo ifdown wlan0
$ sudo ifup wlan0
$ ping -c 1 www.yahoo.co.jp
raspi-config
Change the password and timezone with raspi-config
.
$ sudo raspi-config
apt-get
Change to a Japanese mirror site and update the package. Install vim to edit the file.
$ sudo sed -i".bak" -e "s/mirrordirector.raspbian.org/ftp.jaist.ac.jp/g" /etc/apt/sources.list
$ sudo apt-get update && sudo apt-get dist-upgrade -y
$ sudo apt-get install vim -y
Turn on the SensorTag and scan from the Raspberry Pi 3. Check the BD address.
$ sudo hcitool lescan
...
B0:B4:48:BE:5E:00 CC2650 SensorTag
...
gatttool
Connect to the SensorTag of the BD address confirmed in the interactive mode of the gatttool
command.
$ gatttool -b B0:B4:48:BE:5E:00 --interactive
[B0:B4:48:BE:5E:00][LE]> connect
Attempting to connect to B0:B4:48:BE:5E:00
Connection successful
[B0:B4:48:BE:5E:00][LE]>
Get the sensor value by referring to Getting the value of TI sensor tag (CC2650) with Raspberry Pi I will.
[B0:B4:48:BE:5E:00][LE]> char-write-cmd 0x24 01
[B0:B4:48:BE:5E:00][LE]> char-read-hnd 0x21
Characteristic value/descriptor: 94 0b f0 0d
You will get a 4-byte value. According to SensorTag User Guide, the following data will be displayed in order.
ObjLSB ObjMSB AmbLSB AmbMSB
Converts 4 bytes obtained from Python REPL to Celsius and outputs ambient temperature and object temperature.
>>> raw_data = '94 0b f0 0d'
>>> rval = raw_data.split()
>>> obj_temp = int(rval[1] + rval[0], 16) / 4
>>> amb_temp = int(rval[3] + rval[2], 16) / 4
>>> obj_temp_celcius = obj_temp * 0.03125
>>> amb_temp_celcius = amb_temp * 0.03125
>>> print("Ambient temperature: {:.2f} ℃".format(amb_temp_celcius))
Ambient temperature: 27.88 ℃
>>> print("Object temperature: {:.2f} ℃".format(obj_temp_celcius))
Object temperature: 23.16 ℃
Install bluepy to operate Bluetooth LE devices from Python.
$ sudo apt-get install python-pip libglib2.0-dev -y
$ sudo pip install bluepy
You will be able to use the sensortag
command. Execute with the -T
flag and the BD address as an argument.
$ sensortag -T B0:B4:48:BE:5E:00
If you specify the -T
flag, you can get the ambient temperature and the object temperature in that order.
Connecting to B0:B4:48:BE:5E:00
('Temp: ', (28.34375, 24.25))
('Temp: ', (28.375, 23.28125))
...
SensorTag
Write a Python script to get the ambient temperature, object temperature, and humidity.
~/python_apps/temp.py
# -*- coding: utf-8 -*-
from bluepy.sensortag import SensorTag
import sys
import time
def main():
argvs = sys.argv
argc = len(argvs)
if (argc != 2):
print 'Usage: # python {0} bd_address'.format(argvs[0])
quit()
host = argvs[1]
print('Connecting to ' + host)
timeout = 5.0
count = 3
tag = SensorTag(host)
tag.IRtemperature.enable()
tag.humidity.enable()
time.sleep(1.0)
counter = 0
while True:
counter += 1
tAmb, tObj = tag.IRtemperature.read()
print("temperature:Surroundings: {0:.2f},object: {1:.2f}".format(tAmb, tObj) )
humidy, RH = tag.humidity.read()
print("Humidity: humidy: {0:.2f}, RH: {1:.2f}".format(humidy, RH))
if counter >= count:
break
tag.waitForNotifications(timeout)
tag.disconnect()
del tag
if __name__ == '__main__':
main()
Execute the script with the BD address of SensorTag as an argument.
$ python temp.py B0:B4:48:BE:5E:00
The result of measurement 3 times at 5 second intervals is output. The air conditioner is working, so it's comfortable.
Connecting to B0:B4:48:BE:5E:00
temperature:Surroundings: 25.03,object: 19.94
Humidity: humidy: 25.25, RH: 69.47
temperature:Surroundings: 25.06,object: 20.69
Humidity: humidy: 25.25, RH: 69.37
temperature:Surroundings: 25.06,object: 20.69
Humidity: humidy: 25.25, RH: 69.37
Recommended Posts