Lees X728 voltage en lading en stuur dit naar Domoticz

Gepubliceerd door Robot One op

#!/usr/bin/env python
import struct
import smbus
import sys
import time
import requests


def readVoltage(bus):

     address = 0x36
     read = bus.read_word_data(address, 2)
     swapped = struct.unpack("<H", struct.pack(">H", read))[0]
     voltage = swapped * 1.25 /1000/16
     return voltage


def readCapacity(bus):

     address = 0x36
     read = bus.read_word_data(address, 4)
     swapped = struct.unpack("<H", struct.pack(">H", read))[0]
     capacity = swapped/256
     if (capacity > 100):
          capacity = 100
     return capacity


bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)

voltage = "%.2f" % readVoltage(bus)
battery = "%i" % readCapacity(bus)
url = "http://127.0.0.1:8088/json.htm?type=command&param=udevice&idx=561&nvalue=0&svalue=" + voltage
r = requests.get(url)
print(r) # voor debuggen: resultaat moet 200 zijn = OK

url = "http://127.0.0.1:8088/json.htm?type=command&param=udevice&idx=562&nvalue=0&svalue=" + battery
r = requests.get(url)
print(r) # voor debuggen: resultaat moet 200 zijn = OK

Categorieën: