Cov txheej txheem:

Covid-19 Stats + Raspberry Pi + I2C LCD: 6 Kauj Ruam
Covid-19 Stats + Raspberry Pi + I2C LCD: 6 Kauj Ruam

Video: Covid-19 Stats + Raspberry Pi + I2C LCD: 6 Kauj Ruam

Video: Covid-19 Stats + Raspberry Pi + I2C LCD: 6 Kauj Ruam
Video: Bayes Theorem Calculator 3/3 2024, Hlis ntuj nqeg
Anonim
Covid-19 Stats + Raspberry Pi + I2C LCD
Covid-19 Stats + Raspberry Pi + I2C LCD

Yog li ntawd tawm ntawm xiav ib hnub, Kuv txiav txim siab kom tau ob peb ntu kuv tau dag ib ncig thiab ua qee yam uas yuav xa kuv cov txheeb cais lub sijhawm tiag ntawm Covid-19. Kuv tsis tau siv sijhawm ntau los ua kom nws zoo nkauj vim li cas thiaj ua qee yam ruaj khov thaum qhov xwm txheej no tsis mus? Yog li ntawd, kuv cov zaub tsuas yog ntsia rau hauv lub thawv ntawv me me.

Qhov xav tau:

  • Raspberry Pi - txhua tus qauv. Kuv siv Raspberry Pi 3A+
  • 20x4 I2C LCD Zaub - tsis muaj hom tshwj xeeb … tab sis xav tau I2C hnab ev khoom
  • Poj niam rau poj niam jumper xov hlau - Tsuas yog 4 ntawm lawv txhawm rau txuas I2C rau Pi

www.adafruit.com/product/4027

www.amazon.com/gp/product/B01GPUMP9C/ref=p…

www.amazon.com/gp/product/B01L5ULRUA/ref=p…

Cov kev txuas no ncaj qha mus rau cov peev txheej uas kuv tau yuav los ntawm. Thov txim kom hais tias Adafruit tsis xa tam sim no, tab sis Amazon yog … tsuas yog qeeb vim tias lawv lub hom phiaj tseem ceeb yog ua rau cov khoom tseem ceeb, uas tsis yog. Txhua yam tuaj yeem nrhiav pom lwm qhov ntawm Amazon thiab eBay.

Koj yuav pom tseeb xav tau AC adapter, USB cable, thiab daim npav microSD mus nrog txhua qhov no.

Kauj Ruam 1: Teeb Kho Vajtse

Kho vajtse teeb
Kho vajtse teeb

Siv daim duab txuas txuas txuas. Nws hais tias B+, tab sis nws siv rau txhua lwm yam Raspberry Pi tus qauv uas tau los tom qab tus ntawd ib yam nkaus.

Nrog I2C lub hnab ev ntawv txuas rau lub vijtsam LCD, qhov txuas no tsuas yog xav tau 4 xov hlau los ua haujlwm.

Txuas GND mus rau ib qho ntawm tus pin hauv av ntawm Raspberry Pi: Pin 6, 9, 14, 20, 25, 30, 34, 39. Kuv txuas nws rau tus pin 6.

Txuas VCC mus rau ib qho ntawm 5 volt pins ntawm Raspberry Pi: Pin 2, 4. Kuv siv tus pin 4

Txuas SDA rau tus pin 3.

Txuas SCL rau tus Pin 5.

Yog tias koj ua raws li kuv teeb tsa, koj yuav xaus nrog txhua 4 xov hlau nyob hauv 2x2 tus qauv ntawm GPIO lub taub hau.

Koj txoj kev teeb tsa tuaj yeem yog txhua yam koj tuaj yeem xav … lossis tsis muaj dab tsi hlo li. Raws li kuv tau hais hauv cov lus qhia, hom kab mob coronavirus no yuav tsis nyob mus ib txhis, yog li kuv tsis xav tau kuv teeb tsa rau. Yog tias kuv txiav txim siab khaws qhov teeb tsa no tom qab qhov xwm txheej no dhau mus, kuv yuav hloov nws mus rau hauv huab cua lossis lwm yam.

Kuv txuas cov txiv ntoo thiab ntsia liaj qhov rooj nrog rau nylon spacers rau txhua 4 lub ces kaum ntawm kuv tus Pi 3A+. Qhov no yog nruj me ntsis xaiv. Kuv tau ua qhov no vim tias qee zaum kuv muaj qhov no ntawm cov hlau, tsis nyiam kom kuv teeb tsa ib ntus ntawm Pi uas yog sab hauv ntawm rooj plaub, thiab tsis xav kom pheej hmoo ua rau nws tawg vim kuv tsis hnov qab tshem nws tawm ntawm cov hlau nto ua ntej fais fab nws rau.

Kauj ruam 2: Pi Software Teeb

