Orodha ya maudhui:

Takwimu za Covid-19 + Raspberry Pi + I2C LCD: 6 Hatua
Takwimu za Covid-19 + Raspberry Pi + I2C LCD: 6 Hatua

Video: Takwimu za Covid-19 + Raspberry Pi + I2C LCD: 6 Hatua

Video: Takwimu za Covid-19 + Raspberry Pi + I2C LCD: 6 Hatua
Video: Сводные таблицы Excel с нуля до профи за полчаса + Дэшборды! | 1-ое Видео курса "Сводные Таблицы" 2024, Novemba
Anonim
Takwimu za Covid-19 + Raspberry Pi + I2C LCD
Takwimu za Covid-19 + Raspberry Pi + I2C LCD

Kwa hiari siku moja kutoka kwa bluu siku moja, niliamua kupata sehemu kadhaa ambazo nilikuwa nimelala karibu na kutengeneza kitu ambacho kitanipa takwimu za wakati halisi juu ya Covid-19. Sikuweka muda mwingi kuifanya ionekane nzuri kwa sababu kwanini ufanye kitu cha kudumu wakati hafla hii haitakuwapo? Kwa hivyo, onyesho langu limewekwa kwenye sanduku ndogo la kadibodi.

Sehemu zinahitajika:

  • Raspberry Pi - mfano wowote. Nilitumia Raspberry Pi 3A +
  • Onyesho la LCD la 20x4 I2C - hakuna chapa fulani… lakini inahitaji mkoba wa I2C
  • Waya wa kike na wa kike wa kuruka - 4 tu kati yao kuunganisha I2C na Pi

www.adafruit.com/product/4027

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

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

Viungo hivi huenda moja kwa moja kwenye vyanzo ambavyo nilinunua kutoka. Samahani kusema kwamba Adafruit haitoi sasa hivi, lakini Amazon ni… pole pole kwa sababu ya kulenga kwao kuu kwa vitu muhimu, ambavyo sio hivyo. Zote zinaweza kupatikana mahali pengine kwenye Amazon na eBay.

Kwa wazi utahitaji adapta ya AC, kebo ya USB, na kadi ya MicroSD kwenda na haya yote.

Hatua ya 1: Usanidi wa vifaa

Usanidi wa Vifaa
Usanidi wa Vifaa

Rejelea picha iliyoambatanishwa. Inasema B +, lakini inatumika kwa kila mfano mwingine wa Raspberry Pi ambao umekuja baada ya hiyo pia.

Pamoja na mkoba wa I2C ulioambatanishwa na onyesho la LCD, unganisho hili linahitaji tu waya 4 kufanya kazi.

Unganisha GND kwa pini yoyote ya ardhi kwenye Raspberry Pi: Pini 6, 9, 14, 20, 25, 30, 34, 39. Niliiunganisha na kubandika 6.

Unganisha VCC kwa moja ya pini 5 za volt kwenye Raspberry Pi: Pin 2, 4. Nilitumia pin 4

Unganisha SDA kubandika 3.

Unganisha SCL kwa Pin 5.

Ikiwa ulifuata usanidi wangu, utaishia na waya zote 4 kwa muundo wa 2x2 kwenye vichwa vya GPIO.

Njia yako ya kuongezeka inaweza kuwa chochote unaweza kufikiria… au hakuna chochote. Kama nilivyosema kwenye utangulizi, shida hii ya coronavirus haitadumu milele, kwa hivyo siitaji usanidi wangu. Ikiwa nitaamua kuweka usanidi huu baada ya hafla hii kumalizika, naweza kuigeuza kuwa onyesho la hali ya hewa au kitu.

Niliunganisha nati na bolt pamoja na spacers za nailoni kwa pembe zote 4 za Pi 3A yangu. Hii ni ya lazima. Nilifanya hivi kwa sababu wakati mwingine huwa na hii juu ya uso wa chuma, sikupenda kuwa na mipangilio yangu ya muda kwenye Pi iliyo ndani ya kesi, na sitaki kuhatarisha kuisumbua kwa sababu nilisahau kuiondoa kwenye chuma uso kabla ya kuwasha.

