Custom Python plugin stopped working after importing smbus2 Topic is solved

Please use template to report bugs and problems. Post here your questions when not sure where else to post
Only for bugs in the Domoticz application! other problems go in different subforums!
Forum rules
Before posting here, make sure you are on the latest Beta or Stable version.
If you have problems related to the web gui, clear your browser cache + appcache first.

Use the following template when posting here:

Version: xxxx
Platform: xxxx
Plugin/Hardware: xxxx
Description:
.....

If you are having problems with scripts/blockly, always post the script (in a spoiler or code tag) or screenshots of your blockly

If you are replying, please do not quote images/code from the first post

Please mark your topic as Solved when the problem is solved.
Post Reply
joran
Posts: 7
Joined: Friday 22 June 2018 0:13
Target OS: Raspberry Pi
Domoticz version: 3.8153
Contact:

Custom Python plugin stopped working after importing smbus2

Post by joran » Wednesday 01 August 2018 1:30

Hello,

I'm trying to make a special plugin for my analog output board in combination with Domoticz. This worked in a separate python script, but to make the implementation better I want to build a plugin for it. This is the second plugin a made but this one is causing some problems. I wanted to use the smbus since I have used them in the script before.

Domoticz version: 4.9700
Hardware: Raspberry Pi 3 Model B Rev 1.2
OS:
PRETTY_NAME="Raspbian GNU/Linux 9 (stretch)"
NAME="Raspbian GNU/Linux"
VERSION_ID="9"
VERSION="9 (stretch)"
ID=raspbian
ID_LIKE=debian

This is the old script:
Spoiler: show
#!/usr/bin/python

import smbus
import time
import requests

#I2C
bus = smbus.SMBus(1)
addressAout = 0x58
addressAin = 0x09

#Domoticz
serverIP = "10.0.0.8:8080"
OutIdx1 = "128"
OutIdx2 = "133"
OutIdx3 = "134"
OutIdx4 = "135"
InIdx2 = "129"
InIdx3 = "130"
InIdx4 = "131"
InIdx5 = "132"
username = "username"
password = "password"

#Analog output
oldCmd = "empty"
searchTerm = "LevelInt"
valLvlIndxStart = 0
valLvlIndxEnd = 0
valCmd1 = 0
valCmd2 = 0
valCmd3 = 0
valCmd4 = 0
varReq = "leeg"
bitConst = 10.23
LSB = [0,0,0,0]
HSB = [0,0,0,0]

#Analog input
inputVal = []
inputValDec = [0,0,0,0,0]
bufferSize = 0
oldVal2 = 0
oldVal3 = 0
oldVal4 = 0
oldVal5 = 0

while True:
#Get value from Domoticz
varReq = "http://" + username + ":" + password + "@" + serverIP + "/json.htm?type=devices&rid=" + OutIdx1
d = requests.get(varReq)
valLvl = str(d.json())
valLvlIndxStart = int(valLvl.index(searchTerm))
valLvlIndxEnd = int(valLvl.index(',',valLvlIndxStart))
valLvlIndxStart += 10
valCmd1 = int(valLvl[valLvlIndxStart:valLvlIndxEnd])

varReq = "http://" + username + ":" + password + "@" + serverIP + "/json.htm?type=devices&rid=" + OutIdx2
d = requests.get(varReq)
valLvl = str(d.json())
valLvlIndxStart = int(valLvl.index(searchTerm))
valLvlIndxEnd = int(valLvl.index(',',valLvlIndxStart))
valLvlIndxStart += 10
valCmd2 = int(valLvl[valLvlIndxStart:valLvlIndxEnd])

varReq = "http://" + username + ":" + password + "@" + serverIP + "/json.htm?type=devices&rid=" + OutIdx3
d = requests.get(varReq)
valLvl = str(d.json())
valLvlIndxStart = int(valLvl.index(searchTerm))
valLvlIndxEnd = int(valLvl.index(',',valLvlIndxStart))
valLvlIndxStart += 10
valCmd3 = int(valLvl[valLvlIndxStart:valLvlIndxEnd])

varReq = "http://" + username + ":" + password + "@" + serverIP + "/json.htm?type=devices&rid=" + OutIdx4
d = requests.get(varReq)
valLvl = str(d.json())
valLvlIndxStart = int(valLvl.index(searchTerm))
valLvlIndxEnd = int(valLvl.index(',',valLvlIndxStart))
valLvlIndxStart += 10
valCmd4 = int(valLvl[valLvlIndxStart:valLvlIndxEnd])

