dangerweenie
Posts: 2
Joined: Sat Oct 08, 2016 4:39 am

log door sensor to txt file

Sat Oct 08, 2016 4:43 am

Hi,

I have a raspberry pi 3 with a magnetic door sensor.

i'd like to log the data for the open/close state throughout the day and then have it either posted (on a web server HTML) or as a .txt file with a timestamp.

basically, i have a dog walker and I want to see what time they open and close the door, to evaluate how long they are walking him.

I know this is a pretty 'large' question describing a rather large process, but I'd appreciate any advice pointing me in the right direction. I'm familiar enough with opening ports on my router for dyndns services to go through.

Thanks very much!

ban6
Posts: 6
Joined: Tue Sep 27, 2016 8:40 am

Re: log door sensor to txt file

Sat Oct 08, 2016 3:17 pm

The web app is easy (if it is view only). After sensing the data, you can write it to a file in the folder of the web app. For each request, you can read data from the file and present it with HTML as a table (check table HTML tag).

If you plan to use Python for the web app, I recommend Flask. It is very easy to start with (appropriate for your use case).

JavaFX
Last edited by ban6 on Sat Dec 10, 2016 1:04 pm, edited 1 time in total.

gordon77
Posts: 4070
Joined: Sun Aug 05, 2012 3:12 pm

Re: log door sensor to txt file

Sat Oct 08, 2016 4:31 pm

Here's a simple python script to log to a file.

Door switch between GPIO pins 26 (GPIO 7) and pin 25 (gnd)

You'll get a log file like this ...

16/10/09 - 18:15:01 0
16/10/09 - 18:15:15 1
16/10/09 - 18:20:10 0

Code: Select all

import datetime
import time
import RPi.GPIO as GPIO
GPIO.setwarnings(False)
GPIO.setmode(GPIO.BOARD)
DS = 26
GPIO.setup(DS,GPIO.IN,pull_up_down = GPIO.PUD_UP)

logfile = "log.txt"

start = GPIO.input(DS)

while True:
    
   while GPIO.input(DS) == start:
      time.sleep(.25)
   now = datetime.datetime.now()
   start = GPIO.input(DS)
   timp = now.strftime("%y/%m/%d-%H:%M:%S") + " " + str(start)
   print timp
   with open(logfile, "a") as file:
      file.write(timp +chr(10))
Last edited by gordon77 on Sat Oct 08, 2016 7:17 pm, edited 3 times in total.

dangerweenie
Posts: 2
Joined: Sat Oct 08, 2016 4:39 am

Re: log door sensor to txt file

Sat Oct 08, 2016 6:28 pm

You guys rock, thanks much.

Return to “Beginners”