Quickstart: Send telemetry from a device to an IoT hub and read it with a back-end application (Python)
sudo pip3 install azure-iot-device
sudo pip3 install azure-iot-hub
sudo pip3 install azure-iothub-service-client
sudo pip3 install azure-iothub-device-client
Make sure you run your code with Python3
Now before you run the Python program create an azure shell (CLI). When you open it for the first time it will ask you to create storage.
az extension add --name azure-cli-iot-ext
az iot hub monitor-events --hub-name MSR0806 --device-id MyRPi
Download and Install https://www.aka.ms/installazurecliwindows
az
az login
az account set --subscription "Free Trail"
az extension add --name azure-iot
az iot hub show --query properties.eventHubEndpoints.events.endpoint --name {YourIoTHubName}
az iot hub show --query properties.eventHubEndpoints.events.path --name {YourIoTHubName}
az iot hub policy show --name service --query primaryKey --hub-name {YourIoTHubName}
- Before we can get our Raspberry Pi to retrieve data from our ADXL345 Accelerometer, there are a few changes we must make to the Pi’s configuration.
sudo apt-get update
sudo apt-get upgrade
- Once the Raspberry Pi has finished updating, we will need to go ahead and launch the Raspberry configuration tool so that we can enable I2C on the Raspberry Pi.
Run the following command to launch the raspi configuration tool.
sudo raspi-config
-
On this screen, you need to head to the "5 Interfacing Options" menu. You can navigate the raspi-config tools menus by using the arrow keys. Use the ENTER key to select particular options.
-
Now within the interfacing options menu go ahead and select "P5 I2C". When asked if you would like to enable the "ARM I2C interface", select "YES".
-
After enabling the I2C interface, you will need to restart your Raspberry Pi by running the following command.
sudo reboot.
- Now that we have enabled I2C and restarted the Raspberry Pi, we can now proceed to install the packages that we will rely on to talk with our accelerometer. Run the following command to install.
sudo apt-get install python3-dev python3-pip python3-smbus i2c-tools -y
- With all our required packages installed let’s now check to see whether our Raspberry Pi can see our ADXL345 Accelerometer. From this command, you should see a fair bit displayed on the command line. Within this result, you should at least see a number such as “53“.
sudo i2cdetect -y 1
- With all the packages that we need now installed to the Raspberry Pi, we can now proceed to working directory and install Adafruit’s ADXL34x Python library. Run following command with pip command.
sudo pip3 install adafruit-circuitpython-ADXL34x
To install the Adafruit DHT11 library:
- Enter this at the command prompt to download the library:
git clone https://github.com/adafruit/Adafruit_Python_DHT.git
- Change directories with:
cd Adafruit_Python_DHT
- Now enter this:
sudo apt-get install build-essential python-dev
- Then install the library with:
sudo python setup.py install