Make a BLE thermometer and get the temperature with Pythonista3

Make a BLE thermometer and get the temperature with Pythonista3

Introduction

By the way, Pythonista3 can handle BLE (Bluetooth Low Energy). By the way, I've never used the Bluetooth function of ESP-WROOM-32. Then, let's get the temperature from the BLE thermometer!

Thing you want to do

Get the temperature on your iPhone from the BLE thermometer (ESP-WROOM-32 + temperature sensor).

What you have prepared

ESP-WROOM-32(ESP32-DevKitC) Temperature sensor DS18B20 iPhone (Pythonista 3 installed)

manner

Connect ESP-WROOM-32 and DS18B20 and measure the temperature. Send the temperature with BLE of ESP-WROOM-32. When you really care about the temperature, receive the temperature on your iPhone.

Complete image

system_ds18b20.png

create

Connect ESP-WROOM-32 and DS18B20

DS18B20 is connected by 1-wire. The data sheet looks like this: ds18B20.png Therefore, connect to ESP-WROOM-32 as follows. esp32_ds18B20.png Connection completed esp32_temp.png

Get the temperature from the temperature sensor

1-wire communication with DS18B20 to get temperature. The program referred to feelfreelinux / ds18b20.

Use BLE from ESP-WROOM-32

Source code

It's a little long so I put it on github. esp/thermometer ds18b20.c: Get the value from the temperature sensor. gatts_thermometer.c: Make ESP32 a BLE server. Send the sensor value in the READ event from the client.

The program was created using the GATT Server sample attached to ESP-IDF. Regarding GATT, [Use BLE] GATT (Generic Attribute Profile) Overview was very helpful. Since it is not commercialized, the UUID uses an appropriate value.

Use BLE from Pythonista 3

Source code

Use cb on Pythonista 3 to get temperature data. The UUID matches the above program.

thermo_client.py


import ui
import cb
import sound
import struct

TM_SERVICE_UUID = '00FF'
TM_CHAR_UUID = 'FF01'

class MyCentralManagerDelegate (object):
	def __init__(self):
		self.peripheral = None
		self.temp = 0

	def did_discover_peripheral(self, p):
		global text_state
		print('+++ Discovered peripheral: %s (%s)' % (p.name, p.uuid))
		if p.name and 'ESP_THERMOMETER' in p.name and not self.peripheral:
			# Keep a reference to the peripheral, so it doesn't get garbage-collected:
			self.peripheral = p
			cb.connect_peripheral(self.peripheral)
			text_state.text = 'Detected'

	def did_connect_peripheral(self, p):
		print('*** Connected: %s' % p.name)
		print('Discovering services...')
		p.discover_services()

	def did_fail_to_connect_peripheral(self, p, error):
		print('Failed to connect')

	def did_disconnect_peripheral(self, p, error):
		print('Disconnected, error: %s' % (error,))
		self.peripheral = None

	def did_discover_services(self, p, error):
		for s in p.services:
			if TM_SERVICE_UUID in s.uuid:
				print('+++ Thermometer found')
				p.discover_characteristics(s)

	def did_discover_characteristics(self, s, error):
		if TM_SERVICE_UUID in s.uuid:
			for c in s.characteristics:
				if TM_CHAR_UUID in c.uuid:
					print('read temperature sensor...')
					self.peripheral.read_characteristic_value(c)

	def did_write_value(self, c, error):
		print('Did enable temperature sensor')

	def did_update_value(self, c, error):
		global text_temp
		if TM_CHAR_UUID == c.uuid:
			# 
			self.temp = (c.value[0] + (c.value[1]*256))/16
			print(self.temp)
			text_temp.text=(str(self.temp) + '℃')

view = ui.View()                                      
view.name = 'THERMOMETER'                                    
view.background_color = 'white'

text_state = ui.TextView()
text_state.frame = (view.width * 0.5, view.height * 0.2, view.width, view.height*0.3)
text_state.flex = 'LRTB'
text_state.font = ('<system>', 18)
text_state.text_color = 'grey'
 
text_temp = ui.TextView()
text_temp.frame = (view.width * 0.25, view.height * 0.4, view.width, view.height)
text_temp.flex = 'WHLRTB'
text_temp.font = ('<system-bold>', 50)                                                    

