Exemplo n.º 1
0
"""
An example showing how to keep a running log of a temperature sensor's readings
in a CSV file. Note: uses the Device.pulse method, which allows for watching
only one device at a time.
"""

from _examples import *

from ninja.api import NinjaAPI
from ninja.devices import TemperatureSensor

from datetime import datetime

# Set up the NinjaAPI and Device wrappers:
api = NinjaAPI(secrets.ACCESS_TOKEN)
device = TemperatureSensor(api, secrets.TEMP_ID)

# Prep the logfile with some headings
LOGFILE = 'temp_in_celsius.csv'
file(LOGFILE, 'w').write('Date,Temperature (C)\n')


# Log the current temperature reading to a file, adding each
# new entry of date and temperature in celsius to a new line.
def saveTempCelsius(inst, data):
    date = inst.last_read.isoformat()
    line = date + ',' + str(data.c) + '\n'
    print line[:-2]
    file(LOGFILE, 'a').write(line)

Exemplo n.º 2
0
in a CSV file. Note: uses the Device.pulse method, which allows for watching
only one device at a time.
"""

from _examples import *

from ninja.api      import NinjaAPI
from ninja.devices  import TemperatureSensor

from datetime       import datetime



# Set up the NinjaAPI and Device wrappers:
api     = NinjaAPI(secrets.ACCESS_TOKEN)
device  = TemperatureSensor(api, secrets.TEMP_ID)



# Prep the logfile with some headings
LOGFILE = 'temp_in_celsius.csv'
file(LOGFILE, 'w').write('Date,Temperature (C)\n')



# Log the current temperature reading to a file, adding each
# new entry of date and temperature in celsius to a new line.
def saveTempCelsius(inst, data):
    date = inst.last_read.isoformat()
    line = date + ',' + str(data.c) + '\n'
    print line[:-2]
process.
"""

from _examples import *

from ninja.api import NinjaAPI, Watcher
from ninja.devices import TemperatureSensor, HumiditySensor

from datetime import datetime

# Set up the NinjaAPI and Device wrappers:

# Access token from https://a.ninja.is/you#apiTab
api = NinjaAPI(settings.ACCESS_TOKEN)

device1 = TemperatureSensor(api, settings.TEMP_ID)
device2 = HumiditySensor(api, settings.HUMIDITY_ID)

# The watcher will provide a single loop for polling all of the devices.
watcher = Watcher(device1, device2)


# Output the temperature to stdio.
def printTempCelsius(inst, temperature):
    date = inst.last_read.isoformat()
    print '{date} - {id}: {temperature} C'.format(
        date=date,
        id=inst.guid,
        temperature=temperature.c,
    )
Exemplo n.º 4
0
from ninja.api import NinjaAPI
from ninja.devices import TemperatureSensor, HumiditySensor
from universal import universal_analytics
import settings

api = NinjaAPI(settings.ACCESS_TOKEN)

temp = TemperatureSensor(api, settings.TEMP)
humi = HumiditySensor(api, settings.HUMI)


def sendTempCelsius(inst, temperature):
    universal_analytics(webproperty=settings.WEBPROPERTY,
                        category='QuantifiedHome',
                        action='Temperature',
                        ln=str(int(temperature.c)),
                        lv=int(temperature.c))


def sendRelHumidity(inst, humidity):
    universal_analytics(webproperty=settings.WEBPROPERTY,
                        category='QuantifiedHome',
                        action='Humidity',
                        ln=str(int(humidity)),
                        lv=int(humidity))


temp.onHeartbeat(sendTempCelsius)
humi.onHeartbeat(sendRelHumidity)

temp.heartbeat()
"""

from _examples import *

from ninja.api import NinjaAPI, Watcher
from ninja.devices import TemperatureSensor, HumiditySensor

from datetime import datetime


# Set up the NinjaAPI and Device wrappers:

# Access token from https://a.ninja.is/you#apiTab
api = NinjaAPI(settings.ACCESS_TOKEN)

device1 = TemperatureSensor(api, settings.TEMP_ID)
device2 = HumiditySensor(api, settings.HUMIDITY_ID)

# The watcher will provide a single loop for polling all of the devices.
watcher = Watcher(device1, device2)


# Output the temperature to stdio.
def printTempCelsius(inst, temperature):
    date = inst.last_read.isoformat()
    print "{date} - {id}: {temperature} C".format(date=date, id=inst.guid, temperature=temperature.c)


def printRelHumidity(inst, humidity):
    date = inst.last_read.isoformat()
    print "{date} - {id}: {humidity}%".format(date=date, id=inst.guid, humidity=humidity)