Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
venv*
__pycache*
*.txt

11 changes: 6 additions & 5 deletions BTjuntek.py
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,8 @@ def _callback(self, sender, data):

key = params_keys[position]
values[key] = value_str

print(values)
# check if dir_of_current exist if not asign if charging or dischargin exist
if "dir_of_current" not in values and "charging" not in check:
if "discharge" in values and "charge" not in values:
Expand Down Expand Up @@ -233,7 +235,7 @@ def _callback(self, sender, data):


logger.debug(values)

print(values)
#End of BLESNIFF Plug


Expand Down Expand Up @@ -263,13 +265,13 @@ def _callback(self, sender, data):
f"DISCOVERY_PUB=homeassistant/sensor/{entry['object_id']}/config\n"
+ "PL={json.dumps(entry)}\n"
)
publish.single(
''' publish.single(
topic=f"homeassistant/sensor/{entry['object_id']}/config",
payload=json.dumps(entry),
retain=True,
hostname=config.MQTT_HOST,
auth=auth,
)
) '''

self.discovery_info_sent = True

Expand All @@ -280,7 +282,7 @@ def _callback(self, sender, data):
if not args.quiet:
print(f"{k} = {v}")

publish.multiple(mqtt_msgs, hostname=config.MQTT_HOST, auth=auth)
# publish.multiple(mqtt_msgs, hostname=config.MQTT_HOST, auth=auth)
logger.info("Published updated sensor stats to MQTT")


Expand All @@ -289,7 +291,6 @@ def _callback(self, sender, data):




if hasattr(config, "JT_LOG_FILE"):
logging.basicConfig(
filename=config.JT_LOG_FILE,
Expand Down
37 changes: 37 additions & 0 deletions basejun1_bt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import asyncio
# import bluetooth
from bleak import BleakClient

JUNTEK_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb"
#JUNTEK_UUID = "ae656aea-86c2-d654-f73b-d3f9cc15a9d6"

async def read_juntek_data(address):
async with BleakClient(address) as client:
while True:
try:
data = await client.read_gatt_char(JUNTEK_UUID)
decoded_data = data.decode('utf-8')
voltage, current, power = parse_juntek_data(decoded_data)
print(f"Voltage: {voltage}V, Current: {current}A, Power: {power}W")
await asyncio.sleep(1)
except Exception as e:
print(f"Error: {e}")
break

def parse_juntek_data(data):
# Parse the data string based on Juntek's protocol
# This is a simplified example and may need adjustment
parts = data.split(',')
voltage = float(parts[0])
current = float(parts[1])
power = float(parts[2])
return voltage, current, power

def main():
#juntek_address = "XX:XX:XX:XX:XX:XX" # Replace with your Juntek device's Bluetooth address
juntek_address = "84:C2:E4:44:59:59" # Replace with your Juntek device's Bluetooth address

asyncio.run(read_juntek_data(juntek_address))

if __name__ == "__main__":
main()
75 changes: 75 additions & 0 deletions basejun2_bt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
import asyncio
from bleak import BleakClient, BleakScanner

JUNTEK_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb"

async def connect_juntek(address):
device = await BleakScanner.find_device_by_address(address)
if not device:
print(f"Device with address {address} not found")
return

async with BleakClient(device) as client:
print(f"Connected to {device.name}")

while True:
try:
data = await client.read_gatt_char(JUNTEK_UUID)
decoded_data = data.decode('utf-8')
parsed_data = parse_juntek_data(decoded_data)
print_data(parsed_data)
await asyncio.sleep(1)
except Exception as e:
print(f"Error: {e}")
break

def parse_juntek_data(data):
parts = data.split(',')
return {
'voltage': float(parts[2]) / 100,
'current': float(parts[3]) / 100,
'remaining_capacity': float(parts[4]) / 100,
'cumulative_capacity': float(parts[5]) / 100,
'watt_hours': float(parts[6]) / 10000,
'temperature': float(parts[8]) - 100,
'output_status': int(parts[10]),
'current_direction': "Charging" if int(parts[11]) == 0 else "Discharging",
'battery_life': int(parts[12])
}

def print_data(data):
print(f"Voltage: {data['voltage']}V")
print(f"Current: {data['current']}A")
print(f"Remaining Capacity: {data['remaining_capacity']}Ah")
print(f"Cumulative Capacity: {data['cumulative_capacity']}Ah")
print(f"Watt Hours: {data['watt_hours']}kWh")
print(f"Temperature: {data['temperature']}°C")
print(f"Output Status: {data['output_status']}")
print(f"Current Direction: {data['current_direction']}")
print(f"Battery Life: {data['battery_life']} minutes")
print("---")


async def list_gatt_services(address):
async with BleakClient(address) as client:
print(f"Connected to {client.address}")
services = await client.get_services()

print("\nGATT Services:")
for service in services:
print(f"Service: {service.uuid}")
for char in service.characteristics:
print(f" Characteristic: {char.uuid}")
print(f" Properties: {', '.join(char.properties)}")
for descriptor in char.descriptors:
print(f" Descriptor: {descriptor.uuid}")

async def main():
juntek_address = "XX:XX:XX:XX:XX:XX" # Replace with your Juntek KL140F's Bluetooth address
juntek_address = "84:C2:E4:44:59:59"
await connect_juntek(juntek_address)
await list_gatt_services(juntek_address)

if __name__ == "__main__":
asyncio.run(main())

76 changes: 76 additions & 0 deletions basejun3_bt.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
import asyncio
from bleak import BleakClient, BleakScanner

# JUNTEK_UUID = "0000ffe1-0000-1000-8000-00805f9b34fb"
JUNTEK_UUID = "00002a24-0000-1000-8000-00805f9b34fb"

async def connect_juntek(address):
device = await BleakScanner.find_device_by_address(address)
if not device:
print(f"Device with address {address} not found")
return

async with BleakClient(device) as client:
print(f"Connected to {device.name}")

while True:
try:
data = await client.read_gatt_char(JUNTEK_UUID)
decoded_data = data.decode('utf-8')
parsed_data = parse_juntek_data(decoded_data)
print_data(parsed_data)
await asyncio.sleep(1)
except Exception as e:
print(f"Error: {e}")
break

def parse_juntek_data(data):
parts = data.split(',')
return {
'voltage': float(parts[2]) / 100,
'current': float(parts[3]) / 100,
'remaining_capacity': float(parts[4]) / 100,
'cumulative_capacity': float(parts[5]) / 100,
'watt_hours': float(parts[6]) / 10000,
'temperature': float(parts[8]) - 100,
'output_status': int(parts[10]),
'current_direction': "Charging" if int(parts[11]) == 0 else "Discharging",
'battery_life': int(parts[12])
}

def print_data(data):
print(f"Voltage: {data['voltage']}V")
print(f"Current: {data['current']}A")
print(f"Remaining Capacity: {data['remaining_capacity']}Ah")
print(f"Cumulative Capacity: {data['cumulative_capacity']}Ah")
print(f"Watt Hours: {data['watt_hours']}kWh")
print(f"Temperature: {data['temperature']}°C")
print(f"Output Status: {data['output_status']}")
print(f"Current Direction: {data['current_direction']}")
print(f"Battery Life: {data['battery_life']} minutes")
print("---")


async def list_gatt_services(address):
async with BleakClient(address) as client:
print(f"Connected to {client.address}")
services = await client.get_services()

print("\nGATT Services:")
for service in services:
print(f"Service: {service.uuid}")
for char in service.characteristics:
print(f" Characteristic: {char.uuid}")
print(f" Properties: {', '.join(char.properties)}")
for descriptor in char.descriptors:
print(f" Descriptor: {descriptor.uuid}")

async def main():
juntek_address = "XX:XX:XX:XX:XX:XX" # Replace with your Juntek KL140F's Bluetooth address
juntek_address = "84:C2:E4:44:59:59"
await connect_juntek(juntek_address)
await list_gatt_services(juntek_address)

if __name__ == "__main__":
asyncio.run(main())

5 changes: 3 additions & 2 deletions config.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@
MQTT_USER='user'
MQTT_PASS='pass'
# Mac Address of the Juntec BT
JUNTEC_ADDR="XX:XX:XX:XX:XX:XX"
# JUNTEC_ADDR="XX:XX:XX:XX:XX:XX"
JUNTEC_ADDR="84:C2:E4:44:59:59"
# Battery Bank Capacity in Ah
BATT_CAP=200
BATT_CAP=100
# Port where the RS485 is located
RS485 = '/dev/ttyUSB0'
23 changes: 23 additions & 0 deletions top5ble.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import asyncio
from bleak import BleakScanner

async def scan_ble_devices():
devices = await BleakScanner.discover(timeout=5)
return sorted(devices, key=lambda d: d.rssi, reverse=True)[:50]

def print_device_info(device):
print(f"Name: {device.name or 'Unknown'}")
print(f"Address: {device.address}")
print(f"RSSI: {device.rssi} dBm")
print("---")

async def main():
print("Scanning for nearby BLE devices...")
closest_devices = await scan_ble_devices()

print("\nTop 5 closest BLE devices:")
for device in closest_devices:
print_device_info(device)

if __name__ == "__main__":
asyncio.run(main())