Toto je starší verze dokumentu!
apt update && apt upgrade && apt install python-pip mc
uz jedem pres systemd:
nano /lib/systemd/system/myscript.service
[Unit] Description=My Script Service # Wants=network-online.target # After=network.target network-online.target After=multi-user.target [Service] Type=idle ExecStart=/usr/bin/python /home/pi/myscript.py # ExecStart=/usr/bin/python /home/pi/myscript.py > /home/pi/myscript.log 2>&1 [Install] WantedBy=multi-user.target
sudo chmod 644 /lib/systemd/system/myscript.service
sudo systemctl daemon-reload sudo systemctl enable myscript.service
+-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+ | BCM | wPi | Name | use | V | Physical | V | use | Name | wPi | BCM | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | | | 3.3v | | | 1 || 2 | | | 5v | | | | 2 | 8 | SDA.1 | | 1 | 3 || 4 | | | 5v | | | | 3 | 9 | SCL.1 | | 1 | 5 || 6 | | | 0v | | | | 4 | 7 | GPIO. 7 | | 0 | 7 || 8 | 0 | | TxD | 15 | 14 | | | | 0v | | | 9 || 10 | 1 | | RxD | 16 | 15 | | 17 | 0 | GPIO. 0 | 1RB1 | 0 | 11 || 12 | 0 | | GPIO. 1 | 1 | 18 | | 27 | 2 | GPIO. 2 | 1RB2 | 0 | 13 || 14 | | | 0v | | | | 22 | 3 | GPIO. 3 | 1RB3 | 0 | 15 || 16 | 0 |DHT-22| GPIO. 4 | 4 | 23 | | | | 3.3v | | | 17 || 18 | 0 | | GPIO. 5 | 5 | 24 | | 10 | 12 | MOSI | | 0 | 19 || 20 | | | 0v | | | | 9 | 13 | MISO | | 0 | 21 || 22 | 0 | | GPIO. 6 | 6 | 25 | | 11 | 14 | SCLK | | 0 | 23 || 24 | 1 | | CE0 | 10 | 8 | | | | 0v | | | 25 || 26 | 1 | | CE1 | 11 | 7 | | 0 | 30 | SDA.0 | | 1 | 27 || 28 | 1 | | SCL.0 | 31 | 1 | | 5 | 21 | GPIO.21 | 1RB4 | 1 | 29 || 30 | | | 0v | | | | 6 | 22 | GPIO.22 | 1RB5 | 0 | 31 || 32 | 0 | 2RB1 | GPIO.26 | 26 | 12 | | 13 | 23 | GPIO.23 | 1RB6 | 1 | 33 || 34 | | | 0v | | | | 19 | 24 | GPIO.24 | 1RB7 | 1 | 35 || 36 | 0 | 2RB2 | GPIO.27 | 27 | 16 | | 26 | 25 | GPIO.25 | 1RB8 | 1 | 37 || 38 | 0 | 2RB3 | GPIO.28 | 28 | 20 | | | | 0v | | | 39 || 40 | 0 | 2RB4 | GPIO.29 | 29 | 21 | +-----+-----+---------+------+---+----++----+---+------+---------+-----+-----+ | BCM | wPi | Name | Mode | V | Physical | V | Mode | Name | wPi | BCM | +-----+-----+---------+------+---+---Pi 3B--+---+------+---------+-----+-----+
pip install serial modbus_tk configparser pymodbus paho-mqtt
pouzivam upraveny kod odtud
apt install wiringpi
otestovat
gpio readall
pouzivam gpio1
#!/usr/bin/env python2 import paho.mqtt.client as mqtt import time import Adafruit_DHT from configparser import ConfigParser import json config = ConfigParser(delimiters=('=', )) config.read('config.ini') sensor_type = config['sensor'].get('type', 'dht22').lower() if sensor_type == 'dht22': sensor = Adafruit_DHT.DHT22 elif sensor_type == 'dht11': sensor = Adafruit_DHT.dht11 elif sensor_type == 'am2302': sensor = Adafruit_DHT.AM2302 else: raise Exception('Supported sensor types: DHT22, DHT11, AM2302') pin = config['sensor'].get('pin', 10) topict = config['mqtt'].get('topic', 'temp/chodba') topich = config['mqtt'].get('topic', 'humid/chodba') decim_digits = config['sensor'].getint('decimal_digits', 4) # The callback for when the client receives a CONNACK response from the server. def on_connect(client, userdata, flags, rc): print("Connected with result code {}".format(rc)) client = mqtt.Client() client.on_connect = on_connect client.connect(config['mqtt'].get('hostname', 'homeassistant'), config['mqtt'].getint('port', 1883), config['mqtt'].getint('timeout', 60)) client.loop_start() while True: time.sleep(3) humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) if humidity is not None and temperature is not None: data = round(temperature, decim_digits) client.publish(topict, json.dumps(data)) data = round(humidity, decim_digits) client.publish(topich, json.dumps(data)) print('Published. Sleeping ...') else: print('Failed to get reading. Skipping ...')