无法将数据发布到Xively

时间:2014-07-24 15:56:56

标签: python xively

我正在尝试将一些临时数据上传到Xively中的Feed中,但对于我的生活,我无法让它工作! 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频道。

1 个答案:

答案 0 :(得分:1)

请检查' FermentationTemp'的退货类型。变量。 Typecast' FermentationTemp'进入字符串并尝试。