# Våg
https://wiki.seeedstudio.com/XIAO_ESP32C3_Getting_Started/
# Step 1 - Flash the device with micropython firmware
https://micropython.org/download/esp32c3-usb/
https://micropython.org/resources/firmware/esp32c3-usb-20220618-v1.19.1.bin
```python=
esptool.py --chip esp32c3 --port /dev/cu.usbmodem143201 erase_flash
```
```python=
esptool.py --chip esp32c3 --port /dev/cu.usbmodem143201 --baud 460800 write_flash -z 0x0 ../Downloads/esp32c3-usb-20220618-v1.19.1.bin
```
# Step 2 - Upload the BLE code
https://github.com/davmoz/pm_vanner_scale_ble/blob/master/ble.py
The BLE-class
```python=
from machine import Pin, Timer
from time import sleep_ms
import ubluetooth
import _thread
import struct
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()
self.client_connected = False
self.value = 0
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 disconnected'''
self.connected()
# self.led(1)
self.client_connected = True
elif event == 2:
'''Central disconnected'''
self.advertiser()
self.disconnected()
self.client_connected = False
elif event == 3:
'''New message received'''
buffer = self.ble.gatts_read(self.rx)
message = buffer.decode('UTF-8').strip()
print(message)
def register(self):
HR_UUID = ubluetooth.UUID(0x180D)
HR_CHAR = (ubluetooth.UUID(0x2A37), ubluetooth.FLAG_READ | ubluetooth.FLAG_NOTIFY,)
HR_SERVICE = (HR_UUID, (HR_CHAR,),)
SERVICES = (HR_SERVICE,)
# ((self.tx, self.rx,), ) = self.ble.gatts_register_services(SERVICES)
print(self.ble.gatts_register_services(SERVICES))
((self.hr,),) = self.ble.gatts_register_services(SERVICES)
def send(self, data):
if self.client_connected:
try:
payload = struct.pack('<f', float(data))
self.ble.gatts_notify(0, self.hr, payload + '\n')
except Exception as e:
print()
def advertiser(self):
name = bytes(self.name, 'UTF-8')
self.ble.gap_advertise(100, bytearray('\x02\x01\x02') + bytearray((len(name) + 1, 0x09)) + name)
```
The main code
```python=
from ble import BLE
from machine import UART
from time import sleep_ms
ble_obj = BLE("CPWPlus75")
uart = UART(1, 9600, tx=21, rx=20) # init with given baudrate
uart.init(9600, bits=8, parity=None, stop=1) # init with given parameters
while True:
resp = uart.readline()
if resp and len(resp.split()) > 1:
# resp = binascii.hexlify(resp).decode()
parsed = resp.split()[1].decode()
print(parsed, "kg")
ble_obj.send(parsed)
sleep_ms(300)
```
# Step 3 - Pinout

# step 4 - Scale configuration
