Amir Pournasserian
Published © MIT

COVID 19 IoT Dashboard w/ Raspberry Pi

Choose the COVID 19 data that is most important to you and monitor that data with ease using uBeac.

BeginnerShowcase (no instructions)1 hour3,266
COVID 19 IoT Dashboard w/ Raspberry Pi

Things used in this project

Story

Read more

Code

main.py

Python
Sends data to uBeac
import json 
import threading
import http.client
from world_cases_collector import getting_world_value

# Configuration section
UBEAC_URL = 'hub.ubeac.io'
GATEWAY_URL = 'INSERT GATEWAY URL HERE'
DEVICE_FRIENDLY_NAME = 'World COVID19 Tracker'
SENT_INTERVAL = 900 # Sent data interval in second

day = False
date = input("Update for Today or Yesterday? (T/Y) : ")
if date == 'T':
    day = True
else:
    day = False

def main():
    threading.Timer(SENT_INTERVAL, main).start()
    device_world = [{
        'id': DEVICE_FRIENDLY_NAME,
        'sensors': getting_world_value(day)
        }]    

    connection = http.client.HTTPSConnection(UBEAC_URL)
    connection.request('POST', GATEWAY_URL, json.dumps(device_world))
    response = connection.getresponse()
    print(response.read().decode())           

if __name__ == '__main__':
    main()

world_cases_collector

Python
Collects COVID 19 data from online
from bs4 import BeautifulSoup as bf
import requests
import _const_cases as const

num_places = 220 #number of countries

def getting_world_value(today): #getting the value from website
    data_list = []
    html = requests.get("https://www.worldometers.info/coronavirus")
    soup = bf(html.text,'html.parser')
    if today:
        tag = soup("tr")[9:9 + num_places]
    else:
        tag = soup("tr")[239:239 + num_places]

    def extract_vals(arr):
        temp_list = []
        arr_size = len(arr) - 2
        for j in range(arr_size):
            if j == 1:
                temp_list.append(arr.contents[j].contents[0].contents[0])
            elif j % 2 == 1:
                value = arr.contents[j].contents
                if len(value) == 0:
                    value.append('0')
                value = value[0]
                value = value.replace("\n", "")
                value = value.replace("+", "")
                value = value.replace(",", "")
                value = value.replace("N/A", "")
                if len(value) == 0 or value == ' ':
                    value = '0'
                temp_list.append(value)
        return temp_list  
          
    compare_list = extract_vals(tag[-1])

    for i in range(len(tag)):        
        insert_list = extract_vals(tag[i])

        def continents(arg, day):
            if day:
                switcher = { 
                    212: 'North America', 
                    213: 'Europe', 
                    214: 'Asia',  
                    215: 'South America', 
                    216: 'Oceania', 
                    217: 'Africa', 
                    218: 'Unknown',
                    219: 'World',
                } 
            else:
                switcher = { 
                    211: 'Asia', 
                    212: 'North America', 
                    213: 'Europe',  
                    214: 'South America', 
                    215: 'Oceania', 
                    216: 'Africa', 
                    217: 'Unknown',
                    218: 'World'
                }
            return switcher.get(arg, insert_list[const.country])
        
        data_name = continents(i, today)
        data = {
            const.TOTAL_CASES : insert_list[const.w_total_cases],
            const.NEW_CASES : insert_list[const.w_new_cases],
            const.TOTAL_DEATHS : insert_list[const.w_total_deaths],
            const.NEW_DEATHS : insert_list[const.w_new_deaths],
            const.TOTAL_RECOVERED : insert_list[const.w_total_recovered],
            const.ACTIVE_CASES : insert_list[const.w_active_cases],
            const.SERIOUS_CRITICAL : insert_list[const.w_serious_critical],
            const.TOT_CASES_M : insert_list[const.w_tot_cases_M],
            const.DEATHS_M : insert_list[const.w_deaths_M],
            const.TOTAL_TESTS : insert_list[const.w_total_tests],
            const.TESTS_M : insert_list[const.w_tests_M],
            const.TOTAL_CASES_PERCENT : const.get_percentage(insert_list[const.w_total_cases],compare_list[const.w_total_cases]),
            const.NEW_CASES_PERCENT : const.get_percentage(insert_list[const.w_new_cases],compare_list[const.w_new_cases]),
            const.TOTAL_DEATHS_PERCENT : const.get_percentage(insert_list[const.w_total_deaths],compare_list[const.w_total_deaths]),
            const.NEW_DEATHS_PERCENT : const.get_percentage(insert_list[const.w_new_deaths],compare_list[const.w_new_deaths]),
            const.TOTAL_RECOVERED_PERCENT : const.get_percentage(insert_list[const.w_total_recovered],compare_list[const.w_total_recovered]),
            const.ACTIVE_CASES_PERCENT : const.get_percentage(insert_list[const.w_active_cases],compare_list[const.w_active_cases]),
            const.SERIOUS_CRITICAL_PERCENT : const.get_percentage(insert_list[const.w_serious_critical],compare_list[const.w_serious_critical]),
            const.DEATHS_VS_CASES : const.get_percentage(insert_list[const.w_total_deaths],insert_list[const.w_total_cases]),
            const.RECOVERED_VS_CASES : const.get_percentage(insert_list[const.w_total_recovered], insert_list[const.w_total_cases])
        }
        data_list.append(const.get_sensor(data_name, data))
    return data_list

_const_cases

Python
Holds all the constants
# WORLD CASES CONSTANTS
country = 0
w_total_cases = 1
w_new_cases = 2
w_total_deaths = 3
w_new_deaths = 4
w_total_recovered = 5
w_active_cases = 6
w_serious_critical = 7
w_tot_cases_M = 8
w_deaths_M = 9
w_total_tests = 10
w_tests_M = 11

# JSON CONSTANTS
COUNTRY_OTHER = 'Country'
USA_STATE = 'USA States'
TOTAL_CASES = 'Total Cases'
NEW_CASES = 'New Cases'
TOTAL_DEATHS = 'Total Deaths'
NEW_DEATHS = 'New Deaths'
TOTAL_RECOVERED = 'Total Recovered'
ACTIVE_CASES = 'Active Cases'
SERIOUS_CRITICAL = 'Serious Critical'
TOT_CASES_M = 'Total Cases per Million'
DEATHS_M = 'Deaths per Million'
TOTAL_TESTS = 'Total Tests'
TESTS_M = 'Tests per Million'

# EXTRA JSON CONSTANTS
TOTAL_CASES_PERCENT = 'Total Cases %'
NEW_CASES_PERCENT = 'New Cases %'
TOTAL_DEATHS_PERCENT = 'Total Deaths %'
NEW_DEATHS_PERCENT = 'New Deaths %'
TOTAL_RECOVERED_PERCENT = 'Total Recovered %'
ACTIVE_CASES_PERCENT = 'Active Cases %'
SERIOUS_CRITICAL_PERCENT = 'Serious Critical %'
DEATHS_VS_CASES = 'Deaths Rate %'
RECOVERED_VS_CASES = 'Recovery Rate %'

def get_sensor(id, value, type=None, unit=None, prefix=None, dt=None):
    sensor = {
        'id': id,
        'data': value
    }
    return sensor

def get_percentage(str_num, str_dem):
    if str_dem == '0':
        return '0'
    percent = float(str_num) / float(str_dem) * 100
    return str(float("{:.2f}".format(percent)))

Credits

Amir Pournasserian

Amir Pournasserian

10 projects • 15 followers
Data scientist, machine learning specialist, IoT nerd. CEO of Momentaj and Founder of uBeac, IoT thinktanks.

Comments