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.
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)
Install the Python SDK for Alibaba Cloud Platform
Execute the following command.
pip install aliyun-python-sdk-iot-client
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.
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. 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)))
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.
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.
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.
We are conducting data distribution tests on the online debug panel of cloud products. 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.