However, it wasn't... RPi.GPIO wasn't found by the plugin-system, and the reason is /usr/lib/python3/dist-packages is not present in the path used by the plugin system. It works fine if I run python3 interactively from the console. So I added this directory to the path right in the beginning of the plugin module by means of the following two lines of code:
Code: Select all
import sys
sys.path.append('/usr/lib/python3/dist-packages')
2017-03-13 08:32:52.825 PluginSystem: Entering work loop.
2017-03-13 08:32:52.932 (X) Initialized version 1.0.0, author 'ubee'
2017-03-13 08:32:55.425 Hardware Monitor: Fetching data (System sensors)
(X is the name of the HW).
However, no switch device is created and I can't find anything in the log file until a few minutes later when I see:
2017-03-13 08:35:30.517 Error: X hardware (4) thread seems to have ended unexpectedly
So none of the callback functions was called, not even onStart. My conclusion is adding /usr/lib/python3/dist-packages to the path hurts the plugin system for some reason. I've noticed that before when I tried to use another library in a different plugin. Discovering that, I changed my code to not depend on certain libraries. However, I have seen another post about appending libraries to the path so I thought I could give it a try again.
I include the source code below. As you can see it is more or less as simple as a plugin can get.
Code: Select all
"""
<plugin key="GPIn" name="GPIn" author="ubee" version="1.0.0" wikilink="http://www.domoticz.com/wiki/plugins/plugin.html" externallink="https://www.google.com/">
<params>
<param field="Pin" label="Pin" width="30px" required="true" default=""/>
<param field="Mode1" label="Debug" width="75px">
<options>
<option label="True" value="Debug"/>
<option label="False" value="Normal" default="true" />
</options>
</param>
</params>
</plugin>
"""
import sys
sys.path.append('/usr/lib/python3/dist-packages')
import RPi.GPIO as GPIO
import Domoticz
gpioPin=""
def onStart():
#
#
# Create one switch contact corresponding to the specified pin
#
#
#
global gpioPin
Domoticz.Log("onStart called")
if Parameters["Mode1"] == "Debug":
Domoticz.Debugging(1)
gpioPin=Parameters["Pin"]
GPIO.setmode(GPIO.BCM)
GPIO.setup(gpioPin, GPIO.IN)
Domoticz.Device(Name="Switch Contact BCM pin "+ str(gpioPin), Unit=1, TypeName="Switch").Create()
Domoticz.Log("Switch contact created.")
DumpConfigToLog()
return True
def onStop():
Domoticz.Log("onStop called")
return Tr
def onConnect(Status, Description):
Domoticz.Log("onConnect called")
return True
def onMessage(Data, Status, Extra):
Domoticz.Log("onMessage called")
return True
def onCommand(Unit, Command, Level, Hue):
Domoticz.Log("onCommand called for Unit " + str(Unit) + ": Parameter '" + str(Command) + "', Level: " + str(Level))
return True
def onNotification(Data):
Domoticz.Log("onNotification: " + str(Data))
return True
def onDisconnect():
Domoticz.Log("onDisconnect called")
return True
def onHeartbeat():
#
# Called periodically
#
# Read digital input
#
Domoticz.Log("onHeartbeat called")
data=GPIO.input(gpioPin)
UpdateDevice(1,data,str(data))
return True
# 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))
for item in OneWireIds:
Domoticz.Debug("1-wire sensor "+ item)
return
def UpdateDevice(Unit, nValue, sValue):
# Make sure that the Domoticz device still exists (they can be deleted) before updating it
Domoticz.Debug("Update unit no: "+str(Unit)+" value: "+ str(nValue)+" "+ str(sValue))
if (Unit in Devices):
if (Devices[Unit].nValue != nValue) or (Devices[Unit].sValue != sValue):
Devices[Unit].Update(nValue, str(sValue))
Domoticz.Log("Update "+str(nValue)+":'"+str(sValue)+"' ("+Devices[Unit].Name+")")
return