QMESAR
Posts: 55
Joined: Wed Sep 06, 2017 10:41 am

UDP and IF Statement confusion

Mon Aug 26, 2019 5:26 pm

Dear All
Please help I am sending from my Android phone over UDP a Command ON of OFF which the PI should then switch the LED opn or OFF
at BCM pin 18 ,I run my script in Thonny with the debugger ,I can see the data bytes are correct in the variable window I see data as b'ON' or b'OFF' but the if statements always evaluate as false ,example in the ON case if I do step in debugging I can see the data[0] = 79 dec and it shows the 'O' the same for the data[1] but then it shows false and jump over switch ON the pin statement/command
what am I doing wrong :oops:

Code: Select all

while True:
 data,addr = sock.recvfrom(1024)
 
 if data[0] == 'O' and data[1] == 'N':
     GPIO.output(18,ON)
     elif data[0] == 'O' and data[1] == 'F' and data[2] == 'F':
     GPIO.output(18,OFF)     
 elif data[0] == '#':
     sock.close
     sys.exit()

drgeoff
Posts: 10765
Joined: Wed Jan 25, 2012 6:39 pm

Re: UDP and IF Statement confusion

Mon Aug 26, 2019 5:35 pm

Why is the first elif line indented?

QMESAR
Posts: 55
Joined: Wed Sep 06, 2017 10:41 am

Re: UDP and IF Statement confusion

Mon Aug 26, 2019 5:39 pm

Thank you for the reply however it was just a error in posting the code in the forum

Code: Select all

 if data[0] == 'O' and data[1] == 'N':
      GPIO.output(18,ON)
     
 elif data[0] == 'O' and data[1] == 'F' and data[2] == 'F':
      GPIO.output(18,OFF)
     
 elif  data[0] == '#':
      sock.close
      sys.exit()


drgeoff
Posts: 10765
Joined: Wed Jan 25, 2012 6:39 pm

Re: UDP and IF Statement confusion

Mon Aug 26, 2019 8:02 pm

Try using numerical values eg

Code: Select all

if data[0] == 79 ... 
etc

QMESAR
Posts: 55
Joined: Wed Sep 06, 2017 10:41 am

Re: UDP and IF Statement confusion

Thu Aug 29, 2019 2:51 pm

Hello,

Thank you for the reply and suggestion.
I have tried it but no luck .

Regards

User avatar
B.Goode
Posts: 10356
Joined: Mon Sep 01, 2014 4:03 pm
Location: UK

Re: UDP and IF Statement confusion

Thu Aug 29, 2019 3:02 pm

QMESAR wrote:
Thu Aug 29, 2019 2:51 pm
Hello,

Thank you for the reply and suggestion.
I have tried it but no luck .

Regards


What is the significance of the character 'b' when you write: "I see data as b'ON' or b'OFF' "

Maybe your expected content, that you are comparing with a constant, does not start at offset zero of the data variable?

hippy
Posts: 7731
Joined: Fri Sep 09, 2011 10:34 pm
Location: UK

Re: UDP and IF Statement confusion

Thu Aug 29, 2019 3:19 pm

QMESAR wrote:
Thu Aug 29, 2019 2:51 pm
I have tried it but no luck .
It should have worked -

Code: Select all

data = b'ON'
print( data )
print( data[0] == 'O' )      # False - Doesn't match
print( data[0] == b'O' )     # False - Doesn't match
print( data[0] == ord('O') ) # True
print( chr(data[0]) == 'O' ) # True
print( data[0] == 79 )       # True
print( data[0] == 0x4F )     # True
print( data == b'ON')        # True

Code: Select all

pi@Pi3B:~/tmp $ python3 bytetest.py
b'ON'
False
False
True
True
True
True
True
pi@Pi3B:~/tmp $ 
I would use that last option -

Code: Select all

while True:
 data,addr = sock.recvfrom(1024)
 if data == b'ON':
   ...
 elif data == b'OFF':
   ...

