Exemplo n.º 1
0
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)


# Update the log on the heartbeat event.
device.onHeartbeat(saveTempCelsius)

# Run the heartbeat every 30 seconds.
device.pulse(period=30)
Exemplo n.º 2
0
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)

# Update the log on the heartbeat event.
device.onHeartbeat(saveTempCelsius)

# Run the heartbeat every 30 seconds.
device.pulse(period=30)
# 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,
    )


# Bind the output to the heartbeat event.
device1.onHeartbeat(printTempCelsius)
device2.onHeartbeat(printRelHumidity)

# Watch both devices in the same loop, triggering their heartbeats ever
# 10 seconds.
watcher.start(period=10)
Exemplo n.º 4
0
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()
humi.heartbeat()
# 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)


# Bind the output to the heartbeat event.
device1.onHeartbeat(printTempCelsius)
device2.onHeartbeat(printRelHumidity)

# Watch both devices in the same loop, triggering their heartbeats ever
# 10 seconds.
watcher.start(period=10)