In questo caso verra' impiegato Python per dialogare con Xively come descritto a questo tutorial
Prima di lanciare lo script si devono eseguire i seguenti comandi che impostano Python e scaricano la libreria Xively per Python
$ sudo apt-get install git
$ sudo apt-get install python-setuptools
$ sudo easy_install pip
$ sudo pip install virtualenv
$ mkdir xively_tutorial
$ mkdir xively_tutorial
$ cd xively_tutorial
$ virtualenv .envs/venv
$ source .envs/venv/bin/activate
$ pip install xively-python
terminata la configurazione si puo' lanciare il seguente script in Python che invia Xively i dati sul carico di lavoro. Come nel caso precedente si devono eseguire delle personalizzazione sulla base delle propri settaggi del device su Xively inserendo la chiave, il feed_id ed il nome del feed (vedi righe evidenziate in giallo)
--------------------------------------------------------------
#!/usr/bin/env python
import os
import xively
import subprocess
import time
import datetime
import requests
# extract feed_id and api_key from environment variables
FEED_ID = 302563049
API_KEY = "KPpjdurjzx9jVhp8PwpMCd6byMSatfcKuOu1CHJbdDkxXrI4"
DEBUG = "true"
# initialize api client
api = xively.XivelyAPIClient(API_KEY)
# function to read 1 minute load average from system uptime command
def read_loadavg():
if DEBUG:
print "Reading load average"
return subprocess.check_output(["awk '{print $1}' /proc/loadavg"], shell=True)
# 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("load_avg")
if DEBUG:
print "Found existing datastream"
return datastream
except:
if DEBUG:
print "Creating new datastream"
datastream = feed.datastreams.create("load_avg", tags="load_01")
return datastream
# main program entry point - runs continuously updating our datastream with the
# current 1 minute load average
def run():
print "Starting Xively tutorial script"
feed = api.feeds.get(FEED_ID)
datastream = get_datastream(feed)
datastream.max_value = None
datastream.min_value = None
while True:
load_avg = read_loadavg()
if DEBUG:
print "Updating Xively feed with value: %s" % load_avg
datastream.current_value = load_avg
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()
$ virtualenv .envs/venv
$ source .envs/venv/bin/activate
$ pip install xively-python
terminata la configurazione si puo' lanciare il seguente script in Python che invia Xively i dati sul carico di lavoro. Come nel caso precedente si devono eseguire delle personalizzazione sulla base delle propri settaggi del device su Xively inserendo la chiave, il feed_id ed il nome del feed (vedi righe evidenziate in giallo)
--------------------------------------------------------------
#!/usr/bin/env python
import os
import xively
import subprocess
import time
import datetime
import requests
# extract feed_id and api_key from environment variables
FEED_ID = 302563049
API_KEY = "KPpjdurjzx9jVhp8PwpMCd6byMSatfcKuOu1CHJbdDkxXrI4"
DEBUG = "true"
# initialize api client
api = xively.XivelyAPIClient(API_KEY)
# function to read 1 minute load average from system uptime command
def read_loadavg():
if DEBUG:
print "Reading load average"
return subprocess.check_output(["awk '{print $1}' /proc/loadavg"], shell=True)
# 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("load_avg")
if DEBUG:
print "Found existing datastream"
return datastream
except:
if DEBUG:
print "Creating new datastream"
datastream = feed.datastreams.create("load_avg", tags="load_01")
return datastream
# main program entry point - runs continuously updating our datastream with the
# current 1 minute load average
def run():
print "Starting Xively tutorial script"
feed = api.feeds.get(FEED_ID)
datastream = get_datastream(feed)
datastream.max_value = None
datastream.min_value = None
while True:
load_avg = read_loadavg()
if DEBUG:
print "Updating Xively feed with value: %s" % load_avg
datastream.current_value = load_avg
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()
Nessun commento:
Posta un commento