Pi Software teeb tsa
Pi Software teeb tsa
Pi Software teeb tsa
Pi Software teeb tsa
Pi Software teeb tsa
Pi Software teeb tsa

Raws li kuv tau hais hauv cov lus qhia, nws tsis muaj teeb meem dab tsi Raspberry Pi tus qauv koj siv. Kuv tab tom siv qhov no ntawm Raspberry Pi 3A+ hla WiFi tab sis tseem tau sim qhov no ntawm Raspberry Pi 2 ntawm ethernet cable, thiab Raspberry Pi Zero version 1.3 (thawj Pi Zero nrog lub koob yees duab txuas txuas) nrog USB WiFi dongle.

Kuv yuav tsis ntaus tawm yuav ua li cas rau nruab Raspbian rau hauv daim npav MicroSD vim tias muaj ntau lab cov lus qhia yuav ua li cas. Kuv muaj 16GB microSD khiav Raspbian Buster Lite. Ntawm ib sab ntawv, Kuv yuav luag ib txwm siv Raspbian Lite vim tias kuv tsis xav tau lwm cov software tsis muaj txiaj ntsig hauv ib qho ntawm kuv cov phiaj xwm. Yog tias kuv teeb tsa software siv apt-get, nws yuav teeb tsa yam yuav tsum tau ua ua ntej.

Txuas mus rau lub network. Ib zaug ntxiv, muaj ntau lab cov lus qhia tawm yuav ua li cas ua li no, yog li kuv yuav tsis mus rau qhov tob ntawm no. Koj tuaj yeem mus txuas lossis wireless, tab sis qhov no yuav xav tau kev sib txuas hauv is taws nem.

Yeem tau, tab sis koj tuaj yeem pab SSH txuas nrog siv PuTTY. Kuv ua.

Hloov kho txhua yam ces rov pib dua:

sudo apt hloov tshiab

sudo apt upgrade -y sudo apt dist-upgrade sudo rpi-hloov tshiab sudo reboot

Nov yog ib qho teeb tsa uas kuv yuav hla ntawm no. Ib zaug ntxiv, muaj ntau lab txoj hauv kev los ua qhov no, tab sis qhov siv tau zoo tshaj plaws kuv pom yog nyob ntawm no:

Nov yog cov ntsiab lus tseem ceeb:

sudo apt nruab i2c-cuab yeej

sudo apt nruab python-smbus

Koj tseem yuav xav tau qhib I2C

sudo raspi-teeb tsa

- 5 Kev Xaiv Ua Haujlwm

-P5 I2C

Rov pib dua los siv cov kev hloov pauv

sudo rov pib dua

Tam sim no nws yog lub sijhawm los saib yog tias koj tau ua txhua yam kom raug kom deb li deb

i2cdetect -y 1

Yog tias koj lub teeb pom kev zoo thiab tuaj yeem pom los ntawm koj tus Raspberry Pi, koj yuav muaj daim ntawv qhia uas los. Qhov chaw nyob rau 20x4 Kuv yuav ntawm Amazon thiab siv rau qhov haujlwm no yog 27. Txuj ci qhov no yuav txheeb xyuas raws li 0x27 rau tus nab nab ntawv uas yuav los tom qab. Kuv tau muaj qhov chaw nyob zoo ib yam rau 2 16x2 cov lus Kuv tseem yuav ntawm Amazon thiab ib qho 40x2 kuv pom ntawm eBay.

Kauj Ruam 3: Teeb Python

Yog li tam sim no rau cov khoom nyuaj. Kuv yuav sim ua kom yooj yim li kuv ua tau. Txog rau pib, Kuv yuav tsuas yog sau cov ntawv rau hauv phau ntawv qhia tsev.

kov I2C_LCD_driver.py

nano I2C_LCD_driver.py

Muab cov ntsiab lus hauv qab no tso rau hauv koj tsab ntawv tsim nab nab.

#-*-coding: utf-8-*- # Thawj tus lej pom ntawm: #

