Close

Interrupt handler

A project log for ESP8266 Geiger counter

Simple Geiger counter using ESP8266 PWM for HV generation and network connectivity

biemsterbiemster 08/30/2016 at 17:550 Comments

Today I built the whole circuit on the breadboard, attached the tube and played a bit with the PWM duty to see if it registers something. The circuit keeps the D1 pin of the ESP high, and pulls it to ground if the tube discharges. This is the code that I hoped would then record the event:

from machine import Pin, PWM

cumulative_count = 0

def init(...):
    ...
    # geiger discharge irq handler (pin5=D1)
    discharge = Pin(event_pin, Pin.IN)
    discharge.irq(trigger=Pin.IRQ_FALLING, handler=geiger_discharge_handler)

...

def geiger_discharge_handler():
    # this handler is called everytime the tube discharges
    cumulative_count += 1
    buzz()

def buzz()
    ...
However, it seems not as easy as I thought it was (although I tried to follow the official MicroPython for ESP8266 docs).

When this runs with a PWM duty of 60%, I start getting this in the REPL at random intervals on average every 2 seconds:

NameError:
NameError:
Just this error twice, without any error message. (Well wait, now I think of it the timing of the intervals of this error message is about the same as what I would expect from background radiation detection in the tube, can it be that the rest of the built actually works??).

If I put a print in the handler

print('click!')
I get the following error, at the same random intervals:
TypeError:
Again no error message. I'll have to dive into the restrictions of the IRQ handlers a bit more to see what I'm doing wrong.

But on a side note, everything else seems to work! The handler is called at random intervals, consistent with the tube registering background radiation! Now to figure out how to write a proper handler that actually does something else than throwing an error. Expect the next log titled "SUCCESS!" and with some pictures of a working GM counter soon!

Update:

I found this paragraph in the docs. It explains why I don't see an error report on the throws (simply because the ISR is not able to like this).

Adding the following lines to the code should enable error reporting in the ISR:

import micropython
micropython.alloc_emergency_exception_buf(100)

Also, it seems that I should not call a function inside my ISR, and the global variable 'cumulative_count' should be defined

global cumulative_count

in the ISR. Let's see if those changes improve the situation

Update2:

micropython.alloc_emergency_exception_buf(100)

is not implemented on my version so I guess i need to recompile. Also, the ISR needed to accept a parameter:

def geiger_discharge_handler(p):

now with the global cumulative_count, this variable is increased on every count! Good progress, I only need to figure out how to call a function from the ISR so I can beep the buzzer, and start doing MQTT etc stuff. At the moment when I call the buzz() function I get a

MemoryError:

Discussions