Start and stop a buzzer beeping (GPIO)  [Solved]

Easy to use, 100% Lua-based event scripting framework.

Moderator: leecollings

Post Reply
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Start and stop a buzzer beeping (GPIO)

Post by jsiegmund »

Hi,

I've got a buzzer hooked up to my pi on GPIO 22. I'm able to control it, that's all fine. But instead of making it beep continuously, I'd like to have it do beep-beep-beep with intervals instead. I've got a simple python script which does that, but what I'm struggling with is how to control stopping and starting from my dzVents scripts.

Example scenario:
- Door opens. Event triggers in domoticz and pi starts beeping.
- Door closes. Event triggers and pi should stop beeping now.

I'm familiar with dzVents scripting by now, but this I have no clue how to properly implement. BTW I'm not at all bound to the python script, another solution is fine. It's dead simple probably but I need some help getting there :)

Best case scenario would be if I could create a virtual switch which controls the beeping sound on/off. That way the rest of my scripting only has to toggle the beeping switch.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Start and stop a buzzer beeping (GPIO)

Post by waaren »

Please share the Python script. This will help us helping you.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
rene2716
Posts: 17
Joined: Friday 04 May 2018 11:53
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Start and stop a buzzer beeping (GPIO)

Post by rene2716 »

Hi,
I'm building my home alarm, and want to use a buzzer to sense arm and disarm of domoticz for 45 secs. Please share the script so I can have a headstart of how to do this
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Start and stop a buzzer beeping (GPIO)

Post by jsiegmund »

Sorry for this - very - late reply. For setting up GPIO I used instructions from

https://www.domoticz.com/wiki/GPIO

By using the sysfs hardware I got the switch autoconfigured, that works fine. A sample script on how to do beeping can be found here: https://www.instructables.com/id/Raspbe ... -a-Buzzer/.

I still have this requirement though. How to trigger "start beeping" and "stop beeping" from domoticz?
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Start and stop a buzzer beeping (GPIO)

Post by jsiegmund »

Copy/paste from https://stackoverflow.com/questions/571 ... rom-python

Let me start with noting that my Python skills are not that great, so bear with me please or skip this question if you don't mind noob questions ;)

My set-up consists of a raspberry pi, domoticz (home automation software) and a buzzer hooked up the GPIO. Controlling the buzzer already works, so that's all fine and well. I can start python scripts from the domoticz environment which then output beeping sounds. The main issue is that the domoticz scripting environment will wait for the script to finish. So a 30 second beeping loop will lock up the environment for 30 seconds. A second problem is that once I kick off that 30 second script, I have no way to cancel it.

So basically I have two requirements:

The way that I call the script should be non-blocking. Right now I'm using io.popen() for calling the script from another LUA based script.
Any consecutive commands should override previous issued commands. So when a 30 seconds beep loop is running, a "speaker off" like command should kill that off.
Guess what I need is some guidance in how to best do this within a Python based environment. I know my way around C# but have no clue and disclaimer: I really didn't bother doing a proper course on Python since this is supportive scripting only and I don't have another option.
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Start and stop a buzzer beeping (GPIO)

Post by waaren »

jsiegmund wrote: Saturday 20 July 2019 16:32 Copy/paste from https://stackoverflow.com/questions/571 ... rom-python
Sorry but I still don't see an answer to the original question to share your python script.
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Start and stop a buzzer beeping (GPIO)

Post by jsiegmund »

Ah ok, sorry. If that helps: https://gist.github.com/jsiegmund/d25e3 ... 819d5a84ec

The way that I call if from LUA:

Code: Select all

if (zoneText == 'Arming') then
    -- Arming gives 30 seconds of delay before armed, start countdown
    domoticz.log('Arming, starting buzzer 5 secs')
    io.popen('python /home/pi/domoticz/scripts/buzzer.py 5')
end
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Start and stop a buzzer beeping (GPIO)

Post by waaren »

jsiegmund wrote: Sunday 21 July 2019 21:33 Ah ok, sorry. If that helps
It sure helps.
Please find below 2 dzVents scripts (one to activate the Python code and one to stop it gracefully by (re)moving the logfile) and the changed Python code.

When not yet familiar with dzVents please start with reading Get started Before implementing. Special attention please for
"In Domoticz go to Setup > Settings > Other and in the section EventSystem make sure the checkbox 'dzVents disabled' is not checked. Also make sure that in the Security section in the settings you allow 127.0.0.1 to not need a password. dzVents uses that port to send certain commands to Domoticz. Finally make sure you have set your current location in Setup > Settings > System > Location, otherwise there is no way to determine nighttime/daytime state."

