Pi Program

The reason that I chose the Rasberry Pi for this project was mainly because of its capability and low cost. As I mentioned in the Components page, the CanaKit is $55 and the Pi 3 B+ has builtin WiFi. I wanted a way to port the display or output of the Smart Stove program remotely to my laptop and cell phone.

Raspian operating window using VNC Viewer

The first thing that you'll need to do with the Pi is attach a monitor (or TV) and keyboard and install the operating system software. The Raspberry Pi is based on open source software with tons of support. I followed a combination of this site and installed NOOBS and continued the rest of the install to go 'headless' with this site: http://www.circuitbasics.com/raspberry-pi-basics-setup-without-monitor-keyboard-headless-mode/

Here is another useful website: https://projects.raspberrypi.org/en/projects/raspberry-pi-setting-up

What you will end up with is an interface so that you can write the Smart Stove program.

Here is the entire Smart_Stove.py program written in Python ...
_______________________________________________________________________
import time
import datetime
import pigpio
import RPi.GPIO as GPIO
import csv

# initial start - output to open damper - pin 12 for 2 seconds
GPIO.setmode(GPIO.BCM) # setup gpio pin numbering scheme as the gpio pin number (BCM)
GPIO.setwarnings(False) #ignor warning message
GPIO.setup(23, GPIO.OUT) # fan relay switch
GPIO.setup(12, GPIO.OUT) # setup gpio output pin 12 - damper open
GPIO.setup(16, GPIO.OUT) # setup gpio output pin 16 - damper close
GPIO.output(12,GPIO.HIGH) # initial start - output to open damper - pin 12 for 2 seconds
time.sleep(2)
GPIO.output(12,GPIO.LOW)
GPIO.output(16,GPIO.LOW)
GPIO.output(23,GPIO.LOW)
# damper is now fully open; pin 12 reverses actuator; pin 16 drives actuator forward
condition=1 #set initial damper condition to 1 - fully open
fan_switch=0 # let program know that fan switch is initially set to off
pi = pigpio.pi()
if not pi.connected:
    print("Raspberry Pi not connected")
    exit(0)

sensor = pi.spi_open(0,1000000,0)   # CE0, 1Mbps, main SPI

saveout_dir = r"/home/pi/Documents/Stove/"
saveout_filename = "temperature_data.csv"

with open(saveout_dir + saveout_filename, 'w') as file:
    writer = csv.writer(file)
 
    while True:
        try:
            c, d = pi.spi_read(sensor, 2)
            if (c == 2):
                word = (d[0]<<8) | d[1]
                if (word & 0x8006) == 0: # Bits 15, 2, and 1 should be zero.
                    t = (word >> 3)/4.0
                    temp_c = "{:.2f}".format(t) # formats to 2 decimal string
                    temp_f = float(temp_c) * 9/5 + 32.0   # convert to degrees fahrenheit
                    current_time = datetime.datetime.now().time().strftime('%H:%M:%S')
                    print(current_time,',',temp_f,'F')
                    writer.writerow([current_time,temp_f])

                    if temp_f>=80 and fan_switch==0:
                        GPIO.output(23,GPIO.HIGH)
                        fan_switch=1
                        print('fan switched on')
                        time.sleep(5) #allow fan relay to set and fan to spool up
                    elif temp_f<68 and fan_switch==1:
                        GPIO.output(23,GPIO.LOW)
                        fan_switch=0
                        print('fan switch off')

                    if temp_f<85 and condition !=1:
                        if condition == 2:
                            GPIO.output(12,GPIO.HIGH)
                            time.sleep(1)
                            GPIO.output(12,GPIO.LOW)
                            print('damper fully open')
                        elif condition == 3:
                            GPIO.output(12,GPIO.HIGH)
                            time.sleep(2)
                            GPIO.output(12,GPIO.LOW)
                            print('damper fully open')
                        condition = 1
                    elif 85<=temp_f<95 and condition !=2:
                        if condition == 1:
                            GPIO.output(16,GPIO.HIGH)
                            time.sleep(.85) # close damper half way
                            GPIO.output(16,GPIO.LOW)
                            print('damper half close')
                        elif condition == 3:
                            GPIO.output(12,GPIO.HIGH)
                            time.sleep(.85) # close damper half way
                            GPIO.output(12,GPIO.LOW)
                            print('damper half open')
                        condition = 2                   
                    elif temp_f>=95 and condition !=3:
                        if condition == 1:
                            GPIO.output(16,GPIO.HIGH)
                            time.sleep(2) # fully close damper
                            GPIO.output(16,GPIO.LOW)
                            print('damper fully closed')
                        elif condition == 2:
                            GPIO.output(16,GPIO.HIGH)
                            time.sleep(1)# fully close damper
                            GPIO.output(16,GPIO.LOW)
                            print('damper fully closed')
                        condition = 3             
                else:
                    print("bad reading {:b}".format(word))       
 
                time.sleep(5*60) # Sleep for 5 minutes.

        except (KeyboardInterrupt, SystemExit): # press ctrl-c to exit program
            break
   
GPIO.cleanup()
pi.spi_close(sensor)
print("Program End")
file.close()
pi.stop()
_______________________________________________________________________

I give a great deal of credit to my son Jackson who got me off to a quick start with Python, troubleshooting the code to make the thermocouple read and some basic file write functioning. This is a very bare bones program. You really don't need or may not want the file write function. Having said this, the world is your oyster and you can craft a much better user interface. My next step is to script an animated graphical output with a dashboard indicator for damper position and fan status.

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Sir first upon thanks for valuable Knowledge sharing and your efforts. Sir What we mention in condition=1, condition=2 and condition=3 ???

    ReplyDelete