Bluetooth Low Energy (BLE) for temperature, humidity, pressure, illuminance, and battery level measured by TI SensorTag I sent it to Raspberry Pi3 (RPi3) and sent it from RPi3 to IoT data visualization service "Ambient" to graph the data.
Added 2017/6/1. I wrote that "Sensor Tag is difficult to use as a terminal for long-term measurement because it will enter sleep mode and communication will not be possible if it is left for 2 minutes in the advertised state", but if it is left connected, it will not enter sleep mode Therefore, I found that it can be used as a terminal for long-term measurement without any problem. For details, see "Measure temperature, humidity, etc. with SensorTag and send it to Ambient via Raspberry Pi 3 for graphing".
SensorTag
SensorTag has 3 types of devices with different communication modules. This time I used a "multi-standard SensorTag" that communicates via BLE. Sensors include ambient and surface temperature sensors, humidity sensors, barometric pressure sensors, ambient light sensors, acceleration sensors, gyroscopes, compasses, and magnetic sensors.
The SensorTag goes into sleep mode after being left alone for 3 minutes. Although it is designed to save power, it is difficult to use as a terminal that can be installed somewhere to measure the temperature for a long period of time because communication cannot be performed unless the power button is pressed again.
This time, I focused on this difficult-to-use specification and conducted a test to send data from SensorTag to Ambient via RPi3.
"Multi-standard SensorTag" communicates with BLE. So I used Raspberry Pi3 (RPi3) as a gateway, got SensorTag data from RPi3 with BLE, and sent it to Ambient. The program was written in Python. I used bluepy as the library. First, install bluepy on RPi.
pi$ sudo apt-get install python-pip libglib2.0-dev
pi$ git clone https://github.com/IanHarvey/bluepy.git
pi$ cd bluepy
pi$ python3 setup.py build
pi$ sudo python3 setup.py install
Then install Ambient's Python library as well.
pi$ sudo pip install git+https://github.com/TakehikoShimojima/ambient-python-lib.git
The general flow of a Python program that retrieves data from a SensorTag is as follows.
When I ran this program, I was able to confirm that the SensorTag data was sent to Ambient and graphed.
pi$ sudo python3 st2ambient.py
scanning tag
found SensorTag, addr = 24:71:89:bc:63:84
{'d4': 81, 'd5': 336.32, 'd2': 45.806884765625, 'd1': 25.1875, 'd3': 1016.45}
200
The following is displayed on the Ambient screen.
The program will be posted at the end. This time it is a test to send data from SensorTag to Ambient via RPi3, so if SensorTag goes into sleep mode, you need to manually press the power button to restart, and Python programs have multiple SensorTags. Does not consider the case. These are the next challenges.
# -*- coding: utf-8 -*-
# while True:
#Scan the SensorTag, connect to the device you find,
#Get temperature, humidity, barometric pressure, illuminance, battery level and send to Ambient
#
import bluepy
import time
import sys
import argparse
import ambient
def main():
channelId =Channel ID
writeKey = 'Light key'
parser = argparse.ArgumentParser()
parser.add_argument('-i',action='store',type=float, default=120.0, help='scan interval')
parser.add_argument('-t',action='store',type=float, default=5.0, help='scan time out')
arg = parser.parse_args(sys.argv[1:])
scanner = bluepy.btle.Scanner(0)
am = ambient.Ambient(channelId, writeKey)
while True:
dev = None
print('scanning tag...')
devices = scanner.scan(arg.t) #Scan BLE
for d in devices:
for (sdid, desc, val) in d.getScanData():
if sdid == 9 and val == 'CC2650 SensorTag': #The local name is'CC2650 SensorTag'Find one
dev = d
print('found SensorTag, addr = %s' % dev.addr)
sys.stdout.flush()
if dev is not None:
tag = bluepy.sensortag.SensorTag(dev.addr) #Connect to the device you find
tag.IRtemperature.enable()
tag.humidity.enable()
tag.barometer.enable()
tag.battery.enable()
tag.lightmeter.enable()
# Some sensors (e.g., temperature, accelerometer) need some time for initialization.
# Not waiting here after enabling a sensor, the first read value might be empty or incorrect.
time.sleep(1.0)
data = {}
data['d1'] = tag.IRtemperature.read()[0] # set ambient temperature to d1
data['d2'] = tag.humidity.read()[1] # set humidity to d2
data['d3'] = tag.barometer.read()[1] # set barometer to d3
data['d5'] = tag.lightmeter.read() # set light to d5
data['d4'] = tag.battery.read() # set battery level to d4
tag.IRtemperature.disable()
tag.humidity.disable()
tag.barometer.disable()
tag.battery.disable()
tag.lightmeter.disable()
print(data)
r = am.send(data)
print(r.status_code)
sys.stdout.flush()
time.sleep(arg.i)
# tag.waitForNotifications(arg.t)
tag.disconnect()
del tag
if __name__ == "__main__":
main()
Recommended Posts