Programer / EE / Help - Raspberry Pi

So I am good at scripting for the web. PHP SQL HTML JS etc… That is the extent of my programming abilities at this point and it has suited me until today.

If you have see this thread (http://www.nyspeed.com/showthread.php?283442-5-years-and-2-houses-later-BEER) You will know that I am building a cooler. I was pretty gung ho on buying a coolbot and just letting it control the temperature. Then I changed my mind. I decided that I want a PI to monitor the temperature and control the AC unit. I am already employing one to control the beer list on a monitor so I figured as a start I want it to also display temperature of cooler. In the future I want to be able to hook up a relay board and control asco valves on the kegs based on a keyed input (kid proof the bar!)

So enough of where I was and where I am going. I am stuck on what I am thinking is Python. I need to use the GPIO on the PI to read two temperature sensors and if the values fall within set parameters (I would ideally like to tweak those parameters with a web portal) then it needs to fire a relay output that heats up a small resistor so the ac will trigger on. Simply I am reinventing the coolbot so that I can have more functionality. Someone made a clone using an arduino controller and it is cool but lacks the flexibility that I am looking for down the road. http://people.umass.edu/~dac/projects/ColdSnap/ColdSnap.html

What I know:
How to setup the PI
How to program the web interface
How to solder all connections
What exactly this needs to do.

What I don’t know:
How to programmatically interact the sensors and relays
How to interact the web interface with the above.

This needs to at minimum:
Constantly monitor two temperatures (air and AC Fins) (could poll every 10-20 seconds)
Determine if the temperature falls between points set via Web Interface
Trigger a relay that applies voltage to a resistor.
Log both temperature readings into a SQL table or text file or something that I can use PHP to make visible on the web server

Is anyone interested in helping out on this? I can pay, provide mass quantities of beer, rewire your house etc… I just need to get this working sooner than later.

Don’t reinvent the wheel and use the PHP library to interact with the GPIO pins and even control the script from the web interface by setting up Apache or NGINX if that is what you are familar with. Python sucks. There is also a GPIO nodejs library.

Run your script in a cron and poll the voltage in the sensor depending on what ones you have which translates to to the reading. If you know JS then you can load a Node and then write values to Redis or Mongo and display using your own web app. Bootstrap is a simple responsive framework and will fit any screen and then just piple in the graphs with http://justgage.com or D3.js

Could do it pretty quick and can probably give you the code to run on your Pi if you have the sensors you used and want to read from.

If you want to get really easy, then you should just get a ESP8266. Wifi enabled and connects right to the cloud for sensor data.

https://www.arduino.cc/

Arduino stuff is what most people use for I/O stuff vs the Rasp Pi

You should be able to combine results from these two threads and a few lines of additional code to do what you need:

https://www.raspberrypi.org/forums/viewtopic.php?f=37&t=43625

Most people prefer GPIO on Pi since it runs Debain than the Arduino language and studio since its just loops.

I do have a a few of these which are cheaper and more simple.

http://digistump.com/products/1

Nothing beats toe ESP8266 tho. I bought like 10 of these and they are awesome.

So here is my first run at this code. Again I DO NOT KNOW PYTHON. This is literally the first time I have played here so most of this is derived from other sources and glued together to achieve my desired results.

Feedback?

#!/usr/bin/python

import sys

import Adafruit_DHT
import RPi.GPIO as GPIO
import time

Identify which pin controls the relay

RelayPin = 17

Temperature set. Run cooler above SetTemp F, Shut down under Set Temp F

SetTemp = 38

Basic PWM Settings

SleepTimeL = 3
SleepTimeH = 6

Open Air Sensor Settings

sensor = Adafruit_DHT.AM2302
pin = 4

def GPIOsetup():
GPIO.setmode(GPIO.BCM)
GPIO.setup(RelayPin, GPIO.OUT)
GPIO.setwarnings(False)

def GetTemperature():
humidity, temperature = Adafruit_DHT.read_retry(sensor, pin)
temperature = temperature * 9/5.0 + 32
return(temperature)

def HeaterOn():
GPIO.output(RelayPin, 0)
print “Heater on”
return()

def HeaterOff():
GPIO.output(RelayPin, 1)
print “Heater off”
return()

def RunCooler():
CoolerTemp = float(GetTemperature())
if CoolerTemp>SetTemp:
while CoolerTemp > SetTemp:
print “Triggering AC Heater”
HeaterOn()
time.sleep(SleepTimeL);
HeaterOff()
time.sleep(SleepTimeH);
else:
fanOFF()
print “Temperature is cold enough. no Action”

return()

def main():
GPIOsetup()
RunCooler()
GPIO.cleanup()

try:
main()
finally:
print (“Finish”)
#GPIO .cleanup()

      • Updated - - -

Other notes on the Code above:
This is currently only reading the air temp. I have a second sensor for AC fin temp.

The code kicks a relay on and off on a delay so that it can maintain a temperature under 150*. I don’t want the relay to get too hot on the AC sensor.

Currently it is just reading a temp and acting. The final revision will read a set point from an SQL table and record it’s actions to the same table. This will allow me to have a log and temps/run status on a screen outside the unit.

Is my best option to set this up as a cron and to run every minute? If i do that and after minute one the first iteration of this is still accessing the relay and cooling how do I keep iteration two from acting??

You could create a lockfile to prevent multiple processes from stepping on each other. Might sound complicated, but it’s not. Let’s assume you’re going to use: /var/run/tpgsr as the lockfile. At the start of the process check for the existence of the file and exit if it exists. If it doesn’t, create it and proceed. Then delete the lockfile before exiting.

That ought to make it reasonably safe to run it from cron, but as a UNIX nerd I think I still wouldn’t use cron for this.

I’d do somethinkg like this instead (bourne shell style; feel free to translate to python):
while [ 1 = 1 ];
do
main()
sleep 60
done

No lock file needed this way.

Did you look at BrewPi?

I looked at brewpi. It does not do exactly what I needed this to do, and on top of that this will be expanding to control other elements of the system. Brewpi is good for fermentation and modifying an existing fridge, I built the fridge.

So I get what you are saying at the infinite loop and I guess that I just need to force the script to run at boot so that in the event of power loss or something it will still be acvtive. I was just suggested cron from more than one source and I figured, yeah sure that will work.

A while loop in python about be fine

init.d scripts for service startup also

      • Updated - - -

http://www.tutorialspoint.com/python/python_while_loop.htm

You definitely can write your own init script, and that makes it easy to do whichever of stop/start/restart/reload/whatever that you care to write code to handle, but then there’s the matter of how you keep it running.

Easiest way I can think of is just do a respawn entry in /etc/inittab and combine it with the infinite loop approach above. The Difference Between Linux and GNU/Linux

If you want to get more involved, daemontools is a great way to handle services; it works well and even comes with built-in log rotation. http://cr.yp.to/daemontools.html

Monit and god are also viable alternatives. Probably other options out there as well.

So I wired this thing in and fired it up tonight. Works well. Needs a lot of tweaking and a bunch more programming but in manual mode it is working just fine.

Ignore the wire management.
http://www.nyspeed.com/attachment.php?attachmentid=36108&d=1454113690

This is the manual code executing and looping when i first fired it up.
http://www.nyspeed.com/attachment.php?attachmentid=36107&d=1454113609