Mathieu Passenaud
Published © GPL3+

Raspberry PI push button to IoT PaaS

Use GPIO on your Raspberry PI, detect events and push the change to an IoT PaaS

BeginnerProtip1,199
Raspberry PI push button to IoT PaaS

Things used in this project

Story

Read more

Schematics

IMG_20151123_104421.jpg

Code

Push GPIO event to openTSDB

Python
Use the opentsdb example given
# -*- 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
    

OpenTSDB Python library

Credits

Mathieu Passenaud

Mathieu Passenaud

3 projects • 3 followers
DevOps @ OVH Group

Comments