komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 9:33 am

Heater wrote:
Thu Aug 03, 2017 9:15 am
Your script sleeps for 0.1 seconds then does this:

Code: Select all

while (GPIO.input(pin) == GPIO.LOW):
		count += 1
Which is a tight loop that hogs 100% of the CPU until the pin goes low. And I guess could take a long time.

I have no idea how your input is wired now but why do you need to be measuring an rc time?

Surely you only need to check if the pin is high or low every 0.1 seconds of so?
i've found this part on the web (maybe adafruit forum) and it solved my problem to have an analogical reading of the ldr... i didn't change it because it just worked like this.
i've tried to make a normal (digital) read of the gpio of the ldr but it was always TRUE with the capacitor or always FALSE with a resistor... so i've found this solution

Heater
Posts: 13912
Joined: Tue Jul 17, 2012 3:02 pm

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 10:07 am

OK. Keep the rc time if you need it.

But you still have a CPU hogging tight loop in there. It may work fine but it is not a pleasing way for a program to behave.
Memory in C++ is a leaky abstraction .

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 10:09 am

Heater wrote:
Thu Aug 03, 2017 10:07 am
OK. Keep the rc time if you need it.

But you still have a CPU hogging tight loop in there. It may work fine but it is not a pleasing way for a program to behave.
i see what you mean
but
can you give me an alternative solution to get an analogical input from gpio?
this loop works somehow and returns a low value when the ldr is excited... unfortunately i didn't find any other solution even thou i would love very much to decrease the cpu usage

Heater
Posts: 13912
Joined: Tue Jul 17, 2012 3:02 pm

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 10:32 am

As I said, I have no idea how your input is wired up.

Seems to me that as it is just a button press there is no need for any analog measurement at all. The button is either pressed or not. The GPIO input is a high voltage or not. The logical state in the code is 1 or 0.

There for. Your code need not be making any analog measurement at all. Just check for 0 or 1 every 0.1 seconds or whatever minimum time is required not to miss a button press.

I presume there is some confusion here. That he article you are basing your code on is intended to measure light levels with an LDR and therefor implements an RC time constant analog voltage measurement technique. You don't need that. It's either on or not. Your button is a digital thing not an analog one.

I suspect what you have is a 12v AC signal driving a LED. Which shines on the LDR. So you now have 50 or 60Hz pulses coming out of the LDR. I'd be inclined to filter that with a capacitor to get more constant DC level. That level should be either high or low depending on the button state. Then your code only need to check the input for 0 or 1.
Memory in C++ is a leaky abstraction .

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 10:38 am

Heater wrote:
Thu Aug 03, 2017 10:32 am
As I said, I have no idea how your input is wired up.

Seems to me that as it is just a button press there is no need for any analog measurement at all. The button is either pressed or not. The GPIO input is a high voltage or not. The logical state in the code is 1 or 0.

There for. Your code need not be making any analog measurement at all. Just check for 0 or 1 every 0.1 seconds or whatever minimum time is required not to miss a button press.

I presume there is some confusion here. That he article you are basing your code on is intended to measure light levels with an LDR and therefor implements an RC time constant analog voltage measurement technique. You don't need that. It's either on or not. Your button is a digital thing not an analog one.

I suspect what you have is a 12v AC signal driving a LED. Which shines on the LDR. So you now have 50 or 60Hz pulses coming out of the LDR. I'd be inclined to filter that with a capacitor to get more constant DC level. That level should be either high or low depending on the button state. Then your code only need to check the input for 0 or 1.
i wish it could work like this
but unfortunately the digital read of the gpio is always the same (dont remember if TRUE or FALSE, but i can check)... despite the state of the led in front of the ldr.
and, yes, the ldr is connected to 3v3, gpio and gnd wit ha capacitor.


ps.
this is the article:
https://learn.adafruit.com/basic-resist ... ll-reading

Heater
Posts: 13912
Joined: Tue Jul 17, 2012 3:02 pm

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 10:48 am

