Monitor Raspberry Pi CPU temperature changes through the Yeelink platform

Source: Internet
Author: User

Monitoring the Raspberry Pi temperature through the Yeelink platform is the first lesson for many of your friends to get started. As a novice who was just starting out on the Raspberry Pi bare metal, in the absence of other hardware support, the Yeelink platform was the first step in learning the Raspberry Pi and learning smart hardware by monitoring the CPU temperature change of the Raspberry Pi.

Implementation ideas

The idea of "Monitoring Raspberry Pi CPU temperature changes through the Yeelink platform" is simple, write a python applet (Python 2.7.9 is installed by default in SNAPPY UBUNTU core system), Get the CPU temperature of the Raspberry Pi and upload it to the Yeelink platform, then monitor the temperature changes on the Raspberry Pi online and send an email alert if the temperature is too high.

Step 1th: Write a python program that gets the Raspberry Pi CPU temperature every 60 seconds

This program is relatively simple to implement, save the following code as 01cpu_temp.py, through the Python 01cpu_temp.py can be executed.

#Coding=utf-8Import Time#Get CPU Temperaturedefget_cpu_temp (): Cpu_temp_file= Open ("/sys/class/thermal/thermal_zone0/temp") Cpu_temp=Cpu_temp_file.read () cpu_temp_file.close ( )returnFloat (cpu_temp)/1000defMain (): whileTrue:strftime=time.strftime ("%y-%m-%dt%h:%m:%s")        Print "Time :", StrftimePrint "cpu_temp:", Get_cpu_temp ()#Sleep 60 secondsTime.sleep (60)if __name__=='__main__': Main ()
2nd step: Become the Yeelink platform developer and complete the configuration of the Raspberry Pi device and sensor 1) registered users, get API KEY

Yeelink is a free and open IoT cloud platform that provides sensor data access, storage and presentation services for all open source software and hardware enthusiasts, manufacturing enterprises, to provide a platform for IoT projects, so that hardware and manufacturers can not care about the server implementation details and operation of the case, The ability to deliver IoT-connected electronic products. For enthusiasts and developers, Yeelink offers a range of toolkits and development kits that enable kids to make IoT toys with Yeelink, and everyone can be a dreamer, using code downloaded from the Yeelink website, combined with open source hardware or yeelink dedicated hardware, Almost without any programming, you can create quite complex iphone remote switches, remote water heaters, or DIY works such as PM2.5 detectors.

Yeelink platform address is http://www.yeelink.net/, registration is very simple, after registration, we can in the "Account settings" to obtain the exclusive API KEY.

API key is very important, the next step to modify the Python program upload data to the Yeelink platform will be used.

2) New equipment

Add a new device to my device and my new device name is "Raspberry Pi 2 Model B".

3) New sensor

On the Manage Devices page, add a new numeric sensor named "CPU temperature sensor".

After adding the sensor, we will find a URL message below the data curve on the right side of the sensor:

Url:http://api.yeelink.net/v1.0/device/19283/sensor/33814/datapoints

This is a very important piece of information, the next step to modify the Python program upload data to the Yeelink platform will be used.

Http://api.yeelink.net/v1.0/: Indicates that the API version is v1.0;

device/19283: Indicates that the device number we just added is 19283;

sensor/33814: Indicates that the sensor number we just added is 22814;

DataPoints: Represents a collection of data points, a datapoint is a key-value pair consisting of key and value, and the numeric sensor has a key of timestamp, value is the value;

In more detail we can view the API documentation for the Yeelink platform Http://www.yeelink.net/develop/api.

4) New Sensor trigger action

New sensor trigger action to achieve CPU temperature warning: When the data is greater than 48, notify me by email "Raspberry Pi temperature over 48 degrees Celsius, need to cool!!!!! ”。

3rd Step: Modify the Python applet and upload the data to the Yeelink platform regularly

relative to the 1th step of the program, the main increase Yeelink API configuration and Upload_cpu_temp_to_yeelink () two segment code.

This program is named 02upload_cpu_temp_to_yeelink.py and then executed by Python 02upload_cpu_temp_to_yeelink.py to upload CPU temperature data to the Yeelink platform every 60 seconds.

#Coding=utf-8Import TimeImportJSONImportRequests#Yeelink API ConfigurationApi_url='http://api.yeelink.net/v1.0'Api_key='Api_key' #please fill in the dedicated API keyapi_headers={'U-apikey': Api_key,'Content-type':'Application/json'}raspi_device_id=19283cpu_sensor_id=33814#Get CPU Temperaturedefget_cpu_temp (): Cpu_temp_file= Open ("/sys/class/thermal/thermal_zone0/temp") Cpu_temp=Cpu_temp_file.read () cpu_temp_file.close ( )returnFloat (cpu_temp)/1000#upload CPU temperature to Yeelinkdefupload_cpu_temp_to_yeelink (): URL=r'%s/device/%s/sensor/%s/datapoints'%(api_url,raspi_device_id,cpu_sensor_id) strftime=time.strftime ("%y-%m-%dt%h:%m:%s")    Print "Time :", strftime cpu_temp=get_cpu_temp ()Print "cpu_temp:", cpu_temp data={"timestamp": Strftime,"value": cpu_temp} res=requests.post (url,headers=api_headers,data=json.dumps (data))Print "Status_code:", Res.status_codedefMain (): whileTrue:upload_cpu_temp_to_yeelink ()#Sleep 60 secondsTime.sleep (60)if __name__=='__main__': Main ()

4th step: View CPU temperature data and temperature warning online

The temperature data of the Raspberry Pi can be viewed online via http://www.yeelink.net/devices/19283 (the device number can be modified).

The crest of the figure is more than 50 degrees, because I used a hair dryer to temporarily increase the temperature of the Raspberry Pi, so as to reach the trigger temperature of more than 48 degrees to send email alerts.

But today do not know Yeelink server or QQ mailbox what reason, and did not receive e-mail alerts, had to paste out a few days before the implementation to prove.

Learning Summary

This example is simple, but the whole process and Yeelink platform architecture is worthy of our deep research and learning, the future of certain internet of things era, cloud data age.

Getting started with Python and advanced Learning: Http://www.imooc.com/course/list?c=python

Understanding RESTful Architecture: http://kb.cnblogs.com/page/114905/

Monitor Raspberry Pi CPU temperature changes through the Yeelink platform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.