Open izivkov opened 5 months ago
Yes, sirrrrr. Now I think the first step should be to create our own "bleak" package. I by bleak package i mean one specifically for a g shock thats accept simple data in its function header.
OK, look into an existing Python BLE library fo the ESP32. There should be one already. Creating your own library is way too much effort, and you are re-inventing the wheel. So, if we have a library similar to "bleak", we have to figure out how to use it.
Here is something you can use: https://github.com/2black0/MicroPython-ESP32-BLE
On Fri, Jun 28, 2024, 8:41 AM notrealdreamy @.***> wrote:
Yes, sirrrrr. Now I think the first step should be to create our own "bleaker" package.
— Reply to this email directly, view it on GitHub https://github.com/izivkov/GShockTimeServer/issues/12#issuecomment-2196820076, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37Q3XPMZHSO557FHDBDZJVKWNAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJWHAZDAMBXGY . You are receiving this because you authored the thread.Message ID: @.***>
That's solved some of it.
👍
On Sun, Jun 30, 2024, 8:12 AM notrealdreamy @.***> wrote:
That's solved some of it.
— Reply to this email directly, view it on GitHub https://github.com/izivkov/GShockTimeServer/issues/12#issuecomment-2198541984, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37X2JIKJ3SOG72EYGXLZJ7Y4JAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDCOJYGU2DCOJYGQ . You are receiving this because you authored the thread.Message ID: @.***>
from machine import Pin, Timer, SoftI2C
from time import sleep_ms
import ubluetooth
# Dummy sensor class for emulation
class DummySensor:
def read_temperature(self, fahrenheit=False):
return 25.0 if not fahrenheit else 77.0
def read_humidity(self):
return 50.0
# Initialize sensor
sensor = DummySensor()
class BLE():
def __init__(self, name):
self.name = name
self.ble = ubluetooth.BLE()
self.ble.active(True)
self.led = Pin(2, Pin.OUT)
self.timer1 = Timer(0)
self.timer2 = Timer(1)
self.disconnected()
self.ble.irq(self.ble_irq)
self.register()
self.advertiser()
def connected(self):
self.timer1.deinit()
self.timer2.deinit()
def disconnected(self):
self.timer1.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: self.led(1))
sleep_ms(200)
self.timer2.init(period=1000, mode=Timer.PERIODIC, callback=lambda t: self.led(0))
def ble_irq(self, event, data):
if event == 1:
'''Central connected'''
self.connected()
self.led(1)
elif event == 2:
'''Central disconnected'''
self.advertiser()
self.disconnected()
elif event == 3:
'''New message received'''
buffer = self.ble.gatts_read(self.rx)
message = buffer.decode('UTF-8').strip()
print(message)
if message == 'red_led':
red_led.value(not red_led.value())
print('red_led', red_led.value())
self.send('red_led' + str(red_led.value()))
if message == 'read_temp':
print(sensor.read_temperature(True))
self.send(str(sensor.read_temperature(True)))
def register(self):
# Nordic UART Service (NUS)
NUS_UUID = '6E400001-B5A3-F393-E0A9-E50E24DCCA9E'
RX_UUID = '6E400002-B5A3-F393-E0A9-E50E24DCCA9E'
TX_UUID = '6E400003-B5A3-F393-E0A9-E50E24DCCA9E'
BLE_NUS = ubluetooth.UUID(NUS_UUID)
BLE_RX = (ubluetooth.UUID(RX_UUID), ubluetooth.FLAG_WRITE)
BLE_TX = (ubluetooth.UUID(TX_UUID), ubluetooth.FLAG_NOTIFY)
BLE_UART = (BLE_NUS, (BLE_TX, BLE_RX,))
SERVICES = (BLE_UART, )
((self.tx, self.rx,), ) = self.ble.gatts_register_services(SERVICES)
def send(self, data):
self.ble.gatts_notify(0, self.tx, data + '\n')
def advertiser(self):
name = bytes(self.name, 'utf-8')
adv_data = bytearray(b'\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name
self.ble.gap_advertise(100, adv_data)
# test
i2c = SoftI2C(scl=Pin(22), sda=Pin(21))
red_led = Pin(2, Pin.OUT)
ble = BLE("ESP32")
ran without a error now i will shred it down to what's useful for us.
Ok. For the Casio watches, there are 2 uuid's that are used, one for reading data from the watch, and other for setting values on the watch. In your example, they use different uuid's for another device, like LED blinker. Look at my Python code, and similarly insulate the ble related stuff into a few functions.
I think you can fork my project from GitHub, and make changes to your fork. Also create a new branch on your side. Eventually, we can figure out how to merge the code, or perhaps keep different branches for the esp and Linux versions.
On Mon, Jul 1, 2024, 11:30 AM notrealdreamy @.***> wrote:
ran without a error now i will shred it down to what's useful for us.
— Reply to this email directly, view it on GitHub https://github.com/izivkov/GShockTimeServer/issues/12#issuecomment-2200466585, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37RNH5JHOQWMY475YRDZKFY2VAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBQGQ3DMNJYGU . You are receiving this because you authored the thread.Message ID: @.***>
Also, do not try to integrate with my project yet. Write a small python script to connect to the watch, and try to get the watch's name. This will be a great milestone.
On Mon, Jul 1, 2024, 11:49 AM Ivo Zivkov @.***> wrote:
Ok. For the Casio watches, there are 2 uuid's that are used, one for reading data from the watch, and other for setting values on the watch. In your example, they use different uuid's for another device, like LED blinker. Look at my Python code, and similarly insulate the ble related stuff into a few functions.
I think you can fork my project from GitHub, and make changes to your fork. Also create a new branch on your side. Eventually, we can figure out how to merge the code, or perhaps keep different branches for the esp and Linux versions.
On Mon, Jul 1, 2024, 11:30 AM notrealdreamy @.***> wrote:
ran without a error now i will shred it down to what's useful for us.
— Reply to this email directly, view it on GitHub https://github.com/izivkov/GShockTimeServer/issues/12#issuecomment-2200466585, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37RNH5JHOQWMY475YRDZKFY2VAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBQGQ3DMNJYGU . You are receiving this because you authored the thread.Message ID: @.***>
Ok this is the part that you need to teach me how to send ble data to a gshock.
So, basically what you have to do is the following:
Let's start with the connection. All code related to connection is in the existing project in the file scanner.py.
There are two ways to connect:
So, you need to do the same thing, but using your BLE library, not BleakScanner from the Linux bleak library.
I think this would be a good starting point.
A word about development tools: It would be good to have an IDE, like Visual Code. This will allow you to set breakpoints and look at the data at these breakpoints. This applies to the current Linux code, but it will help you understand how the code works, and what you have to duplicate in your project. That is why the Raspberry Pi is not an ideal environment for development. I would start with installing Visual Code ( https://code.visualstudio.com/) on some old machine, preferably Linux, and then step through the code to understand what is going on. There is a way to run the code on the Pi and use Visual Code on another machine, like the Mac, but this is a bit cumbersome.
So, spend some time learning how to use Visual Code if you are not familiar with it. It will save you a lot of time in the future.
On Tue, Jul 2, 2024 at 7:42 AM notrealdreamy @.***> wrote:
Ok this is the part that you need to teach me how to send ble data to a gshock.
— Reply to this email directly, view it on GitHub https://github.com/izivkov/GShockTimeServer/issues/12#issuecomment-2202930041, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37Q2Z5X6SFHNG6NWDVTZKKG3JAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBSHEZTAMBUGE . You are receiving this because you authored the thread.Message ID: @.***>
BTW, what is the mode of your watch?
On Tue, Jul 2, 2024 at 9:30 AM Ivo Zivkov @.***> wrote:
So, basically what you have to do is the following:
- Connect to your watch.
- Read and write to your watch.
Let's start with the connection. All code related to connection is in the existing project in the file scanner.py.
There are two ways to connect:
- Connect without knowing the MAC address of the watch, something like EC:71:14:33:FD:51. In this case, we scan for BT devices that match certain criteria, like the name starting with "casio". This filters out other devices, like your BT headset. If such a device is found, we save the name and the address in the config.ini file for the next time we try to connect.
- The second way to connect is if we know the MAC address. If we are trying to connect the same watch, we can get the saved address from the config.ini file.
So, you need to do the same thing, but using your BLE library, not BleakScanner from the Linux bleak library.
I think this would be a good starting point.
A word about development tools: It would be good to have an IDE, like Visual Code. This will allow you to set breakpoints and look at the data at these breakpoints. This applies to the current Linux code, but it will help you understand how the code works, and what you have to duplicate in your project. That is why the Raspberry Pi is not an ideal environment for development. I would start with installing Visual Code ( https://code.visualstudio.com/) on some old machine, preferably Linux, and then step through the code to understand what is going on. There is a way to run the code on the Pi and use Visual Code on another machine, like the Mac, but this is a bit cumbersome.
So, spend some time learning how to use Visual Code if you are not familiar with it. It will save you a lot of time in the future.
On Tue, Jul 2, 2024 at 7:42 AM notrealdreamy @.***> wrote:
Ok this is the part that you need to teach me how to send ble data to a gshock.
— Reply to this email directly, view it on GitHub https://github.com/izivkov/GShockTimeServer/issues/12#issuecomment-2202930041, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37Q2Z5X6SFHNG6NWDVTZKKG3JAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBSHEZTAMBUGE . You are receiving this because you authored the thread.Message ID: @.***>
GA-B2100
So, basically what you have to do is the following: 1. Connect to your watch. 2. Read and write to your watch. Let's start with the connection. All code related to connection is in the existing project in the file scanner.py. There are two ways to connect: 1. Connect without knowing the MAC address of the watch, something like EC:71:14:33:FD:51. In this case, we scan for BT devices that match certain criteria, like the name starting with "casio". This filters out other devices, like your BT headset. If such a device is found, we save the name and the address in the config.ini file for the next time we try to connect. 2. The second way to connect is if we know the MAC address. If we are trying to connect the same watch, we can get the saved address from the config.ini file. So, you need to do the same thing, but using your BLE library, not BleakScanner from the Linux bleak library. I think this would be a good starting point. A word about development tools: It would be good to have an IDE, like Visual Code. This will allow you to set breakpoints and look at the data at these breakpoints. This applies to the current Linux code, but it will help you understand how the code works, and what you have to duplicate in your project. That is why the Raspberry Pi is not an ideal environment for development. I would start with installing Visual Code ( https://code.visualstudio.com/) on some old machine, preferably Linux, and then step through the code to understand what is going on. There is a way to run the code on the Pi and use Visual Code on another machine, like the Mac, but this is a bit cumbersome. So, spend some time learning how to use Visual Code if you are not familiar with it. It will save you a lot of time in the future. … On Tue, Jul 2, 2024 at 7:42 AM notrealdreamy @.> wrote: Ok this is the part that you need to teach me how to send ble data to a gshock. — Reply to this email directly, view it on GitHub <#12 (comment)>, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37Q2Z5X6SFHNG6NWDVTZKKG3JAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBSHEZTAMBUGE . You are receiving this because you authored the thread.Message ID: @.>
plan b,i bought a new esp32, flashed micropython and now i will be testing on it.
got esp32 in vs code
Great!
On Tue, Jul 2, 2024, 10:19 AM notrealdreamy @.***> wrote:
Screenshot.2024-07-02.at.7.48.34.PM.png (view on web) https://github.com/izivkov/GShockTimeServer/assets/88233084/9a5db592-2d91-4238-8cde-7b7b1347a786 got esp32 in vs code
— Reply to this email directly, view it on GitHub https://github.com/izivkov/GShockTimeServer/issues/12#issuecomment-2203351101, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37XBNVZ7OMNVGWKDWPTZKKZGRAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBTGM2TCMJQGE . You are receiving this because you authored the thread.Message ID: @.***>
with the help of gpt
from bluetooth import BLE
import ubinascii
def scan_callback(event, data):
if event == BLE.GAP_SCAN_RESULT:
addr_type, addr, adv_type, rssi, adv_data = data
addr_str = ubinascii.hexlify(addr).decode('utf-8')
print(f"Device found: Address={addr_str}, RSSI={rssi}")
# Initialize BLE
ble = BLE()
ble.active(True)
# Start scanning for BLE devices
ble.gap_scan(10000, 30000, 30000, scan_callback)
Do you think it will work?
I see you are testing gab2100 with your python code.
Ok, this would be a good starting point. Scan for all BT devices in the vicinity and display their address and name. Then we can filter for Casio watch only.
On Tue, Jul 2, 2024, 11:49 PM notrealdreamy @.***> wrote:
I see you are testing gab2100 with your python code.
— Reply to this email directly, view it on GitHub https://github.com/izivkov/GShockTimeServer/issues/12#issuecomment-2205034357, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37UD7BBL633QVRSVCYDZKNYFLAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBVGAZTIMZVG4 . You are receiving this because you commented.Message ID: @.***>
You seem to have a good handle on tools and ChatGPT. Next, I would try to really learn Python well. There are many tutorial courses on YouTube. The best one I have seen is this 15-hour course from Harvard University: https://www.youtube.com/watch?v=nLRL_NcnK-4&t=29323s. It is too long for most people, but if you have the time, you will know Python well at the end. Of course, there are many other shorter courses.
Good luck, Ivo
On Wed, Jul 3, 2024 at 8:24 AM Ivo Zivkov @.***> wrote:
Ok, this would be a good starting point. Scan for all BT devices in the vicinity and display their address and name. Then we can filter for Casio watch only.
On Tue, Jul 2, 2024, 11:49 PM notrealdreamy @.***> wrote:
I see you are testing gab2100 with your python code.
— Reply to this email directly, view it on GitHub https://github.com/izivkov/GShockTimeServer/issues/12#issuecomment-2205034357, or unsubscribe https://github.com/notifications/unsubscribe-auth/AA7M37UD7BBL633QVRSVCYDZKNYFLAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBVGAZTIMZVG4 . You are receiving this because you commented.Message ID: @.***>
You seem to have a good handle on tools and ChatGPT. Next, I would try to really learn Python well. There are many tutorial courses on YouTube. The best one I have seen is this 15-hour course from Harvard University: https://www.youtube.com/watch?v=nLRL_NcnK-4&t=29323s. It is too long for most people, but if you have the time, you will know Python well at the end. Of course, there are many other shorter courses. Good luck, Ivo … On Wed, Jul 3, 2024 at 8:24 AM Ivo Zivkov @.> wrote: Ok, this would be a good starting point. Scan for all BT devices in the vicinity and display their address and name. Then we can filter for Casio watch only. On Tue, Jul 2, 2024, 11:49 PM notrealdreamy @.> wrote: > I see you are testing gab2100 with your python code. > > — > Reply to this email directly, view it on GitHub > <#12 (comment)>, > or unsubscribe > https://github.com/notifications/unsubscribe-auth/AA7M37UD7BBL633QVRSVCYDZKNYFLAVCNFSM6AAAAABKBZ72MCVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMZDEMBVGAZTIMZVG4 > . > You are receiving this because you commented.Message ID: > @.***> >
Thank you for sending this, I was also thinking that i was too dependent on these tools.I am gona watch the whole vid part by part.☺️
I have removed one of the dependencies for the project, namely the "reactivex" library. I was using this library before, but it is no longer needed. This should make things easier for MicroPython since it does not support this library.
Also, for the BLE section, they recommend this higher-level library for MicroPython:
https://github.com/micropython/micropython-lib/tree/master/micropython/bluetooth/aioble
I am not familiar with it; maybe it is not useful, but we can look into it when you are ready.
Regards
By reading readme it seems useful.
We would like to port the Python code to work with the ESP32. This thread will discuss the work associated with this port.