Please feel free to ask if something is not clear.

Code: Select all

--Activate python
return 
{
    on = { timer = { 'every 6 minutes' }},  -- just for the example

    logging = { level = domoticz.LOG_DEBUG, marker = 'buzzer set' },

    execute = function( dz )
        local pyCode =  '/usr/local/domotica/pysources/buzzer.py'  
        local buzLog = '/tmp/buzzer.log'
        local seconds = 500
        local backGround = '&'

        local osString = 'sudo python ' .. pyCode .. ' ' .. seconds .. ' ' .. buzLog .. ' ' .. backGround
        dz.log('Executing: ' .. osString,dz.LOG_DEBUG)
        os.execute(osString)

    end
}

Code: Select all

--stop Python
return 
{
    on = { timer = { 'every 4 minutes' }},  -- just for the example

    logging = { level = domoticz.LOG_DEBUG, marker = 'buzzer kill' },

    execute = function( dz )
        local buzLog = '/tmp/buzzer.log'
 
        local osString = 'sudo mv ' .. buzLog .. ' ' .. buzLog .. '-' .. dz.time.dDate 
        dz.log('Executing: ' .. osString,dz.LOG_DEBUG)
        os.execute(osString)
    end
}

Code: Select all

#optional args: 
#     1st parm = seconds to beep
#     2nd parm = log / sentinel File
#

#Libraries 
import sys
import os.path
import RPi.GPIO as GPIO # Importeer de GPIO bibliotheek.
from time import sleep ## Importeer de bibliotheek voor tijdfuncties

# default values
seconds = 5
logFile = "/tmp/buzzer.log"
buzzer_pin = 22  # Geef het nummer van de pin op waar de speaker is aangesloten.


# Set the number of seconds to the provided argument (or use default)
if len(sys.argv) > 1:
  seconds = int(sys.argv[1]) 

# Set the log- / sentinel file (or use default)
if len(sys.argv) > 2:
  logFile = sys.argv[2] 

# initialize
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BCM) # GebruikBroadcom GPIO benaming van de pinnen.
GPIO.setup(buzzer_pin, GPIO.OUT) # Zet de speaker pin als uitgang.


print "seconds: " + str(seconds)
print "Druk op CTRL+C om het programma te beeindigen."
print "Of verwijder logFile (" + logFile + ")"

i = 0
f = open(logFile,"a",buffering=1) # buffering=1 ==>> flush every line 

def cleanup( text ): 
	GPIO.output(buzzer_pin, False) # Zet speakerpin laag.
	GPIO.cleanup()
	print text
	return

def setGPIO( bool ):
	GPIO.output(buzzer_pin, bool) # Zet speakerpin.
	sleep(.5)
	return
 
try:
	while i < seconds and os.path.exists(logFile):
		setGPIO(True)
		setGPIO(False)
		i += 1
		logLine = "buzzer buzzed " + str(i) + " times\n"
		f.write(logLine);

except KeyboardInterrupt:  
	cleanup("GPIO netjes afsluiten (Keyboard interrupt)")

except OSError:
	cleanup("GPIO netjes afsluiten (OS error)")

else:
	cleanup("GPIO netjes afsluiten (normal end)")
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Start and stop a buzzer beeping (GPIO)

Post by jsiegmund »

Thanks for that! I'll have to carve out some evening time this week to try it out, I'll let you know how that worked out :)
User avatar
waaren
Posts: 6028
Joined: Tuesday 03 January 2017 14:18
Target OS: Linux
Domoticz version: Beta
Location: Netherlands
Contact:

Re: Start and stop a buzzer beeping (GPIO)

Post by waaren »

jsiegmund wrote: Monday 22 July 2019 7:55 Thanks for that! I'll have to carve out some evening time this week to try it out, I'll let you know how that worked out :)
Any results so far ?
Debian buster, bullseye on RPI-4, Intel NUC.
dz Beta, Z-Wave, RFLink, RFXtrx433e, P1, Youless, Hue, Yeelight, Xiaomi, MQTT
==>> dzVents wiki
jsiegmund
Posts: 33
Joined: Sunday 14 May 2017 21:13
Target OS: Raspberry Pi / ODroid
Domoticz version:
Contact:

Re: Start and stop a buzzer beeping (GPIO)  [Solved]

Post by jsiegmund »

Sorry for the late reply once again. But yes! I did manage to get it working with your latest set of scripts. It's perfect for now, might extend it a bit in the future. But this is exactly what I was looking for, thanks again.
Post Reply

Who is online

Users browsing this forum: No registered users and 1 guest