Uživatelské nástroje

Nástroje pro tento web


zarizeni:raspberry_pi

Rozdíly

Zde můžete vidět rozdíly mezi vybranou verzí a aktuální verzí dané stránky.

Odkaz na výstup diff

Obě strany předchozí revize Předchozí verze
zarizeni:raspberry_pi [2018/03/05 07:33]
multitricker Oprava nahlášené chyby ve skriptu
zarizeni:raspberry_pi [2018/09/18 09:17] (aktuální)
multitricker
Řádek 324: Řádek 324:
 <​code>​ <​code>​
 *   ​* ​  ​* ​  ​* ​  ​* ​     /​home/​pi/​sht21_python/​tmep_sht21.py > /dev/null 2>&1 *   ​* ​  ​* ​  ​* ​  ​* ​     /​home/​pi/​sht21_python/​tmep_sht21.py > /dev/null 2>&1
 +</​code>​
 +
 +===== Raspberry s HTU21DF a OLED displejem =====
 +
 +Příklad od ''​mikroma''​.
 +
 +**Web autora příkladu:​** http://​mikrom.cz/​\\
 +**Kontakt na autora:** [[mikrom@mikrom.cz]]
 +
 +{{ :​zarizeni:​pasted:​20180918-091506.png?​400 }}
 +
 +<code python>
 +#​!/​usr/​bin/​env python
 +# -*- coding: utf-8 -*-
 +
 +# Author: mikrom, 2018
 +#
 +# Based on:
 +# https://​github.com/​rm-hull/​luma.oled
 +# https://​github.com/​dalexgray/​RaspberryPI_HTU21DF
 +# https://​github.com/​al45tair/​netifaces
 +# https://​github.com/​httplib2/​httplib2
 +#
 +# HTU21DF needs to run $sudo pigpiod
 +# tmep.sh added to $sudo nano /​etc/​rc.local
 +
 +from oled.serial import i2c
 +from oled.device import sh1106
 +from oled.render import canvas
 +from PIL import ImageFont, ImageDraw
 +import time #from time import sleep <- do not use this
 +import HTU21DF
 +import netifaces as ni
 +import httplib2
 +
 +# Settings
 +sleep = 5                                    # Time between two measuring cycles (seconds)
 +url = "​http://​tmep.server.url/?" ​            # Server url (ending with question mark)
 +guid = "​guid" ​                               # Secret guid
 +rotation = 0                                 # Display rotation 0=no, 1=90, 2=180, 3=270
 +fontname = "​BRBELRT0.ttf" ​                   # Font name
 +fontsize = 18                                # Font size
 +serial = i2c(port=1, address=0x3C) ​          # Do not touch
 +device = sh1106(serial,​ rotate=rotation) ​    # Change it to ssd1306 if you have more common display
 +ni.ifaddresses('​eth0'​) ​                      # Probably no need to change it (maybe to wlan0)
 +font = ImageFont.truetype("/​home/​pi/​fonts/"​ + fontname, fontsize) # specify where your fonts are stored
 +
 +# Run infinitely
 +while True:
 +  # Get temperature and humidity from HTU21D
 +  HTU21DF.htu_reset ​                         # Reset sensor first
 +  temp = "​%.1f"​ % HTU21DF.read_temperature() # Round float to 1 decimal and convert to string
 +  time.sleep(1) ​                             # Sensor is slow, so add some delay
 +  hum = "​%.0f"​ % HTU21DF.read_humidity() ​    # Round float to 0 decimal and convert to string
 +  print("​Temp="​ + temp + "​C"​)
 +  print("​Hum="​ + hum + "​%"​)
 +
 +  # Get IP address from netifaces
 +  ip = ni.ifaddresses('​eth0'​)[ni.AF_INET][0]['​addr'​]
 +  print("​IP="​ + ip)
 +
 +  # Display data on OLED
 +  with canvas(device) as draw:
 +    temperature = "Temp: " + temp + u"​\N{DEGREE SIGN}C" ​       # Prepare string to display. Degree sign is little bit challenging to add :)
 +    w, h = font.getsize(temperature) ​                          # Get dimensions of the string
 +    draw.text(((128-w)/​2,​0),​ temperature,​ font=font, fill=255) # Just some positioning,​ it is centered by x and y=0
 +    humidity = "Hum: " + hum + "​%" ​                            # Prepare string to display. ​
 +    w, h = font.getsize(humidity) ​                             # Get dimensions of the string
 +    draw.text(((128-w)/​2,​20),​ humidity, font=font, fill=255) ​  # Just some positioning,​ it is centered by x and y=20
 +    w, h = font.getsize(ip) ​                                   # Get dimensions of the string
 +    draw.text(((128-w)/​2,​40),​ ip, font=font, fill=255) ​        # Just some positioning,​ it is centered by x and y=40
 +
 +  # Send data to tmep server
 +  requesturl = url + guid + "​="​ + temp + "&​humV="​ + hum
 +  print("​Send to: " + requesturl)
 +  resp, content = httplib2.Http().request(requesturl)
 +  if resp.status == 200:
 +    print("​Send OK")
 +  else:
 +    print("​Send Error"​)
 +
 +  # Take a nap
 +  print("​Go to sleep for %d s..." % sleep)
 +  time.sleep(sleep)
 </​code>​ </​code>​
zarizeni/raspberry_pi.txt · Poslední úprava: 2018/09/18 09:17 autor: multitricker