Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python

In this article, MVP Liu Hongfeng shares his experience connecting a Raspberry Pi device to the cloud using Alibaba Cloud IoT Platform.

Written by Hongfeng Liu, MVP of Alibaba Cloud below.

I am Alibaba Cloud MVP and Alibaba Cloud IoT Platform I am honored to be the first person to test / iot) from the user's perspective. I am familiar with Alibaba Cloud IoT Platform and from the beginning the hardware of Feifeng Platform (predecessor of Link Develop, a one-stop development platform). I had the opportunity to participate in some access work. We also witnessed Alibaba Cloud's IoT team expanding rapidly from tens to hundreds. The content hosted on the IoT platform has expanded rapidly as well, with the basic version of the IoT development kit "Hien", the one-stop development platform "Link Develop", and the advanced version of the IoT development kit "Hien", "Flying", There were "City Brain", "Agricultural Brain", "Industrial Interconnection Platform" and so on. Therefore, it is a challenge to catch up with each IoT product of Alibaba Cloud and fully understand it.

Initially, we used the MCU's single chip + .NET MF platform and implemented the relevant code to connect Alibaba Cloud Platform directly using the MQTT protocol. IoT platforms can also be flexibly connected depending on the configuration, but some WEB, AI, and cloud developers are not yet familiar with it. Due to these requirements, advanced languages such as JS, Java, and Python have entered hardware development and can now run directly on the MCU chip, significantly speeding up the connection between the cloud and IoT devices. It was.

