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:
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
Install the Python SDK for the Alibaba Cloud Platform
Execute the following command: $ pip install aliyun-python-sdk-iot-client
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:
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.
It is relatively easy to realize LED control 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)
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).
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:
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
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.
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:
client.on_message = on_message
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:
setjson = json.loads(msg.payload) led = setjson['params']['LED'] GPIO.output(led_pin,(GPIO.HIGH if led==1 else GPIO.LOW ))
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:
At this time, when checking the status of the cloud device, we can see that the data has been uploaded to the cloud normally.
We perform a data distribution test on the online debugging panel of cloud products
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.
Alibaba Clouder - January 11, 2019
GXIC - June 11, 2019
GXIC - January 7, 2019
Alibaba Clouder - July 4, 2018
GXIC - June 11, 2019
GXIC - June 11, 2019
Provides secure and reliable communication between devices and the IoT Platform which allows you to manage a large number of devices on a single IoT Platform.Learn More
API Gateway provides you with high-performance and high-availability API hosting services to deploy and release your APIs on Alibaba Cloud products.Learn More
OpenAPI Explorer allows you to call an API through its web interface or WebCLI, and view the entire process.Learn More
Power your progress in China by working with the NO.1 cloud provider of this dynamic market.Learn More
More Posts by GXIC