Connecting Raspberry Pi to the Alibaba Cloud IoT Platform Using Python

Join us at the Alibaba Cloud ACtivate Online Conference on March 5–6 to challenge assumptions, exchange ideas, and explore what is possible through digital transformation.

Written by Hongfeng Liu, Alibaba Cloud MVP

As an Alibaba Cloud MVP, I have the honor of being one of the first persons to test the Alibaba Cloud IoT Platform from a user’s perspective. I am familiar with the Alibaba Cloud IoT Platform and have had the privilege of participating in some hardware access work for the Feifeng Platform (the predecessor of Link Develop, a one-stop development platform) from the start. I also witnessed the rapid expansion of the Alibaba Cloud IoT team from dozens to hundreds of people. The content hosted on its IoT Platform expanded just as rapidly, including the basic edition of the IoT Development Kit, Feifeng, one-stop Development Platform — Link Develop, the advanced edition of the IoT Development Kit, Feiyan, Feixiang, City Brain, Agricultural Brain, and Industrial Interconnection Platform. It is, therefore, a challenge to keep up with and fully understand each IoT product of Alibaba Cloud.

At first, we used the MCU single-chip +.NET MF platform and directly used the MQTT protocol to implement relevant code to connect the Alibaba Cloud Platform. Although the IoT Platform can also be flexibly connected based on the configuration, it is still unfamiliar to some WEB, AI, and cloud developers. Because of these requirements, advanced languages, such as JS, Java, and Python, have entered into hardware development and can be run directly on MCU chips, greatly speeding up the connection between the cloud and IoT devices.

This article uses Python, an advanced language, to connect the cloud and the IoT device based on Raspberry Pi. In a previous article, I have discussed how to perform this using Node.js.

The following is the test hardware delivered by the Alibaba Cloud team:

Image for post
Image for post
  1. Raspberry Pi 3B with Wi-Fi and Bluetooth
  2. DHT11 temperature and humidity module
  3. LED module

Step 1: Install OS for Raspberry Pi

We must install the operating system for Raspberry Pi

We are installing the Raspbian firmware, which can be installed according to the steps provided here:https://www.yuque.com/cloud-dev/iot-tech/ig269q

Step 2: Install Python SDK

Install the Python SDK for the Alibaba Cloud Platform

Execute the following command: $ pip install aliyun-python-sdk-iot-client

Step 3: Write the Code

After installing the Raspbian firmware and the Python SDK, we need to write a code in Python to run our project. The sample code has already been provided in the Alibaba Cloud Platform document (https://www.yuque.com/cloud-dev/iot-tech/rz6fpl), but the function is relatively simple, just sending two random numbers to the cloud.

We design a relatively complex scenario in which we must realize the two-way communication first, so that the sensor data can be sent to the cloud and a command can be sent from the cloud to control related devices.

For uploading data, we select a real sensor DHT11, which can obtain the temperature and humidity values. In addition, an LED module is also provided to control the on and off state of the light through the commands sent from the cloud.

The pin definition figure of Raspberry Pi is as follows:

Image for post
Image for post

The DHT11 module consists of three lines, which belong to single bus communication. We connect the power line to 5V-4pin, the ground line to GND-6pin, and the communication pin to GPIO16–36pin. The LED module also consists of three lines. We connect the power line to 3V3–1pin, the ground line to GND-9pin, and the control line to GPIO4–7pin.

Image for post
Image for post

It is relatively easy to realize LED control by simply controlling the high and low levels of the pins.

Turn on the light:

The DHT11 is relatively complex. For further information, refer to a blog post I wrote in 2010 (related links:https://blog.csdn.net/yfiot/article/details/5996524).

Image for post
Image for post

Due to the large amount of code, we package it as a function that returns two values at the same time, one for temperature (T) and the other for humidity (H).

Most examples using Python on the Internet read temperature and humidity once. If continuous reading is required, the time interval must be about 3 seconds; otherwise, it can easily fail. Linux is not a real-time system, so we have found that, even if the time interval is 3 seconds, sometimes the temperature and humidity values cannot be correctly obtained, and sometimes incorrect values that have passed verification are retrieved.

The data pin of DHT11 is connected to GPIO16, so we define the pin code as follows:

When these preparations are completed, we are going to define relevant products and devices in the cloud. Unlike the official sample provided by Alibaba Cloud, we added an attribute LED, which is capable of read and write and uses enumerated variables, with 0 indicating to turn the lights off and 1 indicating to turn the lights on.

Image for post
Image for post

When this is defined, we create the device and get the device trituples. The official sample provided is only simulated data and is for one-way upload, so we must modify it.

In the main function, add this code:

This means that we can obtain information pushed from the cloud.

The information content pushed by the cloud is as follows:

We need to obtain the value of LED, so we need to add something to the on_message function:

Turn the light on or off based on the value of the LED.

After the relevant code is modified, we can upload it to the Raspberry Pi device and start running, as shown in the following figure:

Image for post
Image for post

At this time, when checking the status of the cloud device, we can see that the data has been uploaded to the cloud normally.

Image for post
Image for post

We perform a data distribution test on the online debugging panel of cloud products

Image for post
Image for post

When sending 0 or 1, we can see that the LED is off or on.

In general, we find that IoT code can be easily written using the familiar Python language.

Reference:https://www.alibabacloud.com/blog/connecting-raspberry-pi-to-the-alibaba-cloud-iot-platform-using-python_594495?spm=a2c65.12601980.0.0

Written by

Follow me to keep abreast with the latest technology news, industry insights, and developer trends.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store