0 PrefaceThis article reads Raspberry Pi CPU temperature through file operations. In Linux, any device operations are abstracted as file read/write, read the content in the/sys/class/thermal/thermal_zone0/Temp File to get the Raspberry Pi CPU temperature. This article describes how to read and apply the temperature parameter -- 1. shell script Operations; 2 Linux file IO operations; 3. python file operations; 4. upload data to the yeelink platform using Python requset. There are similar articles on the Internet. The general method is to obtain CPU temperature data through python, then write the data into a file, read the file content through the curl script, and send it to yeelink, to improve file reading efficiency, mount the cached temperature file to ram. The method in this article is simpler. The CPU temperature is obtained through python, and then uploaded directly to yeelink through the requset module. [Related blog] [Raspberry Pi Study Notes-index blog] [curl Study Notes-combined with yeelink platform] [Raspberry Pi Study Notes-yeelink remote control led]
1 shell operationFirst, warm up through shell operations. Log on to Raspberry Pi and run the following command to view the CPU temperature: # Enter the directory CD/sys/class/thermal/thermal_zone0 # view the temperature cat temp # Raspberry Pi returns 48692 from the above operations you can get the following [1] Raspberry Pi CPU temperature information is located in the file /sys/class/thermal/thermal_zone0/temp, this file is a read-only file. [2] According to the information on the Internet and the actual situation, the returned temperature parameter should be divided by 1000, in units of degree Celsius.
2 C language file I/O operationsCreate a file named cpu-temp.c with the following content:
# Include <stdio. h> # include <stdlib. h> # include <sys/types. h> # include <sys/STAT. h> # include <fcntl. h> # define temp_path "/sys/class/thermal/thermal_zone0/Temp" # define max_size 32int main (void) {int FD; double temp = 0; char Buf [max_size]; // open/sys/class/thermal/thermal_zone0/temp FD = open (temp_path, o_rdonly); If (FD <0) {fprintf (stderr, "failed to open thermal_zone0/Temp \ n"); Return-1;} // read the content if (read (FD, Buf, max_size) <0) {fprintf (stderr, "failed to read Temp \ n"); Return-1 ;}// convert to floating point number print temp = atoi (BUF)/1000.0; printf ("Temp: %. 2f \ n ", temp); // close the file close (FD );}
Run code: Enter the following command under the cpu-temp.c directory to generate an executable file and then execute it. # Compile the link GCC-O test cpu-temp.c # Run./test # Run the returned temp: 49.2
2 Use python to get the temperatureCompared with C language file I/O operations, python is simpler. Compile a file named yeelink-temp.py with the following content:
! #/Usr/bin/Python #-*-coding: UTF-8-*-# open the file = open ("/sys/class/thermal/thermal_zone0/Temp ") # Read the result and convert it to a floating point temp = float (file. read ()/1000 # close the file. close () # print "Temp: %. 1f "% Temp
Enter the following command in the yeelink-temp.py directory to execute the script
# Execute scripts
Python yeelink-temp.py
# Execution return
Temp: 49.2
3. Use the request to upload data to yeelinkContinue to modify yeelink-temp.py
! #/Usr/bin/Python #-*-coding: UTF-8-*-import requests import JSON # open the file = open ("/sys/class/thermal/thermal_zone0/Temp") # Read the result, and convert it to floating point temp = float (file. read ()/1000 # close the file. close () # print the result print "Temp: %. 1f "% TEMP # device URI apiurl = 'HTTP: // users' # user password, specify the upload code as JSON format apiheaders = {'u-apikey': 'ffa3826972d6cc7ba5b17e%ec5xxxx ', 'content-type': 'application/json'} # dictionary-type data, which is JSON during post. dumps to JSON Format String {"value": 48.123} payload = {'value': temp} # Send request r = requests. post (apiurl, headers = apiheaders, Data = JSON. dumps (payload) # print the return code print "response status: % d" % R. status_code
Run code enter the following command under the yeelink-temp.py directory to execute the script # Run the script Python yeelin-temp.py # Run the returned temp: 49.2 response status: 200
[Description] [1] requests. Post (API, headers, data) requires three code parameters: Device Uri, request header (including user password), and push data (in JSON format ). [2] payload = {'value': temp} load data is written in the python dictionary format. [3] DATA = JSON. dumps (payload) converts a dictionary to a JSON string by using the dumps method.
[Required verification] Temperature parameters are uploaded through Python scripts. to verify whether the upload is successful, you can use the curl command to query the latest sensor data. When writing a blog post, the yeelink platform API is being updated and tested. The new and old APIs are called to view the temperature response results. [Yeelink V1.0 version API] Write a shell script -- read-sensor-temp-v1.0.sh, the specific content is as follows:
APIKEY="U-ApiKey:ffa3826972d6cc7ba5b17e104ec5xxxx" APIURL="http://api.yeelink.net/v1.0/device/1949/sensor/2510/datapoints" curl --request GET --header $APIKEY $APIURL
[Returned results] {"timestamp": "2014-07-28t20: 56: 51", "value": 49.23}
Yeelink V1.1 API: Modify the API version number in the preceding script, and change V1.0 to V1.1. From the returned results, we can find that the API version 1.1 returns more information. [Returned results] {"value": 49.23, "timestamp": "2014-07-28t20: 56: 51", "sensor_id": "2510", "device_id": "1949 "}
4. Summary and prospects[1] JSON. dumps (payload) to convert the temperature results into JSON string format [2] below the blog modify the yeelink-temp.py, so that the boot operation, scheduled upload temperature data.
5 References[1] Let Raspberry Pi say his "temperature"-CPU temperature + GPU Temperature