PragmaticPhilKalbeAbbas
Published © GPL3+

Machine Learning with Azure and XinaBox

Build an XinaBox weather monitor edge device and hook it up to your Azure portal/Machine Learning Studio to create a rain prediction model.

IntermediateFull instructions provided2.5 hours1,405
Machine Learning with Azure and XinaBox

Things used in this project

Hardware components

CW02
XinaBox CW02
×1
IP01
XinaBox IP01
×1
MD01
XinaBox MD01
Optional — for adding strength to xChips assembly
×1
SW01
XinaBox SW01
×1
XC10
XinaBox XC10
Connectors to join the xChips together. This is a pack of 10, but we only need a few (2 or 4, depending on whether you use the MD01)
×1

Software apps and online services

Microsoft Azure
Microsoft Azure
MS Azure Iot Hub and Machine Learning Studio
Microsoft Power BI
Zerynth Studio
Zerynth Studio

Story

Read more

Custom parts and enclosures

XinaBox 3D Printed case

Code

device.conf.json

JSON
contains device configuration.
Replace hub_id and device_id with your IoT Hub Id and Device ID:
{
    "hub_id": "iot-hub-id",
    "device_id": "device-name",
    "api_version": "2017-06-30"
}

helpers.py

Python
contains helper functions to load device configuration and IoT hub key.
You don’t need to make any amends to this code:
# -*- coding: utf-8 -*-
# @Author: Lorenzo
# @Date:   2017-10-03 10:56:02
# @Last Modified by:   lorenzo
# @Last Modified time: 2017-10-26 10:44:43
 
import json
 
def load_key(key_file):
    mstream = open('resource://' + key_file)
    pkey = bytearray()
    while True:
        rd = mstream.read(1)
        if not rd:
            break
        pkey.append(rd[0])
    return pkey
 
def load_device_conf():
    confstream = open('resource://device.conf.json')
    conf = ''
    while True:
        line = confstream.readline()
        if not line:
            break
        conf += line
    return json.loads(conf)

main.py

Python
contains the main body of code
import streams
import json
from wireless import wifi
 
#Import SW01 library
from xinabox.sw01 import sw01
 
# choose a wifi chip supporting secure sockets
from espressif.esp32net import esp32wifi as wifi_driver
 
import requests
# import azure iot module
from azure.iot import iot
 
# import helpers functions to easily load keys and device configuration
import helpers
 
# DEVICE KEY FILE MUST BE PLACED INSIDE PROJECT FOLDER
new_resource('private.base64.key')
# set device configuration inside this json file
new_resource('device.conf.json')
 
# define a callback for twin updates
def twin_callback(twin, version):
    global publish_period
    print('new twin version:', version)
    print('requested publish period:', twin['publish_period'])
    publish_period = twin['publish_period']
    return {'publish_period': publish_period}
 
streams.serial()
wifi_driver.auto_init()
 
# place here your wifi configuration
wifi.link("SSID",wifi.WIFI_WPA2,"PSK")
 
pkey = helpers.load_key('private.base64.key')
device_conf = helpers.load_device_conf()
publish_period = 20000 #publish period in ms, change it to publish frequently
 
# choose an appropriate way to get a valid timestamp (may be available through hardware RTC)
def get_timestamp():
    user_agent = {"user-agent": "curl/7.56.0"}
    return json.loads(requests.get("http://now.zerynth.com/", headers=user_agent).content)['now']['epoch']
 
# create an azure iot device instance, connect to mqtt broker, set twin callback and start mqtt reception loop
device = iot.Device(device_conf['hub_id'], device_conf['device_id'], device_conf['api_version'], pkey, get_timestamp)
device.mqtt.connect()
 
device.on_twin_update(twin_callback)
device.mqtt.loop()
 
# SW01 instance
SW01 = sw01.SW01(I2C0)
 
while True:
    temp = SW01.getTempC()		# return temp in degree celcius
    humid = SW01.getHumidity()	# return humidity in percentage
    device.publish_event({'temperature': temp, 'humidity': humid},{"device":"CW02"})
    
    print("Temperature: ",temp)
    print("Humidity: ",humid)   
    sleep(publish_period)

SQL-Query

SQL
SQL Query for using with Stream Analytics
WITH machinelearning AS (
   SELECT EventEnqueuedUtcTime, temperature, humidity, machinelearning(temperature, humidity) as result from [Your-Input-Alias]
)
Select System.Timestamp time, CAST (result.[temperature] AS FLOAT) AS temperature, CAST (result.[humidity] AS FLOAT) AS humidity, CAST (result.[Scored Probabilities] AS FLOAT ) AS 'probabilities of rain'
Into [Your-Output-Alias]
From machinelearning

Credits

PragmaticPhil

PragmaticPhil

17 projects • 17 followers
Pragmatic hobbyist
KalbeAbbas

KalbeAbbas

25 projects • 20 followers
An enthusiastic and dedicated Electronic engineer graduated from SSUET Karachi, Pakistan. Loves to discover Embedded electronics projects.

Comments