Stream processing for Python and SensorTag, Kafka, Spark Streaming --Part 1: Raspberry Pi 3

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.

What to prepare

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.

Burn Raspbian Lite image on macOS

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

SSH connection with mDNS

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

Wifi

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

Bluetooth and SensorTag

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 ℃

bluepy sensortag command

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

Stream processing for Python and SensorTag, Kafka, Spark Streaming --Part 1: Raspberry Pi 3
Stream processing for Python and SensorTag, Kafka, Spark Streaming --Part 1: Raspberry Pi 3
Streaming Python and SensorTag, Kafka, Spark Streaming-Part 5: Connecting from Jupyter to Spark with Apache Toree
Streaming Python and SensorTag, Kafka, Spark Streaming-Part 5: Connecting from Jupyter to Spark with Apache Toree
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
[Python] Measures and displays the time required for processing
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Install pyenv on Raspberry Pi and version control Python
Raspberry Pi + python + IoT device, environment construction procedure to start image processing and machine learning
Update Python for Raspberry Pi to 3.7 or later with pyenv
Run AWS IoT Device SDK for Python on Raspberry Pi
Create an LCD (16x2) game with Raspberry Pi and Python
Access google spreadsheet using python on raspberry pi (for myself)
Translate I2C device driver for Arduino to Python for Raspberry pi
Measure temperature, humidity, etc. with SensorTag and send it to Ambient via Raspberry Pi 3 to graph it Part 2
Building a Raspberry Pi for studying Python and machine learning (RaspberryPi4 & Buster version (RaspberryPi3 is also possible))
Image processing with Python (Part 2)
Image processing with Python (Part 1)
Raspberry Pi + Python + OpenGL memo
Image processing with Python (Part 3)
raspberry pi 1 model b, python
[Python] Iterative processing (for, while)
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
Link SORACOM, home appliances and LINE Bot [Python / Flask / Raspberry Pi]