#Update value with current value
valCmdScaled = int(bitConst*valCmd1)
HSB[0] = int(valCmdScaled/256)
LSB[0] = int(valCmdScaled-HSB[0]*256)

valCmdScaled = int(bitConst*valCmd2)
HSB[1] = int(valCmdScaled/256)
LSB[1] = int(valCmdScaled-HSB[1]*256)

valCmdScaled = int(bitConst*valCmd3)
HSB[2] = int(valCmdScaled/256)
LSB[2] = int(valCmdScaled-HSB[2]*256)

valCmdScaled = int(bitConst*valCmd4)
HSB[3] = int(valCmdScaled/256)
LSB[3] = int(valCmdScaled-HSB[3]*256)

val = [LSB[0],HSB[0],LSB[1],HSB[1],LSB[2],HSB[2],LSB[3],HSB[3]]
bus.write_i2c_block_data(addressAout,0x00,val)

#Read inputs trought i2c
inputVal = (bus.read_i2c_block_data(addressAin,0x00,11))
bufferSize = len(inputVal)

inputValDec[0] = inputVal[2]*256 + inputVal[1]
inputValDec[1] = inputVal[4]*256 + inputVal[3]
inputValDec[2] = inputVal[6]*256 + inputVal[5]
inputValDec[3] = inputVal[8]*256 + inputVal[7]
inputValDec[4] = inputVal[10]*256 + inputVal[9]

#Send values to Domoticz
Val2 = inputValDec[1]
Val3 = inputValDec[2]
Val4 = inputValDec[3]
Val5 = inputValDec[4]

if (Val2 != oldVal2) :
oldVal2 = Val2
payload = "http://" + username + ":" + password + "@" + serverIP + "/json.htm?type=command&param=udevice&idx=" + InIdx2 + "&nvalue=0&svalue=" + str(Val2)
r = requests.get(payload)

if (Val3 != oldVal3) :
oldVal3 = Val3
payload = "http://" + username + ":" + password + "@" + serverIP + "/json.htm?type=command&param=udevice&idx=" + InIdx3 + "&nvalue=0&svalue=" + str(Val3)
r = requests.get(payload)

if (Val4 != oldVal4) :
oldVal4 = Val4
payload = "http://" + username + ":" + password + "@" + serverIP + "/json.htm?type=command&param=udevice&idx=" + InIdx4 + "&nvalue=0&svalue=" + str(Val4)
r = requests.get(payload)

if (Val5 != oldVal5) :
oldVal5 = Val5
payload = "http://" + username + ":" + password + "@" + serverIP + "/json.htm?type=command&param=udevice&idx=" + InIdx5 + "&nvalue=0&svalue=" + str(Val5)
r = requests.get(payload)


time.sleep(3)
This worked fine but as I said I want to make a plugin for it. The idea is that you can chance the analog output by editing a selector switch. In the script there is also a way to update the values from analog inputs of another board to Domoticz but that's not a priority.

The first problem I ran is was the python version that is used in the plugins I read that this is supposed to be python 3.5. The script ran in python 2.7 so a had to change the library I used to the newer version. After I installed smbus2 Version: 0.2.1 using pip3 Domoticz couldn't find the library. The solution for that was chancing the folder where pip3 installs the librarys to /usr/lib/python3.5/.

Then the real problem started when I uploaded the new code for the plugin (shown in the spoiler below) none of my plugins in the hardware tab would work. The would a message that the working loop started in the log but after that nothing. Not even the def onStart(self): is called. When I comment out the import smbus2 line everything works fine. Is anyone familiar with this problem? Help would be appreciated.
Spoiler: show
# Basic Python Plugin Example
#
# Author: GizMoCuz
#
"""
<plugin key="SOM-Control_Bus" name="SOM-Control Bus" author="joran" version="1.0.0">
<description>
<h2>Plugin for SOM-Control Bus</h2><br/>
<h3>Features</h3>
<ul style="list-style-type:square">
<li>0-10V Output</li>
</ul>
</description>
<params>
<param field="Mode1" label="Address I2C Output" width="150px" required="False"/>
<param field="Mode2" label="Address I2C Input" width="150px" required="False"/>
</params>
</plugin>
"""
import Domoticz
import smbus2

