Mundarija:
- 1 -qadam: Uskuna sozlamalari
- 2 -qadam: Pi dasturiy ta'minotini sozlash
- 3 -qadam: Pythonni sozlash
- 4 -qadam: Python -ni ishga tushiring
- 5 -qadam: Agar men AQShda yashamasam nima bo'ladi?
- 6 -qadam: Yakuniy fikrlar
Video: Covid-19 statistikasi + Raspberry Pi + I2C LCD: 6 qadam
2024 Muallif: John Day | [email protected]. Oxirgi o'zgartirilgan: 2024-01-31 10:26
Shunday qilib, bir kuni tasodifan ko'kda qoldim, men o'zim yotqizgan bir nechta bo'laklarni olishga qaror qildim va menga Covid-19 haqida real vaqtda statistik ma'lumotlarni etkazib beradigan narsalarni yasashga qaror qildim. Men chiroyli ko'rinishga ko'p vaqt sarflamadim, chunki nima uchun bu voqea doimiy bo'lmaganda? Shuning uchun, mening displeyim kichik karton qutiga o'rnatilgan.
Kerakli qismlar:
- Raspberry Pi - har qanday model. Men Raspberry Pi 3A+ dan foydalanardim
- 20x4 I2C LCD displeyi - hech qanday markali emas, lekin I2C xalta kerak
- Ayol -ayol o'tish simlari - I2C -ni Pi -ga ulash uchun atigi 4 tasi
www.adafruit.com/product/4027
www.amazon.com/gp/product/B01GPUMP9C/ref=p…
www.amazon.com/gp/product/B01L5ULRUA/ref=p…
Bu havolalar to'g'ridan -to'g'ri men sotib olgan manbalarga to'g'ri keladi. Kechirasiz, Adafruit hozir etkazib bermayapti, lekin Amazon … asosiy e'tiborini asosiy mahsulotlarga qaratgani uchun. Hammasini Amazon va eBay -ning boshqa joylarida topish mumkin.
Bularning barchasini bajarish uchun sizga AC adapteri, USB kabeli va microSD kartasi kerak bo'ladi.
1 -qadam: Uskuna sozlamalari
Ilova qilingan pinout rasmiga murojaat qiling. Unda B+yozilgan, lekin u Raspberry Pi -ning boshqa modellariga tegishli.
LCD displeyga I2C yukxalta ulangan bo'lsa, bu ulanish uchun faqat 4 ta sim ishlashi kerak.
GND -ni Raspberry Pi -dagi har qanday tuproqli pinlarga ulang: 6, 9, 14, 20, 25, 30, 34, 39 -pin. Men uni 6 -pin bilan bog'ladim.
VCC -ni Raspberry Pi -dagi 5 voltli pinlardan biriga ulang: 2 -pin, 4. Men 4 -pinni ishlatardim.
3 -pinli SDA -ni ulang.
SCL -ni 5 -pinga ulang.
Agar siz mening sozlamalarimga rioya qilgan bo'lsangiz, GPIO sarlavhalarida 2x2 o'lchamdagi 4 ta simni topasiz.
O'rnatish usuli siz tasavvur qilganingizdek bo'lishi mumkin … yoki umuman yo'q. Kirish qismida aytganimdek, koronavirusning bu turi abadiy davom etmaydi, shuning uchun menga sozlashim ham kerak emas. Agar men ushbu tadbir tugagandan so'ng bu sozlamani saqlashga qaror qilsam, uni ob -havo displeyiga yoki boshqa narsaga aylantirishim mumkin.
Men yong'oq va murvatni neylon ajratgich bilan birga Pi 3A+ning 4 burchagiga biriktirdim. Bu qat'iy ixtiyoriy. Men buni shunday qildim, chunki ba'zida menda metall yuza bor edi, vaqtinchalik sozlamalarimni korpus ichidagi Pi -ga o'rnatishni yoqtirmasdim va uni chalkashtirib yubormoqchi emasman, chunki uni metalldan olib tashlashni unutganman. uni yoqishdan oldin uni sirtga qo'ying.
2 -qadam: Pi dasturiy ta'minotini sozlash
Kirish qismida aytganimdek, siz qanday Raspberry Pi modelini ishlatishingiz muhim emas. Men buni Wi -Fi orqali Raspberry Pi 3A+ da ishlataman, lekin men buni Raspberry Pi 2 -da chekilgan kabel orqali va Raspberry Pi Zero 1.3 versiyasida (ketma -ket kamera ulagichi bo'lgan birinchi Pi Zero) USB WiFi -dongle bilan sinab ko'rdim.
Men Raspbian -ni MicroSD kartasiga qanday o'rnatishni yozmoqchi emasman, chunki buning uchun millionlab ko'rsatmalar mavjud. Menda Raspbian Buster Lite bilan ishlaydigan 16 Gb microSD bor. Qolaversa, men deyarli har doim Raspbian Lite -dan foydalanaman, chunki menga hech qanday loyihamda boshqa foydasiz dasturiy paketlar kerak emas. Agar dasturiy ta'minotni apt-get yordamida o'rnatsam, u etishmayotgan old shartlarni o'rnatadi.
Tarmoqqa ulanish. Shunga qaramay, buni qanday qilish kerakligi haqida millionlab ko'rsatmalar mavjud, shuning uchun men bu erda chuqurroq to'xtamayman. Siz simli yoki simsiz ulanishingiz mumkin, lekin buning uchun internetga ulanish kerak bo'ladi.
Majburiy emas, lekin siz SSH -ga PuTTY yordamida ulanishni yoqishingiz mumkin. Men bajardim.
Hammasini yangilang va keyin qayta yoqing:
sudo apt yangilash
sudo apt upgrade -y sudo apt dist-upgrade sudo rpi-update sudo reboot
Bu men bu erda o'tadigan bitta sozlash. Shunga qaramay, buning millionlab usullari bor, lekin men topgan eng yaxshi ma'lumot bu erda:
Mana diqqatga sazovor joylar:
sudo apt i2c-tools ni o'rnating
sudo apt install python-smbus
I2C -ni yoqishingiz kerak bo'ladi
sudo raspi-config
- 5 ta interfeys variantlari
- P5 I2C
O'zgarishlarni qo'llash uchun qayta yoqing
sudo qayta yuklash
Endi bularning barchasini to'g'ri bajarganingizni ko'rish vaqti keldi
i2cdetect -y 1
Agar sizning displeyingiz yoqilgan bo'lsa va uni Raspberry Pi -da ko'rish mumkin bo'lsa, sizda jadval paydo bo'ladi. Men Amazonda sotib olgan va bu loyihada ishlatadigan 20x4 manzili - 27. Texnik jihatdan, bu keyinroq keladigan python skriptlari uchun 0x27 sifatida aniqlanadi. Men Amazonda sotib olgan 2 ta 16x2 displey va eBay -da topilgan 40x2 displey uchun bir xil manzillar ko'rgazmasini o'tkazdim.
3 -qadam: Pythonni sozlash
Xo'sh, endi murakkab narsalar haqida. Men buni iloji boricha sodda saqlashga harakat qilaman. Yangi boshlanuvchilar uchun men faqat fayllarni uy katalogiga yozaman.
I2C_LCD_driver.py ga teging
nano I2C_LCD_driver.py
Quyidagi tarkibni yangi yaratilgan python skriptiga joylashtiring.
#-*-kodlash: utf-8-*- # Asl kod topilgan manzil: #
"""
Compiled, mashed and generally mutilated 2014-2015 by Denis Pleic Made available under GNU GENERAL PUBLIC LICENSE
# Modified Python I2C library for Raspberry Pi
# as found on https://gist.github.com/DenisFromHR/cc863375a6e19… # Joined existing 'i2c_lib.py' and 'lcddriver.py' into a single library # added bits and pieces from various sources # By DenisFromHR (Denis Pleic) # 2015-02-10, ver 0.1
"""
# i2c bus (0 -- original Pi, 1 -- Rev 2 Pi)
I2CBUS = 0
# LCD Address
ADDRESS = 0x27
import smbus
from time import sleep
class i2c_device:
def _init_(self, addr, port=I2CBUS): self.addr = addr self.bus = smbus. SMBus(port)
# Write a single command
def write_cmd(self, cmd): self.bus.write_byte(self.addr, cmd) sleep(0.0001)
# Write a command and argument
def write_cmd_arg(self, cmd, data): self.bus.write_byte_data(self.addr, cmd, data) sleep(0.0001)
# Write a block of data
def write_block_data(self, cmd, data): self.bus.write_block_data(self.addr, cmd, data) sleep(0.0001)
# Read a single byte
def read(self): return self.bus.read_byte(self.addr)
# Read
def read_data(self, cmd): return self.bus.read_byte_data(self.addr, cmd)
# Read a block of data
def read_block_data(self, cmd): return self.bus.read_block_data(self.addr, cmd)
# commands
LCD_CLEARDISPLAY = 0x01 LCD_RETURNHOME = 0x02 LCD_ENTRYMODESET = 0x04 LCD_DISPLAYCONTROL = 0x08 LCD_CURSORSHIFT = 0x10 LCD_FUNCTIONSET = 0x20 LCD_SETCGRAMADDR = 0x40 LCD_SETDDRAMADDR = 0x80
# flags for display entry mode
LCD_ENTRYRIGHT = 0x00 LCD_ENTRYLEFT = 0x02 LCD_ENTRYSHIFTINCREMENT = 0x01 LCD_ENTRYSHIFTDECREMENT = 0x00
# flags for display on/off control
LCD_DISPLAYON = 0x04 LCD_DISPLAYOFF = 0x00 LCD_CURSORON = 0x02 LCD_CURSOROFF = 0x00 LCD_BLINKON = 0x01 LCD_BLINKOFF = 0x00
# flags for display/cursor shift
LCD_DISPLAYMOVE = 0x08 LCD_CURSORMOVE = 0x00 LCD_MOVERIGHT = 0x04 LCD_MOVELEFT = 0x00
# flags for function set
LCD_8BITMODE = 0x10 LCD_4BITMODE = 0x00 LCD_2LINE = 0x08 LCD_1LINE = 0x00 LCD_5x10DOTS = 0x04 LCD_5x8DOTS = 0x00
# flags for backlight control
LCD_BACKLIGHT = 0x08 LCD_NOBACKLIGHT = 0x00
En = 0b00000100 # Enable bit
Rw = 0b00000010 # Read/Write bit Rs = 0b00000001 # Register select bit
class lcd:
#initializes objects and lcd def _init_(self): self.lcd_device = i2c_device(ADDRESS)
self.lcd_write(0x03)
self.lcd_write(0x03) self.lcd_write(0x03) self.lcd_write(0x02)
self.lcd_write(LCD_FUNCTIONSET | LCD_2LINE | LCD_5x8DOTS | LCD_4BITMODE)
self.lcd_write(LCD_DISPLAYCONTROL | LCD_DISPLAYON) self.lcd_write(LCD_CLEARDISPLAY) self.lcd_write(LCD_ENTRYMODESET | LCD_ENTRYLEFT) sleep(0.2)
# clocks EN to latch command
def lcd_strobe(self, data): self.lcd_device.write_cmd(data | En | LCD_BACKLIGHT) sleep(.0005) self.lcd_device.write_cmd(((data & ~En) | LCD_BACKLIGHT)) sleep(.0001)
def lcd_write_four_bits(self, data):
self.lcd_device.write_cmd(data | LCD_BACKLIGHT) self.lcd_strobe(data)
# write a command to lcd
def lcd_write(self, cmd, mode=0): self.lcd_write_four_bits(mode | (cmd & 0xF0)) self.lcd_write_four_bits(mode | ((cmd << 4) & 0xF0))
# write a character to lcd (or character rom) 0x09: backlight | RS=DR< # works! def lcd_write_char(self, charvalue, mode=1): self.lcd_write_four_bits(mode | (charvalue & 0xF0)) self.lcd_write_four_bits(mode | ((charvalue << 4) & 0xF0)) # put string function with optional char positioning def lcd_display_string(self, string, line=1, pos=0): if line == 1: pos_new = pos elif line == 2: pos_new = 0x40 + pos elif line == 3: pos_new = 0x14 + pos elif line == 4: pos_new = 0x54 + pos
self.lcd_write(0x80 + pos_new)
for char in string:
self.lcd_write(ord(char), Rs)
# clear lcd and set to home
def lcd_clear(self): self.lcd_write(LCD_CLEARDISPLAY) self.lcd_write(LCD_RETURNHOME)
# define backlight on/off (lcd.backlight(1); off= lcd.backlight(0)
def backlight(self, state): # for state, 1 = on, 0 = off if state == 1: self.lcd_device.write_cmd(LCD_BACKLIGHT) elif state == 0: self.lcd_device.write_cmd(LCD_NOBACKLIGHT)
# add custom characters (0 - 7)
def lcd_load_custom_chars(self, fontdata): self.lcd_write(0x40); for char in fontdata: for line in char: self.lcd_write_char(line)
The address in that content assumes your LCD address is 0x27. If this is not the case for you, you will need to change it on the line "ADDRESS = 0x27" before you type Ctrl+X to save and exit. Otherwise, just save and exit. This file will need to exist in the same directory as the script that we will use later.
That code was on "https://www.circuitbasics.com/raspberry-pi-i2c-lcd-set-up-and-programming/" just in case it did not paste correctly onto this page.
Now create and edit the main python script:
touch covid19.py
nano covid19.py
Paste the below text into your newly created python script.
import I2C_LCD_driverimport socket import time import fcntl import struct import json import urllib2
display = I2C_LCD_driver.lcd()
url = ("https://coronavirus-19-api.herokuapp.com/countries/usa") data = json.load(urllib2.urlopen(url))
try:
while True: data = json.load(urllib2.urlopen(url)) cases = data['cases'] todaycases = data['todayCases'] deaths = data['deaths'] todaydeaths = data['todayDeaths'] recovered = data['recovered'] active = data['active'] critical = data['critical'] casesperonemillion = data['casesPerOneMillion'] deathsperonemillion = data['deathsPerOneMillion'] display.lcd_display_string("COVID-19 Total Stats", 1) display.lcd_display_string("Cases: " + str(cases), 2) display.lcd_display_string("Deaths: " + str(deaths), 3) display.lcd_display_string("Recovered: " + str(recovered), 4) time.sleep(30) display.lcd_display_string(" ", 1) display.lcd_display_string(" ", 2) display.lcd_display_string(" ", 3) display.lcd_display_string(" ", 4) display.lcd_display_string("COVID-19: " + "%s" %time.strftime("%Y/%m/%d"), 1) display.lcd_display_string("Cases: " + str(todaycases), 2) display.lcd_display_string("Deaths: " + str(todaydeaths), 3) display.lcd_display_string("Active: " + str(active), 4) time.sleep(20) display.lcd_display_string(" ", 1) display.lcd_display_string(" ", 2) display.lcd_display_string(" ", 3) display.lcd_display_string(" str(recovered),="">
Bilaman, bu skript juda chalkash, lekin u samarali. U Qo'shma Shtatlardagi Covid-19 holatlarining hozirgi statistikasini ko'rsatadi. Asosiy ma'lumotlar bazasi har 5 daqiqada yangilanadi. Mening skriptim 3 sahifani to'liq aylantirish uchun 1 daqiqa vaqtni oladi va tsikl qayta boshlanganda yangilangan raqamlarni tortib oladi.
4 -qadam: Python -ni ishga tushiring
Boshlaylik:
python covid19.py
Birinchi sahifada koronavirus mamlakatga birinchi marta kirib kelganidan beri holatlar va o'limlarning umumiy soni ko'rsatilgan. Ikkinchi sahifada faqat shu kuni sodir bo'lgan holatlar va o'limlar soni ko'rsatilgan. Uchinchisi og'ir ahvolda bo'lgan odamlarni, so'ngra har million kishiga to'g'ri keladigan holatlar va o'limlarni ko'rsatadi. Uchinchi sahifadagi ikkinchi satrda mamlakatdagi birinchi ishning sanasi ko'rsatilardi, lekin men uni olib tashlashga majbur bo'ldim, chunki skriptda ba'zan xato bo'ladi va bu satrni xato bilan ko'rsatib qo'yadi.
Bu skriptni avtomatik ishga tushirish usullari bor, lekin men bu erda bu haqda batafsil ma'lumot bermayman. Men SSH -ga PuTTY orqali ulanganimdan so'ng, o'z buyrug'imni bajaraman. Ishlayotganda, siz Ctrl+C tugmalar birikmasini bosmaguningizcha boshqa buyruqlarni bajara olmaysiz.
5 -qadam: Agar men AQShda yashamasam nima bo'ladi?
Bu skriptni boshqa davlatlar statistikasini ko'rsatish uchun o'zgartirish mumkin. Ko'rib turganingizdek, mening skriptimdagi URL bu erda APIdan olinadi: (bu sahifalarni ko'rish uchun Internet Explorer -dan foydalanmang. U.json faylini yuklashga harakat qiladi. Men Chrome ishlatganman)
coronavirus-19-api.herokuapp.com/countries/usa
Endi o'sha manzilga tashrif buyuring, lekin bitta papka yuqoriroq
coronavirus-19-api.herokuapp.com/countries
Bu har bir mamlakat uchun statistikani ko'rsatadi. Shubhasiz, bu sahifadan API ma'lumotlarini olib tashlashga urinish kabus bo'ladi. Shunday qilib, o'z mamlakatingiz uchun sahifani ochgan ma'qul. Kanadadagi do'stlarimiz skriptni ushbu URL manziliga o'zgartirishlari kerak bo'ladi:
coronavirus-19-api.herokuapp.com/countries/canada
Bu erda juda muhim eslatma. API uchun URL aniq bo'lishi kerak … ya'ni URLda bo'sh joy yo'q. Veb -ko'rishda veb -manzildagi bo'sh joylar "%20" bilan almashtiriladi va aytilganidek, 2 qismli mamlakatlardagi do'stlarimiz, masalan, Yangi Zelandiya, bu skriptdagi URLni quyidagicha o'zgartirishi kerak bo'ladi.
coronavirus-19-api.herokuapp.com/countries/new%20zealand
6 -qadam: Yakuniy fikrlar
Men ko'p yillar davomida Raspberry Pi va Arduino bilan ko'p ishlarni qildim, lekin men qurgan narsalarning aksariyati boshqalarning g'oyalarini takrorlashdir. Bu deyarli bir xil, agar men ushbu sozlamaga ko'plab manbalardan ma'lumotlarni to'plagan bo'lsam. Garchi bu sozlash sizni og‘ir vaqtda xavfsiz va sog‘lom saqlashga imkon bermasa -da, lekin, albatta, siz uni o‘rnatganingizda sizni band qilib qo‘yadi va bundan keyin sizni xabardor qiladi.
Agar sizda bu qismlar yo'q bo'lsa, uni qurishga jiddiy yondashmasangiz, o'zingizni sotib olishga zo'r bermang. Men ilgari aytganimdek, yuk tashish vaqtlari endi uzayib bormoqda, chunki bu harakatlar asosiy narsalarga qaratilgan. Menda bu qismlar faqat o'rganish va tajriba uchun bor edi. Qutiga o'rnatilgan displey dastlab Pi-Hole ishlaydigan mening tarmog'imdagi boshqa Raspberry Pi statistikasini ko'rish uchun o'rnatildi. Ushbu Covid-19 tadbiri tugagandan so'ng, men uni ob-havo displeyiga aylantirishim mumkin.
O'qiganlar uchun men ushbu ko'rsatmaga baqirishni xohlayman:
www.instructables.com/id/DIY-Hand-Sanitize…
Men hali sinab ko'rmadim, lekin menda aniq ingredientlar bor va men buni bir muncha vaqt sinab ko'rishim mumkin.
Tavsiya:
Raspberry Pi - HIH6130 I2C namlik va harorat sensori Python darsligi: 4 qadam
Raspberry Pi - HIH6130 I2C namlik va harorat sensori Python qo'llanmasi: HIH6130 - bu raqamli chiqishga ega namlik va harorat sensori. Bu sensorlar ± 4% RH aniqlik darajasini ta'minlaydi. Sanoatning etakchi uzoq muddatli barqarorligi, haqiqiy harorat kompensatsiyalangan raqamli I2C, sanoatning etakchi ishonchliligi, energiya samaradorligi
Raspberry Pi - HIH6130 I2C namlik va harorat sensori Java darsligi: 4 qadam
Raspberry Pi - HIH6130 I2C namlik va harorat sensori Java darsligi: HIH6130 - bu raqamli chiqishga ega namlik va harorat sensori. Bu sensorlar ± 4% RH aniqlik darajasini ta'minlaydi. Sanoatning etakchi uzoq muddatli barqarorligi, haqiqiy harorat kompensatsiyalangan raqamli I2C, sanoatning etakchi ishonchliligi, energiya samaradorligi
Raspberry PI bir nechta I2C qurilmalari: 3 qadam
Raspberry PI bir nechta I2C qurilmalari: Sizning loyihangizda bir xil I2C qurilmalarining ko'pini ishlata olmaganingiz uchun xafa bo'ldingiz. Sekin multipleksorlardan foydalanish shart emas. Eng yangi raspbian yadrosi GPIO pinlari yordamida bir nechta I2C avtobuslarini yaratishni qo'llab -quvvatlaydi. Bu yechim juda tez