3

I'm trying to upload some temp data to a feed in Xively but for the life of me I just cannot get it to work! The data is being read and printed by the RPi however no data is being pushed to Xively. I've used the tutorial code and was able to have that push data but I can't seem to replicate that with my own code. I get no errors.

#!/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()

It seems as though as soon as I take out the bit that defines the temp data, it's happy to then connect to the relevant Xively channel.

Ahmed Ashour
  • 5,179
  • 10
  • 35
  • 56

1 Answers1

1

Please check the return type of 'FermentationTemp' variable. Typecast 'FermentationTemp' into string and try .

rose
  • 162
  • 1
  • 1
  • 13