falk0069 / hue-upnp

Philips Hue UPNP and HTTP emulator (works with the Android Hue app and Logitech Harmony Home Remotes)
30 stars 10 forks source link

Http Socket Error: [Errno 99] Cannot assign requested address #19

Closed PedroDelCargo closed 3 years ago

PedroDelCargo commented 3 years ago

Hello, I'm trying to use hue-upnp on a raspi to send commands to Domoticz. I've been able to connect it to my Harmony hub (my devices came automatically). When I call the hue-upnp-helper.sh script with expected parameters, commands are sent to Domoticz and do what theu are supposed to do, but button presses on Harmony remote do nothing. It seems that something is wrong in my setup... Here is what I get in the log :

2021-04-22 22:24:26,697 [DEBUG] Adding device: Eclairage salon - type: script_handler 2021-04-22 22:24:26,698 [DEBUG] Adding device: Eclairage salle - type: script_handler 2021-04-22 22:24:26,698 [DEBUG] Adding device: Volet salon - type: script_handler 2021-04-22 22:24:26,699 [DEBUG] Adding device: Volet salle - type: script_handler 2021-04-22 22:24:26,700 [INFO ] hueUpnp: Server starting 2021-04-22 22:24:26,705 [INFO ] hueUpnp: Starting HTTP server for 192.168.1.30:3535 2021-04-22 22:24:26,706 [INFO ] hueUpnp: Http Socket Error: [Errno 99] Cannot assign requested address Traceback (most recent call last): File "hueUpnp.py", line 703, in run(hueUpnp_config); File "hueUpnp.py", line 644, in run responder.join(1) File "/usr/lib/python2.7/threading.py", line 951, in join self.block.wait(delay) File "/usr/lib/python2.7/threading.py", line 359, in wait _sleep(delay) TypeError: 'int' object is not callable Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "hueUpnp.py", line 168, in run sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) File "/usr/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) error: [Errno 19] No such device Exception in thread Thread-2: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in bootstrap_inner self.run() File "hueUpnp.py", line 142, in run sock.bind((CONFIG.standard['IP'],0)) File "/usr/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) error: [Errno 99] Cannot assign requested address

I tried to use differents ports (3535, 80, 8080 which is Domoticz port on same raspi) without success. Would you have an idea of what's wrong in my setup ?

falk0069 commented 3 years ago

Are you sure your server's IP address is 192.168.1.30? What you can try doing is just listen on all interfaces. Update your hueUpnp_config.py file and change the IP line to this: 'IP': "0.0.0.0", See if that allows it to start without an error. You should then be able to validate that you are listening on the port you choose by running netstat. For example to validate port 3535 is listening, run this: netstat -an | grep :3535

PedroDelCargo commented 3 years ago

Thanks for your reply. The port 3535 is not listening. Only 22, 443, 6144, 8080 ports are listening and I can't find how I can add a port listening. If you have an idea...

PedroDelCargo commented 3 years ago

And here is what I get in log when I change the IP to 0.0.0.0 :

2021-04-23 19:22:19,357 [DEBUG] Adding device: Eclairage salon - type: script_handler 2021-04-23 19:22:19,358 [DEBUG] Adding device: Eclairage salle - type: script_handler 2021-04-23 19:22:19,358 [DEBUG] Adding device: Volet salon - type: script_handler 2021-04-23 19:22:19,359 [DEBUG] Adding device: Volet salle - type: script_handler 2021-04-23 19:22:19,360 [INFO ] hueUpnp: Server starting 2021-04-23 19:22:19,364 [INFO ] hueUpnp: Starting HTTP server for 0.0.0.0:3535 Exception in thread Thread-2: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "hueUpnp.py", line 146, in run sock.sendto(UPNP_BROADCAST, (CONFIG.standard['BCAST_IP'], CONFIG.standard['UPNP_PORT'])) error: [Errno 101] Network is unreachable

Exception in thread Thread-1: Traceback (most recent call last): File "/usr/lib/python2.7/threading.py", line 801, in __bootstrap_inner self.run() File "hueUpnp.py", line 168, in run sock.setsockopt(socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq) File "/usr/lib/python2.7/socket.py", line 228, in meth return getattr(self._sock,name)(*args) error: [Errno 19] No such device

falk0069 commented 3 years ago

