Skip to content

IECCLES4/Azure-IoT-Raspberry-Pi

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

61 Commits
 
 
 
 

Repository files navigation

Build your own IoT Projects using Azure and Raspberry Pi

AZUAzure-IoT Raspberry Pi full video

Stream Live data on Azure using Raspberry Pi

  • General arctecture

pi (1)

Raspberry_Pi_dht_bb

  • The circuit connection

Preliquisites

  1. Azure account
  2. Raspberry pi
  3. DHT Sensor
  4. Wifi Connectivity

Azure configurations

  • To create a resource on Azure, you need Azure account. Click the link to create a free azure account Azure Account

  • If you are a student you can use your student account in accessing $100 worth Azure Student Account

  • Navigate to azure market place to create your resource. Azure Market place

Creating Resources in Azure Cloud

  • We need IoT hub, Storage resource and stream analytics.

Creating IoT Hub and IoT Device.

a. Navigate to Azure Market place and search for 'IoT Hub'

1hub

b. Click Create

2bub

c. Create a new Resource Group and give approprite name followed by a name to the IoT Hub.

4hub

d. Choose Free Tier, can keep everything else default for starters.

5hub

Cogratulations you have successfully created azure IoT resource Group and Azure IoT Hub 😁 👏

Creating Device in IoT Hub

e. Navigate to the azure dashboard and click on the IoT Hub you have created.

6

f. On the left side scroll to Device Management and click Device

7b

g. Click Create and give an appropriate name, then click save.

8a

Connection Strings

  • To establish communication betwwen your Raspberry Pi and Azure cloud, we shall use MQTT protocol, a connection string which contains the HostHame - the hub name, Device Id and Shared AccessKey is used. Copy the primary connection string.

9a

  • Connection string is a properties of three, HubName, Device Id and Access Key

Creating Stream Analytics

h. Navigate to Azure home market place > Search for Stream Analytics Job.

st1

i. Fill in the parts below and leave others default and click create. st2

Creating Storage Resource.

j. Navigate to Azure market place and search Storage account and click the one circled below.

sto1

k. Fill in as shown below and click create, you can change the location depending on where you are located.

sto2

Linking IoT Hub to Storage account through Stream analytics

l. See the picture bellow and the video for this part.

st5

Azure-IoT Raspberry Pi Full video

AZUAzure-IoT Raspberry Pi full video

Configurations.

  • In this project, I am using DHT22 as my sensor of choice - this can be replaced with whatever kind of parameter value you want to monitor or measure.

  • The circuit Diagram is as shown Below:

-Importing relevant dependent libraries

import random  
import Adafruit_DHT
import time
from azure.iot.device import IoTHubDeviceClient, Message  

  • Defining Our GPIO pin in Raspberry Pi.
#We are using pin number 4 and my DHT is 22
sensor = Adafruit_DHT.DHT22
pin = 4 #this is pin number 8 (SOC)
  • Defining your connection String, which is copied in Device from your IoT Hub
#define the connection String, go to IoT Hub -Devices - Primary connection String
CONNECTION_STRING = "HostName=hub name.azure-devices.net;DeviceId=your device id;SharedAccessKey=copied key"  
  • This array stores temperature and humidity data
#This is the array that will be used to store the data
MSG_SND = '{{"temperature": {temperature},"humidity": {humidity}}}'
  • This block of code reads temperature and humidity using Adafruit Library
while True:
    humidity, temperature = Adafruit_DHT.read_retry(sensor, pin) #data holding variables
  • This function is inside While Loop and connect the pi to our hub and i am using try and except to capture any errors incase of keyboard interrupt.
  • the second While loop sends the data to the hub
    #Function to connect Pi to IoT Hub using the connection string that is predefined up there
    def iothub_client_init():  
        client = IoTHubDeviceClient.create_from_connection_string(CONNECTION_STRING)  
        return client  
        #function to send the data to hub
    def iothub_client_telemetry_sample_run():  
        try:  #this is to help catch any possible errors.
            client = iothub_client_init()  
            print ( "AronAyub Pi is Sending data to IoT Hub, To exit, press Ctrl-C " )  
            while True:  
                msg_txt_formatted = MSG_SND.format(temperature=temperature, humidity=humidity)  
                message = Message(msg_txt_formatted)  
                print( "Sending message: {}".format(message) )  
                client.send_message(message)  
                print ( "Message successfully sent" )  
                time.sleep(10)  
        except KeyboardInterrupt:  
            print ( "IoTHubClient stopped" )  
    if __name__ == '__main__':  
        print ( "Press Ctrl-C to exit" )  
        iothub_client_telemetry_sample_run()

Incase of any issue, kindly refer the video

AZUAzure-IoT Raspberry Pi full video

About

Azure-IoT-Raspberry Pi

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published