我正试图上传一些临时数据到feed inXively中,但对于我来说,我就是无法让它工作!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()

似乎只要我取出定义temp数据的位,就很高兴连接到相关的Xively通道。

最佳答案

请检查“酵素温度”变量的返回类型。将“发酵温度”键入字符串并尝试。

09-11 17:33
查看更多