gpt4 book ai didi

python - 无法将数据发布到 Xively

转载 作者:行者123 更新时间:2023-11-28 18:42:08 25 4
gpt4 key购买 nike

我正在尝试将一些临时数据上传到 Xively 中的提要,但我无法让它正常工作! RPi 正在读取和打印数据,但是没有数据被推送到 Xively。我已经使用了教程代码并且能够获得该推送数据,但我似乎无法用我自己的代码复制它。我没有收到任何错误。

#!/usr/bin/env python

import os
import xively
import subprocess
import time
import datetime
import requests
import glob

# extract feed_id and api_key from environment variables
FEED_ID = 1374874100
API_KEY = 'ONdefUJlt3d5cifAvTYocqvOPm824F3P1QnT99OtdKQpL7ZD'
DEBUG = 'TRUE'


# initialize api client
api = xively.XivelyAPIClient(API_KEY)

os.system('modprobe w1-gpio')
os.system('modprobe w1-therm')

# My temp probe details
base_dir = '/sys/bus/w1/devices/'
device1_folder = glob.glob(base_dir + '28-00000503b862')[0]
device1_file = device1_folder + '/w1_slave'

# Function to read temp sensor
def read_temp_raw():
f = open(device1_file, 'r')
lines = f.readlines()
f.close()
return lines

def read_temp():
lines = read_temp_raw()
while lines[0].strip()[-3:] != 'YES':
time.sleep(0.2)
lines = read_temp_raw()
equals_pos = lines[1].find('t=')
if equals_pos != -1:
temp_string = lines[1][equals_pos+2:]
temp_c = float(temp_string) /1000
return temp_c

while True:
print("Temp is currently...", read_temp())
time.sleep(5)


# function to return a datastream object. This either creates a new datastream,
# or returns an existing one
def get_datastream(feed):
try:
datastream = feed.datastreams.get("FermTemp")
if DEBUG:
print "Found existing datastream"
return datastream
except:
if DEBUG:
print "Creating new datastream"
datastream = feed.datastreams.create("FermTemp", tags="temp_01")
return datastream


# main program entry point - runs continuously updating our datastream with the
# current temps
def run():
print "Getting Fermentation Temps"

feed = api.feeds.get(FEED_ID)

datastream = get_datastream(feed)
datastream.max_value = None
datastream.min_value = None

while True:
FermentationTemp = read_temp()
datastream.current_value = FermentationTemp
datastream.at = datetime.datetime.utcnow()
try:
datastream.update()
except requests.HTTPError as e:
print "HTTPError({0}): {1}".format(e.errno, e.strerror)

time.sleep(10)

run()

似乎只要我取出定义临时数据的位,它就会很高兴地连接到相关的 Xively channel 。

最佳答案

请检查“FermentationTemp”变量的返回类型。将“FermentationTemp”类型转换为字符串并尝试。

关于python - 无法将数据发布到 Xively,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24938596/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com