renowden
Posts: 6
Joined: Sat Nov 12, 2016 8:18 am
Location: Bristol, England
Contact: Website

Spurious interrupt?

Sat Nov 12, 2016 8:34 am

Hi - I have a program I am using to monitor a pendulum clock which works fine. However when I added a DS18B20 temperature sensor I started to get what looked like a spurious GPIO interrupt. I am at a loss to discover what is causing this. I have stripped the code to the bare essentials and tested it and it still shows the problem. It is independent of which pin I use to detect the pulse. The separate button sense is only there to stop the program as CTRL-C doesn't do it, otherwise I have to use "sudo kill".

Code: Select all

#!/usr/bin/env python

# Clock monitor program

import RPi.GPIO as GPIO
import time
import os

PULSE_SENSE_PIN = 17
BUTTON_PIN = 18
TEMPERATURE_DEVICE = "/sys/bus/w1/devices/28-0000073ddbf8/w1_slave"

def Initialise_GPIO():
	GPIO.setmode(GPIO.BCM)
	GPIO.setup(BUTTON_PIN, GPIO.IN)
	GPIO.setup(PULSE_SENSE_PIN, GPIO.IN)
	os.system("modprobe w1-gpio")
	os.system("modprobe w1-therm")

def Write_log(now_time, temp):
	# Append to the file
	log_file = open ("clock.log", "a")
	str_time = time.strftime("%Y-%m-%d,%H:%M:%S", time.localtime(now_time))
	miliseconds = (now_time % 1) * 1000
	log_string = "%s.%03i %.1f\n" % (str_time, miliseconds, temp)
	log_file.write(log_string)
	log_file.close()
	print log_string

def Raw_temp():
	t = open(TEMPERATURE_DEVICE)
	lines = t.readlines()
	t.close()
	return lines

def Temperature():
	data = Raw_temp()
	while data[0].strip()[-3:] != "YES":
		time.sleep(0.1)
		text = Raw_temp()
	tt = data[1].split(" ")[9]
	return float(tt[2:]) / 1000.0

# Interrupt mode when pulse is received

def Pulse_callback(pin):
	global interval_count
	now_time = time.time() # in seconds since the epoch (float)
	print "time=", time.asctime( time.localtime(now_time)), ".", (now_time % 1) * 1000
	interval_count -= 1
	if interval_count > 0:
		return
	Write_log(now_time, Temperature())
	interval_count = 10

# The main program

Initialise_GPIO()
interval_count = 10

# We are detecting the training edge of the pulse here
GPIO.add_event_detect(PULSE_SENSE_PIN, GPIO.RISING, callback=Pulse_callback, bouncetime=100)
while True: # Now service interrupts until the button is pressed.
	GPIO.wait_for_edge(BUTTON_PIN, GPIO.FALLING)
	GPIO.cleanup()
	break

A sample of the output is below. The realclock pulses at 30 second intervals but you can see that after each 10th pulse when I collect the temperature that there is an extra pulse about 820ms later.

Code: Select all

time= Sat Nov 12 08:09:33 2016 . 856.426954269
time= Sat Nov 12 08:10:03 2016 . 37.535905838
time= Sat Nov 12 08:10:33 2016 . 37.9140377045
time= Sat Nov 12 08:11:03 2016 . 37.9920005798
time= Sat Nov 12 08:11:33 2016 . 35.7570648193
time= Sat Nov 12 08:12:03 2016 . 34.8780155182
time= Sat Nov 12 08:12:33 2016 . 34.2879295349
time= Sat Nov 12 08:13:03 2016 . 36.712884903
time= Sat Nov 12 08:13:33 2016 . 38.7270450592
time= Sat Nov 12 08:14:03 2016 . 37.2750759125
2016-11-12,08:14:03.037 18.8

time= Sat Nov 12 08:14:03 2016 . 856.573104858
time= Sat Nov 12 08:14:33 2016 . 36.8630886078
time= Sat Nov 12 08:15:03 2016 . 37.3780727386
time= Sat Nov 12 08:15:33 2016 . 35.5999469757
time= Sat Nov 12 08:16:03 2016 . 36.7639064789
time= Sat Nov 12 08:16:33 2016 . 38.4850502014
time= Sat Nov 12 08:17:03 2016 . 38.407087326
time= Sat Nov 12 08:17:33 2016 . 38.4240150452
time= Sat Nov 12 08:18:03 2016 . 39.5829677582
time= Sat Nov 12 08:18:33 2016 . 38.2490158081
2016-11-12,08:18:33.038 18.8

