O Internet, please give us an inspirational quote ...

Your Weather Station in the Cloud with Azure, Part 2: Implementation

Created: 06 May 2020

weather_station_live

Project: Your Home in the Cloud - Weather Monitoring with Azure

Set up services for PATH 2

Note: I will use a Raspberry Pi Azure IoT Online Simulator to simulate HOME for demonstration.

1. Create and send test data to Azure IoT Hub

2. Create Azure SQL Database

3. Deploy Azure Functions

My __init__.py is along this line.

import logging
import azure.functions as func
import json
import os
import pyodbc


def update_table(schema_name, table_name, payload):
    ## Get connection string
    # from Azure Functions' configuration
    conn_str = os.environ["SQLCONNSTR_SQLConnectionString"]

    # initilize connection
    cnxn   = pyodbc.connect(conn_str)
    cursor = cnxn.cursor()

    # update payload
    timestamp   = payload['timestamp']
    device_name = payload['device_name']
    temperature = payload['temperature']
    humidity    = payload['humidity']

    # stage part to the table
    sql_command = f"""
    insert into {schema_name}.{table_name}(timestamp, device_name, temperature, humidity)
    values ("{timestamp}", "{device_name}", {temperature}, {humidity})
    ;
    """
    cursor.execute(sql_command)
    logging.info(f'Update {index}/{len(payload)} of payload')

    # commit when all updates are staged
    cnxn.commit()

    return True



def main(event: func.EventHubEvent):
    schema_name = 'dbo',
    table_name  = 'home_stat_temp'
    logging.info('Python EventHub trigger processed an event: %s', event.get_body().decode('utf-8'))

    # convert string to a list of dicts
    payload = json.loads(event.get_body().decode('utf-8'))

    # update payload to table
    update_result = update_table(schema_name, table_name, payload)

    # log success/fail status
    if update_result: logging.info(f'Payload update successful!')
    else: logging.info(f'Payload update failed!')

4. Set up Grafana dashboard on your machine

5. Build your own weather station

When all pieces are available. You can send data from actual sensors.
p2_5

Extra: Quick look at Azure Stream Analytics and Power BI

Additional steps to implement PATH 3: One can simply follow the tutorial – https://docs.microsoft.com/en-us/azure/iot-hub/iot-hub-live-data-visualization-in-power-bi.

Here is my attempt

End

That concludes implementation steps of our project.
I hope you have fun :)