piątek, 22 stycznia 2016

DS18B20 i Raspberry PI - pomiar temperatury

DS18B20 - pomiar temperatury

1. podłączenie do raspberry pi


2. jak czytać
pi@raspberrypi:~ $ cat /etc/modules
# /etc/modules: kernel modules to load at boot time.
#
# This file contains the names of kernel modules that should be loaded
# at boot time, one per line. Lines beginning with "#" are ignored.

i2c-dev
w1-gpio
w1-therm
i2c-dev
pi@raspberrypi:~ $ cat /sys/bus/w1/devices/w1_bus_master1/w1_master_slaves
28-021463ebceff
28-021463ea71ff
28-04146b0925ff
pi@raspberrypi:~ $ cat /sys/bus/w1/devices/28-021463ebceff/w1_slave
9a 01 55 00 7f ff 0c 10 a2 : crc=a2 YES
9a 01 55 00 7f ff 0c 10 a2 t=25625
pi@raspberrypi:~ $ 
3. Odczyt w python i zapis do grafany
    def send(self, message):
        sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
        sock.sendto(message, ('127.0.0.1', 8089))

    def temperature(self):
        try:
            with open('/sys/bus/w1/devices/w1_bus_master1/w1_master_slaves', 'rt') as f:
                for line in f:
                    file = open("/sys/bus/w1/devices/%s/w1_slave" % line.strip())
                    temperature = float(file.read().split(
                        "\n")[1].split(" ")[9][2:]) / 1000
                    # print line.strip()
                    # print temperature
                    message = "%s,machine=unit42,type=assembly value=%s" % (
                        line.strip(), temperature)
                    file.close()
                    self.send(message)
        except Exception as e:
            self.log.error(str(e))
4. Prezentacja w grafanie

Brak komentarzy:

Prześlij komentarz