pcmanbob
Posts: 7966
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: DHT22 to close a relay if the temperature drops below a set value

Sat Feb 08, 2020 3:48 pm

Don't think you will need as long as 5 seconds 2 or 3 seconds should be long enough depending on how far and how fast it moves.

Yes will help if you post a copy of your code as it stands now.

Is you relay active high or active low ?

Do you want the second relay to auto reset once the temperature rises above alarm level ?
if so at what temperature ?
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

pcmanbob
Posts: 7966
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: DHT22 to close a relay if the temperature drops below a set value

Sun Feb 09, 2020 10:20 am

So I used the last copy of your code that you posted.

Tied it up a bit so its easier to read and added the second relay activation and set it to clear when the temperature returned to normal .

Code: Select all

import RPi.GPIO as GPIO
import Adafruit_DHT #DHT22 sensor library
import I2C_LCD_driver #I2C lcd  library  GPIO 2 PI pin 3
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT) #Gpio23 pin 16 heating
GPIO.setup(13, GPIO.OUT) #Gpio27 pin 13 alarm

mylcd = I2C_LCD_driver.lcd()

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4 #DHT22 set to GPIO 4 Pie pin 7 

set_temp = 37.8  # this is the required temperature

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if humidity is not None and temperature is not None:
       #print("Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(temperature, humidity))
        mylcd.lcd_clear()
        mylcd.lcd_display_string("  Incubator Status" ,1)
        mylcd.lcd_display_string("    Temp={0:0.1f}*C".format(temperature, humidity), 2)
        mylcd.lcd_display_string("   Humidity={1:0.1f}%".format(temperature, humidity), 3)
        
        
        # test for low temperature
        if temperature < set_temp:
            GPIO.output(16,1)
        
        # test for high temperature
        if temperature > (set_temp + 1):
            GPIO.output(16,0)
            
        # test for low temperature alarm
        if temperature < (set_temp - 10):
            GPIO.output(13,1)
            
        # clear low temperature alarm
        if temperature >= (set_temp):
            GPIO.output(13,0)    
            
        sleep(10) #this is the time between taking readings and acting on them you can reduce it but not below 5 seconds 
            
    else:

        mylcd.lcd_clear()
        mylcd.lcd_display_string("    DHT22 Failed!", 2)
    
        sleep(5) #this is the time between a failed sensor read and trying again
        
untested .
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Tony-d
Posts: 79
Joined: Mon Jun 02, 2014 11:54 pm

Re: DHT22 to close a relay if the temperature drops below a set value

Sun Feb 09, 2020 3:42 pm

Thank You I will test this out sorry I did not get back to you sooner working on the lay out seems to be growing!
LOL
Thanks again.

Tony-d
Posts: 79
Joined: Mon Jun 02, 2014 11:54 pm

Re: DHT22 to close a relay if the temperature drops below a set value

Sun Feb 09, 2020 5:16 pm

I works like a charm, I am now mounting all the components on a plastic cutting bord, and adding a good power supply for the relays and servo.after that i am going to add a pi cam and apache web server so the inside of the incubator can be viewed with out opening the unit. and i want to put some led lighting in side that will come on when the heater relay goes off, i think i can handle that. I think that's just a simple if then kind of thing. I'll get back to you in a few days with a report and some photos! This is very cool Thank You!

Tony-d
Posts: 79
Joined: Mon Jun 02, 2014 11:54 pm

Re: DHT22 to close a relay if the temperature drops below a set value

Mon Feb 10, 2020 9:12 pm

I have both python 2 and 3 loaded on my pi and i think that is tripping me up?
[email protected]:~ $ python --version
Python 2.7.16
[email protected]:~ $ python3 --version
Python 3.7.3
I keep getting syntax errors when I try example code fond on line. (after triple checking it).
The why I am creating my code is vie SSH using Tera term. I do a sudo nano file name.py
then save my file and run it with python filename.py . Am I programming in python 2? if so what would you recommend doing?
I would think as i am new i would be better of going with 3. this can be very frustrating.
Thank you for your guidance.
Tony

pcmanbob
Posts: 7966
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: DHT22 to close a relay if the temperature drops below a set value

