adil.aziz3198
Posts: 1
Joined: Sat Apr 09, 2016 8:28 pm

Python temperature and relay code

Sat Apr 09, 2016 9:02 pm

Hi im new to the whole raspberry pi / python coding thing so forgive me if i get anything wrong

I'm trying a little home incubator project using a dht22 temperature sensor as an input to control a 4 channel 5v relay board

I know how to code for the relay board and temperature sensor separately but i want to use them together so i can turn the relay on/off when the temperature reaches a certain threshold.

For the temperature sensor i installed pigpio and used a tutorial to make a simple code that prints the temp/humidity every few second. for the relay board i made a simple on/off code with RPi.GPIO

I tried (and failed) to combine the two codes to control my relay with the temp sensor but it didn't work
I've attached images of the codes and was wondering if anyone with more experience could help me fix it?
Attachments
12970343_908679732574920_1438532136_o.jpg
temp sensor code
12970343_908679732574920_1438532136_o.jpg (61.05 KiB) Viewed 3033 times
12980835_908677489241811_302763244_o.jpg
my attempt at combining the two codes
12980835_908677489241811_302763244_o.jpg (53.65 KiB) Viewed 3033 times

hippy
Posts: 7731
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: Python temperature and relay code

Sun Apr 10, 2016 4:04 pm

It is much better to post the actual code as text rather than an image. What you need is something like the following, pseudo code rather than complete python -

Code: Select all

while True:
  read the temperature
  if temperature > whatever:
    turn on
  else:
    turn off

Davies
Posts: 150
Joined: Sat Apr 04, 2015 4:24 pm

Re: Python temperature and relay code

Mon Apr 11, 2016 1:16 am

You've probably worked this one out already, but i happen to have a DHT22 and Raspberry at the side of me that ive been using for humidity only readings,.. so thought id have a play and put something together for the temps. most of this is copy/paste from previous projects and im self taught from reading forums so theres probably much better code available.. columns, rows, screen size is off.
this code does work, at the moment changes a tkinter label from "off" red to "on" green when temp is equal or greater than designated setting.

Code: Select all

import Tkinter as tk
from Tkinter import *
import threading
import Adafruit_DHT
import RPi.GPIO as GPIO
import time

GPIO.setmode(GPIO.BCM)

sensor = Adafruit_DHT.DHT22
pin = 17

root = tk.Tk()
root.geometry("1200x720")
canvas = Canvas(width=1280, height=720, bg='black')
canvas.grid(rowspan=5, columnspan=8, sticky='W,E,N,S')

i_fan_slow = 18


# Input Fan Slow Temp
def is_minus_temp():
    global i_fan_slow
    if i_s_m["text"] == "-":
        i_fan_slow -= 1
        ifan_s.config(text=i_fan_slow)
        root.update_idletasks()


def is_plus_temp():
    global i_fan_slow
    if i_s_p["text"] == "+":
        i_fan_slow += 1
        ifan_s.config(text=i_fan_slow)
        root.update_idletasks()


# Maths
def maths():
    while 1:
        humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
        temp = ("%02d" % temperature)
        if temperature is not None:
            c_temp.config(text=temp)
        if temperature >= i_fan_slow:
            gpio.config(text="On", bg='green')
            root.update_idletasks()
        if temperature < i_fan_slow:
            gpio.config(text="Off", bg='red')
            root.update_idletasks()
        time.sleep(1)


i_s_m = tk.Button(root, text="-", width=1, font=('aharoni', 30, 'bold'), command=is_minus_temp, bg='black', fg='white')
i_s_p = tk.Button(root, text="+", width=1, font=('aharoni', 30, 'bold'), command=is_plus_temp, bg='black', fg='white')
ifan_s = Label(root, text=i_fan_slow, width=4, font=('aharoni', 32, 'bold'), bg='black', fg='white')
ifan_s.grid(padx=5, pady=10, row=2, column=1, sticky='N')
i_s_m.grid(row=2, column=1, padx=15, pady=2, sticky="N,W")
i_s_p.grid(row=2, column=1, padx=15, pady=2, sticky="N,E")

c_temp = Label(root, text=" ", width=4, font=('aharoni', 32, 'bold'), bg='black', fg='white')
c_temp.grid(padx=5, pady=10, row=2, column=3, sticky='N')

gpio = Label(root, text="Off", width=4, font=('aharoni', 32, 'bold'), bg='red', fg='white')
gpio.grid(padx=5, pady=10, row=3, column=1, sticky='N')


t11 = threading.Thread(target=maths)
t11.daemon = True
t11.start()


root.mainloop()

Return to “Python”