view.add_subview(text_state)
view.add_subview(text_temp)
view.present('sheet')

delegate = MyCentralManagerDelegate()
print('Scanning for peripherals...')
text_state.text = 'Scanning'
cb.set_central_delegate(delegate)
cb.scan_for_peripherals()

# Keep the connection alive until the 'Stop' button is pressed:
try:
	while True: pass
except KeyboardInterrupt:
	# Disconnect everything:
	cb.reset()

This is also on github. thermo_client.py

test

Run termo_client.py on Pythonista3. 5C5AAD7B-08A1-43DC-93CC-39F265A8BEF1.jpeg Successful acquisition !! A little chilly temperature.

Finally

I will not accept the tsukkomi that says __ Does it make sense to make it remote? It's not very useful in the house, but it may be useful in the farm (it saves power).

Reference site

How to send and receive signals by connecting bluetooth from ubuntu to ESP32 cb — Connecting to Bluetooth LE Peripherals feelfreelinux/ds18b20 [Use BLE] GATT (Generic Attribute Profile) Overview

Recommended Posts

Make a BLE thermometer and get the temperature with Pythonista3
Make a thermometer with Raspberry Pi and make it visible on the browser Part 3
Get the stock price of a Japanese company with Python and make a graph
Make a thermometer with Raspberry Pi and make it viewable with a browser Part 4
[Personal memo] Get data on the Web and make it a DataFrame
[Python] Get the files in a folder with Python
Get temperature and humidity with DHT11 and Raspberry Pi
Specify the volume on linux and make a sound
Let's make a simple game with Python 3 and iPhone
Make a breakpoint on the c layer with python
Get comments and subscribers with the YouTube Data API
Get the matched string with a regular expression and reuse it when replacing on Python3
Get the id of a GPU with low memory usage
Get UNIXTIME at the beginning of today with a command
Easily make a TweetBot that notifies you of temperature and humidity with Raspberry Pi + DHT11.
Make a SonicWall SSL VPN connection with the Linux CLI
Make a fortune with Python
Make a 2D RPG with Ren'Py (3) -Items and Tool Shop
Get the trading price of virtual currency and create a chart with API of Zaif exchange
Make a fire with kdeplot
Get the strongest environment with VS Code, Remote-Containers and remote docker-daemon
How to make a command to read the configuration file with pyramid
How to make a surveillance camera (Security Camera) with Opencv and Python
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 1
Get data from MySQL on a VPS with Python 3 and SQLAlchemy
Notify LINE of body temperature from BLE thermometer with Raspberry Pi # 2
I tried to make a periodical process with Selenium and Python
Make a Kanji display compass with Raspberry Pi and Sense Hat
Get the URL of a JIRA ticket created with the jira-python library
Solve the Python knapsack problem with a branch and bound method
Get OCTA simulation conditions from a file and save with pandas
Let's make a GUI with python.
Make a sound with Jupyter notebook
Let's make a breakout with wxPython
Get the weather with Python requests
Get the weather with Python requests 2
[Python] Make the function a lambda function
Make a recommender system with python
Make a filter with a django template
Let's make a graph with python! !!
Let's make a supercomputer with xCAT
Make a model iterator with PySide
Make a nice graph with plotly
Get a list of camera parameters that can be set with cv2.VideoCapture and make it a dictionary type
Replace the directory name and the file name in the directory together with a Linux command.
Calculate the shortest route of a graph with Dijkstra's algorithm and Python
Get the number of searches with a regular expression. SeleniumBasic VBA Python
How to get the date and time difference in seconds with python
Make a wireless LAN Ethernet converter and simple router with Raspberry Pi
[Python + PHP] Make a temperature / humidity / barometric pressure monitor with Raspberry Pi
Get and convert the current time in the system local timezone with python
I get a Python No module named'encodings' error with the aws command
Read the graph image with OpenCV and get the coordinates of the final point of the graph
The story of making a sound camera with Touch Designer and ReSpeaker
Get the number of articles accessed and likes with Qiita API + Python
Get the average salary of a job with specified conditions from indeed.com
Tweet the weather forecast with a bot
Make a video player with PySimpleGUI + OpenCV
[Raspberry Pi] Add a thermometer and a hygrometer
Play around with the pythonista3 ui module
Make a rare gacha simulator with Flask