- General arctecture
- The circuit connection
- Azure account
- Raspberry pi
- DHT Sensor
- Wifi Connectivity
-
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
- We need IoT hub, Storage resource and stream analytics.
a. Navigate to Azure Market place and search for 'IoT Hub'
b. Click Create
c. Create a new Resource Group and give approprite name followed by a name to the IoT Hub.
d. Choose Free Tier, can keep everything else default for starters.
Cogratulations you have successfully created azure IoT resource Group and Azure IoT Hub ๐ ๐
e. Navigate to the azure dashboard and click on the IoT Hub you have created.
f. On the left side scroll to Device Management and click Device
g. Click Create and give an appropriate name, then click save.
- 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.
- Connection string is a properties of three, HubName, Device Id and Access Key
h. Navigate to Azure home market place > Search for Stream Analytics Job.
i. Fill in the parts below and leave others default and click create.
j. Navigate to Azure market place and search Storage account and click the one circled below.
k. Fill in as shown below and click create, you can change the location depending on where you are located.
l. See the picture bellow and the video for this part.
Azure-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