# -*- coding: utf8 -*-
import RPi.GPIO as GPIO
import time
from openTSDB.Metric import Metric
from openTSDB.OpenTSDBProfile import OpenTSDBProfile
from openTSDB.OpenTSDBPusher import OpenTSDBPusher
# Place your token id here
token_id = 'xxxxxxxxxxxxxxxxxxxxxx'
# Place your token here
token_key = 'xxxxxxxxxxxxxxxxxxxxxx'
# IoT Lab OpenTSDB Endpoint
end_point = 'https://opentsdb.iot.runabove.io/api/put'
openTSDBProfile = OpenTSDBProfile(token_id, token_key, end_point, 0)
#################################
# def my callback for GPIO event
#################################
def gpio24_callback(channel):
ts = int(time.time())
if GPIO.input(24):
print("up")
metric = Metric("GPIO24", 1, ts, {"source" : "OVH-iot-pi-example"})
else:
print("down")
metric = Metric("GPIO24", 0, ts, {"source" : "OVH-iot-pi-example"})
openTSDBPusher.pushData(openTSDBProfile, metric)
try:
GPIO.setup(24, GPIO.IN, pull_up_down=GPIO.PUD_DOWN)
GPIO.add_event_detect(24, GPIO.BOTH, callback=gpio24_callback, bouncetime=75)
except KeyboardInterrupt:
GPIO.cleanup()
stop = False
while True:
if stop == True :
GPIO.cleanup()
break
Comments