简体   繁体   English

用于轮询Raspberry Pi内部SoC温度的Python脚本

[英]Python script for polling Raspberry Pi internal SoC temperature

I have been cobbling together a script from multiple sources to poll my Raspberry Pi's internal SoC temperature. 我一直在整理来自多个来源的脚本来轮询Raspberry Pi的内部SoC温度。 I want to then stream that data to my Plotly account. 然后,我想将该数据流式传输到我的Plotly帐户。

I have a semi-working script but once the chip temp is read, it continues streaming that temperature indefinitely. 我有一个半工作脚本,但是一旦读取芯片温度,它将无限期地继续传输该温度。 As a noob to Python, I cannot seem to figure out how to take the temperature (on a user-set interval) and continuously update it with a fresh value. 作为Python的新手,我似乎无法弄清楚如何获取温度(按用户设置的时间间隔)并用新的值连续更新。 My code is below: 我的代码如下:

#!/usr/bin/env python

import plotly.plotly as py # plotly library
from plotly.graph_objs import * # all plotly graph objects
import json # used to parse config.json
import time # timer functions
import datetime
import os # used to acquire internal SoC temperature
import sys

# Initialize some variables with your credentials
with open('./config.json') as config_file:
    plotly_user_config = json.load(config_file)

username = plotly_user_config['plotly_username']
api_key = plotly_user_config['plotly_api_key']
stream_token = plotly_user_config['plotly_streaming_tokens'][0]

# Initialize a Plotly Object
py.sign_in(username, api_key)

# Initialize your graph (not streaming yet)
data = [Scatter(
    x=[],y=[],
    mode='lines+markers',
    stream={'token': stream_token, 'maxpoints': 1000},
    name='UCBPD')
]
layout = Layout(
    title='Raspberry Pi Temperature',
    xaxis={'autorange': True, 'title': 'Time of Day'},
    yaxis={'autorange': True, 'title': 'Degrees (Celsuis)'}
)
your_graph_url = py.plot(Figure(data=data, layout=layout), filename='Raspberry Pi     Temp', auto_open=False)

# Acquire internal SoC temperature
cmd = '/opt/vc/bin/vcgencmd measure_temp'
line = os.popen(cmd).readline().strip()

if "error" in line:
    print "Error ... is your firmware up-to-date? Run rpi-update"
else:
  # line now contains something like: temp=41.2'C
  # to get the temperature, split on =, and then on '

    temp = line.split('=')[1].split("'")[0]

# Initialize the Plotly Streaming Object
stream = py.Stream(stream_token)
stream.open()

# Start looping and streaming!
while True:
    stream.write({'x': datetime.datetime.now(), 'y': temp})
    time.sleep(1) # delay between stream posts

Whatever code sets the value of the temp (temperature) variable needs to be in the while loop or else there is no way the temperature variable can change. 无论使用什么代码设置temp (温度)变量的值,都必须在while循环中,否则温度变量就无法更改。

If you continue to have trouble you should simplify this by removing the config file and the graphing, and just print raw readings to the console. 如果您仍然遇到问题,则应通过删除配置文件和图形来简化此过程,并将原始读数打印到控制台。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM