Page 3 of 3

Re: Web enabled thermostat project

Posted: Sun Nov 17, 2013 3:36 pm
by ruud909
Hello onepoint21,

Thanks a lot for your code ;-)
I have copied your codes to a php file (see below), but when i want's to set the "set temperature"
with the pulldown menu nothing writes to the thermostat file :(
It only shows the current "set temperature" i wrote manualy to the file and the current temperature
The only thing that's not working is the set function.

I'm a newbie in programming and this is the last problem i'm strugling with.
Can you help me out on this??

Many thanks

Ruud


<?php echo exec('python /var/bin/gettemp.py 2>&1'); ?>
<?php

if(isset($_POST["settemp"])) {

$settemp = $_POST["settemp"];
$fp = fopen("/var/bin/thermostat", "w+");
$savestring = $settemp;
fwrite($fp, $savestring);
fclose($fp);
}
?>

<form name="termostat" method="post" class="label-top" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<div>
<label for="settemp" class="inline">Temperature set at</label>
<select name="settemp" id="settemp" onChange="this.form.submit()">
<option value=null SELECTED><?php $curSet = file_get_contents('/var/bin/thermostat'); echo $curSet; ?> graden </option>
<option value="21.5">High(21.5)</option>
<option value="20.0">20</option>
<option value="19.5">Home(19.5)</option>
<option value="19.0">19</option>
<option value="18.5">18.5</option>
<option value="15.5">15.5</option>
<option value="15.0">Away(15)</option>
<option value="14.5">14.5</option>
<option value="10.0">Low(10)</option>
</select>
</div>
</form>

Re: Web enabled thermostat project

Posted: Sun Nov 17, 2013 11:33 pm
by onepoint21
The only thing I can think of off hand is permissions. You need to ensure that the user (mine is www-data) has read and write access to the thermostat file. I had some issue with this when I was first setting it up. Have a peek and let me know what the thermostat file permissions are set to.

Re: Web enabled thermostat project

Posted: Wed Nov 20, 2013 7:32 pm
by raphex
Thanks .. it work ... just seeting www-data the write access ...
TY

Re: Web enabled thermostat project

Posted: Wed Nov 20, 2013 9:03 pm
by onepoint21
:D no problem!

Re: Web enabled thermostat project

Posted: Fri Nov 22, 2013 11:11 pm
by thienson30
I have Android App for Thermostat to control HVAC. Checkout my video
https://www.youtube.com/watch?v=KZ59sDgKqXE

Re: Web enabled thermostat project

Posted: Mon Dec 16, 2013 1:08 pm
by onepoint21
raphex wrote:Bonsoir,

I have adapted your code for my regulation (relay is replaced by a servo motor that drive the air trap of my DDG heater)
one problem is remaining, the temp setup performed by the web interface doesbn't work ... i have'nt modifiied your code except the path ....

j'ai adapté vos source pour ma regulation (à la place du relay j'ai un servo moteur qui ouvre et ferme la trappe à air de mon insert chauffage central DDG)
il me reste un soucis, le parametrage de la temperature de consigne via l'inteface web ne fonctionne pas ...
j'ai rien modifié par rapport à votre code pour ecrire dans le fichier si ce n'est que le chemin ...

Merci
I so sorry I didn't see your question until now. Probably the issue is the serial number for the DS18B20 sensor. This tutorial should help you find out the code for your sensor.

http://learn.adafruit.com/downloads/pdf ... ensing.pdf

Re: Web enabled thermostat project

Posted: Fri Mar 14, 2014 9:39 pm
by dm3281
onepoint21 wrote:Another update. I've added a sump pump monitor to my Pi. Using a magnetic float switch and combined with tropo.com. I'm now able to get SMS and phone calls when my sump pump fails. It's a wicked combination.

Image

Image

I apologize for the video quality, however for this demo I need my phone.
http://youtu.be/7u0ac1GEGXY
Hi, David.

I wanted to ask what kind of sump pump float switch are you using and how do you have it hooked up to the Pi?

Can you provide any sample code.

I have a sewage pump that I want to monitor how many times it kicks in per day... and thought this would be an excellent project.

Any help would be appreciated.

Re: Web enabled thermostat project

Posted: Mon Mar 17, 2014 2:08 pm
by racso
Hi David,

This project has been so much help. Thanks!

I've used your project and integrated a bit's here and there.
Because a outdoor thermometer is difficult for me to mount I used a Weather API to tell me the outside temperature.

Ajax based web thermostat.
Untitled.png
Mobile thermostat
Untitled.png (25.43 KiB) Viewed 4783 times


I've expanded the php script with a new php page to set the temperature without a form.
Now crontab can set my temperature using WGET.

When the program mode is set crontab can change the temperature. When the program is off the php script ignores the command.
To minimize load of my raspberry I used a config file in stead of a database.
http://www.phpclasses.org/package/1602- ... files.html

Just to be sure I password protected my page.

When using a DS18B20 I noticed that when placed in a Universal thermostat mounting the temperature readings are more stable.
http://www.smartronix.nl/Webwinkel-Prod ... ensor.html (Dutch)

I used a universal box to mount next to my furnace and used UTP cable with RJ45 connectors to connect.
The switch is to overrule the system if something goes wrong. The normal thermostat will take over.
foto.JPG
foto.JPG (42.11 KiB) Viewed 4783 times
I've also used a project for temperature monitoring.
Untitled3.png
Untitled3.png (59.88 KiB) Viewed 4783 times
http://iada.nl/en/blog/article/temperat ... spberry-pi

Re: Web enabled thermostat project

Posted: Mon Apr 21, 2014 12:21 am
by glenos
Hi,
I have been woring on building a fermentation chamber (fridge) to ferment home brew beer in and have based my set-up on David's code and php, but I have got stuck.

I modified the HotPi set-up so that it checks the temperature and either;
  • does nothing if temp is in range,
    switches on the cooling if too hot or
    switches on heating if too cold.
The problem I have is David's code creates an infinite loop which I don't need for my application, a check on the temperature every 5 minutes and then switching on wither the heating or cooling for 30 seconds is all I need.

What do I need to do to make the python run only once when called? I can set up the cron job with no worries, but I cannot get the code to run just once, it either inifite loops or doesn't run..

My code for infinite loop is below. This version is switching on for 30s and off for 30s before the next loop.

Code: Select all

#!/usr/bin/env python

import os
import re
import RPi.GPIO as GPIO
import time
import math

def main():

    GPIO.setwarnings(False)
    GPIO.cleanup()
    GPIO.setmode(GPIO.BCM)
# set BCM pin number for the furnace.
    COOLING = 17
    HEATING = 18
    GPIO.setup(COOLING, GPIO.OUT)
    GPIO.output(COOLING, GPIO.HIGH)
    GPIO.setup(HEATING, GPIO.OUT)
    GPIO.output(HEATING, GPIO.HIGH)

# see if the one-wire "w1" interfaces are loaded
    modfile = open("/proc/modules")
    moduletext = modfile.read()
    modfile.close()
    if not (re.search("w1_gpio", moduletext) and re.search("w1_therm", moduletext)):
# if modules not found, install them
        os.system('sudo modprobe w1-gpio')
        os.system('sudo modprobe w1-therm')
# define serial number for the DS18B20 temperature sensor
    sensmaintemp = "/sys/bus/w1/devices/28-000005505397/w1_slave" #inside sensor
    sensoutsidetemp = "/sys/bus/w1/devices/28-000005502ec7/w1_slave"

# this reads the temperature and rounds the value to the nearest decimal.
    def currtemp():
        while(1):
            tfile = open(sensmaintemp)
            text = tfile.read()
            tfile.close()
            firstline  = text.split("\n")[0]
            crc_check = text.split("crc=")[1]
            crc_check = crc_check.split(" ")[1]
            if crc_check.find("YES")>=0:
                break
        secondline = text.split("\n")[1]
        temperaturedata = secondline.split(" ")[9]
        temperature = float(temperaturedata[2:]) 
        temperature = temperature / 1000.0
        temperature = round(temperature, 1)
        return temperature

# set desired temperature using thermostat file from web server                
    def settemp():
        readtemp = open("/var/bin/thermostat", "r")
        settemp = readtemp.readline(4)
        readtemp.close()
        return float(settemp)
        
# hold the temperature at the settemp
    def holdtemp():
        if currtemp() < 0 or currtemp() > 50:
            GPIO.output(HEATING, GPIO.HIGH)
            GPIO.output(COOLING, GPIO.HIGH)
            status = open("/var/bin/status", "w+")
            status.write("Temperature Error")
            status.close()
            print "Temp Error: ", currtemp()
        else:
	    if currtemp() > settemp() - 0.5 and currtemp() < settemp() + 0.5:
	        GPIO.output(COOLING, GPIO.HIGH)
                GPIO.output(HEATING, GPIO.HIGH)
                status = open("/var/bin/status", "w+")
                status.write("Temperate is in control")
                status.close()
                print "Temperature is: ", currtemp()
                time.sleep(30)
            else:
                while currtemp() >= settemp() + 0.5:
                    GPIO.output(COOLING, GPIO.LOW)
                    GPIO.output(HEATING, GPIO.HIGH)
                    status = open("/var/bin/status", "w+")
                    status.write("Fridge is on.")
                    status.close()
                    print "Fridge is on: ", currtemp()
                    time.sleep(30)
                    GPIO.output(COOLING, GPIO.HIGH)
                    GPIO.output(HEATING, GPIO.HIGH)
                    print "Fridge is off"
                    status = open("/var/bin/status", "w+")
                    status.write("Waiting ...")
                    status.close()
                    time.sleep(30)
                else:
                    while currtemp() <= settemp() - 0.5:
                        GPIO.output(HEATING, GPIO.LOW)
	                GPIO.output(COOLING, GPIO.HIGH)
                        status = open("/var/bin/status", "w+")
                        status.write("Heating is on.")
                        status.close()
                        print "Heating is on: ", currtemp()
                        time.sleep(30)
                        GPIO.output(COOLING, GPIO.HIGH)
                        GPIO.output(HEATING, GPIO.HIGH)
                        print "Heater is off"
                        status = open("/var/bin/status", "w+")
                        status.write("Waiting ...")
                        status.close()
                        time.sleep(30)

# this constructs an infinite loop
    infloop = 1
    while infloop == 1 :
        holdtemp()

if __name__ == '__main__':
	main()
I also have a cron job running which is logging the temperature every 5 minutes to an RRDTool database. If anyone could tell me how to get the last or average of the last 6 readings out of it to use as Currtemp I would be very grateful.

Cheers
Glenos

Re: Web enabled thermostat project

Posted: Tue Aug 26, 2014 4:36 pm
by BrewingTim
Hi All,

first post here, so be gentle please!

Thanks to David's excellent posts, I've been able to use my Pi to control a water boiler to control the temperature of the mashing phase of my brewing process using the following program:

http://pastebin.com/q6LfbAAU

the problem is that i would like the status to be appended to the existing data in the temperaturedata.log file to get some idea of the time that the boiler is on; at the moment, the temperaturedata.log tells me the time of the measurement, the temperature and the boiler status, but as soon as the next measurement is taken, it overwrites the existing file....

any ideas welcomed! :?:

Re: Web enabled thermostat project

Posted: Thu Sep 11, 2014 9:22 pm
by amorphic
If you're interested in using your Pi to manage temperature you might want to check out my newly-released project Braubuddy (http://braubuddy.org).

Image

Braubuddy is a temperature management framework written in Python. It provides a simple web interface and API as well as extensible outputs for publishing metrics. The thermostat is also extensible so it's fairly easy to create a thermostat algorithm for your specific requirements.

If you want to use Braubuddy to control temperature, (e.g. for home brewing) you can pair it with a device like my USB powerboard to control heating and cooling:

http://jimter.net/how-to-build-a-usb-po ... ith-python

Re: Web enabled thermostat project

Posted: Fri Sep 12, 2014 12:08 am
by DougieLawson
Take a look at the Mashberry Pi article in Issue #26 of the MagPi magazine
http://www.themagpi.com/issue/issue-26/

Re: Web enabled thermostat project

Posted: Sat Nov 08, 2014 10:46 pm
by onepoint21
Hi everyone! I do apologize for not being more active on this project. Between work and family there is little extra time. I have finally invested in a 3D printer and am awaiting delivery. So in the meantime I've been working on a wall mount. It's available on Thingiverse but us as of yet untested. Basically to use this print you would drill a 30mm hole in the wall where your temperature sensor will be placed and this housing is inserted into the wall to make it nice and clean. The thermostat is into its 3rd winter now and works very well!

http://www.thingiverse.com/thing:534846

Hope all is well.

And yes my sensor has been hanging off the wall like this for ages!
Image

Re: Web enabled thermostat project

Posted: Mon Oct 19, 2015 7:59 pm
by Canedje
Inspired by this project I did do my own web enabled thermostat project.

After some months playing around with two raspberry pi2 and a lot of software I do have the basic system running very wel.
At the background several crontab jobs are running Python programms reading the temperature and running the heating system.
My used method is a simpel on/off system.
Last week I tried a PID controlled system. This was not working for me.

My problem is that the heating is sweeping over the setpoint.
I like to write code in Python to reduce the sweep by learning from the sweep in the past and stop heating based on this sweep above the setpoint.

I only don't know how to start this code.
Is somebody able to help me to start with some code?

Re: Web enabled thermostat project

Posted: Sun Mar 06, 2016 12:57 am
by deac99
onepoint21 wrote:
Paul Versteeg wrote:
onepoint21 wrote:These are the temperature sensors that I will be mounting (in some fashion) to the wall. They cost about $3 per board. Very small and easy to hide.
Image
David, this is an interesting board for a great price. Where did you find this?
This board also has some more components beside the DS and the resistor. I'd like to know what they are.
Hi Paul, This board was purchased on ebay. (http://www.ebay.ca/itm/DS18B20-temperat ... 1247wt_952)

Image

There are 2 resistors and one LED(d1) soldered to the board. Resistor r1 is 10K ohm and r2 is 1K ohm. Looks like r1 is between pin 2/3 and r2 is for the LED. Although I haven't officially traced it out yet.

I plugged it in and set the new bus code yesterday and it worked perfectly.

Glad to hear you got the code working. Permissions are a pain! More often than not they are the culprit for things not executing.

Hi - I'm new to all this as well. I've got my pi set up, running a web server and just got my first temp to show up (not controlling anything yet). I like the little sensor module that you found! I wanted to share these as well for anyone who wants a sensor outside or in wet conditions. The only disadvantage to these is that you will need to install a pullup resister with each sensor. Got them from Amazon but came from China as well. http://www.amazon.com/gp/product/B00QGN ... ge_o06_s00

Mark

Re: Web enabled thermostat project

Posted: Mon Jun 26, 2017 9:39 pm
by stressdidri
I'd like the damn thing to read temp not from the sensor but from a text file


#!/usr/bin/env python

import os
import re
import RPi.GPIO as GPIO
import time
import math

def main():

GPIO.setwarnings(False)
GPIO.cleanup()
GPIO.setmode(GPIO.BCM)
# set BCM pin number for the furnace.
CENTRALA = 26
GPIO.setup(CENTRALA, GPIO.OUT)
GPIO.output(CENTRALA, GPIO.LOW)

with open ("umid-temp.txt", "r") as myfile:
sensmaintemp=myfile.readlines()




def currtemp():
while(1):
tfile = open(sensmaintemp)
text = tfile.read()
tfile.close()
firstline = text.split("\n")[0]
crc_check = text.split("crc=")[1]
crc_check = crc_check.split(" ")[1]
if crc_check.find("YES")>=0:
break
secondline = text.split("\n")[1]
temperaturedata = secondline.split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000.0
temperature = round(temperature, 1)
return temperature

# set desired temperature using thermostat file from web server
def settemp():
readtemp = open("set_temp.txt", "r")
settemp = readtemp.readline(4)
readtemp.close()
return float(settemp)

# hold the temperature at the settemp
def holdtemp():
while currtemp() >= settemp() - 0.4:
GPIO.output(CENTRALA, GPIO.LOW)
status = open("status.txt", "w+")
status.write("<font color=\"#3333ff\">Centrala oprita</font>")
status.close()
#print "Off loop", currtemp()
time.sleep(10)
else:
while currtemp() <= settemp() + 0.4:
GPIO.output(CENTRALA, GPIO.HIGH)
status = open("status.txt", "w+")
status.write("<font color=\"#e60073\">Centrala pornita</font>")
status.close()
#print "On loop", currtemp()
time.sleep(10)

# this constructs an infinite loop
infloop = 1
while infloop == 1 :
holdtemp()

if __name__ == '__main__':
main()