NickWaterton / Roomba980-Python

Python program and library to control iRobot Roomba 980 Vacuum Cleaner
MIT License
367 stars 103 forks source link

Failing to Parse JSON Responses #21

Open starlightknight opened 6 years ago

starlightknight commented 6 years ago

Hi, When I try to run the quick start, I get the output shown below. It seems to scan and find my Roombas correctly, but fails to parse the responses even though it is receiving them, and thus fails out thinking it didn't find any. OS is Linux Mint 18.2 (Ubuntu 16.04 base).

$ roomba
[I 2017-11-26 20:28:29,106] *******************
[I 2017-11-26 20:28:29,106] * Program Started *
[I 2017-11-26 20:28:29,106] *******************
[I 2017-11-26 20:28:29,106] Roomba.py Version: 1.2.1
[I 2017-11-26 20:28:29,106] Python Version: 3.5.2 (default, Sep 14 2017, 22:51:06) [GCC 5.4.0 20160609]
[I 2017-11-26 20:28:29,106] Paho MQTT Version: 1.3.1
[I 2017-11-26 20:28:29,106] CV Version: 3.3.0
[I 2017-11-26 20:28:29,107] PIL Version: 4.3.0
[I 2017-11-26 20:28:29,107] <CNTRL C> to Exit
[I 2017-11-26 20:28:29,107] Roomba 980 MQTT data Interface
[I 2017-11-26 20:28:29,107] reading info from config file ./config.ini
[W 2017-11-26 20:28:29,107] No roomba or config file defined, I will attempt to discover Roombas, please put the Roomba on the dock and follow the instructions:
waiting on port: 5678 for data
supplied address 255.255.255.255 does not match discovered address 192.168.1.118, using discovered address...
supplied address 255.255.255.255 does not match discovered address 192.168.1.247, using discovered address...
json decode error: the JSON object must be str, not 'bytes'
RECEIVED: %s (b'{"ver":"2","hostname":"Roomba-3117481C00311810","robotname":"Roomba1","ip":"192'
 b'.168.1.247","mac":"F0:03:8C:67:F3:14","sw":"v2.2.11-4","sku":"R980020","nc":'
 b'0,"proto":"mqtt"}')
supplied address 255.255.255.255 does not match discovered address 192.168.1.121, using discovered address...
json decode error: the JSON object must be str, not 'bytes'
RECEIVED: %s (b'{"ver":"2","hostname":"Roomba-3117450410338620","robotname":"Roomba2","ip":"192.'
 b'168.1.121","mac":"F0:03:8C:53:11:12","sw":"v2.2.11-4","sku":"R980020","nc":0'
 b',"proto":"mqtt"}')
No Roombas found, try again...
[I 2017-11-26 20:28:39,273] reading info from config file ./config.ini
[E 2017-11-26 20:28:39,273] No Roombas found! You must specify RoombaIP, blid and roombaPassword to run this program, or have a config file, use -h to show options.
NickWaterton commented 6 years ago

This is (another) python 3 slight incompatibility. Python 3 receives string arrays as type "bytes" - Python 2.7 receives these are type "string".

The json library expects to receive a string, not a byte array as Python 3 produces. You would think the built in libraries would accept the results of other built in libraries in Python 3 (like Python 2.7 does), but not so easy. Python 3.6 works, but Python 3.5 does not.

There is a lot of debate about this sort of pointless incompatibility, but the fix is to change line 54 of the password.py module from: parsedMsg = json.loads(udp_data) To: parsedMsg = json.loads(udp_data.decode('utf-8'))

The program was originally written for Python 2.7, and it's hard to make it work with Python 3.X as they keep changing this type of compatibility in each version of Python 3 (which is why I don't use it...).

You can read the debate here if you want https://bugs.python.org/issue10976

Unfortunately, you are likely to run into more of these kinds of problems in the code, as it works in Python 2.7 and Python 3.6, but Python 3.5 has various problems including the json deserializing error you have already found.

You could always run it as a python 2 program python2 roomba.py I think ubuntu 16.04 has both python 2 and python 3 as stock. In fact I thought python 2 was still the default.

starlightknight commented 6 years ago

Fair enough on 3.5/3.6, but you should update the README as it says this is tested with 3.5.

FWIW, I had tried running 2.7 before 3.5, but I had gotten other crashes, so I then tried 3.5:

roomba
[I 2017-11-28 18:12:20,989] *******************
[I 2017-11-28 18:12:20,989] * Program Started *
[I 2017-11-28 18:12:20,989] *******************
[I 2017-11-28 18:12:20,989] Roomba.py Version: 1.2.1
[I 2017-11-28 18:12:20,989] Python Version: 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609]
[I 2017-11-28 18:12:20,989] Paho MQTT Version: 1.3.1
[I 2017-11-28 18:12:20,990] CV Version: 2.4.9.1
[I 2017-11-28 18:12:20,990] PIL Version: 4.1.1
[I 2017-11-28 18:12:20,990] <CNTRL C> to Exit
[I 2017-11-28 18:12:20,990] Roomba 980 MQTT data Interface
[I 2017-11-28 18:12:20,990] reading info from config file ./config.ini
[W 2017-11-28 18:12:20,990] No roomba or config file defined, I will attempt to discover Roombas, please put the Roomba on the dock and follow the instructions:
waiting on port: 5678 for data
No handlers could be found for logger "roomba.password.Roomba_getpassword"
found 2 Roomba(s)
Make sure your robot (Roomba1) at IP 192.168.1.247 is on the Home Base and powered on (green lights on). Then press and hold the HOME button on your robot until it plays a series of tones (about 2 seconds). Release the button and your robot will flash WIFI light.
Press Enter to continue...
Traceback (most recent call last):
  File "/usr/local/bin/roomba", line 9, in <module>
    load_entry_point('Roomba980-Python==1.2.1', 'console_scripts', 'roomba')()
  File "/usr/local/lib/python2.7/dist-packages/Roomba980_Python-1.2.1-py2.7.egg/roomba/__main__.py", line 482, in main
    Password(file=arg.configfile)
  File "/usr/local/lib/python2.7/dist-packages/Roomba980_Python-1.2.1-py2.7.egg/roomba/password.py", line 27, in __init__
    self.get_password()
  File "/usr/local/lib/python2.7/dist-packages/Roomba980_Python-1.2.1-py2.7.egg/roomba/password.py", line 94, in get_password
    input("Press Enter to continue...")
  File "<string>", line 0

    ^
SyntaxError: unexpected EOF while parsing

I'll try upgrading to 3.6 in a bit, but its not included by default in the 16.04 repos, so I'll have to get a PPA for it or something.

NickWaterton commented 6 years ago

I’ll take a look tomorrow, where did you download this from? From pip or from my github? I ask because Pschmitt packaged it up for Pypi, and after I noticed some problems like you are seeing.

I fixed them and uploaded the fixed version to github.

I have Ubuntu 14.04 and 16.04 running in clean VM’s, and the code was tested (and works) on both (so I guess that’s where the tested on 3.5 comes from).

I haven’t tested it in a couple of months, so maybe I’ll spin up the VM’s again, and retest the code. See if anything has changed in any of the libraries/dependencies.

Nick Waterton P.Eng Sent from my iPhone

On Nov 28, 2017, at 6:17 PM, Aaron Knight notifications@github.com<mailto:notifications@github.com> wrote:

Fair enough on 3.5/3.6, but you should update the README as it says this is tested with 3.5.

FWIW, I had tried running 2.7 before 3.5, but I had gotten other crashes, so I then tried 3.5:

roomba [I 2017-11-28 18:12:20,989] [I 2017-11-28 18:12:20,989] Program Started [I 2017-11-28 18:12:20,989] [I 2017-11-28 18:12:20,989] Roomba.py Version: 1.2.1 [I 2017-11-28 18:12:20,989] Python Version: 2.7.12 (default, Nov 19 2016, 06:48:10) [GCC 5.4.0 20160609] [I 2017-11-28 18:12:20,989] Paho MQTT Version: 1.3.1 [I 2017-11-28 18:12:20,990] CV Version: 2.4.9.1 [I 2017-11-28 18:12:20,990] PIL Version: 4.1.1 [I 2017-11-28 18:12:20,990] to Exit [I 2017-11-28 18:12:20,990] Roomba 980 MQTT data Interface [I 2017-11-28 18:12:20,990] reading info from config file ./config.ini [W 2017-11-28 18:12:20,990] No roomba or config file defined, I will attempt to discover Roombas, please put the Roomba on the dock and follow the instructions: waiting on port: 5678 for data No handlers could be found for logger "roomba.password.Roomba_getpassword" found 2 Roomba(s) Make sure your robot (Roomba1) at IP 192.168.1.247 is on the Home Base and powered on (green lights on). Then press and hold the HOME button on your robot until it plays a series of tones (about 2 seconds). Release the button and your robot will flash WIFI light. Press Enter to continue... Traceback (most recent call last): File "/usr/local/bin/roomba", line 9, in load_entry_point('Roomba980-Python==1.2.1', 'console_scripts', 'roomba')() File "/usr/local/lib/python2.7/dist-packages/Roomba980_Python-1.2.1-py2.7.egg/roomba/main.py", line 482, in main Password(file=arg.configfile) File "/usr/local/lib/python2.7/dist-packages/Roomba980_Python-1.2.1-py2.7.egg/roomba/password.py", line 27, in init self.get_password() File "/usr/local/lib/python2.7/dist-packages/Roomba980_Python-1.2.1-py2.7.egg/roomba/password.py", line 94, in get_password input("Press Enter to continue...") File "", line 0

^

SyntaxError: unexpected EOF while parsing

I'll try upgrading to 3.6 in a bit, but its not included by default in the 16.04 repos, so I'll have to get a PPA for it or something.

— You are receiving this because you commented. Reply to this email directly, view it on GitHubhttps://github.com/NickWaterton/Roomba980-Python/issues/21#issuecomment-347696833, or mute the threadhttps://github.com/notifications/unsubscribe-auth/ALCUZ3Hn3yv1175hO8DXHtySX1OAJfjxks5s7JRugaJpZM4QrBl3.

Unsubscribe from GE's commercial electronic messages: http://sc.ge.com/*casl-unsubscribe Désabonner des messages électroniques commerciaux de GE: http://sc.ge.com/*lcap-desabonnement

starlightknight commented 6 years ago

I installed from Github. Thanks for looking, appreciated :-)

NickWaterton commented 6 years ago

OK, I have uploaded what may be a fix. Do a git pull, or re-clone from git and try it again. Bear in mind I only have 1 roomba, and you have two, so the "multiple roobmas" part of the code is completely untested...

I'm thinking that there is an incompatibility between python 2.7 and 3.x in the input("Press Enter to continue...") statement (ie python 2.7 does something completely different to python 3.x). I originally used raw_input(), but this got changed to input() in the python 3.x compatibility fixes, and may have broken something in some versions of 2.7.

Having said that, it does work perfectly on my machine (Ubuntu 14.04/Python 2.7), so I'm not totally sure that is what it is.

I also can't get discovery to work at all on my Ubuntu 16.04 VM with Python 3.5 (program works, just not discovery), but I think that's something to do with UDP broadcasts from a VM, not the code itself...

I did try it in some virtualenv's for different versions of Python and it worked, but I don't have every possible combinations of OS's and Python versions/libraries to test.

Give it a go, and let me know what happens...

starlightknight commented 6 years ago

That fix worked for me using Python 2, so i appear to be up and running. I'll get it integrated into Openhab over the next few days or so and start testing with 2 Roombas.