Send the temperature, humidity, etc. measured by SensorTag to Ambient via Raspberry Pi 3 and graph it.

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

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.

Relay to Ambient with Python on Raspberry Pi 3 (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. sensortag_ambient

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

Send the temperature, humidity, etc. measured by SensorTag to Ambient via Raspberry Pi 3 and graph it.
Measure temperature, humidity, etc. with SensorTag and send it to Ambient via Raspberry Pi 3 to graph it Part 2
I connected the thermo sensor to the Raspberry Pi and measured the temperature (Python)
Measure temperature and humidity with Raspberry Pi3 and visualize with Ambient
Read the data of the NFC reader connected to Raspberry Pi 3 with Python and send it to openFrameworks with OSC
Get temperature and humidity with DHT11 and Raspberry Pi
The temperature is automatically measured using Raspberry Pi 3 and automatically uploaded to the server (Docker python3 + bottle + MySQL) for viewing!
Record temperature and humidity with systemd on Raspberry Pi
[pyqtgraph] Add region to the graph and link it with the graph region
Measure and compare temperature with Raspberry Pi and automatically generate graph
Raspberry Pi --1 --First time (Connect a temperature sensor to display the temperature)
[python] Send the image captured from the webcam to the server and save it
Connect to the console of Raspberry PI and display local IP and SD information
[Free] Hit the Clash Royale API from lambda and send it to LINE
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Use Raspberry Pi Python to TMP36 analog temperature sensor and MCP3008 AD converter