Azure IoT Hub and SQL With Python – A Tutorial

by May 30, 2020IoT Cloud, IoT Programming

Introduction

In the following tutorial we will learn how to use the Microsoft Azure Cloud service in our IoT projects. We will be using the Azure IoT Hub, Stream Analytics Jobs & an SQL database to store our data. This will be a step by step tutorial and almost every step has an image associated with it to make it easier to follow.

Contents

  1. Setup
    1. Resource Group
    2. Azure IoT Hub
    3. SQL Database
    4. Stream Analytics
  2. Code Explanation
  3. Output

Setting Up a Resource Group

  • When You first log in to the Azure portal, you will see a screen like this. Click on the Resource groups button.
Select resource groups from the options
  • Next select Add
  • Enter the name of your Resource Group and then click the Review + Create
create the resource group
  • Then click create

Setting Up Azure IoT Hub

  • Select IoT Hub in the Azure services list or look for it in the search bar.
  • Next, select Add on the top left of your screen
  • Next, fill in the data similar to the ones shown in the image below and then click Review + Create
  • Then select Create
  • Now we have to create a device. For this select your temperaturedata in the IoT Hub page
  • Next, from the list select IoT devices under Explorers and then select New
  • Enter a desired Device ID and then click Save
  • Now open this device and copy the primary connection string this is required to authenticate your requests created from your python code.

Setting Up SQL Database

  • Select SQL databases in the Azure services list or look for it in the search bar.
  • Then select add from the SQL dashboard
  • Fill in the required details
  • Before you are able to review and create you also have to create a new server
  • Enter the details as per your requirements and then select OK
  • Now you can click on Create + Review and then Create.
  • Next lets create the table where we are going to store our data. For this open the dashboard of your database and select Query editor
  • While logging in you may face an issue where your IP hasn’t been authorized, click on Set server firewall
  • In that page you should see a button that says Add Client IP select that.
  • Now we have to write a simple SQL query to create our table.
CREATE TABLE [dbo].[temperature](
   [temp] [float] NULL,
)

Setting Up Stream Analytics

  • Select Stream Analytics jobs in the Azure services list or look for it in the search bar.
  • Next, click on the Add button in this page
  • Fill in the details as shown below and click create.
  • Now open your Stream analytics dashboard and select input from the Job topology sub-menu
  • Now choose + Add stream input and choose IoT Hub from the drop down
  • You will have to enter an input alias and leave everything else as the defaults and click save.
  • Now open your Stream analytics dashboard and select output from the Job topology sub-menu
  • Select +Add and choose SQL Database form the drop-down.
  • Enter your table’s name which is temperature in our case.
  • Now open your Stream analytics dashboard and select Query from the Job topology sub-menu
  • Enter the Query as shown below
  • Click on Save Query
  • Finally go to the Overview page and click on Start

NOTE :- In case everything seems to be working fine yet there are no entries in your SQL database check if you have whitelisted the IP (in the SQL server) assigned to your stream analytics process

Code Explanation

import random
import time
import threading

# Using the Python Device SDK for IoT Hub:
#   https://github.com/Azure/azure-iot-sdk-python
# The sample connects to a device-specific MQTT endpoint on your IoT Hub.
from azure.iot.device import IoTHubDeviceClient, Message, MethodResponse

# The device connection string to authenticate the device with your IoT hub.
# Using the Azure CLI:
# az iot hub device-identity show-connection-string --hub-name {YourIoTHubName} --device-id MyNodeDevice --output table
CONNECTION_STRING = <your connection string of the iothub device>
# Define the JSON message to send to IoT Hub.
TEMPERATURE = 20.0
HUMIDITY = 60
MSG_TXT = '{{"temp": {temperature}}}'

INTERVAL = 1

def iothub_client_init():
    # Create an IoT Hub client
    client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)
    return client


def device_method_listener(device_client):
    global INTERVAL
    while True:
        method_request = device_client.receive_method_request()
        print (
            "\nMethod callback called with:\nmethodName = {method_name}\npayload = {payload}".format(
                method_name=method_request.name,
                payload=method_request.payload
            )
        )
        if method_request.name == "SetTelemetryInterval":
            try:
                INTERVAL = int(method_request.payload)
            except ValueError:
                response_payload = {"Response": "Invalid parameter"}
                response_status = 400
            else:
                response_payload = {"Response": "Executed direct method {}".format(method_request.name)}
                response_status = 200
        else:
            response_payload = {"Response": "Direct method {} not defined".format(method_request.name)}
            response_status = 404

        method_response = MethodResponse(method_request.request_id, response_status, payload=response_payload)
        device_client.send_method_response(method_response)



def iothub_client_telemetry_sample_run():

    try:
        client = iothub_client_init()
        print ( "IoT Hub device sending periodic messages, press Ctrl-C to exit" )

        # Start a thread to listen 
        device_method_thread = threading.Thread(target=device_method_listener, args=(client,))
        device_method_thread.daemon = True
        device_method_thread.start()

        while True:
            # Build the message with simulated telemetry values.
            temperature = TEMPERATURE + (random.random() * 15)
            msg_txt_formatted = MSG_TXT.format(temperature=temperature)
            message = Message(msg_txt_formatted)

            # Add a custom application property to the message.
            # An IoT hub can filter on these properties without access to the message body.
            if temperature > 30:
              message.custom_properties["temperatureAlert"] = "true"
            else:
              message.custom_properties["temperatureAlert"] = "false"

            # Send the message.
            print( "Sending message: {}".format(message) )
            client.send_message(message)
            print( "Message sent" )
            time.sleep(INTERVAL)

    except KeyboardInterrupt:
        print ( "IoTHubClient sample stopped" )

if __name__ == '__main__':
    print ( "IoT Hub Quickstart #2 - Simulated device" )
    print ( "Press Ctrl-C to exit" )
    iothub_client_telemetry_sample_run()
  • This is a code block provided by azure on their GitHub page. You can find it here. Their README file will give a comprehensive explanation of the code.
  • In line 13 you will have to type in the connection string that you had copied earlier.
  • This program generates random float (as shown in line 68) values to act as temperature to simulate a sensor. You can also write a function for any sensor you wish to and send that data.

Output

  • Open the Query editor for the database that you had created earlier.
  • Run the following query
SELECT * FROM [dbo].[temperature]
  • If everything goes according to plan you should see something like this

Conclusion

I hope you all learnt the basics of Azure IoTHub and how we can use it to store data into an SQL database. Feel free to leave your questions and suggestions in the comments below I will get back to them as soon as possible.

Happy Learning 😀

Creating a multiplication Skill in Alexa using python

Written By Sashreek Shankar

Hey reader! I am Sashreek, a 16 year old programmer who loves Robotics, IoT and Open Source. I am extremely enthusiastic about the Raspberry Pi and Arduino hardware as well. I also believe in sharing any knowledge I have so feel free to ask me anything 🙂

RELATED POSTS

Creating REST API CRUD with Node.js, Express and MongoDB

Creating REST API CRUD with Node.js, Express and MongoDB

In our previous blog, we explored the fundamentals of creating a REST API with in-memory CRUD operations using Node.js and Express. Now, we're taking a significant step forward by integrating our product data with a database. This transition is pivotal for achieving...

How to create REST API using Node.js and Express?

How to create REST API using Node.js and Express?

In the vast landscape of web development, creating a robust API is a fundamental skill. APIs, or Application Programming Interfaces, serve as the communication bridge between different software applications. Today, we'll embark on a journey to build a simple blog API...

Advanced Generics: Higher-Order Functions

Advanced Generics: Higher-Order Functions

In our journey through TypeScript generics, we've covered the basics, interfaces, and classes. Now, it's time to explore advanced concepts by combining generics with higher-order functions. These functions, which take other functions as arguments or return them,...

Basic Usage of Generics in Typescript

Basic Usage of Generics in Typescript

Keypoints Show how to declare generic functions and classes in TypeScript. Provide examples of generic functions that work with different data types. Demonstrate the use of built-in generics like Array<T> and Promise<T>. Here's the content...

Getting Started with Bash Script : A Simple Guide

Getting Started with Bash Script : A Simple Guide

Introduction In this tutorial, we will be looking into Bash Script - a tool used by developers to increase productivity and manage tasks. A lot of professionals use it for tasks such as system administration, data crunching, web app development, automated backups,...

How to Extract REST API Data using Python

How to Extract REST API Data using Python

Introduction In this tutorial, we will be discussing in detail on how to extract REST API data using Python. It is one of the most popular APIs and is used by a majority of applications. We will be using VS code editor for executing the python code. The extracted API...

Create a Simple ReactJs Application – Part 1

Create a Simple ReactJs Application – Part 1

ReactJs is one of the famous front-end open-source libraries of JavaScript developed by Facebook. It aims to allow developers to quickly create fast user interfaces for websites and applications. In this blog, we will create a simple and basic ReactJs...

VIDEOS – FOLLOW US ON YOUTUBE

EXPLORE OUR IOT PROJECTS

IoT Smart Gardening System – ESP8266, MQTT, Adafruit IO

Gardening is always a very calming pastime. However, our gardens' plants may not always receive the care they require due to our active lifestyles. What if we could remotely keep an eye on their health and provide them with the attention they require? In this article,...

How to Simulate IoT projects using Cisco Packet Tracer

In this tutorial, let's learn how to simulate the IoT project using the Cisco packet tracer. As an example, we shall build a simple Home Automation project to control and monitor devices. Introduction Firstly, let's quickly look at the overview of the software. Packet...

All you need to know about integrating NodeMCU with Ubidots over MQTT

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

All you need to know about integrating NodeMCU with Ubidots over Https

In this tutorial, let's discuss Integrating NodeMCU and Ubidots IoT platform. As an illustration, we shall interface the DHT11 sensor to monitor temperature and Humidity. Additionally, an led bulb is controlled using the dashboard. Besides, the implementation will be...

How to design a Wireless Blind Stick using nRF24L01 Module?

Introduction Let's learn to design a low-cost wireless blind stick using the nRF24L01 transceiver module. So the complete project is divided into the transmitter part and receiver part. Thus, the Transmitter part consists of an Arduino Nano microcontroller, ultrasonic...

Sending Temperature data to ThingSpeak Cloud and Visualize

In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world. But "What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows...

Amaze your friend with latest tricks of Raspberry Pi and Firebase

Introduction to our Raspberry Pi and Firebase trick Let me introduce you to the latest trick of Raspberry Pi and Firebase we'll be using to fool them. It begins with a small circuit to connect a temperature sensor and an Infrared sensor with Raspberry Pi. The circuit...

How to implement Machine Learning on IoT based Data?

Introduction The industrial scope for the convergence of the Internet of Things(IoT) and Machine learning(ML) is wide and informative. IoT renders an enormous amount of data from various sensors. On the other hand, ML opens up insight hidden in the acquired data....

Smart Display Board based on IoT and Google Firebase

Introduction In this tutorial, we are going to build a Smart Display Board based on IoT and Google Firebase by using NodeMCU8266 (or you can even use NodeMCU32) and LCD. Generally, in shops, hotels, offices, railway stations, notice/ display boards are used. They are...

Smart Gardening System – GO GREEN Project

Automation of farm activities can transform agricultural domain from being manual into a dynamic field to yield higher production with less human intervention. The project Green is developed to manage farms using modern information and communication technologies....