GPIO status 1 or 0 to database

Hi all

Im trying to get some data in my influxdb database. But I can´t get any data into my database :frowning:

I have installe Influxdb on my Raspberry Pi 3.
The sensor (I/0) is connected directly to GPIO pin 4 on my raspberry.

Now I just wanna log the status of the sensor I or 0 together with a timestamp.

Can anyone help me? I know some python or maybe else the CLI? What will work?

im running: InfluxDB Admin UI: v1.0.2 Server: v1.0.2

Hope someone can help me, havent been able to find a similar case on google.

Best regards Rasmus

@Rsanderhoff We have a number of ways to get data into the database. Telegraf (our collector) has a ipmi_sensor plugin that might work for your usecase.

Otherwise you can use the python client library to pull the sensor data and write it into the database.

Let me know if you have any issues!

Hi jackzampolin :smile:

Thanks for your reply. I prefer to use python eventhough im quite new with python and coding in general.

I have created a code that is "printing the value I want to store in my influxdb.
The code is checking the status (0/1) of GPIO pin 4 every second for one minute, and then printing the value (a number between 0 and 60)

This value I would like to go into my influxdb with at timestamp. And then create a graph in openhab so I can monitor the status from remote.

Heres my code I have in python for printing the “count” value:

import time
import RPi.GPIO as GPIO

GPIO.setmode(GPIO.BCM)
GPIO.setup(4,GPIO.IN)

while True:
count = 0

for loop_count in range(0,60):
sensor = GPIO.input(4) # 0, 1
if sensor == 1:
count += 1
time.sleep(1)

print count, “-”, time.strftime("%d-%m-%Y %H:%M:%S")

I tried with the cod below, but its not working. :frowning:

#!/usr/bin/python

import time

import RPi.GPIO as GPIO

import json

import math

import requests

import sys

IP = “192.168.0.100” # The IP of the machine hosting your influxdb instance

DB = “furnace” # The database to write to, has to exist

USER = “Johansen” # The influxdb user to authenticate with

PASSWORD = “1234” # The password of that user

GPIO.setmode(GPIO.BCM)

GPIO.setup(4,GPIO.IN)

while True:

count = 0

for loop_count in range(0,10):

  sensor = GPIO.input(4) # 0, 1

  if sensor == 1:

   count += 1

   time.sleep(1)

print count, “-”, time.strftime("%d-%m-%Y %H:%M:%S")

r = requests.post(“http://%s:8086/write?db=%s” %(IP, DB), auth=(USER, PASSWORD), data=[count])

if r.status_code != 204:

print 'Failed to add point to influxdb (%d) - aborting.' %r.status_code

sys.exit(1)

@Rsanderhoff The POST request you have is not properly formatted. Can you try using the python client?