Raspberry Pi model B
Dallas DS18B20 temperature sensor
LCD display (Nokia 5110-3310)
Edimax WiFi nano USB adapter
Some packages are required from the standard Raspbian repository:
apt-get install python-dev apt-get install python-imaging
apt-get install python-pip pip install RPi.GPIO
Then download and install the Adafruit PCD8544 Nokia 5110 LCD library:
git clone https://github.com/adafruit/Adafruit_Nokia_LCD.git cd Adafruit_Nokia_LCD python setup.py install
We also used PF Tempesta Seven, a free TrueType font for the LCD display.
The Nokia 5110/3310 LCD
Follow this guide to make an Hello world program with the LCD display: Nokia 5110/3310 LCD Python Library.
We can export data (current temperature, programmed temperature and relay status) to remote systems using SNMP, in this way we can – e.g. – plot a nice temperature graph into a Cacti system. The required packages are:
apt-get install snmpd snmp snmp-mibs-downloader
The shell script
/usr/local/bin/protherm-snmp reads values from the shared memory file
/run/shm/protherm/stats, then we add some
extend commands into
extend temp /usr/local/bin/protherm-snmp temp extend tprog /usr/local/bin/protherm-snmp tprog extend switch /usr/local/bin/protherm-snmp switch
If everything is working well, we can query the current temperature via SNMP on the command line
snmpwalk -v 2c -c public 127.0.0.1 'NET-SNMP-EXTEND-MIB::nsExtendOutput1Line."temp"'
We want to replace the file-based syslog (provided by the rsyslog package), with a memory ring-buffer logger, like the one provided by the busybox-syslogd package (this will remove rsyslog automatically):
apt-get install busybox-syslogd
The memory buffer can be read with
logread, use the
-f switch to see data as it arrives.
The size of the memory buffer is configured into
/etc/default/busybox-syslogd, the default option is
-C128, which allocates only 128 kb.
We also decreased the verbosity of cron: we want to log only errors in cron jobs, not every started job. In
/etc/default/cron we added:
Apache does not fit well in a low-memory device like the Raspberry Pi, so we looked at lighttpd an nginx alternatives. It seems that nginx is more modern and suitable for the work. See also this Tutorial – Install Nginx and PHP on Raspbian. We want also the relative PHP module:
apt-get install nginx php5-fpm
The web interface is rather simple, it offers a snapshot of the LCD display and a graph of the temperature of the last day. It is also possible to emulate the button press, to change the program.
Hardware Test and Debug
Once assembled, we can test each piece of hardware separately.
WARNINIG Current version of ProTherm uses GPIO2 for push button. Beware that GPIO2 and GPIO3 are not intended as generic INPUT ports, see notes below.
Let’s say that the push button is attached to the GPIO #18, we need to expose the port and configure it as an input port:
echo "18" > /sys/class/gpio/export echo "in" > /sys/class/gpio/gpio18/direction
reading the port will return 1 if button is released, zero if it is pressed (the Raspberry Pi has a default pull-up configured via software):
This is a Python program which prints some text as the button is pressed:
import RPi.GPIO as GPIO import time GPIO.setmode(GPIO.BCM) GPIO.setup(18, GPIO.IN, pull_up_down=GPIO.PUD_UP) while True: input_state = GPIO.input(18) if input_state == False: print('Button Pressed') time.sleep(0.2)
Fake (erratic) button presses
Strange to say, the push button was the most challenging task of the entire project. At the first I connected the button directly to the GPIO and to the ground, the GPIO was programmed with the software PULL-UP. In this way the GPIO reads normally an HIGH state, when the button is pressed, the state becomes LOW.
With this configuration I got several fake button press, i.e. the GPIO reads a LOW state despite no-one was pressing the button. I suspected that the problem was a floating in the power supply and indeed the problem sometimes arises when the relay is switched to ON and it draws 5v from the Raspberry, may be causing a little voltage sag in the PULL-UP
Someone (see the replies to this post) states that the PULL-UP resistor activated by software is too big (may be 50 kΩ), so the PULL-UP current is too weak and voltage can drop down unexpectedly. The suggested solution is to add an external 4.7 kΩ PULL-UP resistor anyway.
I tried also to use the software PULL-DOWN resistor, in this case the push button is used to connect the GPIO to the 3.3 v. I don’t know why this mode is not so widespread as I would expect, nevertheless here it is an article using the PULL-DOWN configuration. With this configuration the false signals are worse: almost every time the relay changes state (from OFF to ON or vice-versa), the Raspberry Pi gets a false GPIO signal. So it seems that it is not a voltage sag, because the GPIO registered a fake HIGH state. May be the wire acts like an antenna, getting the 50 Hz hum.
For more detail visit Niccolo Rigacci Original blog at:: Programmable Thermostat with the Raspberry Pi