Can you post your actual schematic?

You should be able to arrange that the output from the LDR is either high or low. 3.3v or 0v.

This may require a suitable arrangement of divider resistors and or a filter capacitor.

If it were me I might do a couple of extra things:

1) Run the output of the LDR circuit through a schmitt trigger circuit so as to get a nice sharp ON or OFF digital signal into the GPIO.

2) Rather than poll the GPIO input with a loop have the input cause some function to run when it changes from low to high or vice versa.

I have no idea how to do 2) in Python but I believe there is a way to respond to such events in Python. I use Javascript under node,js for these kind of things. It's much easier.
Memory in C++ is a leaky abstraction .

Heater
Posts: 13912
Joined: Tue Jul 17, 2012 3:02 pm

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 10:56 am

That ada fruit article is totally not what you want.

A schmitt trigger circuit need not be complex. Just use a 74xx244 chip. Like the Gert board does.
Memory in C++ is a leaky abstraction .

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 11:29 am

i changed the script like this:

Code: Select all

def pinReset():
	GPIO.setup(pin, GPIO.OUT)
	GPIO.output(pin, GPIO.LOW)
	time.sleep(0.1)
	GPIO.setup(pin, GPIO.IN)

while True:
	time.sleep(0.1)
	if (GPIO.input(pin)):
		try:
			print("1. ha suonato il CAMPANELLO")
			with picamera.PiCamera() as camera:
				camera.vflip = True
				camera.resolution = (720, 500)
				camera.capture("/home/pi/Documents/PY/campanello/foto.jpg", quality = 35)
			print("2. ho scattato la FOTO")
			print("3. avviso MQTT ")
			mqttc.connect("192.168.1.3", 1883)
			mqttc.publish("campanello", "1")
			print("4. spedisco la MAIL")
			SendMail("/home/pi/Documents/PY/campanello/foto.jpg")
			mqttc.loop(2)
			print("FINITO")
			print("  ")
			pinReset()
		except Exception as ecc:
			print(ecc)
and now the script takes 0.5% of CPU

everything seems to work much better
but sometimes the loop (taking picture and sending it) repeat twice... don't know why... like the LDR stay excited for a while


ps.
thank you again for your hints

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 11:40 am

i will test it for a while and tell you the results
anyway
to have a Pi running at 5% instead of 100% is totally differently responsive

Heater
Posts: 13912
Joined: Tue Jul 17, 2012 3:02 pm

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 1:59 pm

That's much nicer.

Sounds like sometimes your button presses are longer than 0.1 seconds. So the body of the loop gets run more than once per press.

What you could do is ensure that the button goes low again at the end of the loop. That would remove double hits.

Code: Select all

while GPIO.input(pin):
   # Do nothing waiting for button to be released
Memory in C++ is a leaky abstraction .

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 2:26 pm

Heater wrote:
Thu Aug 03, 2017 1:59 pm
That's much nicer.

Sounds like sometimes your button presses are longer than 0.1 seconds. So the body of the loop gets run more than once per press.

What you could do is ensure that the button goes low again at the end of the loop. That would remove double hits.

Code: Select all

while GPIO.input(pin):
   # Do nothing waiting for button to be released
yes i did it
now it seems to work just fine
except for one electrical problem.

sometimes when i open the gate
somehow the gpio turns on
it's probably some voltage peak
considering the gate is opened by a relay turned on by the same 12VAC line

any idea to fix that?
it's happening not often but annoying

Heater
Posts: 13912
Joined: Tue Jul 17, 2012 3:02 pm

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 3:03 pm

I don't know how your gate and button are wired.
Memory in C++ is a leaky abstraction .

User avatar
davidcoton
Posts: 4257
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 3:09 pm

komarek wrote:
Thu Aug 03, 2017 2:26 pm
sometimes when i open the gate
somehow the gpio turns on
it's probably some voltage peak
considering the gate is opened by a relay turned on by the same 12VAC line