Well you got further, but I'm not sure why you'd be getting a network unreachable when sending to the upnp broadcast unless the network it is using is down. Can you confirm that your BCAST_IP and UPNP_PORT are still the default? Is this in your hueUpnp_config.py file?

    #Standard Broadcast IP UPNP
    'BCAST_IP': "239.255.255.250",
    #Standard UPNP Port
    'UPNP_PORT': 1900,

Could you also share your ifconfig output so we can see if any of your interfaces look odd? I run this script on a RPi as well, so it should be able to send udp packets to the reserved upnp broadcast address. In the meantime, I'll see if I can find any other command we could run to test udp.

falk0069 commented 3 years ago

Another thing to try is to seeing if you can sent out a generic message to the upnp broadcast address. Try running this netcat command and make sure it doesn't have an error: echo hello | nc -w1 -u -v 239.255.255.250 1900

I believe netcat (nc) is available by default on a rpi. If not, you should be able to easily install it with this command: sudo apt-get install netcat

PedroDelCargo commented 3 years ago

Thanks for taking some of your time on this. I did not touch this part of the hueUpnp_config.py file.

Here is what I get :

pi@raspberrypi:~ $ ipconfig -bash: ipconfig: command not found pi@raspberrypi:~ $ ifconfig -a eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.30 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fdec:5623:e4d8:7a00:a3e7:75a:d9ff:4909 prefixlen 64 scopeid 0x0 inet6 fe80::ee56:1bb9:81d9:7497 prefixlen 64 scopeid 0x20 ether b8:27:eb:d6:1d:1c txqueuelen 1000 (Ethernet) RX packets 23210 bytes 5524342 (5.2 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 14711 bytes 5280194 (5.0 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536 inet 127.0.0.1 netmask 255.0.0.0 inet6 ::1 prefixlen 128 scopeid 0x10 loop txqueuelen 1000 (Local Loopback) RX packets 0 bytes 0 (0.0 B) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 0 bytes 0 (0.0 B) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0

pi@raspberrypi:~ $ echo hello | nc -w1 -u -v 239.255.255.250 1900 Connection to 239.255.255.250 1900 port [udp/*] succeeded!

I don't know if this is what we expect ?

PedroDelCargo commented 3 years ago

I found that if I do a sudo service hueUpnp restart, I get this in log :

2021-04-24 09:06:45,703 [DEBUG] Adding device: Eclairage salon - type: script_handler 2021-04-24 09:06:45,704 [DEBUG] Adding device: Eclairage salle - type: script_handler 2021-04-24 09:06:45,704 [DEBUG] Adding device: Volet salon - type: script_handler 2021-04-24 09:06:45,705 [DEBUG] Adding device: Volet salle - type: script_handler 2021-04-24 09:06:45,706 [INFO ] hueUpnp: Server starting 2021-04-24 09:06:45,710 [INFO ] hueUpnp: Starting HTTP server for 192.168.1.30:3535

And then everything works ! 😊 Now I need to find how I can solve this issue because, each time I reboot the raspi, I have to restart the service to make it work...

falk0069 commented 3 years ago

hmm, I wonder if it is starting before the network is ready. Are you using the sample start script: start-script.howto ? I was reading that you can delay the start of the script by updating the Required lines. Something like this:

# Required-Start: $ local_fs $ remote_fs $ syslog $ network
# Required-Stop: $ local_fs $ remote_fs $ syslog $ network

Or you could try adding a generic sleep 15 in the start) section.

Let me know how it goes.

PedroDelCargo commented 3 years ago

Bingo!!! Adding a sleep in the start section did it! It seems that my raspi needed more time to wake up! The other solution (the first one) did not work and generated errors... Now that everything works on this side, I'm going to continue my work on the Domoticz side. Since I'm making my Domoticz script as generic and commented as possible, if you are interrested, I can send it to you so you can add it to your package if you wish? Many thanks for your great work and for beeing so helpful!

falk0069 commented 3 years ago

Cool, thanks for testing and confirming.

If you get something that works and you want me to share here, I'd be more than happy to add it.

PedroDelCargo commented 3 years ago

Domoticz control script.zip Hi, Here is my Domoticz control script that works perfectly for me and can easyly be adapted to everyone's need. You can publish it if you think it can be useful.

falk0069 commented 3 years ago

Very cool. Thank you. I added a subdirectory with your script: https://github.com/falk0069/hue-upnp/tree/master/domoticz

PedroDelCargo commented 3 years ago

Thanks again for your great work and help.