izivkov / GShockTimeServer

A python script to update time for G-Shock Watches
MIT License
16 stars 4 forks source link

Port to ESP32 hardware #12

Open izivkov opened 4 days ago

izivkov commented 4 days ago

We would like to port the Python code to work with the ESP32. This thread will discuss the work associated with this port.

notrealdreamy commented 4 days 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.

izivkov commented 4 days ago

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.

izivkov commented 4 days ago

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: @.***>

notrealdreamy commented 2 days ago

That's solved some of it.

izivkov commented 2 days ago

👍

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: @.***>

notrealdreamy commented 1 day ago
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")
notrealdreamy commented 1 day ago

ran without a error now i will shred it down to what's useful for us.

izivkov commented 1 day ago

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: @.***>

izivkov commented 1 day ago

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: @.***>

notrealdreamy commented 20 hours ago

Ok this is the part that you need to teach me how to send ble data to a gshock.

izivkov commented 19 hours ago

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 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: @.***>

izivkov commented 19 hours ago

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:

  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 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: @.***>

notrealdreamy commented 18 hours ago

GA-B2100

notrealdreamy commented 18 hours ago

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.

notrealdreamy commented 18 hours ago

Screenshot 2024-07-02 at 7 48 34 PM got esp32 in vs code

izivkov commented 17 hours ago

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: @.***>

notrealdreamy commented 4 hours ago

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?

notrealdreamy commented 4 hours ago

I see you are testing gab2100 with your python code.