help with script. - Raspberry Pi Forums


hi all,
using script take data currentcost envir monitor , upload iy xively.

works without error not way want.

keeps running continuously uploading every few seconds, want run once can set con job , set interval way.

not sure sets looping.

interval used in kwh calculation can't see interval set, running once muck calculation up?

code: select all

#!/usr/bin/env python   import serial import xml.etree.elementtree et import sys import requests import json import time import syslog   feed_id = 'feed_id' api_key = 'api_key_with_update_rights_for_your_feed' url = 'http://api.cosm.com/v2/feeds/' + feed_id + '.json'   def read_currentcost():   syslog.syslog("reading data currentcost")   print "reading currentcost"   sensor = none   watts = none   temp = none     while watts == none:     try:       line = ser.readline()       tree = et.xml(line)       watts = int(tree.findtext("ch1/watts"))       sensor = tree.findtext("sensor")       temp = float(tree.findtext("tmpr"))     except exception, inst:       sys.stderr.write("xml error: " + str(inst) + "\n")       line = none     ser.flushinput()   return sensor, watts, temp   def send_to_cosm(sensor, watts, temp):   print "sending cosm"   headers = { 'x-apikey': api_key }   payload = { 'version': '1.0.0', 'datastreams': [ { 'id': 'temperature', 'current_value': str(temp) }, { 'id' : 'watts', 'current_value': str(watts) }, { 'id' : 'kwh', 'current_value': kwh(watts, 6) } ] }   try:     r = requests.put(url, data=json.dumps(payload), headers=headers)   except exception, inst:     sys.stderr.write("http error: " + str(inst) + "\n")   def kwh(watts, interval):   divideramount = ((60.0 / interval) * 60)   kwh = (watts / divideramount) / 1000   return "%.6f" % kwh   def run():   print "starting currentcost.py"     while 1:     sensor, watts, temp = read_currentcost()       send_to_cosm(sensor, watts, temp)     ser = serial.serial('/dev/ttyusb0', 57600) ser.timeout = 20 ser.flushinput()   run() 

code: select all

def run():   print "starting currentcost.py"     while 1:     sensor, watts, temp = read_currentcost()       send_to_cosm(sensor, watts, temp)  
remove line "while 1:" , adjust indent of next 2 lines this...

code: select all

def run():   print "starting currentcost.py"     sensor, watts, temp = read_currentcost()     send_to_cosm(sensor, watts, temp)   

calculation seems have hard coded "6" interval in kwh function call parameters.
hth
petero

ps: go , read python tutorial !


raspberrypi



Comments

Popular posts from this blog

Convierte tu Raspberry en un NAS. Firmware fvdw-sl 15.3 - Raspberry Pi Forums

How to format a Get Request

avrdude: verification error, first mismatch at byte 0x0000 0x0c != 0x62