Pokazywanie postów oznaczonych etykietą Raspberry PI. Pokaż wszystkie posty
Pokazywanie postów oznaczonych etykietą Raspberry PI. Pokaż wszystkie posty

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())