GreenhouseMike
Posts: 2
Joined: Tue May 11, 2021 9:04 pm

Python code check for Form-C mechanical relay

Wed May 12, 2021 8:01 pm

Hello, and thanks for taking the time to read this and help me. Definitely new to the Pi world and Python. My problem, I have a greenhouse and I need to monitor the temperature for heat or cooling failures. I used to use a device (Sensaphone) that called me on a landline if there was a failure. I no longer have a landline so I want to use a different function of the Sensaphone and the Pi to email me and text me if there is a temperature alarm. Briefly, the Sensaphone has a relay output, Form-C Normally Open/Normally Closed mechanical relay rated for up to 30v AC 1A that I plan to wire to my Pi as in the diagram below. The Pi outputs 5V DC (pin 4 and 6) to a DC-AC voltage module inverter since the Sensaphone can only switch AC. When the NO switch closes, the AC voltage goes to a buck step-down converter where the output is changed to 3V DC (I can vary this). This I then run to pin 14 (ground) and GPIO 23 (pin 16). I found code online that will send the text and email messages, I’m just not sure about the GPIO coding for GPIO23 and “connecting “ it to the coding for the messaging. Any help would be appreciated in reviewing my code to make sure it will work and not burn up my pi. Important note – the NO switch will stay closed sending a constant 3v to the Pi until I can manually turn off the alarm. That could be hours.

Code: Select all

import smtplib
import RPi.GPIO as GPIO
import time
  
#Email Variables
SMTP_SERVER = 'smtp.gmail.com' #Email Server 
SMTP_PORT = 587 #Server Port 
GMAIL_USERNAME = 'myaccount@gmail.com' #change to gmail account
GMAIL_PASSWORD = 'yourPassword'  #change to gmail password
 
#Set GPIO pins to use BCM pin numbers
GPIO.setmode(GPIO.BCM)
 
#Set digital pin 23(BCM) to an input
GPIO.setup(23, GPIO.IN)
 
#Set digital pin 23(BCM) to an input and enable the pullup 
GPIO.setup(23, GPIO.IN, pull_up_down=GPIO.PUD_UP)
 
#Event to detect NO switch change to closed position
GPIO.add_event_detect(23, GPIO.FALLING)
 
  class Emailer:
    def sendmail(self, recipient, subject, content):
          
        #Create Headers
        headers = ["From: " + GMAIL_USERNAME, "Subject: " + subject, "To: " + recipient,
                   "MIME-Version: 1.0", "Content-Type: text/html"]
        headers = "\r\n".join(headers)
  
        #Connect to Gmail Server
        session = smtplib.SMTP(SMTP_SERVER, SMTP_PORT)
        session.ehlo()
        session.starttls()
        session.ehlo()
  
        #Login to Gmail
        session.login(GMAIL_USERNAME, GMAIL_PASSWORD)
  
        #Send Email & Exit
        session.sendmail(GMAIL_USERNAME, recipient, headers + "\r\n\r\n" + content)
        session.quit
  
sender = Emailer()
 
while True:
    if GPIO.event_detected(23):
        sendTo = 'anotheremail@email.com, phonenumber@txt.att.net'
        emailSubject = "GREENHOUSE ALARM!"
        emailContent = "The greenhouse temperature is too high or too low: " + time.ctime()
        sender.sendmail(sendTo, emailSubject, emailContent)
        print("Email Sent")
 
    time.sleep(0.1)

[attachment=0]circuit.jpg[/attachment]
Attachments
circuit.jpg
diagram
circuit.jpg (9.66 KiB) Viewed 258 times

jayben
Posts: 274
Joined: Mon Aug 19, 2019 9:56 pm

Re: Python code check for Form-C mechanical relay

Fri May 14, 2021 8:41 am

Just because the relay says 30V AC, doesn't mean it can't switch DC. Forget the AC-DC-AC conversion, just pull the Pi input up to 3.3V using a 1K resistor, than use the relay to short the signal to ground.

However, I'd first use a DMM to check there is no hint of mains voltage on the relay outputs, just in case the documentation is wrong, and it is actually a mains switch.

GreenhouseMike
Posts: 2
Joined: Tue May 11, 2021 9:04 pm

Re: Python code check for Form-C mechanical relay

Fri May 14, 2021 2:31 pm

Thank you for the reply and suggestion. I've already built the circuit I included in my post and tested it, so I know that all the voltage conversions and the relay are working properly. And, I can vary the output DC voltage to a higher or lower level while keeping amps within tolerances but went with 3V DC just to be on the safe side. So, I'd like to keep the setup I have built and really just want to know if the Python code is correct. Can you check that?

User avatar
B.Goode
Posts: 12060
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: Python code check for Form-C mechanical relay

Fri May 14, 2021 2:57 pm

GreenhouseMike wrote:
Fri May 14, 2021 2:31 pm
Thank you for the reply and suggestion. I've already built the circuit I included in my post and tested it, so I know that all the voltage conversions and the relay are working properly. And, I can vary the output DC voltage to a higher or lower level while keeping amps within tolerances but went with 3V DC just to be on the safe side. So, I'd like to keep the setup I have built and really just want to know if the Python code is correct. Can you check that?

Can I suggest that for testing/commissioning purposes you consider replacing the input from the relay with a simple open/closed switch between Ground and Pin #16 on the header, and then testing the logic of your code yourself?


Just 'eyeballing' the code, you setup the input pin twice. It doesn't matter, but in the future you may wonder why... ?

User avatar
joelostinspace
Posts: 129
Joined: Sat Aug 10, 2019 2:51 pm
Location: Earth

Re: Python code check for Form-C mechanical relay

Sat May 15, 2021 3:30 am

When the NO switch closes, the AC voltage goes to a buck step-down converter where the output is changed to 3V DC
I would like to know the temp and humidity and with those parameters heat, cool, add moisture or reduce and if that fails then send off an email. How many emails can you send in a couple hours with a .1 of second interval? maybe limit the frequency after the first 20(2 seconds).

If a little knowledge is dangerous, where is the man who has so much as to be out of danger?


--- Thomas Huxley

Return to “Python”