By Amit Maity, Alibaba Cloud Tech Share Author. Tech Share is Alibaba Cloud's incentive program to encourage the sharing of technical knowledge and best practices within the cloud community.
Alibaba Cloud IoT Platform allows you to easily and securely connect, manage, and ingest data from various types of electronic devices located globally. By using IoT Platform in combination with other cloud services (i.e. ApsaraDB, Quick BI, and Message Queue), you can build complete solution for collecting, processing, analyzing, and visualizing IoT data in real time to support improved operational efficiency.
There are two different types of communication channels, which you can setup between end devices and IoT cloud. A 2-way communication will allow you to send and receive data using the same data channel, whereas a 1-way communication will enable you to either send or receive data from IoT devices to IoT platform. Below are the few example projects, which can be implemented using 1-way communication channel,
These projects can be further extended to control the monitoring parameters using 2-way communication. Best example would be soil moisture controller of garden. You can monitor soil humidity and if the humidity drops below certain threshold, you can send a signal to IoT device to switch on the water sprinkler so that humidity comes back to acceptable level.
However, in this tutorial I'll only demonstrate 1-way communication of sending messages from end devices to IoT cloud. Raspberry PI will measure the temperature using temperature sensors in every 60 seconds and publish the temperature data to Alibaba IoT cloud as a JSON serialized string in the below form:
{ 'temperature': 12 }
where 12 is the temperature measured by the sensor.
Published temperature readings are then stored into a table setup in an Alibaba Cloud Table Store instance.
The following diagram gives a simplified illustration how data flows through the example system. Note that all of the events in the diagram occur asynchronously.
Figure 1 Data Flowchart
This tutorial can easily be understood and followed if you are familiar with the below technologies,
Before you start working on this project, please ensure you have built the electronic circuit as per below circuit diagram and it's working correctly. Also, please download the below libraries into your raspberry pi environment.
Figure 2 Circuit Diagram
Figure 3 Raspberry Pi 1-Wire Configuration
sudo pip install aliyun-python-sdk-core
sudo pip install aliyun-python-sdk-iot
sudo pip install w1thermsensor
Figure 4 Product Setup
Figure 5 Device Setup
Figure 6 SQL Query Expression
Figure 7 Data Forwarding Rule
from aliyunsdkcore import client
from aliyunsdkiot.request.v20170420 import RegistDeviceRequest
from aliyunsdkiot.request.v20170420 import PubRequest
import time
import base64
from w1thermsensor import W1ThermSensor
accessKeyId='<This will be found in your account security management page>'
accessKeySecret='<This will be found in account security management page>'
clt = client.AcsClient(accessKeyId, accessKeySecret, '<IoT Region>')
productKey ='< This is available in ProductKey field of the product configured >'
deviceName = '<Name of the device created>'
topicName = '/'+productKey+'/'+deviceName+'/update'
request = PubRequest.PubRequest()
request.set_accept_format('json') # Set the response format as json.
request.set_ProductKey(productKey)
request.set_TopicFullName(topicName) #Full name of the topic to which the messages are sent.
sensor = W1ThermSensor()
sensor.get_temperature
and publish the message to IoT device using clt.do_action_with_exception function
in every 60 secondswhile True:
temperature = sensor.get_temperature()
timevalue = time.time()
print("The temperature is %s celsius" % temperature)
message = "{ 'temperature': %s, 'time': %s }" % (temperature,timevalue)
print (base64.urlsafe_b64encode(message))
request.set_MessageContent(base64.urlsafe_b64encode(message)) #JSON message in Base64 String
request.set_Qos(0)
result = clt.do_action_with_exception(request)
print 'result : ' + result
time.sleep(60)
Now let's execute the python code. Make sure the sensor is connected to Raspberry GPIO pins correctly.
In the Raspberry Unix terminal, navigate to the directory of the application code and execute the code using python command,
pi@raspberrypi:~/iot/AlibabaIoTCloudDemo $ python tempMonitor.py
You'll see bellow messages in the terminal as output,
Figure 8 Raspberry Pi Output
Temperature readings will be available in Table Store table as below,
Figure 9 Table Store Data
w1thermsensor.errors.NoSensorFoundError: No Unknown temperature sensor with id '' found
Run below commands to see if you can cat w1_slave
cd /sys/bus/w1/devices
ls
cd 28-XXXXXXXXXXXX (change the X's to your own address)
cat w1_slave
if you can't see any devices starting with 28, then either the DS18B20 incorrectly connected, faulty wiring or sensor is faulty. For connection, exactly follow the diagram given above.
Verify the messages are received and pushed to table store in Device Log section.
Figure 10 Device Log
View Database Tables as Standard Java Streams Using Speedment
2,599 posts | 762 followers
FollowGXIC - February 25, 2019
GXIC - January 7, 2019
GXIC - June 11, 2019
GXIC - June 11, 2019
Alibaba Clouder - November 28, 2019
Alibaba Developer - May 31, 2021
2,599 posts | 762 followers
FollowProvides 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 MoreA fully managed NoSQL cloud database service that enables storage of massive amount of structured and semi-structured data
Learn MoreLearn More
More Posts by Alibaba Clouder
Raja_KT February 8, 2019 at 5:02 pm
It goes to show that we can do monitor our home,houses , while in office or elsewhere. Many ready-made codes are available.