Muhsina
Posts: 3
Joined: Mon Jul 09, 2018 12:31 pm

To check the status of relay using interrupt in raspberry pi

Tue Jul 10, 2018 5:32 am

Hi, I am working with a raspberry pi with a relay with the incandescent bulb, buzzer, and button via SSH. The code is in a way that when I press a key from the keyboard the bulb and buzzer should be ON and when I press button both should get OFF. But my button code is not working properly.
And also I want to check the status of the relay using the interrupt and whenever the light on it should be sent a message to the database. But I don't know with interrupts. Below is my code. Please help me. Thanks in advance

Code: Select all

import RPi.GPIO as GPIO    
import time    
in1 = 25 #GPIO25 pin22    
buzzer=24 #GPIO24 PIN18    
led=8 #GPIO8 PIN24    
button=23 #GPIO23 PIN 16   

GPIO.setmode(GPIO.BCM)    
GPIO.setwarnings(False)    
GPIO.setup(in1, GPIO.OUT)    
GPIO.setup(buzzer,GPIO.OUT)    
GPIO.setup(led,GPIO.OUT)    
        #GPIO.setup(button,GPIO.IN,pull_up_down=GPIO.PUD_DOWN)    
GPIO.setup(button, GPIO.IN, pull_up_down=GPIO.PUD_UP)    
GPIO.output(in1, False)    
GPIO.output(buzzer,False)    
GPIO.output(led,False)    
try:   

        GPIO.output(in1,False)    
        while True:    

                GPIO.output(in1,False)   
# check from database whether it is effective or deffective .currently I have inputted from keyboard     
                variable=raw_input()    
                if variable=="a":    
                        m=variable   
                        for x in m:    

                                GPIO.output(in1, True)    
                                time.sleep(0.05)    
                                GPIO.output(in1, False)    
                                time.sleep(0.05)    
                        GPIO.output(buzzer,True)    
                        GPIO.output(led,True)    
                        if in1==True || buzzer==True:    
                                print "messge to database:pending"     
                #def my_callback(channel):    
                #if GPIO.input(button) == True:        
                GPIO.wait_for_edge(button, GPIO.FALLING)       
                #if variable=="b":       
                        #while 1:        
                GPIO.output(in1,False)        
                GPIO.output(led,False)        
                GPIO.output(buzzer,False)        
                print "mesage to database:ack completed"         
                        #else:    
                                #pass    
                #else:     
                        #pass     
                GPIO.output(in1,False)     

except:     
        GPIO.cleanup()    
        GPIO.output(in1,False)    

If there is any error in my code please help me to rectify that. Thanks

User avatar
Z80 Refugee
Posts: 358
Joined: Sun Feb 09, 2014 1:53 pm

Re: To check the status of relay using interrupt in raspberry pi

Tue Jul 10, 2018 6:43 am

Muhsina wrote:
Tue Jul 10, 2018 5:32 am
But my button code is not working properly.
Are you sure it isn't your button that is not working properly? Beginners Guide to Wiring Things to the GPIO
Muhsina wrote:
Tue Jul 10, 2018 5:32 am
And also I want to check the status of the relay using the interrupt and whenever the light on it should be sent a message to the database.
I don't think that's a very good idea. The purpose of a relay is to adapt the RPi's weak output drive capability to a high power load and to isolate the potentially damaging power from getting back to the RPi. In order to "check the status" of the relay, you would need to connect the relay's output back to the RPi (probably via an opto-isolator, depending on what the relay is actually driving). You already know what the status of the relay should be - it's responding to the RPi output. I assume you are looking for fault monitoring, in which case you should be checking the status of the driven load and not just the relay output. The problem with fault monitoring is that it can more than double your circuitry, and then you need something to check your fault monitoring circuit...

However, that said, it is easy enough if the relay has a spare pair of contacts, which can be used as a dedicated switch input to the RPi. That will confirm the relay has switched, but tell you nothing about the load circuit.

You don't need interrupts either - interrupts should be reserved for events which occur unpredictably and must be acted on as soon as possible. With a suitable status circuit wired to an input pin, read the pin periodically in the main loop of your program instead of using an interrupt.
Military and Automotive Electronics Design Engineer (retired)

For the best service: make your thread title properly descriptive, and put all relevant details in the first post (including links - don't make us search)!

Return to “Beginners”