Météo Pi Zero
Météo Pi Zero
from PIL import ImageFont
from PIL import ImageDraw
from PIL import Image
from BME280 import *
import SSD1306 as Adafruit_SSD1306
import os
import glob
import time
import importlib
import sys
import PostHTTP
import requests
import smbus # pour la communication i2c
import tsl2561
os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')
apiKey = 'Api_Key-s'
url = 'https://api.ms-only.net/meteo/'
RST = None
disp = Adafruit_SSD1306.SSD1306_128_64(rst=RST)
# Initialize library.
disp.begin()
# Clear display.
disp.clear()
disp.display()
# Create blank image for drawing.
# Make sure to create image with mode '1' for 1-bit color.
width = disp.width
height = disp.height
image = Image.new('1', (width, height))
# Get drawing object to draw on image.
draw = ImageDraw.Draw(image)
# Draw a black filled box to clear the image.
draw.rectangle((0, 0, width, height), outline=0, fill=0)
# Load default font.
font = ImageFont.load_default()
base_dir = '/sys/bus/w1/devices/'
device_folder = glob.glob(base_dir + '28*')[0]
device_file = device_folder + '/w1_slave'
def read_temp_raw():
f = open(device_file, 'r')
lines = f.readlines()
f.close()
return lines
def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) / 1000.0
temp_f = temp_c * 9.0 / 5.0 + 32.0
return str(temp_c)
sensor = BME280(t_mode=BME280_OSAMPLE_8, p_mode=BME280_OSAMPLE_8, h_mode=BME280_OSAMPLE_8)
bus = smbus.SMBus(1)
sensorLux = tsl2561.Tsl2561(bus)
import smbus
import time
# Get I2C bus
bus = smbus.SMBus(1)
# TSL2561 address, 0x39(57)
# Select control register, 0x00(00) with command register, 0x80(128)
# 0x03(03) Power ON mode
bus.write_byte_data(0x39, 0x00 | 0x80, 0x03)
# TSL2561 address, 0x39(57)
# Select timing register, 0x01(01) with command register, 0x80(128)
# 0x02(02) Nominal integration time = 402ms
bus.write_byte_data(0x39, 0x01 | 0x80, 0x02)
time.sleep(0.5)
while True:
draw.rectangle((0, 0, width, height), outline=0, fill=0)
mlux, mir = sensorLux.lux
lux = round(mlux, 2)
ir = round(mir, 2)
temp = read_temp()
temp2 = round(sensor.read_temperature(), 2)
pa = round(sensor.read_pressure() /100, 2)
hum = round(sensor.read_humidity(), 2)
dustDensity = str(0)
print(temp)
print(temp2)
print(pa)
print(hum)
print(lux)
print(ir)
# Output data to screen
# Read data back from 0x0C(12) with command register, 0x80(128), 2 bytes
# ch0 LSB, ch0 MSB
data = bus.read_i2c_block_data(0x39, 0x0C | 0x80, 2)
# Read data back from 0x0E(14) with command register, 0x80(128), 2 bytes
# ch1 LSB, ch1 MSB
data1 = bus.read_i2c_block_data(0x39, 0x0E | 0x80, 2)
# Convert the data
ch0 = data[1] * 256 + data[0]
ch1 = data1[1] * 256 + data1[0]
print ("Full Spectrum(IR + Visible) :%d lux" %ch0)
print ("Infrared Value :%d lux" %ch1)
print ("Visible Value :%d lux" %(ch0 - ch1))
lux = round(ch0, 2)
vis = round(ch0 - ch1, 2)
ir = round(ch1, 2)
draw.text((0, 0), "Tem: " + str(temp2) + " c", font=font, fill=255)
draw.text((0, 10), "Hum: " + str(hum) + " %", font=font, fill=255)
draw.text((0, 20), "Pre: " + str(pa) + " hpa", font=font, fill=255)
draw.text((0, 30), "Lum: " + str(lux) + " lux", font=font, fill=255)
draw.text((0, 40), "Vis: " + str(vis) + " lux", font=font, fill=255)
draw.text((0, 50), "Ir: " + str(ir) + " lux", font=font, fill=255)
disp.image(image)
disp.display()
headers = {'Content-Type': 'application/json', 'charset': 'utf-8'}
dict = PostHTTP.POSTHTTP.send(apiKey, str(temp2), str(pa), str(hum), str(lux), str(ir), dustDensity)
#print(dict)
response = requests.post(url, data=dict, headers=headers)
#print(response.content )
time.sleep(30)
Raspberry Pi
Python
Publié le : 01/08/2022
Mise à jour : 01/08/2022
Catégories
- Arduino 1
- Bash 0
- C 0
- Capteurs 0
- Code sources 2
- CPP 1
- ESP8266MOD 1
- MicroPython 1
- PHP 0
- Python 1
- Raspberry Pi 1