Tue Feb 11, 2020 10:21 am

You should really be using python3 now , all my examples were written to use python3.

so to run your program at the command line you would use python3 myprogram.py

examples you find on line could very well be written for python 2 so my not work under python 3 there are some differences, for example the print statement.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported


Tony-d
Posts: 79
Joined: Mon Jun 02, 2014 11:54 pm

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 5:00 pm

I hate to admit defeat! But there is no other word for it. What i am trying to do here is turn on pin 15 if pin 16 is off,
i have been stuck on a syntax error in my while true loop for days.
Thank you.


Code: Select all

import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(16,GPIO.IN)
GPIO.setup(15, GPIO.OUT

while True:
    if GPIO.input(16):
    GPIO.output(15, GPIO.low)
    else:
    time.sleep(2)
    GPIO.output(15, GPIO.high)
    time.sleep(2)

pcmanbob
Posts: 7966
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 6:09 pm

Well I would have thought you would at least got the indention correct by now ,

So corrected indention, corrected high/low to capitals, and set the output to the correct value on input being low, and place sleep so it will be run no matter which action is taken.

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(16,GPIO.IN)
GPIO.setup(15, GPIO.OUT

while True:
    if GPIO.input(16) == 0:
        GPIO.output(15, GPIO.HIGH)
    else:
        GPIO.output(15, GPIO.LOW)
    time.sleep(2)
    
and next time please include the error message, it was simple to fix the error this time byt more complex code may be un-fixable without the error message.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Tony-d
Posts: 79
Joined: Mon Jun 02, 2014 11:54 pm

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 6:25 pm

I am sorry I'll work harder.
Thank you.

Tony-d
Posts: 79
Joined: Mon Jun 02, 2014 11:54 pm

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 6:31 pm

I am sorry i am still getting the same error,
i copied your code and pasted it to a new file on the pi. saved and ran. this is the result.
i am sorry



[email protected]:~ $ python3 relay.py
File "relay.py", line 8
while True:
^
SyntaxError: invalid syntax

pcmanbob
Posts: 7966
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 6:44 pm

So posting the error message helps,

if you look at the code which I copied from your post, you will see that the ")" is missing from the gpio 15 setup line.

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(16,GPIO.IN)
GPIO.setup(15, GPIO.OUT   <<<<<<<<<<<<<<<<< missing " ) "
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Tony-d
Posts: 79
Joined: Mon Jun 02, 2014 11:54 pm

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 7:58 pm

Thank You very much, the program runs but is not acting like i think it should,
What i thought i was doing was if pin 16 was low set pin 15 to high, and if 16 was high set 15 to low
16 is the heater light bulb 15 are led lights. whats happening when i run it is it turns 16 off or "low" & 15 On or "high"
I am ver confused. there are no errors to post.
Thank you

Code: Select all

import RPi.GPIO as GPIO
from time import sleep
GPIO.setwarnings(False)
GPIO.setmode (GPIO.BOARD)
GPIO.setup(16,GPIO.IN)
GPIO.setup(15, GPIO.OUT)

while True:
    if GPIO.input(16) == 0:
        GPIO.output(15, GPIO.HIGH)
    else:
        GPIO.output(15, GPIO.LOW)
        sleep(2)

pcmanbob
Posts: 7966
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 8:06 pm

The posted code only reads the input from pin 16 , there is no output from pin 16. The only output is on pin 15 which will be high or low depending on the state of pin 16.

Why are you doing this as a separated program you could just add this the the main program that controls the actual heater.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Tony-d
Posts: 79
Joined: Mon Jun 02, 2014 11:54 pm

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 8:14 pm

I wanted to get it working first so i did not mess up the main program.

pcmanbob
Posts: 7966
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 8:19 pm

If you post the main program and a list of any other gpio you are using , I will add this to the code its only 3 lines,
along with a diagram showing which pins are being used for what to make sure you are connecting things to the right gpio pins.

Edit....
so reading your post again you said.

if pin 16 was low set pin 15 to high
if 16 was high set 15 to low.

when i run it is it turns 16 off or "low" & 15 On or "high" which is what you asked for , the posted code cant actually turn pin 16 low or high it only reads the state of pin 16

Edit 2.....

so I added pin 15 to the last lot of code I posted , so when pin 16 is set low , pin 15 is set high and when pin 16 is set high , pin 15 is set low.

Code: Select all

import RPi.GPIO as GPIO
import Adafruit_DHT #DHT22 sensor library
import I2C_LCD_driver #I2C lcd  library  GPIO 2 PI pin 3
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT) #Gpio23 pin 16 heating
GPIO.setup(13, GPIO.OUT) #Gpio27 pin 13 alarm
GPIO.setup(15, GPIO.OUT) #Gpio22 pin 15 LED lights

mylcd = I2C_LCD_driver.lcd()

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4 #DHT22 set to GPIO 4 Pie pin 7 

set_temp = 37.8  # this is the required temperature

while True:
    humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

    if humidity is not None and temperature is not None:
       #print("Temp={0:0.1f}*C  Humidity={1:0.1f}%".format(temperature, humidity))
        mylcd.lcd_clear()
        mylcd.lcd_display_string("  Incubator Status" ,1)
        mylcd.lcd_display_string("    Temp={0:0.1f}*C".format(temperature, humidity), 2)
        mylcd.lcd_display_string("   Humidity={1:0.1f}%".format(temperature, humidity), 3)
        
        
        # test for low temperature
        if temperature < set_temp:
            GPIO.output(16,1)
            GPIO.output(15,0)
        
        # test for high temperature
        if temperature > (set_temp + 1):
            GPIO.output(16,0)
            GPIO.output(15,1)
            
        # test for low temperature alarm
        if temperature < (set_temp - 10):
            GPIO.output(13,1)
            
        # clear low temperature alarm
        if temperature >= (set_temp):
            GPIO.output(13,0)    
            
        sleep(10) #this is the time between taking readings and acting on them you can reduce it but not below 5 seconds 
            
    else:

        mylcd.lcd_clear()
        mylcd.lcd_display_string("    DHT22 Failed!", 2)
    
        sleep(5) #this is the time between a failed sensor read and trying again
   
and from the posted code so far these are the gpio pins you are using, but you seem to be using a mixture of board numbering and gpio number in different blocks of program code.

Image
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Tony-d
Posts: 79
Joined: Mon Jun 02, 2014 11:54 pm

Re: DHT22 to close a relay if the temperature drops below a set value

Thu Feb 13, 2020 11:50 pm

Thank You sir this works fine
one last thing I promise!
I have a program that turns the eggs every 8 hrs
i start both programs with the path in the crontab file.

Code: Select all

@reboot python /home/pi/humidity.py
@reboot python /home/pi/egg90.py
is this the right way to do this? it does work but is it the best way to do it?
Can you combine the two programs or should i just let it be?
It does work so its not a problem.
Thank You SO much!!!


This is the egg turning code

Code: Select all

import RPi.GPIO as GPIO                    # Importing RPi library to use the GPIO pins
from time import sleep                       # Importing sleep from time library to add delay in code
servo_pin = 11                                      # Initializing the GPIO 11 for servo motor
GPIO.setmode(GPIO.BOARD)            # We are using the Board pin numbering
GPIO.setup(servo_pin, GPIO.OUT)    # Declaring GPIO 17 as output pin
p = GPIO.PWM(servo_pin, 50)           # Created PWM channel at 50Hz frequency
sleep(2)
p.start(2.5)
try:
        while 1:                                     # Loop will run forever
            p.ChangeDutyCycle(2.5)   # Move servo to 0 degrees
            sleep(1)                              # Delay of 1 sec
            p.ChangeDutyCycle(0)    # turn servo off
            sleep(28800)                    # Delay of 8 hours
            p.ChangeDutyCycle(6)    # Move servo to 90 degrees
            sleep(1)                             # Delay for 1 sec
            p.ChangeDutyCycle(0)    # turns servo off
            sleep(28800)                    # delay 8 hours
            p.ChangeDutyCycle(2.5)  # Move servo to 0 degrees
            sleep(1)                             # Delay for 1 sec
            p.ChangeDutyCycle(0)    # Turns servo off
    # If Keyborad Interrupt (CTRL+C) is pressed
except KeyboardInterrupt:
        pass   # Go to next line
GPIO.cleanup()              # Make all GPIO pins LOW



























































































































































GNU nano 3.2

import RPi.GPIO as GPIO
import Adafruit_DHT #DHT22 sensor library
import I2C_LCD_driver #I2C lcd library GPIO 2 PI pin 3
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT) #Gpio23 pin 16 heating
GPIO.setup(13, GPIO.OUT) #Gpio27 pin 13 alarm
GPIO.setup(15, GPIO.OUT) #Gpio22 pin 15 LED lights

mylcd = I2C_LCD_driver.lcd()

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4 #DHT22 set to GPIO 4 Pie pin 7

set_temp = 37.8 # this is the required temperature

while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

if humidity is not None and temperature is not None:
#print("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity
mylcd.lcd_clear()
mylcd.lcd_display_string(" Incubator Status" ,1)
mylcd.lcd_display_string(" Temp={0:0.1f}*C".format(temperature, humid
mylcd.lcd_display_string(" Humidity={1:0.1f}%".format(temperature, hum


# test for low temperature
if temperature < set_temp:
GPIO.output(16,1)
GPIO.output(15,0)

# test for high temperature
if temperature > (set_temp + 1):
GPIO.output(16,0)
GPIO.output(15,1)

# test for low temperature alarm
if temperature < (set_temp - 10):
GPIO.output(13,1)

# clear low temperature alarm
if temperature >= (set_temp):
GPIO.output(13,0)

sleep(10) #this is the time between taking readings and acting on them y

else:

mylcd.lcd_clear()
mylcd.lcd_display_string(" DHT22 Failed!", 2)

sleep(5) #this is the time between a failed sensor read and trying again
























[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py






























































































































































GNU nano 3.2

import RPi.GPIO as GPIO # Importing RPi library to use the GPIO pins
from time import sleep # Importing sleep from time library to add delay
servo_pin = 17 # Initializing the GPIO 17 for servo motor
GPIO.setmode(GPIO.BCM) # We are using the BCM pin numbering
GPIO.setup(servo_pin, GPIO.OUT) # Declaring GPIO 17 as output pin
p = GPIO.PWM(servo_pin, 50) # Created PWM channel at 50Hz frequency
sleep(2)
p.start(2.5)
try:
while 1: # Loop will run forever
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay of 1 sec
p.ChangeDutyCycle(0) # turna servo off
sleep(28800) # Delay of 8 hours
p.ChangeDutyCycle(6) # Move servo to 90 degrees
sleep(1) # Dealy for 1 sec
p.ChangeDutyCycle(0) # turns servo off
sleep(28800) # delay 8 hours
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay for 1 sec
p.ChangeDutyCycle(0) # Turns servo off
# If Keyborad Interrupt (CTRL+C) is pressed
except KeyboardInterrupt:
pass # Go to next line
GPIO.cleanup() # Make all GPIO pins LOW























































[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo nano ink.py





























































































































































GNU nano 3.2

import RPi.GPIO as GPIO
import Adafruit_DHT #DHT22 sensor library
import I2C_LCD_driver #I2C lcd library GPIO 2 PI pin 3
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT) #Gpio23 pin 16 heating
GPIO.setup(13, GPIO.OUT) #Gpio27 pin 13 alarm
GPIO.setup(15, GPIO.OUT) #Gpio22 pin 15 LED lights

mylcd = I2C_LCD_driver.lcd()

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4 #DHT22 set to GPIO 4 Pie pin 7

set_temp = 37.8 # this is the required temperature

while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

if humidity is not None and temperature is not None:
#print("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity
mylcd.lcd_clear()
mylcd.lcd_display_string(" Incubator Status" ,1)
mylcd.lcd_display_string(" Temp={0:0.1f}*C".format(temperature, humid
mylcd.lcd_display_string(" Humidity={1:0.1f}%".format(temperature, hum


# test for low temperature
if temperature < set_temp:
GPIO.output(16,1)
GPIO.output(15,0)

# test for high temperature
if temperature > (set_temp + 1):
GPIO.output(16,0)
GPIO.output(15,1)

# test for low temperature alarm
if temperature < (set_temp - 10):
GPIO.output(13,1)

# clear low temperature alarm
if temperature >= (set_temp):
GPIO.output(13,0)

sleep(10) #this is the time between taking readings and acting on them y

else:

mylcd.lcd_clear()
mylcd.lcd_display_string(" DHT22 Failed!", 2)

sleep(5) #this is the time between a failed sensor read and trying again






















^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justi
^X Exit ^R Read File ^\ Replace ^U Uncut Text ^T To Sp
























[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo nano ink.py









































GNU nano 3.2

import RPi.GPIO as GPIO
import Adafruit_DHT #DHT22 sensor library
import I2C_LCD_driver #I2C lcd library GPIO 2 PI pin 3
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT) #Gpio23 pin 16 heating
GPIO.setup(13, GPIO.OUT) #Gpio27 pin 13 alarm
GPIO.setup(15, GPIO.OUT) #Gpio22 pin 15 LED lights

mylcd = I2C_LCD_driver.lcd()

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4 #DHT22 set to GPIO 4 Pie pin 7

set_temp = 37.8 # this is the required temperature

while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

GNU nano 3.2 ink.py

import RPi.GPIO as GPIO
import Adafruit_DHT #DHT22 sensor library
import I2C_LCD_driver #I2C lcd library GPIO 2 PI pin 3
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT) #Gpio23 pin 16 heating
GPIO.setup(13, GPIO.OUT) #Gpio27 pin 13 alarm
GPIO.setup(15, GPIO.OUT) #Gpio22 pin 15 LED lights

mylcd = I2C_LCD_driver.lcd()

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4 #DHT22 set to GPIO 4 Pie pin 7

set_temp = 37.8 # this is the required temperature


^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
^X Exit ^R Read File ^\ Replace ^U Uncut Text^T To Spell ^_ Go To Line


















































































[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]spberrypi:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo nano ink.py





























































































































































GNU nano 3.2 ink.py

import RPi.GPIO as GPIO
import Adafruit_DHT #DHT22 sensor library
import I2C_LCD_driver #I2C lcd library GPIO 2 PI pin 3
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT) #Gpio23 pin 16 heating
GPIO.setup(13, GPIO.OUT) #Gpio27 pin 13 alarm
GPIO.setup(15, GPIO.OUT) #Gpio22 pin 15 LED lights

mylcd = I2C_LCD_driver.lcd()

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4 #DHT22 set to GPIO 4 Pie pin 7

set_temp = 37.8 # this is the required temperature


^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos
^X Exit ^R Read File ^\ Replace ^U Uncut Text^T To Spell ^_ Go To Line


























































GNU nano 3.2 ink.py

import RPi.GPIO as GPIO
import Adafruit_DHT #DHT22 sensor library
import I2C_LCD_driver #I2C lcd library GPIO 2 PI pin 3
from time import sleep

GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)

GPIO.setup(16, GPIO.OUT) #Gpio23 pin 16 heating
GPIO.setup(13, GPIO.OUT) #Gpio27 pin 13 alarm
GPIO.setup(15, GPIO.OUT) #Gpio22 pin 15 LED lights

mylcd = I2C_LCD_driver.lcd()

DHT_SENSOR = Adafruit_DHT.DHT22
DHT_PIN = 4 #DHT22 set to GPIO 4 Pie pin 7

set_temp = 37.8 # this is the required temperature

while True:
humidity, temperature = Adafruit_DHT.read_retry(DHT_SENSOR, DHT_PIN)

if humidity is not None and temperature is not None:
#print("Temp={0:0.1f}*C Humidity={1:0.1f}%".format(temperature, humidity))
mylcd.lcd_clear()
mylcd.lcd_display_string(" Incubator Status" ,1)
mylcd.lcd_display_string(" Temp={0:0.1f}*C".format(temperature, humidity), 2)
mylcd.lcd_display_string(" Humidity={1:0.1f}%".format(temperature, humidity), 3)


# test for low temperature
if temperature < set_temp:
GPIO.output(16,1)
GPIO.output(15,0)

# test for high temperature
if temperature > (set_temp + 1):
GPIO.output(16,0)
GPIO.output(15,1)

# test for low temperature alarm
if temperature < (set_temp - 10):
GPIO.output(13,1)

# clear low temperature alarm
if temperature >= (set_temp):
GPIO.output(13,0)

sleep(10) #this is the time between taking readings and acting on them you can reduce it but not below 5 seconds

else:

mylcd.lcd_clear()
mylcd.lcd_display_string(" DHT22 Failed!", 2)

sleep(5) #this is the time between a failed sensor read and trying again
























[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sudo nano egg90.py




























































































































































GNU nano 3.2 egg90.py

import RPi.GPIO as GPIO # Importing RPi library to use the GPIO pins
from time import sleep # Importing sleep from time library to add delay in code
servo_pin = 11 # Initializing the GPIO 11 for servo motor
GPIO.setmode(GPIO.BCM) # We are using the Bord pin numbering
GPIO.setup(servo_pin, GPIO.OUT) # Declaring GPIO 17 as output pin
p = GPIO.PWM(servo_pin, 50) # Created PWM channel at 50Hz frequency
sleep(2)
p.start(2.5)
try:
while 1: # Loop will run forever
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay of 1 sec
p.ChangeDutyCycle(0) # turna servo off
sleep(28800) # Delay of 8 hours
p.ChangeDutyCycle(6) # Move servo to 90 degrees
sleep(1) # Dealy for 1 sec
p.ChangeDutyCycle(0) # turns servo off
sleep(28800) # delay 8 hours
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay for 1 sec
p.ChangeDutyCycle(0) # Turns servo off
# If Keyborad Interrupt (CTRL+C) is pressed
except KeyboardInterrupt:
pass # Go to next line
GPIO.cleanup() # Make all GPIO pins LOW























































[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
^[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_LCD_driver.py I2C_LCD_driver.pyc ink.py MagPi Music Pictures Public relay.py servo-ang.py Templates Videos
[email protected]:~ $ python servo-ang.py
Enter angle between 0 & 180: 30
Enter angle between 0 & 180: ^CGoodbye!
Traceback (most recent call last):
File "servo-ang.py", line 21, in <module>
angle = float(input('Enter angle between 0 & 180: '))
KeyboardInterrupt
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python3 egg90.py
^[email protected]:~ $ sudo nano egg90.py















































































































































GNU nano 3.2 egg90.py

import RPi.GPIO as GPIO # Importing RPi library to use the GPIO pins
from time import sleep # Importing sleep from time library to add delay in code
servo_pin = 11 # Initializing the GPIO 11 for servo motor
GPIO.setmode(GPIO.BORED) # We are using the Bored pin numbering
GPIO.setup(servo_pin, GPIO.OUT) # Declaring GPIO 17 as output pin
p = GPIO.PWM(servo_pin, 50) # Created PWM channel at 50Hz frequency
sleep(2)
p.start(2.5)
try:
while 1: # Loop will run forever
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay of 1 sec
p.ChangeDutyCycle(0) # turna servo off
sleep(28800) # Delay of 8 hours
p.ChangeDutyCycle(6) # Move servo to 90 degrees
sleep(1) # Dealy for 1 sec
p.ChangeDutyCycle(0) # turns servo off
sleep(28800) # delay 8 hours
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay for 1 sec
p.ChangeDutyCycle(0) # Turns servo off
# If Keyborad Interrupt (CTRL+C) is pressed
except KeyboardInterrupt:
pass # Go to next line
GPIO.cleanup() # Make all GPIO pins LOW























































[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
^[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_LCD_driver.py I2C_LCD_driver.pyc ink.py MagPi Music Pictures Public relay.py servo-ang.py Templates Videos
[email protected]:~ $ python servo-ang.py
Enter angle between 0 & 180: 30
Enter angle between 0 & 180: ^CGoodbye!
Traceback (most recent call last):
File "servo-ang.py", line 21, in <module>
angle = float(input('Enter angle between 0 & 180: '))
KeyboardInterrupt
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python3 egg90.py
^[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
Traceback (most recent call last):
File "egg90.py", line 4, in <module>
GPIO.setmode(GPIO.BORED) # We are using the Bored pin numbering
AttributeError: 'module' object has no attribute 'BORED'
[email protected]:~ $ sudo nano egg90.py









































































































































GNU nano 3.2 egg90.py

import RPi.GPIO as GPIO # Importing RPi library to use the GPIO pins
from time import sleep # Importing sleep from time library to add delay in code
servo_pin = 11 # Initializing the GPIO 11 for servo motor
GPIO.setmode(GPIO.BOARD) # We are using the Board pin numbering
GPIO.setup(servo_pin, GPIO.OUT) # Declaring GPIO 17 as output pin
p = GPIO.PWM(servo_pin, 50) # Created PWM channel at 50Hz frequency
sleep(2)
p.start(2.5)
try:
while 1: # Loop will run forever
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay of 1 sec
p.ChangeDutyCycle(0) # turna servo off
sleep(28800) # Delay of 8 hours
p.ChangeDutyCycle(6) # Move servo to 90 degrees
sleep(1) # Dealy for 1 sec
p.ChangeDutyCycle(0) # turns servo off
sleep(28800) # delay 8 hours
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay for 1 sec
p.ChangeDutyCycle(0) # Turns servo off
# If Keyborad Interrupt (CTRL+C) is pressed
except KeyboardInterrupt:
pass # Go to next line
GPIO.cleanup() # Make all GPIO pins LOW























































[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
^[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_LCD_driver.py I2C_LCD_driver.pyc ink.py MagPi Music Pictures Public relay.py servo-ang.py Templates Videos
[email protected]:~ $ python servo-ang.py
Enter angle between 0 & 180: 30
Enter angle between 0 & 180: ^CGoodbye!
Traceback (most recent call last):
File "servo-ang.py", line 21, in <module>
angle = float(input('Enter angle between 0 & 180: '))
KeyboardInterrupt
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python3 egg90.py
^[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
Traceback (most recent call last):
File "egg90.py", line 4, in <module>
GPIO.setmode(GPIO.BORED) # We are using the Bored pin numbering
AttributeError: 'module' object has no attribute 'BORED'
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python servo-ang.py
Enter angle between 0 & 180: 25
Enter angle between 0 & 180: 50
Enter angle between 0 & 180: ^CGoodbye!
Traceback (most recent call last):
File "servo-ang.py", line 21, in <module>
angle = float(input('Enter angle between 0 & 180: '))
KeyboardInterrupt
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python egg90.py
^[[A^[email protected]:~ $ sudo nano egg90.py





























































































































GNU nano 3.2 egg90.py

import RPi.GPIO as GPIO # Importing RPi library to use the GPIO pins
from time import sleep # Importing sleep from time library to add delay in code
servo_pin = 11 # Initializing the GPIO 11 for servo motor
GPIO.setmode(GPIO.BOARD) # We are using the Board pin numbering
GPIO.setup(servo_pin, GPIO.OUT) # Declaring GPIO 17 as output pin
p = GPIO.PWM(servo_pin, 50) # Created PWM channel at 50Hz frequency
sleep(2)
p.start(2.5)
try:
while 1: # Loop will run forever
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay of 1 sec
p.ChangeDutyCycle(0) # turna servo off
sleep(28800) # Delay of 8 hours
p.ChangeDutyCycle(6) # Move servo to 90 degrees
sleep(1) # Dealy for 1 sec
p.ChangeDutyCycle(0) # turns servo off
sleep(28800) # delay 8 hours
p.ChangeDutyCycle(2.5) # Move servo to 0 degrees
sleep(1) # Delay for 1 sec
p.ChangeDutyCycle(0) # Turns servo off
# If Keyborad Interrupt (CTRL+C) is pressed
except KeyboardInterrupt:
pass # Go to next line
GPIO.cleanup() # Make all GPIO pins LOW























































[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
^[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_LCD_driver.py I2C_LCD_driver.pyc ink.py MagPi Music Pictures Public relay.py servo-ang.py Templates Videos
[email protected]:~ $ python servo-ang.py
Enter angle between 0 & 180: 30
Enter angle between 0 & 180: ^CGoodbye!
Traceback (most recent call last):
File "servo-ang.py", line 21, in <module>
angle = float(input('Enter angle between 0 & 180: '))
KeyboardInterrupt
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python3 egg90.py
^[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
Traceback (most recent call last):
File "egg90.py", line 4, in <module>
GPIO.setmode(GPIO.BORED) # We are using the Bored pin numbering
AttributeError: 'module' object has no attribute 'BORED'
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python servo-ang.py
Enter angle between 0 & 180: 25
Enter angle between 0 & 180: 50
Enter angle between 0 & 180: ^CGoodbye!
Traceback (most recent call last):
File "servo-ang.py", line 21, in <module>
angle = float(input('Enter angle between 0 & 180: '))
KeyboardInterrupt
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python egg90.py
^[[A^[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo crontab -e




























































































































GNU nano 3.2 /tmp/crontab.xv78Dk/crontab Modified

# Edit this file to introduce tasks to be run by cron.
#
# Each task to run has to be defined through a single line
# indicating with different fields when the task will be run
# and what command to run for the task
#
# To define the time you can provide concrete values for
# minute (m), hour (h), day of month (dom), month (mon),
# and day of week (dow) or use '*' in these fields (for 'any').
#
# Notice that tasks will be started based on the cron's system
# daemon's notion of time and timezones.
#
# Output of the crontab jobs (including errors) is sent through
# email to the user the crontab file belongs to (unless redirected).
#
# For example, you can run a backup of all your user accounts
# at 5 a.m every week with:
# 0 5 * * 1 tar -zcf /var/backups/home.tgz /home/
#
# For more information see the manual pages of crontab(5) and cron(8)
#
# m h dom mon dow command

@reboot python /home/pi/humidity.py
@reboot python /home/pi/humidity.py
@reboot python /home/pi/[email protected] python /home/pi/egg90.py





















































[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_L
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sedo nano egg90.py
-bash: sedo: command not found
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo nano ink.py
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
^[email protected]:~ $ ls
Adafruit_Python_DHT Desktop Documents Downloads egg90.py humidity.py I2C_LCD_driver.py I2C_LCD_driver.pyc ink.py MagPi Music Pictures Public relay.py servo-ang.py Templates Videos
[email protected]:~ $ python servo-ang.py
Enter angle between 0 & 180: 30
Enter angle between 0 & 180: ^CGoodbye!
Traceback (most recent call last):
File "servo-ang.py", line 21, in <module>
angle = float(input('Enter angle between 0 & 180: '))
KeyboardInterrupt
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python3 egg90.py
^[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
Traceback (most recent call last):
File "egg90.py", line 4, in <module>
GPIO.setmode(GPIO.BORED) # We are using the Bored pin numbering
AttributeError: 'module' object has no attribute 'BORED'
[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python servo-ang.py
Enter angle between 0 & 180: 25
Enter angle between 0 & 180: 50
Enter angle between 0 & 180: ^CGoodbye!
Traceback (most recent call last):
File "servo-ang.py", line 21, in <module>
angle = float(input('Enter angle between 0 & 180: '))
KeyboardInterrupt
[email protected]:~ $ python egg90.py
^[email protected]:~ $ python egg90.py
^[[A^[email protected]:~ $ sudo nano egg90.py
[email protected]:~ $ sudo crontab -e
No modification made
[email protected]:~ $ sudo nano egg90.py



























































[ Read 25 lines (Converted from DOS format) ]
^G Get Help ^O Write Out ^W Where Is ^K Cut Text ^J Justify ^C Cur Pos M-U Undo M-A Mark Text M-] To Bracket M-Q Previous ^B Back ^? Prev Word ^A Home
^X Exit ^R Read File ^\ Replace ^U Uncut Text ^T To Spell ^_ Go To Line M-E Redo M-6 Copy Text ^Q Where Was M-W Next ^F Forward ^? Next Word ^E End

y
this works the way it is but i don't know if its the best.
Thank you very very much!

pcmanbob
Posts: 7966
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: DHT22 to close a relay if the temperature drops below a set value

Fri Feb 14, 2020 9:20 am

Please edit your last post .

I don't mind the questions but please,

Put the code in code tags and please remove the lines of white space , its making your post many pages long and un-readable.


Add [code] at the top

code goes here

and [/code] at the bottom.
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

Return to “Automation, sensing and robotics”