any idea to fix that?
Does the Pi know when the gate release is pressed? If it does ( like if you use the Pi to activate the relay) then use the gate open signal to inhibit the bell push input.
If it doesn't, then use a similar isolator circuit as before to add a "Gate is being opened" signal, and use that to inhibit as above.
Signature retired

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 4:10 pm

Heater wrote:
Thu Aug 03, 2017 3:03 pm
I don't know how your gate and button are wired.
i will try to make a diagram

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 4:14 pm

davidcoton wrote:
Thu Aug 03, 2017 3:09 pm
komarek wrote:
Thu Aug 03, 2017 2:26 pm
sometimes when i open the gate
somehow the gpio turns on
it's probably some voltage peak
considering the gate is opened by a relay turned on by the same 12VAC line

any idea to fix that?
Does the Pi know when the gate release is pressed? If it does ( like if you use the Pi to activate the relay) then use the gate open signal to inhibit the bell push input.
If it doesn't, then use a similar isolator circuit as before to add a "Gate is being opened" signal, and use that to inhibit as above.
usually i open the gate with a MQTT command
i can set the Pizero to intercept the command and push the gpio to zero... if that will be quick enough

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 5:27 pm

davidcoton wrote:
Thu Aug 03, 2017 3:09 pm
komarek wrote:
Thu Aug 03, 2017 2:26 pm
sometimes when i open the gate
somehow the gpio turns on
it's probably some voltage peak
considering the gate is opened by a relay turned on by the same 12VAC line

any idea to fix that?
Does the Pi know when the gate release is pressed? If it does ( like if you use the Pi to activate the relay) then use the gate open signal to inhibit the bell push input.
If it doesn't, then use a similar isolator circuit as before to add a "Gate is being opened" signal, and use that to inhibit as above.
you suggest me to trigger another gpio to tell the Pi that the gate is being opened? and then to inhibit that part of the script...

User avatar
davidcoton
Posts: 4257
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 7:55 pm

komarek wrote:
Thu Aug 03, 2017 5:27 pm
you suggest me to trigger another gpio to tell the Pi that the gate is being opened? and then to inhibit that part of the script...
Yes, exactly that. The MQTT signal will probably work just as well, should be able to catch that on the gate Pi.
Signature retired

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 8:51 pm

ok this thing is a pain in the ***
i tried to fix it via software
i've made a complicate loop, bell-picture system and MQTT client together:

Code: Select all

apriCancello = False

def on_connect(client, userdata, flags, rc):
	#print("Connected with result code "+str(rc))
	client.subscribe("aa/bb")

def on_message(client, userdata, msg):
	messaggio = str(msg.payload)
	global apriCancello
	if "openGate" in messaggio:
		apriCancello = True
	

def pinReset():
	GPIO.setup(pin, GPIO.OUT)
	GPIO.output(pin, GPIO.LOW)
	time.sleep(0.1)
	GPIO.setup(pin, GPIO.IN)

while True:
	time.sleep(0.1)
	try:
		client = mqtt.Client()
		client.on_connect = on_connect
		client.on_message = on_message
		client.connect("192.168.1.3",1883,60)
	except Exception as eccloop:
		print(eccloop)
	else:
		print("ciclo")
		if apriCancello:
			print("stanno aprendo il cancello")
			time.sleep(3)
		print(apriCancello)
		if (GPIO.input(pin) and apriCancello == False):
			try:
				print("1. ha suonato il CAMPANELLO")
				with picamera.PiCamera() as camera:
					camera.vflip = True
					camera.resolution = (720, 500)
					camera.capture("/home/pi/Documents/PY/campanello/foto.jpg", quality = 35)
				print("2. ho scattato la FOTO")
				print("3. avviso MQTT ")
				mqttc.connect("192.168.1.3", 1883)
				mqttc.publish("campanello", "1")
				print("4. spedisco la MAIL")
				SendMail("/home/pi/Documents/PY/campanello/foto.jpg")
				#mqttc.loop(2)
				print("FINITO")
				print("  ")
				pinReset()
			except Exception as ecc:
				print(ecc)
		apriCancello = False		
		client.loop()
