Coder Social home page Coder Social logo

azure-iot-raspberry-pi's Introduction

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

azure-iot-raspberry-pi's People

Contributors

aronayub avatar

Stargazers

 avatar  avatar

Watchers

 avatar  avatar

azure-iot-raspberry-pi's Issues

Conversion into C++

Hi I followed your tutorial on YouTube and found it extremely useful and very informative. However, I was wondering if you would know how to implement this MQTT messaging into a C++ programme rather than using a Python programme? I have tried looking online but I haven't found what dependencies I'd need to include for my CMake compiler. Any help would be greatly appreciated, thanks!

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.