time= Sat Nov 12 08:18:33 2016 . 866.270065308
time= Sat Nov 12 08:19:03 2016 . 37.691116333
time= Sat Nov 12 08:19:33 2016 . 38.8429164886
time= Sat Nov 12 08:20:03 2016 . 39.5340919495
time= Sat Nov 12 08:20:33 2016 . 40.5609607697
time= Sat Nov 12 08:21:03 2016 . 40.4450893402
time= Sat Nov 12 08:21:33 2016 . 40.7791137695
time= Sat Nov 12 08:22:03 2016 . 39.2189025879
time= Sat Nov 12 08:22:33 2016 . 37.6479625702
time= Sat Nov 12 08:23:03 2016 . 37.7190113068
2016-11-12,08:23:03.037 18.9

time= Sat Nov 12 08:23:03 2016 . 856.252908707
time= Sat Nov 12 08:23:33 2016 . 37.8129482269
time= Sat Nov 12 08:24:03 2016 . 41.0211086273
time= Sat Nov 12 08:24:33 2016 . 41.3289070129
DOes anyone have any suggestions please?

User avatar
davef21370
Posts: 897
Joined: Fri Sep 21, 2012 4:13 pm
Location: Earth But Not Grounded

Re: Spurious interrupt?

Mon Nov 14, 2016 7:20 pm

From what I gather there are 10 pulses recording the time and the tenth also records the temperature?
If that's correct you aren't getting an "extra" pulse, there are still only 10 times recorded. My thought is that it's something to do with the global interval_count but I'm not in a position to test anything.

Dave.
Apple say... Monkey do !!

renowden
Posts: 6
Joined: Sat Nov 12, 2016 8:18 am
Location: Bristol, England
Contact: Website

Re: Spurious interrupt?

Fri Nov 18, 2016 8:38 am

That is right - the interval count is working correctly, but if you look at the times you will see that the first one of each set is only milliseconds after the last one of the previous set so what are supposed to be the 10 x 30 second pulses don't add up to 5 minutes. The first one of each set is actually a spurious one. The only thing I can see that could be causing it is the temperature sensor - if I remove that one call then it works perfectly.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Spurious interrupt?

Fri Nov 18, 2016 9:47 am

Have you an up to date version of RPi,GPIO? It may be a fixed problem.

I note you are modprobing the w1 modules which has been deprecated for quite some time.

renowden
Posts: 6
Joined: Sat Nov 12, 2016 8:18 am
Location: Bristol, England
Contact: Website

Re: Spurious interrupt?

Fri Nov 18, 2016 11:54 am

RPi.GPIO should be recent as I built and updated fresh for this project only a few weeks ago but I will check that.

Thanks for the tip about w1 - I find it very hard to get reliable and up to date information on stuff like this and many "sample projects" online are not dated. I will check that out and see if it makes a difference. These will both be next week now.

What I have just discovered is that if I use .wait_for_edge instead of .add_event_detect on PULSE_SENSE_PIN and just check BUTTON_PIN using .input after each pulse then it is ok. So it is a combination of event_detect with w1 that causes the problem.

User avatar
joan
Posts: 14960
Joined: Thu Jul 05, 2012 5:09 pm
Location: UK

Re: Spurious interrupt?

Fri Nov 18, 2016 12:02 pm

I tried the script on my Pi and it seemed to work okay, which is why I thought you might have an old RPi.GPIO.

Use device tree now for modules.

dtoverlay=w1-gpio

or

dtoverlay=w1-gpio,gpiopin=X

is all you need in /boot/config.txt.

renowden
Posts: 6
Joined: Sat Nov 12, 2016 8:18 am
Location: Bristol, England
Contact: Website

Re: Spurious interrupt?

Tue Nov 22, 2016 10:00 am

This is very curious. I have now updated/upgraded raspbian to the very latest version. I have removed the calls to modprobe. I have disconnected from the clock from sense pin 17 (as it is a very noisy interface) and simulated it with a button on pin 14. And I STILL get the problem.

I can click the button 10 times and on the tenth one I get the log message and ~820ms later I get an extra pulse thrown in.

Is there a problem with my Pi? Can any one else reproduce this - it only requires the temperature sensor and two buttons (the second one to stop the program)?

Return to “Python”