Python Forum
first code - need some explaining of logic
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
first code - need some explaining of logic
#1
Hello,
I wrote a code that send UDP,print to terminal \ save to log file whenever I press a button on PI,
and when I press ENTER in the terminal it exit the code.
the code is working - but I can't seem to understand the logic - hope you can help me with this

this is the code:
import socket  #Import udp library
import RPi.GPIO as GPIO # Import Raspberry Pi GPIO library
import json  # import json read\write library
import time # import get time option library
import logging # import make log option  library

def button_callback(channel):
    print("Button was pushed!")
    CurrentTime = str(time.ctime())
    MESSAGE = (CurrentTime + " :User " +str(data['user']) + " -- " + str(data['ID']))
    sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) # UDP
    sock.sendto(MESSAGE.encode(), (UDP_IP, UDP_PORT))
    print (MESSAGE)
    logging.basicConfig(filename='/home/pi/Desktop/message.log',level=logging.DEBUG)
    logging.debug(MESSAGE)
    time.sleep(1)

## - I'm reading this part only 1 time at the startup of the code 
with open('/home/pi/Documents/config.json') as json_file:
    data = json.load(json_file)
    print("user is " + str(data['user']) )
    print ("ID  is " + str(data['id']))
## setting of UDP 
UDP_IP = "10.0.0.51"
UDP_PORT = 9051

## the main code ?
GPIO.setwarnings(False) # Ignore warning for now
GPIO.setmode(GPIO.BOARD) # Use physical pin numbering
GPIO.setup(10, GPIO.IN, pull_up_down=GPIO.PUD_DOWN) # pin 10 to be an input pin and set initial value to be pulled low (off)
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback) # Setup event on pin 10 rising edge
message = input("Press enter to quit\n\n") # Run until someone presses enter
GPIO.cleanup() # Clean up
1. it the code writting in the correct way? do I need to change the order of the setting?
2. I cant seem to understand this line
GPIO.add_event_detect(10,GPIO.RISING,callback=button_callback
how does it works?
3. why the code is in loop? (it's a good thing - but I don;t understand why)
4. why does only "Enter" exit the program ?
5. how does callback work ?
6. what does this line do? what is channel ?
def button_callback(channel):
is it like a sub function to the main?

Thanks ,
Reply
#2
That's a lot of questions for a code you supposedly wrote yourself....

For some of them - https://sourceforge.net/p/raspberry-gpio...ki/Inputs/
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#3
I started to it yesterday , and took every part from the internet
after I finish and got what I needed - I see that I don't understadn what I did
which is not good for me , because I want to know and understand and not just copy\paste from the internet ....

so can you amswer my questions? I want to learn

Thanks ,
Reply
#4
(May-07-2019, 08:59 AM)korenron Wrote: so can you amswer my questions? I want to learn
I've edited my answer, because I post unfinished
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
by the way, I think part of your code is missing, because there is no loop
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#6
OK - very helpful
now I understadn the callback function and the GPIO setting

but
why the code is still in loop ?
shouldn't it run only 1 time ? or wait until I press "Enter"?
and also why "Enter" to exit? where do I see it?

Thanks ,


*just saw your replay
and this is what I don't understadn - why loop?
this is teh full code I wrote - you can try it yourself and see
Reply
#7
I cannot try because don't have Pi and GPIO board
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#8
When you say press button, do you mean keyboard button?
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#9
no - a physical button
isn't nothing you can do ?
very vert starnge (the code , not you :-) )
Reply
#10
(May-07-2019, 10:03 AM)korenron Wrote: no - a physical button
OK, that makes sense.
It's a guess but I think event_detection() function listens for events in a parallel (separate) thread and will fire interrupt when button is pressed. So the main thread is blocked from your message = input() line.
when you press a button that is wired to pin 10 it will detect change and call the button_callback function. As you can see from the link I posted above call backs are run in separate tread (check Threaded callback section)
If I am right your program will end not only when you press Enter, but any other keyboard key
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Explaining Code kozaizsvemira 2 1,787 Nov-07-2019, 06:53 PM
Last Post: kozaizsvemira
  Please help explaining this recursive function armeros 3 2,753 Nov-14-2018, 05:55 AM
Last Post: armeros

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020