DS18b20: Skirtumas tarp puslapio versijų
Jump to navigation
Jump to search
(multiple sensoriai ir temperaturu nuskaitymas) |
|||
21 eilutė: | 21 eilutė: | ||
* PIN14 - GND (Juodas) | * PIN14 - GND (Juodas) | ||
* PIN11 - DATA (Geltonas) | * PIN11 - DATA (Geltonas) | ||
+ | |||
+ | Taip pat galima jungti kelis sensorius, tokia tvarka: | ||
+ | |||
+ | [[Vaizdas:46695522054 335a5086f5 z.jpg]] | ||
== Raspbian paruošimas == | == Raspbian paruošimas == | ||
68 eilutė: | 72 eilutė: | ||
</syntaxhighlight> | </syntaxhighlight> | ||
+ | === Python scriptas temperatūrai nuskaityti (jeigu naudojami keli sensoriai) === | ||
+ | |||
+ | |||
+ | <syntaxhighlight lang="python"> | ||
+ | #!/usr/bin/python | ||
+ | import os | ||
+ | import glob | ||
+ | import time | ||
+ | |||
+ | |||
+ | sensors = [{"hwid":"28-03049779621c", "name":"1 sensorius", "color":"#ff0000"}, | ||
+ | {"hwid":"28-03049779691d", "name":"2 sensorius", "color":"#0000ff"}, | ||
+ | {"hwid":"28-03049779b25b", "name":"3 sensorius", "color":"#3cb371"}, | ||
+ | {"hwid":"28-03049779d1ab", "name":"4 sensorius", "color":"#6a5acd"}] | ||
+ | |||
+ | |||
+ | os.system('modprobe w1-gpio') | ||
+ | os.system('modprobe w1-therm') | ||
+ | |||
+ | def read_temp_raw(dev): | ||
+ | f = open("/sys/bus/w1/devices/"+dev+"/w1_slave",'r') | ||
+ | lines = f.readlines() | ||
+ | f.close() | ||
+ | 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 = round((float(temp_string) / 1000.0),2) | ||
+ | return temp_c | ||
+ | |||
+ | def ReadAllSensors(): | ||
+ | for sensor in sensors: | ||
+ | temp = str(read_temp_raw(sensor["hwid"])) | ||
+ | print "Sensor: "+str(sensor["name"])+" temp: "+temp+"C" | ||
+ | |||
+ | ReadAllSensors() | ||
+ | </syntaxhighlight> | ||
[[Category:Hardware]] | [[Category:Hardware]] | ||
[[Category:RaspberryPI]] | [[Category:RaspberryPI]] | ||
[[Category:OrangePI]] | [[Category:OrangePI]] |
11:25, 10 spalio 2019 versija
Temperatūros sensorius.
RaspberryPI pajungimas naudojant 4.7k Ohm rezistorių
Standartinis jungimas
Alternatyvus jungimas, kai standartiniai pinai jau yra užimti
- PIN17 - VCC (Raudonas)
- PIN14 - GND (Juodas)
- PIN11 - DATA (Geltonas)
Taip pat galima jungti kelis sensorius, tokia tvarka:
Raspbian paruošimas
Įgalinam wire 1 overlay, į /boot/config.txt įrašome šią eilutę:
dtoverlay=w1-gpio,gpiopin=17
Išsaugome ir perkrauname RaspberryPI. Perkrovus turėtų atsirasti šie įrenginiai:
ls /sys/bus/w1/devices
Daugiau apie wire 1 sąsają. DĖMESIO! Jungiant standartiškai kai standartiniai pinai nėra užimti, žiūr. pav. 1, nereikia prirašyti gpiopin parametro, tuomet duomenys eis standartiškai per PIN 7.
Python scriptas temperatūrai nuskaityti
#!/usr/bin/env 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)
Python scriptas temperatūrai nuskaityti (jeigu naudojami keli sensoriai)
#!/usr/bin/python
import os
import glob
import time
sensors = [{"hwid":"28-03049779621c", "name":"1 sensorius", "color":"#ff0000"},
{"hwid":"28-03049779691d", "name":"2 sensorius", "color":"#0000ff"},
{"hwid":"28-03049779b25b", "name":"3 sensorius", "color":"#3cb371"},
{"hwid":"28-03049779d1ab", "name":"4 sensorius", "color":"#6a5acd"}]
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
def read_temp_raw(dev):
f = open("/sys/bus/w1/devices/"+dev+"/w1_slave",'r')
lines = f.readlines()
f.close()
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 = round((float(temp_string) / 1000.0),2)
return temp_c
def ReadAllSensors():
for sensor in sensors:
temp = str(read_temp_raw(sensor["hwid"]))
print "Sensor: "+str(sensor["name"])+" temp: "+temp+"C"
ReadAllSensors()