Hatua ya 2: Usanidi wa Programu ya Pi

Usanidi wa Programu ya Pi
Usanidi wa Programu ya Pi
Usanidi wa Programu ya Pi
Usanidi wa Programu ya Pi
Usanidi wa Programu ya Pi
Usanidi wa Programu ya Pi

Kama nilivyosema kwenye utangulizi, haijalishi unatumia mfano gani wa Raspberry Pi. Ninatumia hii kwenye Raspberry Pi 3A + juu ya WiFi lakini pia nimejaribu hii kwenye Raspberry Pi 2 kwenye kebo ya ethernet, na toleo la Raspberry Pi Zero 1.3 (Pi Zero ya kwanza kabisa na kiunganishi cha kamera ya serial) na dongle ya USB WiFi.

Sitaandika jinsi ya kusanidi Raspbian kwenye kadi ya MicroSD kwa sababu kuna maagizo ya mamilioni ya jinsi ya kufanya hivyo. Nina 16GB microSD inayoendesha Raspbian Buster Lite. Kwa kumbuka upande, karibu kila wakati ninatumia Raspbian Lite kwa sababu sihitaji vifurushi vingine vya programu visivyo na maana katika miradi yangu yoyote. Ikiwa ninasakinisha programu kwa kutumia upataji mzuri, itasakinisha mahitaji ya kukosa.

Unganisha kwenye mtandao. Tena, kuna mamilioni ya maagizo huko nje juu ya jinsi ya kufanya hivyo, kwa hivyo sitaenda kwa kina hapa. Unaweza kwenda kwa waya au bila waya, lakini hii itahitaji unganisho la mtandao.

Hiari, lakini unaweza kuwezesha SSH kuungana kwa kutumia PuTTY. Nilifanya.

Sasisha kila kitu kisha uwashe upya:

sasisho la sudo apt

Sudo apt kuboresha -sudo apt dist-kuboresha sudo rpi-sasisha sudo reboot

Hii ni usanidi mmoja ambao nitapitia hapa. Tena, kuna mamilioni ya njia za kufanya hivyo, lakini rejea bora niliyoipata ni hapa:

Hapa kuna mambo muhimu:

Sudo apt kufunga i2c-zana

Sudo apt kufunga python-smbus

Utahitaji pia kuwezesha I2C

Sudo raspi-config

- 5 Interfacing Chaguzi

- P5 I2C

Anzisha upya ili utumie mabadiliko

Sudo reboot

Sasa ni wakati wa kuona ikiwa umefanya haya yote kwa usahihi hadi sasa

i2cdectect -y 1

Ikiwa onyesho lako linawezeshwa na linaweza kuonekana na Raspberry yako ya Pi, utakuwa na chati inayokuja. Anwani ya 20x4 niliyonunua kwenye Amazon na kutumia mradi huu ni 27. Kitaalam hii itatambua kama 0x27 kwa hati za chatu ambazo zitakuja baadaye. Nimekuwa na onyesho sawa la anwani kwa maonyesho 2 16x2 pia nilinunua kwenye Amazon na 40x2 moja nimepata kwenye eBay.

Hatua ya 3: Usanidi wa Python

Kwa hivyo sasa kwa vitu ngumu. Nitajaribu kuiweka rahisi kama ninavyoweza. Kwa kuanzia, nitaandika faili kwenye saraka ya nyumbani.

gusa I2C_LCD_driver.py

nano I2C_LCD_driver.py

Bandika yaliyomo hapo chini kwenye hati yako mpya ya chatu.

# - * - kuorodhesha: utf-8 - * - # Nambari halisi inapatikana kwenye: #

