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

Error with example code Socket Program

Sun Sep 08, 2019 3:30 pm

Dear All.

I am using the book Android Development from Prof Dogan Ibrahim ,This example of using a socket on the RPI to send the temperature read from the sense HAT to a Andriod device,
my Andrioud device app is running but when running the python script I get the following error

that sock.sendto(str(T),(UDP_IP,UDP_PORT)) needs a byte type not string ,Please help me how do I send the
temperature over the socket

Code: Select all

 #=================================================================
# get Temperature and send to phone(Android)
# SenseHAT + RPI 3B
#=================================================================
import socket                  #import Network Sockets
import time                    # import time module
from sense_hat import SenseHat #import Sensehat library                   
sense = SenseHat()             # create instance of Sense HAT
 
#---------------------------------------------------------------
# create socket and initialize network
UDP_PORT = 2000
UDP_IP = "192.168.1.160"        #Mobile IP adress
sock = socket.socket(socket.AF_INET,socket.SOCK_DGRAM)

#===============================================================
#Run App
while True: 
    T = sense.get_temperature() #read the temperature sensor
    T = round(T,1)                        #round off the results
    sock.sendto(str(T),(UDP_IP,UDP_PORT)) # send temperature to Android device
    time.sleep(2)                         # wait 2 seconds  


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

Re: Error with example code Socket Program

Sun Sep 08, 2019 4:03 pm

Noting that you are the only member of these forums encountering issues with the work of this author...

Have you considered asking the Professor for help with the book?


If you don't need a string, try not deliberately converting the value to a string before using it as an argument...

Code: Select all

  sock.sendto(str(T),(UDP_IP,UDP_PORT))

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

Re: Error with example code Socket Program

Sun Sep 08, 2019 4:19 pm

Python2 and Python3 handle strings differently and can cause this sort of error. Check that you are using the correct Python version.

User avatar
neilgl
Posts: 2183
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Error with example code Socket Program

Sun Sep 08, 2019 4:20 pm

The sock.sendto first parameter should be bytes not a string. So you want to convert the temperature string to bytes using .encode e.g.:

Code: Select all

# convert string to bytes
T = 23.456
print(T)
T = round(T,1)
mystring = str(T)
print(mystring)
b = mystring.encode('utf-8')
print(b)

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

Re: Error with example code Socket Program

Sun Sep 08, 2019 4:22 pm

Thank you for the reply ,

Are you saying that I am the only one having problems with the author examples ?

Yes I did consider asking the author however I do not see any contact information in the book
any how thank you for the reply again

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

Re: Error with example code Socket Program

Sun Sep 08, 2019 4:23 pm

Dear neilgl
Thank you for your kind reply and helpful example appreciated,
I am using python 3

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

Re: Error with example code Socket Program

Sun Sep 08, 2019 4:51 pm

QMESAR wrote:
Sun Sep 08, 2019 4:22 pm

Are you saying that I am the only one having problems with the author examples ?



No. Maybe there are others who do not post here.


But it is a matter of public record that you are the only forum user to mention this author and his book in these forums.

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

Re: Error with example code Socket Program

Sun Sep 08, 2019 5:22 pm

But it is a matter of public record that you are the only forum user to mention this author and his book in these forums.
I fail to understand the issue here is it not allowed here to ask a question related to the book or what difference it makes from where the example code comes it is python code .
Is it a problem for you me asking help in this forum using a RPI and some book if so , just say so .
Last edited by QMESAR on Sun Sep 08, 2019 5:31 pm, edited 1 time in total.

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

Re: Error with example code Socket Program

Sun Sep 08, 2019 5:28 pm

joan wrote:
Sun Sep 08, 2019 4:19 pm
Python2 and Python3 handle strings differently and can cause this sort of error.
Exactly that. Python 2 accepts a string, Python 3 expects bytes ...

Corrected cut-n-paste errors

Code: Select all

import socket
UDP_PORT = 2000
UDP_IP = "192.168.0.204"
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
T = 123.4
sock.sendto(str(T), (UDP_IP, UDP_PORT))
print("sent")

Code: Select all

pi@Pi3B:~/tmp $ python sockettest.py 
sent
pi@Pi3B:~/tmp $

Code: Select all

pi@Pi3B:~/tmp $ python3 sockettest.py 
Traceback (most recent call last):
  File "sockettest.py", line 6, in <module>
    sock.sendto(str(T), (UDP_IP, UDP_PORT))
TypeError: a bytes-like object is required, not 'str'
pi@Pi3B:~/tmp $
joan wrote:
Sun Sep 08, 2019 4:19 pm
Check that you are using the correct Python version.
Technically the better thing to do, considering Python 2 is going end of life, and many developers are committed to only providing Python 3 compatible libraries, is to make the code Python 3 compatible.

I expect we will get to see more of these types of issues in the future when only Python 3 is supported but a lot of examples, tutorials and books out there are targeting Python 2.
Last edited by hippy on Sun Sep 08, 2019 5:38 pm, edited 2 times in total.

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

Re: Error with example code Socket Program

Sun Sep 08, 2019 5:36 pm

Dear Hippy
Thank you very much for your help and information,
appreciated

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

Re: Error with example code Socket Program

Sun Sep 08, 2019 5:47 pm

hippy wrote:
Sun Sep 08, 2019 5:28 pm
...
Technically the better thing to do, considering Python 2 is going end of life, and many developers are committed to only providing Python 3 compatible libraries, is to make the code Python 3 compatible.

I expect we will get to see more of these types of issues in the future when only Python 3 is supported but a lot of examples, tutorials and books out there are targeting Python 2.
Indeed.

As a matter of interest the pigpio Python module only has a few lines to cater for the different Python versions.

Code: Select all

# A couple of hacks to cope with different string handling
# between various Python versions
# 3 != 2.7.8 != 2.7.3

if sys.hexversion < 0x03000000:
   def _b(x):
      return x
else:
   def _b(x):
      return x.encode('latin-1')

if sys.hexversion < 0x02070800:
   def _str(x):
      return buffer(x)
else:
   def _str(x):
      return x

User avatar
neilgl
Posts: 2183
Joined: Sun Jan 26, 2014 8:36 pm
Location: Near Aston Martin factory

Re: Error with example code Socket Program

Sun Sep 08, 2019 6:18 pm

Does it work now with my suggested encode method?

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

Re: Error with example code Socket Program

Sun Sep 08, 2019 6:43 pm

Dear neilgl,

I have done as you have advised and it is working perfect Thanks a million :D
The RPI read the temp on the senseHAT and send it to my phone
I really appreciate your kind help

Return to “Beginners”