0 PrefaceThis article uses Python file I/O operations to obtain Raspberry Pi CPU temperature information. The Python request library periodically uploads temperature information to the yeelink platform and modifies RC. the local script enables the Python script to run in the background at startup, and reports the temperature information to the platform every 5 minutes. I have read a lot of blog posts about Linux Startup on the Internet, and tried several times to find that the method cannot achieve the boot effect. I think it may be caused by minor differences in the operating system or development environment. If any problem exists in the content in the blog post, please leave a message in time and I will modify it after verifying it.
[Related blog] [Raspberry Pi Study Notes-Get Raspberry Pi CPU temperature] [Raspberry Pi Study Notes-index blog]-more blog posts please follow.
1. Modify the Python scriptModify the Python script in Raspberry Pi learning notes-Get Raspberry Pi CPU temperature. The interval is 5 minutes. Because the script runs in the background, after each CPU temperature parameter is uploaded, the temperature parameter is written to the TXT file in the same directory to verify whether the script is running successfully, whether the uploaded parameters are normal or not (yeelink platform does not have the list function, and it is troublesome to view all historical data) [Python script]
#! /Usr/bin/ENV Python #-*-coding: UTF-8-*-import requestsimport jsonimport timedef main (): filerecord = open ("result.txt", "W") filerecord. write ("connect to yeelink \ n"); filerecord. close () While true: # 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 () # device URI apiurl = 'HTTP: // users' # user password, specify the upload code as JSON format apiheaders = {'u-apikey': 'ffa3826972d6cc7ba5b17e417ec59fa3 ', '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 result filerecord = open ("result.txt", "A") strtime = time to the console. strftime ('% Y-% m-% d: % H-% m-% s', time. localtime (time. time () filerecord. writelines (strtime + "\ n") strtemp = "Temp: %. 1f "% TEMP +" \ n "filerecord. writelines (strtemp) filerecord. writelines (STR (R. status_code) + "\ n") filerecord. close () time. sleep (5*60) If _ name _ = '_ main _': Main ()
2 boot-add script[Auto-start.sh ]#! /Bin/bash CD/home/PI/Python-Works/yeelink-Temp Python yeelink-temp.py & [note] [1] & indicate script background run [2] sudo chmod A + x auto-start.sh
3 boot-Modify/etc/rc. LocalAdd a line before exit 0 with the content/home/PI/path/to/auto-start.sh start, change to the directory where the auto-start.sh is located. The modified RC. Local content is as follows:
#! /Bin/sh-e # RC. local ## this script is executed at the end of each multiuser runlevel. # Make sure that the script will "Exit 0" on success or any other # value on error. # In order to enable or disable this script just change the execution # bits. # By default this script does nothing. # print the IP address_ip =$ (hostname-I) | trueif ["$ _ IP"]; then printf "my IP address is % s \ n" "$ _ IP" fi # upload Raspberry Pi CPU temperature/home/PI/Python-Works/yeelink-temp/auto-start.sh to yeelink startexit 0
[Necessary verification] You can use the ps command to check whether the script is running in the background. PS aux | grep yeelink-temp.py
Figure 1 PS aux executes the returned results. Note that the returned results also contain the grep filter command, with the yeelink-temp.py PID 2836.
[Possible regret] If there is a problem with debugging, you can use the kill command to terminate the process, at this time the yeelink-temp.py process PID is 2836 kill-S 9 2836
4. Running result[Platform view]
Figure 2 view local logs on the platform are saved to the result.txt file. The air conditioner was turned off within 15 minutes, the indoor temperature increased rapidly, and the CPU temperature of Raspberry Pi increased from 42.2 to 47.1 degrees.
Figure 3 local view
5. Summary and references[1] modifying RC. Local enables Linux to automatically execute user programs after startup. [2] when the air conditioner in the room is turned off, the Raspberry Pi CPU temperature increases from 42.2 degrees to 47.1 degrees, in line with actual expectations, indicating that the experiment was successful. [3] Let's use another example to store the temperature to the database, and use another restful API to read historical temperature data. [Tucao] The yeelink platform has some problems in processing historical data. I thought I could use the API to obtain the sensor's historical data and set the query time. I read the documentation on the yeelink platform and found that this function is not available. In the yeelink user center, I opened the webpage debugging tool and found that the webpage called the API is sensor-data? The data returned by sensor_id = 2510 & timescale = 3600 is two arrays, one representing the data point and the other representing the Unix timestamp. From the perspective of API and returned data formats, there is no pursuit of a restful structure. I don't know if the developer has changed, so the style has changed. [Reference] [1] Raspberry Pi plug-in automatic logon and automatic running of programs [1] Understand Linux/etc/init. d directory and/etc/rc. local script-csdn blog [2] n methods for killing processes (kill) in Linux-csdn blog