"""

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),="">

Kuv paub tsab ntawv no tsis zoo nkauj, tab sis nws muaj txiaj ntsig. Nws yuav qhia txog kev txheeb cais tam sim no rau Covid-19 mob hauv Tebchaws Meskas. Lub hauv paus tseem ceeb tau hloov kho txhua txhua 5 feeb. Kuv tsab ntawv yuav siv sijhawm 1 feeb kom tiav mus txog 3 nplooj ntawv thiab yuav rub tus lej tshiab txhua lub sijhawm pib pib dua.

Kauj ruam 4: Khiav Python

Khiav Python
Khiav Python
Khiav Python
Khiav Python

Cia peb pib:

python covid19.py

Thawj nplooj ntawv qhia tag nrho cov neeg mob thiab tuag txij li tus mob coronavirus tau ntaus thawj lub tebchaws. Nplooj ntawv thib ob qhia cov lej ntawd rau tus neeg mob thiab tuag uas tau tshwm sim tsuas yog nyob rau niaj hnub no. Qhov thib peb qhia tib neeg nyob rau qhov xwm txheej tseem ceeb, tom qab ntawd kis thiab tuag rau ib lab tus tib neeg. Kab thib ob ntawm nplooj ntawv thib peb tau siv los qhia hnub tim ntawm thawj kis hauv lub tebchaws, tabsis kuv yuav tsum tshem nws tawm vim tias tsab ntawv yuav qee zaum yuam kev tawm thiab tsoo vim hais tias kab ntawv muaj qhov ua yuam kev.

Muaj ntau txoj hauv kev los ua tsab ntawv no sau cia, tab sis kuv yuav tsis mus rau hauv cov ntsiab lus hais txog ntawm no. Kuv tsuas yog khiav kuv qhov kev txib tom qab kuv SSH txuas rau nws los ntawm PuTTY. Thaum nws tab tom khiav, koj yuav tsis tuaj yeem ua lwm yam lus txib kom txog rau thaum koj nias Ctrl+C.

Kauj Ruam 5: Yuav Ua Li Cas Yog Kuv Tsis Nyob Hauv Tebchaws Meskas?

Tsab ntawv no tuaj yeem hloov kho los qhia stats rau lwm lub tebchaws. Raws li koj tuaj yeem pom, URL hauv kuv tsab ntawv rub los ntawm API ntawm no: (tsis txhob siv Internet Explorer los saib cov nplooj ntawv no. Nws yuav sim dowload cov ntaub ntawv.json. Kuv siv Chrome)

coronavirus-19-api.herokuapp.com/countries/usa

Tam sim no mus ntsib tib qhov chaw nyob, tab sis ib daim nplaub tshev siab dua

coronavirus-19-api.herokuapp.com/countries

Qhov no teev cov txheeb cais rau txhua lub tebchaws. Pom tseeb nws yuav yog npau suav phem sim rub API cov ntaub ntawv los ntawm nplooj ntawv no. Yog li nws zoo tshaj qhib nplooj ntawv rau koj lub tebchaws tshwj xeeb. Peb cov phooj ywg hauv tebchaws Canada yuav tsum tau kho tsab ntawv mus rau qhov URL no:

coronavirus-19-api.herokuapp.com/countries/canada

Tseem ceeb heev ntawm no. Qhov URL rau API xav tau tshwj xeeb… lub ntsiab lus tsis muaj chaw nyob hauv URL. Hauv kev tshawb xyuas lub vev xaib, cov chaw hauv lub vev xaib tau hloov pauv los ntawm "%20" thiab nrog qhov hais tias, peb cov phooj ywg hauv tebchaws uas muaj 2 ntu npe, xws li New Zealand piv txwv, yuav xav hloov qhov URL hauv tsab ntawv no nrog:

coronavirus-19-api.herokuapp.com/countries/new%20zealand

Kauj Ruam 6: Kev Xav Thaum Kawg

Kuv tau ua ntau yam nrog Raspberry Pi thiab Arduino ntau xyoo, tab sis feem ntau ntawm qhov kuv tau ua yog tsuas yog rov ua dua ntawm lwm tus lub tswv yim. Qhov no yuav luag zoo ib yam tsuas yog kuv suav ua ke los ntawm ntau qhov chaw rau hauv qhov teeb tsa no. Txawm hais tias qhov teeb tsa no yuav tsis ua rau koj nyab xeeb thiab noj qab nyob zoo nyob rau lub sijhawm nyuaj no, nws yuav muaj tseeb ua rau koj nyob raws li koj teeb tsa nws thiab nws yuav qhia rau koj paub tom qab.

Yog tias koj tsis muaj cov ntu no, tsis txhob nyuab siab koj tus kheej yuav lawv tshwj tsis yog koj mob siab rau tsim nws. Raws li kuv tau hais ua ntej, lub sijhawm thauj khoom tau siv sijhawm ntev dua tam sim no vim tias cov kev siv zog tau muab tso rau cov khoom tseem ceeb. Kuv tsuas muaj cov ntu no twb tau kawm thiab sim ua lawm. Lub thawv teeb tsa tau teeb tsa thaum xub thawj los txheeb xyuas lub sijhawm tiag tiag ntawm lwm tus Raspberry Pi ntawm kuv lub network uas tau khiav Pi-Qhov. Tom qab Covid-19 qhov xwm txheej no dhau mus, Kuv yuav hloov nws mus ua huab cua.

Rau txhua tus neeg nyeem, Kuv xav ua suab nrov rau cov lus qhia no:

www.instructables.com/id/DIY-Hand-Sanitize…

Kuv tseem tsis tau sim nws, tab sis kuv muaj cov khoom xyaw ntawd, thiab kuv yuav sim nws qee lub sijhawm.

Pom zoo: