Plugin - ShiftRegister 74HC595

Python and python framework

Moderator: leecollings

Post Reply
Neutrino
Posts: 21
Joined: Sunday 08 May 2016 19:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: France
Contact:

Plugin - ShiftRegister 74HC595

Post by Neutrino »

Hi everybody,

I try to add shift-register 75hc595 support in domoticz on a Pi.
I tryed this :

Code: Select all

#           ShitfRegister Plugin
#
#           Author:     Neutrino, 2016
#
"""
<plugin key="ShiftPi" name="Shitf Register 74HC595 Raspberry" author="neutrino" version="1.0.0" wikilink="">
    <params>
        <param field="Mode1" label="SER (DS) Pin" width="30px" required="true" default="25"/>
        <param field="Mode2" label="RCLK (ST_CP) Pin" width="30px" required="true" default="24"/>
        <param field="Mode3" label="SRCLK (SH_CP) Pin" width="30px" required="true" default="23"/>
        <param field="Mode4" label="Number of Shift Registers" width="30px" required="true" default="1"/>
        <param field="Mode6" label="Debug" width="75px">
            <options>
                <option label="True" value="Debug"/>
                <option label="False" value="Normal"  default="true" />
            </options>
        </param>
    </params>
</plugin>
"""
import Domoticz
import os
#import RPi.GPIO as GPIO

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

	def onStart(self):
		Domoticz.Log("onStart called")
		if Parameters["Mode6"] == "Debug":
			Domoticz.Debugging(1)
		Domoticz.Log("On Start SR")
		i=len(Devices)
		if (len(Devices)<int(Parameters["Mode4"]*8)):
			while i< int(Parameters["Mode4"])*8 :
				Domoticz.Device(Name="SR Pin "+str(i),  Unit=i,Type=17, Switchtype=0).Create()
				i += 1		
		os.system("sudo /usr/local/bin/gpio mode "+Parameters["Mode1"]+" out")
		os.system("sudo /usr/local/bin/gpio mode "+Parameters["Mode2"]+" out")
		os.system("sudo /usr/local/bin/gpio mode "+Parameters["Mode3"]+" out")
		Domoticz.Heartbeat(10)
		DumpConfigToLog()
		return True

	def onStop(self):
		Domoticz.Log("onStop called")

	def onCommand(self, Unit, Command, Level, Hue):
		Domoticz.Log("Unit : "+ str(Unit))
		Domoticz.Log("Command : "+ str(Command))
		i=0
		com = 0
		if Command == 'On':
			com = 1
		Devices[Unit].Update(com,'')
		os.system("sudo /usr/local/bin/gpio write "+Parameters["Mode2"]+" 0")
		while i< int(Parameters["Mode4"])*8 :
			os.system("sudo /usr/local/bin/gpio write "+Parameters["Mode3"]+" 0")
			if Unit == i:
				os.system("sudo /usr/local/bin/gpio write "+Parameters["Mode1"]+" "+str(com))
				Domoticz.Debug("sudo /usr/local/bin/gpio write "+Parameters["Mode1"]+" "+str(com))
			else:
				os.system("sudo /usr/local/bin/gpio write "+Parameters["Mode1"]+" "+str(Devices[i].nValue))
				Domoticz.Debug("sudo /usr/local/bin/gpio write "+Parameters["Mode1"]+" "+str(Devices[i].nValue))
			os.system("sudo /usr/local/bin/gpio write "+Parameters["Mode3"]+" 1")
			i += 1
		os.system("sudo /usr/local/bin/gpio write "+Parameters["Mode2"]+" 1")
		
		
		return True

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

global _plugin
_plugin = BasePlugin()

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

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

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

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
Heartbeat is called every 10 sec, but i don't need that. Is it possible to disable this ?
Also, it could be used to force the 74hc595 states in case of parasites.

Actually, it works. But sometimes, OnCommand is called up very quickly, and sometimes it takes 10 seconds...
I'm unable to use the RPi.GPIO library.
If I add :

Code: Select all

import RPi.GPIO as GPIO
Plugins system stops working with no message. :(
User avatar
Dnpwwo
Posts: 820
Joined: Sunday 23 March 2014 9:00
Target OS: Raspberry Pi / ODroid
Domoticz version: Beta
Location: Melbourne, Australia
Contact:

Re: Plugin - ShiftRegister 74HC595

Post by Dnpwwo »

@Neutrino,

You can remove the onHeartbeat call back in the same way that you have removed the other you don't use. Domoticz won't care. You don't need to specifiy an interval either (it defaults to 10 if it is required at all).

The plugin framework's command queue is checked 20/second and executes immediately so you response should be consistent unless:
  • You are running other plugins that are slow to return control to domoticz
  • Your plugin is not leaving onCommand as quickly as you think
I would suggest you add a debug log message to show when your onCommand exits so you can see where the delay is.

I will have a look at the RPi.GPIO import crashing Domoticz but a Google search leads me to believe that it does not work on versions of Python above 3.2 which is an issue when the plugin framework requires Python 3.4 or above. Have you looked at http://pythonhosted.org/RPIO/rpio_py.html as an alternative?
The reasonable man adapts himself to the world; the unreasonable one persists to adapt the world to himself. Therefore all progress depends on the unreasonable man. George Bernard Shaw
Neutrino
Posts: 21
Joined: Sunday 08 May 2016 19:00
Target OS: Raspberry Pi / ODroid
Domoticz version: 2021.1
Location: France
Contact:

Re: Plugin - ShiftRegister 74HC595

Post by Neutrino »

Dnpwwo wrote:
  • You are running other plugins that are slow to return control to domoticz
That's the point!
I made a crappy plugin to replace the original One-wire hardware, but it takes 10 sec to get all the data from all the ds18b20.
If I enable only the Shift-register script, it works like a charm.
Thank you !

I tried the RPIO library, but it doesn't work on my Lenny.
I'll switch on Jessie very soon :)
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest