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

poniedziałek, 11 stycznia 2016

PCF8574 i Raspberry PI

Raspberry Pi 2m układ pinów




PCF8574



Vcc - zasilanie 3.3 V lub 5 V
GND - masa zasilania
SDA - sygnał danych magistrali i2c (TWI)
SCL - sygnał zegara magistrali i2c (TWI)
/INT - zanegowane wyjście przerwania informujące, że na wejściu zmienił się stan logiczny
A0 .. A2 - ustawienie adresu dla kolejnych układów w magistrali
P0 .. P7 - wejścia/wyjścia cyfrowe układu

1. Włączenie modułu i2c
root@raspberrypi:~# echo 'i2c-dev' >> /etc/modules
root@raspberrypi:~# reboot
2. Instalacja tools
root@raspberrypi:~# apt-get install i2c-tools
    
3. Sprawdzenie adresu
root@raspberrypi:~# i2cdetect -y 1



 4. Wszytkie piny na stan 0, czyli włączone
root@raspberrypi:~# i2cset -y 1 0x20 0x00
5. Wszystkie piny na stan 1, czyli wyłączone
root@raspberrypi:~# i2cset -y 1 0x20 0xFF
6. pin 0,1,2 wyłączone, piny 3,4,5,6,7 włączone
>>> hex(int('00000111', 2))
    '0x7'
root@raspberrypi:~# i2cset -y 1 0x20 0x7
7. Sprawdzenie stanu pinów
root@raspberrypi:~# i2cget -y 1 0x20
0x07

    # konwersja z hex na bin
>>> bin(int('0x07', 16))[2:].zfill(8)
    '00000111'
    # Widać, że pin 0,1,2 są wyłączone
8. Instalacja smbus
apt-get install python-smbus
9. Przykładowy program
from smbus import SMBus

addr = 0x20
bus = SMBus(1)
val = int('00000111', 2)
print val
# 7

# wylacz piny 0,1,2
bus.write_byte(addr, val)

# czytaj stan
s = bus.read_byte(addr)
print bin(s)[2:].zfill(8)
# 00000111

podłączenie

niedziela, 10 stycznia 2016

Czujnik wilgotności i temperatury DHT11

Podpięcie pod Raspberry Pi
VCC - 3.3V
GND - GND
DAT - GPIO 23

Instalacja
https://github.com/adafruit/Adafruit_Python_DHT

Test w python
>>> import Adafruit_DHT
>>> DHT_TYPE = Adafruit_DHT.DHT11
>>> DHT_PIN = 23
>>> humidity, temp = Adafruit_DHT.read(DHT_TYPE, DHT_PIN)
>>> print temp
23.0
>>> print humidity
38.0

niedziela, 3 stycznia 2016

Powiadomienia sms gdy brak prądu na Raspberry Pi

Potrzebujemy, stary telefon, starą ładowarkę do telefonu, transoptor cny17, 2 oporniki i płytkę raspberry Pi z zainstalowanym systemem. Ja użyłem raspbian wheezy

1. transoptor cny17-1
2. Schemat połączenia

Z lewej strony podłączam starą ładowarkę 5V od telefonu
Z prawej strony Raspberry Pi, Vcc 3.3V, output signal daję na pin 24, output gnd do gnd
Tak wygląda na płytce stykowej
z lewej, ładowarka, czerwony +, czarny -
z prawej, raspberry pi, czerwony 3,3V, żółty pin 24 input, czarny -


3. Test, jak jest 1 to brak prądu, 0 to prąd jest


4. Test z python


5. instalacja gammu

apt-get install gammu
apt-get install python-gammu
pip install python-gammu --upgrade

6. Podłączenie starego telefonu i test czy nasz system widzi telefon. Najpierw trzeba zrobić plik ~/.gammurc. Do wygenerowania pliku użyłem wammu, graficznej nakładki na gammu


7. Program do monitoringu
import RPi.GPIO as GPIO
import gevent
from time import time
import gammu
class Alert:
    def __init__(self):
        self.telephone = ['telephone number 1','telephone number 2']
        self.telephone_alert_sleep = 1800 # 30 minutes
        self.country_code = '+48'
        GPIO.setmode(GPIO.BCM)
        self.pin = 24
        self.clear()
        print "Start"
         
    def __del__(self):
        GPIO.cleanup()
     
    def clear(self):
        self.time_send_sms = 0
    def error(self):
        GPIO.setup(self.pin, GPIO.IN)
        if GPIO.input(self.pin) == 1:
            print "no energy"
            if (int(time()) - self.time_send_sms) > self.telephone_alert_sleep:
                try:
                    self.send_sms(message='no energy')
                except Exception as e:
                    print 'sms ' + str(e)
        else:
            print "yes energy"
            self.clear()
         
    def send_sms(self, message):
        state_machine = gammu.StateMachine()
        state_machine.ReadConfig()
        state_machine.Init()
     
        for num in self.telephone:
            print "send sms %s %s" % (num, message)
            m = {
                'Text': message,
                'SMSC': {'Location': 1},
                'Number': self.country_code+num,
            }          
            state_machine.SendSMS(m)
            gevent.sleep(1)
         
        for dv in self.telephone:
            print "dial voice %s " % dv
            state_machine.DialVoice(dv)
            gevent.sleep(30)
         
        self.time_send_sms = int(time())
    def run(self):
        while True:
            print "run"
            try:
                self.error()
                gevent.sleep(5)
            except Exception as e:
                print str(e)
if __name__ == '__main__':
    alert = Alert()
    gevent.spawn(alert.run())