In this tutorial, we will demonstrate how to use ** Alibaba Cloud ** IoT Platform to establish 1way communication for message delivery from the end device to the cloud.
Alibaba Cloud IoT Platform allows you to easily and securely connect and manage data from various types of globally deployed electronic devices. You can ingest. IoT Platform with other cloud services (ApsaraDB, Quick BI ja / product / quickbi), Message Queue, etc.) to collect, process, analyze and visualize IoT data in real time. You can build a complete solution to help you improve your business efficiency.
There are two types of communication channels that can be configured between the end device and the IoT cloud. In 2-way communication, data can be sent and received using the same data channel, while in 1-way communication, data can be sent and received from the IoT device to the IoT platform. Below are some examples of projects that can be implemented using 1-way communication channels.
These projects can be further extended to control monitoring parameters using two-way communication. The best example would be a garden soil moisture controller. You can monitor the humidity of the soil and if the humidity falls below a certain threshold, you can signal the IoT device to switch on the sprinkler and allow the humidity to return to acceptable levels.
However, this tutorial only demonstrates one-way communication that sends a message from an end device to the IoT cloud. Raspberry PI measures the temperature using a temperature sensor every 60 seconds and publishes the temperature data to the Alibaba IoT cloud as a JSON serialized character string in the following format.
{ 'temperature': 12 }
Where 12 is the temperature measured by the sensor.
Published temperature readings are stored in the table setup of your Alibaba Cloud Table Store instance.
The figure below simplifies how data flows through the example system. Note that all the events in this figure occur asynchronously. Figure 1 Data flowchart
This tutorial is easy to understand and follow if you are familiar with the following techniques:
1, programming language Python 2. Construction of electronic circuits using breadboards 3, Alibaba cloud environment
** Hardware and software prerequisites ** Before working on this project, please build the electronic circuit according to the schematic below and make sure it is working properly. Also, download the following library to the Raspberry pi environment.
1, w1thermsensor python library (https://pypi.org/project/w1thermsensor/) 2. Python code example (https://github.com/itexpertshire/AlibabaIoTCloudDemo)
Figure 2 Circuit diagram
Log in to the Raspberry Pi and enable the 1-Wire interface in the Raspberry Pi settings. Figure 3 Raspberry Pi 1-Wire configuration
Open a shell terminal with Raspberry pi and install the following libraries.
sudo pip install aliyun-python-sdk-core
sudo pip install aliyun-python-sdk-iot
sudo pip install w1thermsensor
Log in to the IoT Platform Console and select the Region China (Shanghai) Region.
Create a basic product with the node type as a device Figure 4 Product setup
Add the device under the product created in the previous step. Make sure the device state is enabled as shown below. Figure 5 Device settings
Create a rule with JSON data type, click the admin button and set the following options.
Write an SQL expression to select the temperature attribute value that can be used in the JSON data published by Raspberry. Figure 6 SQL query expression
Define data transfer rules. The data extracted from the IoT message is stored in a table called myIOT created in the Alibaba Table Store instance. Tables in the table store have at least one primary key. The current time is included in the JSON message for use with the primary key. To get the time value from a published message, you need to use the following syntax $ {JSON message attribute}. The remaining JSON attributes are automatically stored as separate attributes in the Table Store table. Figure 7 Data transfer rules
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>'
4, To issue a message, the IoT cloud sets the default topic to / $ {productKey} / $ {deviceName} / update. Therefore, prepare the following character string of the public topic name.
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
to read the temperature and use the clt.do_action_with_exception
function to issue a message to the IoT device every 60 seconds.while 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 run the python code. Make sure the sensor is properly connected to the Raspberry GPIO pin.
In the Raspberry Unix terminal, go to the application code directory and use the python command to execute the code.
pi@raspberrypi:~/iot/AlibabaIoTCloudDemo $ python tempMonitor.py
The following message is output to the terminal. Figure 8 Raspberry Pi output The temperature readings are displayed in the table in the table store as shown below. Figure 9 Table store data
1, ** Python code that returns the following error ** w1thermsensor.errors.NoSensorFoundError. No unknown temperature sensor with ID'' found.
Execute the following command to see if cat w1_slave is possible.
cd /sys/bus/w1/devices
ls
cd 28-XXXXXXXXXXXX (change the X's to your own address)
cat w1_slave
If you don't see any devices that start with 28, either the DS18B20 isn't connected properly, the wiring is bad, or the sensor is bad. Follow the figure above exactly for the connection.
2, ** Message is not saved in table store ** Make sure the message is received and pushed to the table store in the device log section. Figure 10 Device log
Recommended Posts