Help to find the error! I can not display the battery charge on the site map!
ups_18650.py
#!/usr/bin/env python
import struct
import smbus
import sys
import time
def readVoltage(bus):
"This function returns as float the voltage from the Raspi UPS Hat via the provided SMBus object"
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):
"This function returns as a float the remaining capacity of the battery connected to the Raspi UPS Hat via the provided SMBus object"
address = 0x36
read = bus.read_word_data(address, 4)
swapped = struct.unpack("<H", struct.pack(">H", read))[0]
capacity = swapped/256
return capacity
bus = smbus.SMBus(1) # 0 = /dev/i2c-0 (port I2C0), 1 = /dev/i2c-1 (port I2C1)
while True:
print "++++++++++++++++++++"
print "Voltage:%5.2fV" % readVoltage(bus)
print "Battery:%5i%%" % readCapacity(bus)
if readCapacity(bus) == 100:
print "Батарея ЗАРЯЖЕНА"
if readCapacity(bus) < 20:
print "Батарея РАЗРЯЖЕНА"
print "++++++++++++++++++++"
time.sleep(2)
исправил, но не заработало. в лог теперь вот такая ошибка.
2019-03-27 13: 07: 26.616 [WARN] [hab.binding.exec.handler.ExecHandler] - Не удалось преобразовать ответ, так как услуга translationService типа 'REGEX' недоступна
2019-03-27 13:13:35.169 [WARN ] [hab.binding.exec.handler.ExecHandler] - Couldn't transform response because transformationService of type 'REGEX' is unavailable
2019-03-27 13:13:35.178 [WARN ] [ui.internal.items.ItemUIRegistryImpl] - Exception while formatting value '
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
sudo: no tty present and no askpass program specified
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
sudo: no tty present and no askpass program specified' of item ups_18650 with format '%5i%%': Conversion = 'i'
==> /var/log/openhab2/events.log <==
2019-03-27 13:13:35.181 [vent.ItemStateChangedEvent] - ups_18650 changed from python: can't open file '/home/openhabian/UPS_18650.py': [Errno 13] Permission denied
python: can't open file '/home/openhabian/UPS_18650.py': [Errno 13] Permission denied to
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
sudo: no tty present and no askpass program specified
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:
#1) Respect the privacy of others.
#2) Think before you type.
#3) With great power comes great responsibility.
sudo: no tty present and no askpass program specified
Yep, as stated before, it does not suffice to write sudo in front of a command.
GNU/Linux sudo is used to extend permissions to root, so sudo will ask for a password every time it’s executed. The user openhab (this is the one that openHAB is using) needs permission to use the sudo command without typing a password.
NOT openhabian, the user is openhab!
User openhabian is a common user to get command shell access.
User openhab is a user without regular shell access. this user is used only to run openHAB.