the loop is maybe correct
but
it seems that the the electrical pulse to the led/ldr come first than the mqtt command to the script.
so the opening sequence begin.

so... when i open the gate from my phone:
1. a mqtt command has been sent to my central server(mqtt broker)
2. the central server open a relay that send a pulse to the gate relay to open
3. when the pulse arrives the same line of the bell (apparently) seems to have a pulse too and the led turn on... and so on...

in the meantime:
4. my script receives the MQTT command from the central broker and
5. the variable apriCancello turn True... but it's too late because the picam sequence already began

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 8:57 pm

davidcoton wrote:
Thu Aug 03, 2017 7:55 pm
komarek wrote:
Thu Aug 03, 2017 5:27 pm
you suggest me to trigger another gpio to tell the Pi that the gate is being opened? and then to inhibit that part of the script...
Yes, exactly that. The MQTT signal will probably work just as well, should be able to catch that on the gate Pi.
ok so...
probably the best way to fix this would be
to intercept the line that trigger the relay that opens the gate... and to connect through an opto-isolator to another gpio of the PI... when this gpio is True obviously the other gpio input (the one triggered by the bell) is ignored.

CORRECT?
sounds like alot of work to do.

and... what about to put just a capacitor on the line that from the bell-press is turning the led on? would it avoid the peak from the other line?

User avatar
davidcoton
Posts: 4257
Joined: Mon Sep 01, 2014 2:37 pm
Location: Cambridge, UK

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 9:12 pm

komarek wrote:
Thu Aug 03, 2017 8:57 pm
ok so...
probably the best way to fix this would be
to intercept the line that trigger the relay that opens the gate... and to connect through an opto-isolator to another gpio of the PI... when this gpio is True obviously the other gpio input (the one triggered by the bell) is ignored.

CORRECT?
sounds like alot of work to do.

and... what about to put just a capacitor on the line that from the bell-press is turning the led on? would it avoid the peak from the other line?
Without investigation with an oscilloscope, to see what is really happening, it's all guesswork.
Try the easy things first.
You could try to work out how long the pulses are (the genuine one and the false one) by measuring in your code. Then you can see whether you can tell the difference, or whether you need the inhibit signal.
You could try to time from the bell trigger to the MQTT inhibit, to know how much too late the inhibit arrives. Then you could try to delay the response to the bell trigger, and check whether the MQTT inhibit has arrived before takingf any action.
I'm sure there are other things to try as well. Welcome to real engineering!
Signature retired

komarek
Posts: 146
Joined: Fri Mar 31, 2017 1:19 am

Re: Bricked my Pi. don't know why

Thu Aug 03, 2017 9:50 pm

davidcoton wrote:
Thu Aug 03, 2017 9:12 pm
komarek wrote:
Thu Aug 03, 2017 8:57 pm
ok so...
probably the best way to fix this would be
to intercept the line that trigger the relay that opens the gate... and to connect through an opto-isolator to another gpio of the PI... when this gpio is True obviously the other gpio input (the one triggered by the bell) is ignored.

CORRECT?
sounds like alot of work to do.

and... what about to put just a capacitor on the line that from the bell-press is turning the led on? would it avoid the peak from the other line?
Without investigation with an oscilloscope, to see what is really happening, it's all guesswork.
Try the easy things first.
You could try to work out how long the pulses are (the genuine one and the false one) by measuring in your code. Then you can see whether you can tell the difference, or whether you need the inhibit signal.
You could try to time from the bell trigger to the MQTT inhibit, to know how much too late the inhibit arrives. Then you could try to delay the response to the bell trigger, and check whether the MQTT inhibit has arrived before takingf any action.
I'm sure there are other things to try as well. Welcome to real engineering!
Or... :)
I change everything
And I build up some sort of crafty mini switch with 2 small slices of copper to place below the bell button. And then I will just have a simple button to trigger all the hell

Lol


Return to “General discussion”