In this article, I tried to connect the cloud and IoT devices based on Raspberry Pi using Python, which is an advanced language. [Previous article](https://www.alibabacloud.com/blog/developing-apps-on-alibaba-cloud-iot-platform-with-raspberry-pi-and-node-js_594346?spm=a2c65.11461447.0. 0.497c7a71qSel56) introduced how to run it using Node.js.

Below is the test hardware delivered by the Alibaba Cloud team. image.png

  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

You need to install the operating system for the Raspberry Pi.

I have the Raspbian firmware installed, but you can install it by following the steps provided here: [https://www.yuque.com/cloud-dev/iot-tech/ig269q](https: // www.yuque.com/cloud-dev/iot-tech/ig269q?spm=a2c65.11461447.0.0.497c7a71iYpNQM)

Step 2: Install Python SDK

Install the Python SDK for Alibaba Cloud Platform

Execute the following command.

pip install aliyun-python-sdk-iot-client

Step 3: Write code

After installing the Raspbian firmware and the Python SDK, you need to write the code in Python to run the project. The sample code is the Alibaba Cloud Platform documentation ([https://www.yuque.com/cloud-dev/iot-tech/rz6fpl](https://www.yuque.com/cloud-dev/iot-tech/rz6fpl] ? spm = a2c65.11461447.0.0.497c7a71iYpNQM))), but the functionality is relatively simple, just send two random numbers to the cloud.

We are designing a relatively complex scenario that first realizes two-way communication so that sensor data can be sent to the cloud and commands can be sent from the cloud to control related devices.

For uploading data, we have selected the real sensor DHT11, which can acquire temperature and humidity values. There is also an LED module that controls the on / off status of the light with commands sent from the cloud.

The pin definition diagram of Raspberry Pi is as follows. image.png

The DHT11 module consists of three lines belonging to single bus communication. The power line is connected to 5V-4pin, the ground line is connected to GND-6pin, and the communication pin is connected to GPIO16-36pin. The LED module also consists of three lines. The power line is connected to 3V3-1pin, the ground line is connected to GND-9pin, and the control line is connected to GPIO4-7pin. image.png LED control can be achieved relatively easily by simply controlling the high and low levels of the pins.

led_pin = 4 //GPIO is 4
 GPIO.setmode(GPIO.BCM) //GPIO definition of BCM 
 GPIO.setup(led_pin, GPIO.OUT) //Set to output mode

Turn on the light:

GPIO.output(led_pin, GPIO.HIGH)
Turn off the light:
GPIO.output(led_pin, GPIO.LOW)

DHT11 is relatively complex. For more information, see the blog post I wrote in 2010 (Related link: https://blog.csdn.net/yfiot/article/details/5996524 / article / details / 5996524))) image.png

Due to the large amount of code, it is packaged as a function that returns two values of temperature (T) and humidity (H) at the same time.

Many Python-based examples on the Internet read temperature and humidity once. If you want to read continuously, you have to set the time interval to about 3 seconds, otherwise it will easily fail. Since Linux is not real-time, we have found that even with a time interval of 3 seconds, we may not be able to get the correct temperature and humidity values, and we may get the wrong values that passed the validation.

Since the data pin of DHT11 is connected to GPIO16, the pin code is defined as follows.

dht_pin =16
The code that encapsulates the function is as follows:
def GetDTH(): 
 data = []
 j = 0 
 GPIO.setup(dht_pin, GPIO.OUT)
 GPIO.output(dht_pin, GPIO.LOW)
 time.sleep(0.02)
 GPIO.output(dht_pin, GPIO.HIGH)
 GPIO.setup(dht_pin, GPIO.IN)
  
 while GPIO.input(dht_pin) == GPIO.LOW:
  continue
 while GPIO.input(dht_pin) == GPIO.HIGH:
  continue
  
 while j < 40:
  k = 0
  while GPIO.input(dht_pin) == GPIO.LOW:
  continue
  while GPIO.input(dht_pin) == GPIO.HIGH:
  k += 1
  if k > 100:
  break
  if k < 8:
  data.append(0)
  else:
  data.append(1)  
  j += 1
  
 humidity_bit = data[0:8]
 humidity_point_bit = data[8:16]
 temperature_bit = data[16:24]
 temperature_point_bit = data[24:32]
 check_bit = data[32:40]
  
 humidity = 0
 humidity_point = 0
 temperature = 0
 temperature_point = 0
 check = 0
  
 for i in range(8):
  humidity += humidity_bit[i] * 2 ** (7-i)
  humidity_point += humidity_point_bit[i] * 2 ** (7-i)
  temperature += temperature_bit[i] * 2 ** (7-i)
  temperature_point += temperature_point_bit[i] * 2 ** (7-i)
  check += check_bit[i] * 2 ** (7-i)
  
 tmp = humidity + humidity_point + temperature + temperature_point
 if check == tmp:
  return temperature,humidity
 else:
  print "wrong"
  return 0,0 

Once these preparations are complete, we will define the relevant products and devices in the cloud. Unlike the official sample provided by Alibaba Cloud, we have added a read / write attribute LED and used an enumeration variable where 0 is off and 1 is on. image.png

Once these preparations are complete, we will define the relevant products and devices in the cloud. Unlike the official sample provided by Alibaba Cloud, we have added a read / write attribute LED and used an enumeration variable where 0 is off and 1 is on.

client.on_message = on_message

In other words, you can get the information pushed from the cloud.

The information content pushed by the cloud is as follows.

{"method":"thing.service.property.set","id":"169885527","params":{"LED":1},"version":"1.0.0"}

We need to get the value of the LED, so we need to add something to the on_message function.

 setjson = json.loads(msg.payload)
 led = setjson['params']['LED']
 GPIO.output(led_pin,(GPIO.HIGH if led==1 else GPIO.LOW ))

Turns on and off based on the LED value.

After modifying the relevant code, upload it to your Raspberry Pi device and start running, as shown in the following figure. image.png

At this time, if you check the status of the cloud device, you can see that the data has been uploaded to the cloud normally. image.png

We are conducting data distribution tests on the online debug panel of cloud products. image.png Send 0 or 1 to see if the LED is off or on.

In general, you'll find that IoT code is easy to write using the familiar Python language.

Recommended Posts

Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Connect to MySQL with Python on Raspberry Pi
Monitor temperature using Raspberry Pi + Alibaba cloud IoT platform
Connect to Wikipedia with Python
Easy IoT to start with Raspberry Pi and MESH
Update Python for Raspberry Pi to 3.7 or later with pyenv
Use vl53l0x with Raspberry Pi (python)
[Raspberry Pi] Changed Python default to Python3
How to connect to Cloud Firestore from Google Cloud Functions with python code
Connect two USB cameras to Raspberry Pi 4
Connect to s3 with AWS Lambda Python
Connect to pepper with PEPPER Mac's python interpreter
Working with GPS on Raspberry Pi 3 Python
How to upload a file to Cloud Storage using Python [Make a fixed point camera with Raspberry PI # 1]
Handmade Alexa Festival! Serverless, Invitation to IoT + Voice. Raspberry Pi + Alexa Voice Servce (Python)
Use python on Raspberry Pi 3 to light the LED with switch control!
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
Connect your SQL Server database to Alibaba Cloud Function Compute using Python
Discord bot with python raspberry pi zero with [Notes]
How to use Raspberry Pi pie camera Python
I tried L-Chika with Raspberry Pi 4 (Python edition)
Connect your Raspberry Pi to your smartphone using Blynk
GPS tracking with Raspberry Pi 4B + BU-353S4 (Python)
Measure CPU temperature of Raspberry Pi with Python
Try to use up the Raspberry Pi 2's 4-core CPU with Parallel Python
GPGPU with Raspberry Pi
DigitalSignage with Raspberry Pi
From setting up Raspberry Pi to installing Python environment
Try debugging Python on Raspberry Pi with Visual Studio.
From python to running instance on google cloud platform
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
Output to "7-segment LED" using python on Raspberry Pi 3!
Display USB camera video with Python OpenCV with Raspberry Pi
Let's operate GPIO of Raspberry Pi with Python CGI
Raspberry Pi with Elixir, which is cooler than Python
Introduced python3-OpenCV3 to Raspberry Pi
Easy introduction to home hack with Raspberry Pi and discord.py
Easy to use Nifty Cloud API with botocore and python
Python beginner opens and closes interlocking camera with Raspberry Pi
[Raspberry PI & Garmin GLO] Until you connect Bluetooth GPS to Raspberry Pi
Post to slack with Python 3
I tried running Movidius NCS with python of Raspberry Pi3
I talked to Raspberry Pi
Introducing PyMySQL to raspberry pi3
Run BNO055 python sample code with I2C (Raspberry Pi 3B)
How to upload files to Cloud Storage with Firebase's python SDK
Raspberry Pi + Python + OpenGL memo
SSD 1306 OLED can be used with Raspberry Pi + python (Note)
Connect to sqlite from python
Switch python to 2.7 with alternatives
Write to csv with Python
getrpimodel: Recognize Raspberry Pi model (A, B, B +, B2, B3, etc) with python
raspberry pi 1 model b, python
Translate I2C device driver for Arduino to Python for Raspberry pi
Getting Started with Heroku-Viewing Hello World in Python Django with Raspberry PI 3
Control the motor with a motor driver using python on Raspberry Pi 3!
I tried to automate the watering of the planter with Raspberry Pi
I want to run the Python GUI when starting Raspberry Pi
Use python on Raspberry Pi 3 to illuminate the LED (Hello World)
Get US stock price from Python with Web API with Raspberry Pi
Create your own IoT platform using raspberry pi and ESP32 (Part 1)