class BasePlugin:
enabled = False
def __init__(self):
#self.var = 123
return

def onStart(self):
Domoticz.Log("onStart called")
Domoticz.Device(Name="Status1", Unit=150, TypeName="Selector Switch", Options = {"LevelActions": "||||||||||","LevelNames": "Off|10%|20%|30%|40%|50%|60%|70%|80%|90%|100%","LevelOffHidden": "false","SelectorStyle": "1"}, Used=1).Create()
Domoticz.Device(Name="Status2", Unit=151, TypeName="Selector Switch", Options = {"LevelActions": "||||||||||","LevelNames": "Off|10%|20%|30%|40%|50%|60%|70%|80%|90%|100%","LevelOffHidden": "false","SelectorStyle": "1"}, Used=1).Create()
def onStop(self):
Domoticz.Log("onStop called")

def onConnect(self, Connection, Status, Description):
Domoticz.Log("onConnect called")

def onMessage(self, Connection, Data):
Domoticz.Log("onMessage called")

def onCommand(self, Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
Devices[Unit].Update(Level, str(Level))
Domoticz.Log(str(Level))

def onNotification(self, Name, Subject, Text, Status, Priority, Sound, ImageFile):
Domoticz.Log("Notification: " + Name + "," + Subject + "," + Text + "," + Status + "," + str(Priority) + "," + Sound + "," + ImageFile)

def onDisconnect(self, Connection):
Domoticz.Log("onDisconnect called")

def onHeartbeat(self):
Domoticz.Log("Heartbeat called")

global _plugin
_plugin = BasePlugin()

def onStart():
global _plugin
_plugin.onStart()

def onStop():
global _plugin
_plugin.onStop()

def onConnect(Connection, Status, Description):
global _plugin
_plugin.onConnect(Connection, Status, Description)

def onMessage(Connection, Data):
global _plugin
_plugin.onMessage(Connection, Data)

def onCommand(Unit, Command, Level, Hue):
global _plugin
_plugin.onCommand(Unit, Command, Level, Hue)

def onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile):
global _plugin
_plugin.onNotification(Name, Subject, Text, Status, Priority, Sound, ImageFile)

def onDisconnect(Connection):
global _plugin
_plugin.onDisconnect(Connection)

def onHeartbeat():
global _plugin
_plugin.onHeartbeat()

# Generic helper functions
def DumpConfigToLog():
for x in Parameters:
if Parameters[x] != "":
Domoticz.Debug( "'" + x + "':'" + str(Parameters[x]) + "'")
Domoticz.Debug("Device count: " + str(len(Devices)))
for x in Devices:
Domoticz.Debug("Device: " + str(x) + " - " + str(Devices[x]))
Domoticz.Debug("Device ID: '" + str(Devices[x].ID) + "'")
Domoticz.Debug("Device Name: '" + Devices[x].Name + "'")
Domoticz.Debug("Device nValue: " + str(Devices[x].nValue))
Domoticz.Debug("Device sValue: '" + Devices[x].sValue + "'")
Domoticz.Debug("Device LastLevel: " + str(Devices[x].LastLevel))
return

jia175891641
Posts: 10
Joined: Tuesday 12 June 2018 17:07
Target OS: Raspberry Pi
Domoticz version: V3.9000
Location: China-Zhejiang
Contact:

Re: Custom Python plugin stopped working after importing smbus2

Post by jia175891641 » Wednesday 01 August 2018 3:37

The problem maybe from request lib .demoticz is not supprt this lib. Delect it then try it
只要够努力 时间总会给你想要的

joran
Posts: 7
Joined: Friday 22 June 2018 0:13
Target OS: Raspberry Pi
Domoticz version: 3.8153
Contact:

Re: Custom Python plugin stopped working after importing smbus2

Post by joran » Wednesday 01 August 2018 13:44

When I comment out the line import smbus2 the plugins are fine, but I need this library for I2C communications. Is there another library that I can use instead of smbus2?

joran
Posts: 7
Joined: Friday 22 June 2018 0:13
Target OS: Raspberry Pi
Domoticz version: 3.8153
Contact:

Re: Custom Python plugin stopped working after importing smbus2

Post by joran » Thursday 20 September 2018 21:15

I found a workaround using only python scripts in the domoticz/scripts folder.
And I have added these scripts to level actions of my selector switch.

Post Reply

Who is online

Users browsing this forum: No registered users and 5 guests