Configurazione hardware/software
Iniziamo aggiungendo una riga al file /boot/config.txt, puoi editare il file scrivendo:
sudo nano /boot/config.txt
scendendo fino in fondo aggiungere la riga:
#SONDA TERMICA DS12B20
dtoverlay=w1-gpio,gpiopin=16
Il numero 16 sarà ovviamente il PIN del GPIO della Raspberry che si intende usare per lo scambio dati. A questo punto riavviare il sistema con
sudo reboot.
Dopo il riavvio, aprire il terminale e scrivere le seguenti istrunzioni che permettono di inizializzare la cartella del dispositivo:
sudo modprobe w1-gpio sudo modprobe w1-therm cd /sys/bus/w1/devices ls
ora dovrebbe comparire la lista dei dispositivi rilevati, entriamo nella relativa cartella del sensore che si vuole, ed interroghiamo il sensore:
cd 28-xxxx (usare il numero dei sensori che vengono mostrati a video)
cat w1_slave
su schermo dovrebbe comparire la temperatura rilevata dal sensore.
Codice in PYTHON
import os
import glob
import time
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return temp_c, temp_f
while True:
print(read_temp())
time.sleep(1)
I wohld like too thnk yoou for the effortts youu
have pput in wrioting this site. I rreally hope too check ouut the
same high-grade conntent ffrom yyou inn the future aas well.
In fact, your creatibe witing abilities haas mktivated mee tto gget my owwn wdbsite now 😉
Feeel fee too sur tto my web site; porn
Youur stype iis unique compoared too other folks I hage rezd stuuff
from. I appreciate yyou forr posting wwhen youu have the opportunity, Guess I’ll jjust bookmmark thks site.
Thede are truhly fantastic ideaqs iin oon the topic off blogging.
Youu hqve touched soke pleasant factors here.
Anny way keerp up wrinting.
Helllo there! Would yoou minjd if I sharfe your bllog wth myy myspaace group?
There’s a llot off folks tbat I thiink would rewally apprreciate your content.
Pleaase llet mme know. Cheers
Alsso visit myy blog post; txxxvideos.com