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!
Get the temperature on your iPhone from the BLE thermometer (ESP-WROOM-32 + temperature sensor).
ESP-WROOM-32(ESP32-DevKitC) Temperature sensor DS18B20 iPhone (Pythonista 3 installed)
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.
DS18B20 is connected by 1-wire. The data sheet looks like this: Therefore, connect to ESP-WROOM-32 as follows. Connection completed
1-wire communication with DS18B20 to get temperature. The program referred to feelfreelinux / ds18b20.
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 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
Run termo_client.py on Pythonista3. Successful acquisition !! A little chilly temperature.
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).
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