pcmanbob
Posts: 9464
Joined: Fri May 31, 2013 9:28 pm
Location: Mansfield UK

Re: UDP and IF Statement confusion

Thu Aug 29, 2019 3:48 pm

Have you actually tried changing your code so it prints on the screen what is actually in data[0], data[1], etc

Code: Select all

while True:
 data,addr = sock.recvfrom(1024)
 print(data[0])
 print(data[1])
 print(data[2])
 if data[0] == 'O' and data[1] == 'N':
     GPIO.output(18,ON)
     elif data[0] == 'O' and data[1] == 'F' and data[2] == 'F':
     GPIO.output(18,OFF)     
 elif data[0] == '#':
     sock.close
     sys.exit()
     
you may find that they don't contain what you are expecting............
We want information… information… information........................no information no help
The use of crystal balls & mind reading are not supported

User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: UDP and IF Statement confusion

Thu Aug 29, 2019 4:39 pm

If data = b'ON' then the type of data is bytes and the type of one element e.g. data[0] is integer.

This is why trying to compare data[0] == 'O' or data[0] == b'O' fails, an integer is neither a string nor a bytes so will always fail to be equal.
She who travels light — forgot something.

QMESAR
Posts: 55
Joined: Wed Sep 06, 2017 10:41 am

Re: UDP and IF Statement confusion

Fri Aug 30, 2019 3:04 pm

Hi All

Thank you for your replies ,
I had not much time to play with my PI the last week due to work commitments, I will try all suggestions in the next days and hope fully
learn something from it

QMESAR
Posts: 55
Joined: Wed Sep 06, 2017 10:41 am

Re: UDP and IF Statement confusion

Sun Sep 01, 2019 7:35 am

Dear All

Thank you very much for the comments and info much appreciated-
just to answer a few of the comments made above

(1) my android is sending the bytes I expect it too send , as said in my first post I see the ON characters in the pi however confusing me
is the fact that the in the Thony IDE watch window I see b'ON'
(2) my Android is sensing bytes over UDP and it seems python has a problem with bytes/ characters as such at this point in time a newbie with python (coming from the embedded C world) this is still an open topic to me to learn and understand.
(3) one comment mentioned try to select on data[0] == b'O and data[1] == b'N' this gives many syntax errors during compile time
(4) another comment mention select on the numerical value such as O = dec 79 and N = 78 thus data[0] == 79 and data[1] == 78 This work
perfectly and my RPI does with the led exactly what command is send from my android ON or OFF

my take on this is I have to learn how python handle strings and bytes/ variables

Code: Select all

while True:
    data,addr = sock.recvfrom(1024)
    if data[0] == 79 and data[1] == 78 :
       GPIO.output(LED1,True)
    elif data[0] ==79 and data[1] == 70 and data[2] == 70:
        GPIO.output(LED1,False)
    elif data[0] == 35:
        sock.close()
        sys.exit()


User avatar
Paeryn
Posts: 2966
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: UDP and IF Statement confusion

Sun Sep 01, 2019 2:06 pm

As hippy said earlier, testing the whole data rather than individual elements is better especially for readability as long as data isn't going to have extra characters after the ON or OFF.

Code: Select all

if data == b'ON':
    ....
If data == b'OFF':
    ....
The thing with the type btyes is that it is an array of 8-bit unsigned integers which can be represented by a string of characters where each character represents one byte (and the string literal is prefixed with b to distinguish it from any other string). A normal Python string is a string of UTF-8 characters where each character can be anywhere from one to four bytes long (the standard ASCII characters only take one byte).
She who travels light — forgot something.

QMESAR
Posts: 55
Joined: Wed Sep 06, 2017 10:41 am

Re: UDP and IF Statement confusion

Sun Sep 01, 2019 2:50 pm

Thank you very much for the explanation appreciated
I will work a bit on all of this string vs byte stuff in python it is clear it is an area where I have problems

Return to “Beginners”