Monitor temperature using Raspberry Pi + Alibaba cloud IoT platform

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.

Overview

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.

  1. Monitor the temperature of your home aquarium.
  2. Detects and receives safety alarms such as gas leaks, fires, and unauthorized intrusions. 3, you can monitor the humidity and receive notification of rain drops. 4, monitor the status of electrical appliances and electronic devices.

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. image.png Figure 1 Data flowchart

Prerequisites

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)

image.png Figure 2 Circuit diagram

Raspberry environment settings

  1. Log in to the Raspberry Pi and enable the 1-Wire interface in the Raspberry Pi settings. image.png Figure 3 Raspberry Pi 1-Wire configuration

  2. 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

Set up IoT devices on Alibaba Cloud

  1. Log in to the IoT Platform Console and select the Region China (Shanghai) Region.

  2. Create a basic product with the node type as a device image.png Figure 4 Product setup

  3. Add the device under the product created in the previous step. Make sure the device state is enabled as shown below. image.png Figure 5 Device settings

  4. Create a rule with JSON data type, click the admin button and set the following options.

  5. Write an SQL expression to select the temperature attribute value that can be used in the JSON data published by Raspberry. image.png Figure 6 SQL query expression

  6. 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. image.png Figure 7 Data transfer rules

Python application walkthrough

  1. Download / clone the python sample code from the github repository. https://github.com/itexpertshire/AlibabaIoTCloudDemo
  2. Import the IoT python SDK, W1TermSensor, base64 library.
from aliyunsdkcore import client
from aliyunsdkiot.request.v20170420 import RegistDeviceRequest
from aliyunsdkiot.request.v20170420 import PubRequest
import time
import base64
from w1thermsensor import W1ThermSensor
  1. Initialize the following variables with your cloud account credentials and IoT device key.
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'
  1. Initialize the message object "request" with the topic name and product key.
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.
  1. Initialize the temperature sensor method before reading the temperature from the sensor
sensor = W1ThermSensor()

  1. In an infinite loop, use 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)

Run and test

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. image.png Figure 8 Raspberry Pi output The temperature readings are displayed in the table in the table store as shown below. image.png Figure 9 Table store data

trouble shooting

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. image.png Figure 10 Device log

Recommended Posts

Monitor temperature using Raspberry Pi + Alibaba cloud IoT platform
Connect Raspberry Pi to Alibaba Cloud IoT Platform with Python
Create your own IoT platform using raspberry pi and ESP32 (Part 3) ~ ESP32 settings Analog temperature sensor
Create your own IoT platform using raspberry pi and ESP32 (Part 1)
Detect temperature using python on Raspberry Pi 3!
Send data from Raspberry Pi using AWS IOT
Try using the temperature sensor (LM75B) on the Raspberry Pi.
Create your own IoT platform using raspberry pi and ESP32 (Part 2) ~ ESP32 setting L Chika
Indoor monitoring using Raspberry Pi
Detect "temperature (using A / D converter)" using python on Raspberry Pi 3!
I tried using the DS18B20 temperature sensor with Raspberry Pi
Creating a temperature / humidity monitor with Raspberry Pi (pigpio version)
How to get temperature from switchBot thermo-hygrometer using raspberry Pi
USB over ethernet using Raspberry pi
Try using ArUco on Raspberry Pi
Using a webcam with Raspberry Pi
[Python + PHP] Make a temperature / humidity / barometric pressure monitor with Raspberry Pi
Detect "brightness" using python on Raspberry Pi 3!
Make Raspberry Pi speak Japanese using OpenJtalk
Run servomotor on Raspberry Pi 3 using python
Using the 1-Wire Digital Temperature Sensor DS18B20 from Python on a Raspberry Pi
Detect slide switches using python on Raspberry Pi 3!
Try using a QR code on a Raspberry Pi
Detect magnet switches using python on Raspberry Pi 3!
Automatic launch of Raspberry Pi programs using Systemd
Easy connection between Raspberry Pi and AWS IoT
Sound the buzzer using python on Raspberry Pi 3!
Create a visitor notification system using Raspberry Pi
Display CPU temperature every 5 seconds on Raspberry Pi 4
Get temperature and humidity with DHT11 and Raspberry Pi
Connect your Raspberry Pi to your smartphone using Blynk
Raspberry Pi and AWS IoT connection program example
[Google Cloud Platform] Use Google Cloud API using API Client Library
Measure CPU temperature of Raspberry Pi with Python