"""

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

Najua hati hii ni mbaya sana, lakini ni nzuri. Itaonyesha takwimu za sasa za kesi za Covid-19 huko Merika. Hifadhidata kuu inasasishwa kila dakika 5. Hati yangu inachukua dakika 1 kuzungusha kabisa kurasa 3 na itavuta nambari zilizosasishwa kila wakati mzunguko unapoanza tena.

Hatua ya 4: Run Python

Run Python
Run Python
Run Python
Run Python

Wacha tuanze:

chatu covid19.py

Ukurasa wa kwanza unaonyesha jumla ya visa na vifo tangu coronavirus ilipotokea nchini mara ya kwanza. Ukurasa wa pili unaonyesha nambari hizo za visa na vifo ambavyo vilitokea tu kwa siku ya sasa. Ya tatu inaonyesha watu walio katika hali mbaya, na kesi na vifo kwa watu milioni moja. Mstari wa pili kwenye ukurasa wa tatu ulitumia kuonyesha tarehe ya kesi ya kwanza nchini, lakini ilibidi niiondoe kwa sababu hati hiyo wakati mwingine ilikosea na ikaanguka ikinukuu mstari huo na kosa.

Kuna njia za kufanya hati hii iendeshwe kiatomati, lakini sitaenda kwa maelezo kuhusu hilo hapa. Ninaendesha tu yangu kwa amri baada ya mimi SSH kuungana nayo kupitia PuTTY. Wakati inaendesha, hautaweza kutekeleza amri zingine zozote mpaka ubonyeze Ctrl + C.

Hatua ya 5: Je! Ikiwa Sitaishi USA?

Hati hii inaweza kubadilishwa ili kuonyesha takwimu kwa nchi nyingine. Kama unavyoona, URL katika hati yangu inavuta kutoka kwa API hapa: (usitumie Internet Explorer kutazama kurasa hizi. Itajaribu kupakua faili ya.json. Nilitumia Chrome)

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

Sasa tembelea anwani hiyo hiyo, lakini folda moja juu

coronavirus-19-api.herokuapp.com/countries

Hii inaorodhesha takwimu kwa kila nchi. Kwa wazi itakuwa ndoto ya kujaribu kujaribu kuvuta data ya API kutoka ukurasa huu. Kwa hivyo ni bora kufungua ukurasa kwa nchi yako maalum. Rafiki zetu nchini Canada watahitaji kuhariri hati kwenda kwa URL hii:

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

Ujumbe muhimu sana hapa. URL ya API inahitaji kuwa maalum… ikimaanisha hakuna nafasi katika URL. Katika kuvinjari wavuti, nafasi katika anwani ya wavuti hubadilishwa na "% 20" na kwa kusema hivyo, marafiki wetu katika nchi zilizo na majina ya sehemu 2, kama New Zealand kwa mfano, watahitaji kubadilisha URL katika hati hii na:

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

Hatua ya 6: Mawazo ya Mwisho

Nimefanya mambo mengi na Raspberry Pi na Arduino zaidi ya miaka, lakini mengi ya yale ambayo nimejenga ni mfano tu wa maoni ya wengine. Hii ni karibu sawa isipokuwa nilikusanya vipande kutoka vyanzo vingi kwenye usanidi huu. Ingawa usanidi huu hautakuweka salama na mwenye afya wakati huu mgumu, hakika utakushika unapoiweka na itakujulisha baadaye.

Ikiwa tayari hauna sehemu hizi, usijisumbue mwenyewe kuzinunua isipokuwa una nia ya kuijenga. Kama nilivyosema hapo awali, nyakati za usafirishaji zinachukua muda mrefu sasa kwa sababu juhudi hizo zinawekwa kwa vitu muhimu. Nilikuwa na sehemu hizi tu tayari za kujifunza na kujaribu. Sanduku lililowekwa hapo awali liliwekwa ili kutazama takwimu za wakati halisi wa Raspberry Pi nyingine kwenye mtandao wangu ambao unaendesha Pi-Hole. Baada ya hafla hii ya Covid-19 kumalizika, naweza kuigeuza kuwa onyesho la hali ya hewa.

Kwa mtu yeyote anayesoma, ninataka kutoa kelele kwa huyu anayefundishwa:

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

Bado sijaijaribu, lakini nina viungo hivyo halisi, na nitajaribu kwa muda.

Ilipendekeza: