BlogThe start of DIY home automation: replacing the thermostat, part 2

The following errors were encountered: Please rectify them and try again.

My DS18B20 sensors arrived the other day. Using Adafruit's tutorial on the sensor I was able to get a reading out of it in a matter of minutes. During Friday evening I was able to knock up a quick thermostat script.

  1. import time;
  2. import sys;
  3. import RPi.GPIO as GPIO;
  4.  
  5. targetTemp = 21;
  6. sendPin = 24;
  7. sendAttempts = 3;
  8. tempSensor = '28-03157469ceff';
  9. onCode = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1];
  10. offCode = [1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 1, 1, 1, 2, 2, 1, 1, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2, 1, 1, 1, 1, 2, 1, 1, 1];
  11.  
  12.  
  13. def sendSignal(code):
  14. signal = False;
  15. for t in range(sendAttempts):
  16. for i in code:
  17. signal = not signal;
  18. if (signal):
  19. GPIO.output(sendPin, 1);
  20. else:
  21. GPIO.output(sendPin, 0);
  22. time.sleep(0.00045 * i);
  23. GPIO.output(sendPin, 0);
  24. time.sleep(1);
  25.  
  26.  
  27. def readTempSensor(sensor):
  28. file = open('/sys/bus/w1/devices/' + sensor + '/w1_slave');
  29. lines = file.readlines();
  30. file.close();
  31. return lines;
  32.  
  33.  
  34. def readTemp():
  35. sensorOutput = readTempSensor(tempSensor);
  36.  
  37. if sensorOutput[0].strip()[-3:] != 'YES':
  38. return targetTemp;
  39.  
  40. delimiterPos = sensorOutput[1].find('t=');
  41. if (delimiterPos < 0):
  42. return targetTemp;
  43.  
  44. tempReadingString = sensorOutput[1][delimiterPos + 2:];
  45.  
  46. return float(tempReadingString) / 1000.0;
  47.  
  48.  
  49. try:
  50. GPIO.setmode(GPIO.BCM);
  51. GPIO.setup(sendPin, GPIO.OUT);
  52. prevTemp = targetTemp;
  53. while True:
  54. currentTemp = readTemp();
  55. print currentTemp;
  56. if currentTemp < targetTemp and currentTemp < prevTemp:
  57. print 'on';
  58. sendSignal(onCode);
  59. elif currentTemp > targetTemp and currentTemp > prevTemp:
  60. print 'off';
  61. sendSignal(offCode);
  62. prevTemp = currentTemp;
  63. time.sleep(1);
  64. except:
  65. pass;
  66. GPIO.cleanup();

This basically watches the temperature and sends the on or off signal based on the following conditions:
- If we are below target temperature, then turn on
- If we are above target temperature, then turn off
- If we are below target temperature and still falling, then keep sending the on signal
- If we are above target temperature and still rising, then keep sending the off signal
For the most part this works, however there seems to be a problem as the receiver isn't flipping the relay sometimes. This is odd, since when I used the script from my previous post it was flawless. I figure it's down to 2 things:
1) the aerial is insufficient
2) the signal timings are out
My first step to will be to solder on an aerial properly, instead of propping a wire in the antenna hole. If that fails I'll add a random fudge factor in to the timings each time the signal is sent. If that fails then I'll have to think of something else.

For now, however, I'm going to work on code to communicate what the thermostat is doing. I need this eventually so doing it now should save me hassle in the long run.

Posted on Saturday the 12th of March 2016