Opis podłączenia ekranu HD44780 i 2 czujników DS18B20 do maliny. Wszystko pochodzi z 3 stron podanych na samym dole, z własnego doświadczenia dodałem tylko rezystor 4k(moze być też mniejszy) na kontraście od lcd oraz zmieniłem troche kod programu aby wyświetlał temeperaturę z 2 czujników.
Instalujemy kilka rzeczy:
sudo apt-get install python-dev
sudo apt-get install python-setuptools
sudo apt-get install screen
sudo apt-get install python-pip
sudo pip install rpi.gpio
git clone https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code.git
Tak podłączamy czujniki ds18b20 do GPIO:
Edytujemy plik w celu automatycznego ładowania sensorów przy każdym uruchomieniu:
sudo nano /etc/modules
w1-gpio
w1-therm
Odczytujemy identyfikatory naszych czujników które wykorzystamy w końcowym programie:
ls -l /sys/devices/w1_bus_master1/
Dodajemy na początku pliku /boot/config.txt:
sudo nano /boot/config.txt
dtoverlay=w1-gpio,gpiopin=4
sudo reboot
Tak podłączamy lcd do GPIO:
LCD Pin | Function | Pi Function | Pi Pin |
01 | GND | GND | 06 |
02 | +5V | +5V | 02 |
03 | Contrast przez ok. 4kohm | GND | 06 |
04 | RS | GPIO7 | 26 |
05 | RW | GND | 06 |
06 | E | GPIO8 | 24 |
07 | Data 0 |
|
|
08 | Data 1 |
|
|
09 | Data 2 |
|
|
10 | Data 3 |
|
|
11 | Data 4 | GPIO25 | 22 |
12 | Data 5 | GPIO24 | 18 |
13 | Data 6 | GPIO23 | 16 |
14 | Data 7 | GPIO27 | 13 |
15 | +5V przez ok. 560ohm | +5V | 02 |
16 | GND | GND | 06 |
Tworzymy nowy plik CharLCD_Temp.py w /Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCD
#!/usr/bin/python
from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
from datetime import datetime
lcd = Adafruit_CharLCD(pin_rs=7, pin_e=8, pins_db=[25, 24, 23, 27], GPIO=None)
file = "/sys/bus/w1/devices/28-0000063dd97c/w1_slave"
file1 = "/sys/bus/w1/devices/28-0000063d7619/w1_slave"
lcd.begin(16,1)
def get_temp(file):
#Open file written to by temp sensor
tfile = open(file)
#read all text in file
text = tfile.read()
#Close file once text is read
tfile.close()
#pull out the temperature value
temperaturedata = text.split("\n")[1].split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature $
temperature = float(temperaturedata[2:])
# Put the decimal point in the right place and display it.
temperature = temperature / 1000
temperature = round(temperature, 1)
return temperature
def get_temp1(file1):
#Open file written to by temp sensor
tfile1 = open(file1)
#read all text in file
text1 = tfile1.read()
#Close file once text is read
tfile1.close()
#pull out the temperature value
temperaturedata1 = text1.split("\n")[1].split(" ")[9]
# The first two characters are "t=", so get rid of those and convert the temperature $
temperature1 = float(temperaturedata1[2:])
# Put the decimal point in the right place and display it.
temperature1 = temperature1 / 1000
temperature1 = round(temperature1, 1)
return temperature1
while 1:
lcd.clear()
tempVal = get_temp(file)
tempVal1 = get_temp1(file1)
lcd.message(datetime.now().strftime('%a %d %b %y\n'))
lcd.message(datetime.now().strftime(' %H:%M:%S'))
sleep(30)
lcd.clear()
lcd.message('TEMP ZEW %s C\n' % ( tempVal ) )
lcd.message('TEMP WEW %s C' % ( tempVal1 ) )
sleep(30)
Program odpalamy przez komendę:
./CharLCD_Temp.py
Auto odpalanie przy starcie systemu:
sudo nano boot.sh
sleep 15s
python /home/pi/Adafruit-Raspberry-Pi-Python-Code/Adafruit_CharLCD/CharLCD_Temp.py &
chmod 755 boot.sh
sudo nano /etc/rc.local i dodajemy przed exit 0:
/home/pi/boot.sh &
A oto rezultat:
i po 30 sekundach:
Update 20.12.2015
Oto trochę zmodyfikowany skrypt, Temperaturę zewnętrzną pobiera z api Forecast(
https://developer.forecast.io/). Wszystko teraz wyświetla się na jednym ekranie odświeżając zegar co minutę a pogodę co 2 minuty. Dodatkowo nazwy miesięcy są wyświetlane po polsku. Pojawił się również znak stopnia Celsjusza.
#!/usr/bin/python
from Adafruit_CharLCD import Adafruit_CharLCD
from subprocess import *
from time import sleep, strftime
from datetime import datetime
import locale
import json
import urllib
locale.setlocale(locale.LC_TIME, "pl_PL.ISO-8859-2")
lcd = Adafruit_CharLCD(pin_rs=7, pin_e=8, pins_db=[25, 24, 23, 18], GPIO=None)
file = "/sys/bus/w1/devices/28-0000063dd97c/w1_slave"
lcd.begin(16,1)
def get_temp(file):
tfile = open(file)
text = tfile.read()
tfile.close()
temperaturedata = text.split("\n")[1].split(" ")[9]
temperature = float(temperaturedata[2:])
temperature = temperature / 1000
temperature = round(temperature, 1)
return temperature
while 1:
lcd.clear()
tempVal = get_temp(file)
apikey="XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX"
lati="XX.XXXXXX"
longi="XX.XXXXXX"
url="https://api.forecast.io/forecast/"+apikey+"/"+lati+","+longi+"?units=si"
meteo=urllib.urlopen(url).read()
meteo = meteo.decode('utf-8')
weather = json.loads(meteo)
weather2 = round(weather['currently']['temperature'], 1)
lcd.message(datetime.now().strftime('%d %b %H:%M\n'))
lcd.message("z%s" % str(weather2)+chr(223)+"C"+" w%s" % (tempVal)+chr(223)+"C")
sleep(60)
lcd.clear()
lcd.message(datetime.now().strftime('%d %b %H:%M\n'))
lcd.message("z%s" % str(weather2)+chr(223)+"C"+" w%s" % (tempVal)+chr(223)+"C")
sleep(60)
A oto nowy rezultat:
Źródło:
http://www.raspberrypi-spy.co.uk/2012/07/16x2-lcd-module-control-using-python/
https://pel.ly/raspberry-pi-lcd-screen-and-temperature-sensor/
https://github.com/adafruit/Adafruit-Raspberry-Pi-Python-Code/tree/master/Adafruit_CharLCD