Connecting Raspberry Pi to the Alibaba Cloud IoT Platform Using Python

  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

Step 2: Install Python SDK

Install the Python SDK for the Alibaba Cloud Platform

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.

led_pin = 4 //GPIO is 4
GPIO.setmode(GPIO.BCM) //GPIO definition of BCM
GPIO.setup(led_pin, GPIO.OUT) //Set to output mode
GPIO.output(led_pin, GPIO.HIGH)
Turn off the light:
GPIO.output(led_pin, GPIO.LOW)
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
client.on_message = on_message
{"method":"thing.service.property.set","id":"169885527","params":{"LED":1},"version":"1.0.0"}
setjson = json.loads(msg.payload)
led = setjson['params']['LED']
GPIO.output(led_pin,(GPIO.HIGH if led==1 else GPIO.LOW ))

--

--

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
Alibaba Cloud

Alibaba Cloud

Follow me to keep abreast with the latest technology news, industry insights, and developer trends. Alibaba Cloud website:https://www.alibabacloud.com