yllibed / Zigbee2MqttAssistant

GUI for Zigbee2Mqtt running in docker and HASS.IO
450 stars 42 forks source link

[BUG]Unable to connect to mqtt, error always reported #216

Open unpack8866 opened 4 years ago

unpack8866 commented 4 years ago

Starting Zigbee2MqttAssistant v0.3.133+Branch.master.Sha.f490bd70a0401bc6d96a4d02d5f9022a43bb5e9e... Hosting environment: Production Content root path: /app Now listening on: http://[::]:80 Application started. Press Ctrl+C to shut down. warn: Zigbee2MqttAssistant.Services.MqttConnectionService[0] Unable to connect to MQTT server 192.168.100.180:1883. MQTTnet.Exceptions.MqttCommunicationException: Resource temporarily unavailable ---> System.Net.Internals.SocketExceptionFactory+ExtendedSocketException (00000001, 11): Resource temporarily unavailable at System.Net.Dns.InternalGetHostByName(String hostName) at System.Net.Dns.ResolveCallback(Object context) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source) at System.Net.Dns.HostResolutionEndHelper(IAsyncResult asyncResult) at System.Net.Dns.EndGetHostAddresses(IAsyncResult asyncResult) at System.Net.Sockets.Socket.DoDnsCallback(IAsyncResult result, MultipleAddressConnectAsyncResult context) at System.Net.Sockets.Socket.DnsCallback(IAsyncResult result) --- End of stack trace from previous location where exception was thrown --- at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw(Exception source) at System.Net.Sockets.Socket.EndConnect(IAsyncResult asyncResult) at System.Net.Sockets.Socket.<>c.b__277_0(IAsyncResult iar) --- End of stack trace from previous location where exception was thrown --- at MQTTnet.Implementations.MqttTcpChannel.ConnectAsync(CancellationToken cancellationToken) at MQTTnet.Internal.MqttTaskTimeout.WaitAsync(Func`2 action, TimeSpan timeout, CancellationToken cancellationToken) at MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken) --- End of inner exception stack trace --- at MQTTnet.Adapter.MqttChannelAdapter.WrapException(Exception exception) at MQTTnet.Adapter.MqttChannelAdapter.ConnectAsync(TimeSpan timeout, CancellationToken cancellationToken) at MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken) at MQTTnet.Client.MqttClient.ConnectAsync(IMqttClientOptions options, CancellationToken cancellationToken) at MQTTnet.Extensions.ManagedClient.ManagedMqttClient.ReconnectIfRequiredAsync()

magpern commented 4 years ago

@unpack8866 I think you have typed :1883 after the server ip address. The port number goes on a separate line eg:

  "settings": {
    "MqttServer": "192.168.1.224",
    "MqttPort": 1883,
    "MqttUsername": "",
    "MqttPassword": "",
    "Basetopic": "zigbee2mqtt2"
  }

or in docker-compose

    environment:
      - Z2MA_SETTINGS__MQTTSERVER=192.168.1.224
      - Z2MA_SETTINGS__MQTTUSERNAME=""
      - Z2MA_SETTINGS__MQTTPASSWORD=""
      - Z2MA_SETTINGS__MQTTPORT=1883
      - Z2MA_SETTINGS__BASETOPIC=zigbee2mqtt2
carldebilly commented 4 years ago

@magpern It's a common mistake... I think Z2MA should support this and also the mqtt:// uri syntax. What do you think?

unpack8866 commented 4 years ago

@ unpack8866我认为您已:1883在服务器IP地址之后输入。端口号放在单独的行上,例如:

  "settings":{
     "MqttServer": "192.168.1.224",
     "MqttPort": 1883,
     "MqttUsername": "",
     "MqttPassword": "",
     "Basetopic": "zigbee2mqtt2" 
  }

或在docker-compose中

    环境:
      -Z 2 MA_SETTINGS__MQTTSERVER = 192.168.1.224 
      -Z 2 MA_SETTINGS__MQTTUSERNAME = "" 
      -Z 2 MA_SETTINGS__MQTTPASSWORD = "" 
      -Z 2 MA_SETTINGS__MQTTPORT = 1883 
      -Z 2 MA_SETTINGS__BASETOPIC = zigbee2mqtt2

Well done, thank you!

magpern commented 4 years ago

@magpern It's a common mistake... I think Z2MA should support this and also the mqtt:// uri syntax. What do you think?

Yes, that was my though too, when replying to this issue. What are there three different ways to set the settings?

One setting style, regardless of runtime platform, would be best, imo perhaps a config file that you point out in the docker run, like zigbee2mqtt then the same format is used in home assistant.

or as in zwave2mqtt, there is no settings file at all. You just start the docker or home assistant add-on, then you configure everything inside in a settings page.

But as a start, if you type :1883 after the ip address, Z2MA could assume it is port 1883 to use, and not use the MQTTPORT setting (or throw an error, invalidconfiguration)

carldebilly commented 4 years ago

About location of settings

Right now, there's a line specially for HASS.IO: https://github.com/yllibed/Zigbee2MqttAssistant/blob/346c8dacd126fa37b206d2184f1a9b88ae38e552/Zigbee2MqttAssistant/Program.cs#L22

It's a file generated by HASS.IO containing the settings. I never documented it officially, but it's currently the best way to configure settings using a file. Documentation: https://developers.home-assistant.io/docs/hassio_addon_config/#add-on-script

We can simply add this file as an "official" way to setup Z2MA...

About mqtt:// syntax and port number

Do you think it should be another setting? From my point of vue, the MQTT URI is very similar to a SQL connection string... Maybe it should be something like MqttConnection... so you'll be able to specify it as a string. At runtime, we it's specified, it could simply be parsed and fill other fields.

You know, the main reason why I didn't support it was because I was too lazy to parse the URI and the MQTTnet component is not supporting them out-of-the-box except for the websocket connection.

wisekki commented 4 years ago

Okay now that my issue is closed can someone please help me what to put in home assistant's config.. because I still can't get it to connect :(

wisekki commented 4 years ago

I copy/pasted configs listed here and tested out different typing scenarios.. at least now I get different error:

Application started. Press Ctrl+C to shut down. warn: Zigbee2MqttAssistant.Services.MqttConnectionService[0] Unable to connect to MQTT server 192.168.1.1. MQTTnet.Exceptions.MqttCommunicationException: Cannot access a disposed object. Object name: 'System.Net.Sockets.Socket'.

magpern commented 4 years ago

@wisekki please post your home assistant zigbee2mqttassistant settings

wisekki commented 4 years ago

This is the last one I tried: (I can't get this to format yaml correctly but the paste is from the config section of HA)

settings: MqttServer: 192.168.1.1 MqttUsername: antti MqttPassword: pwd MqttPort: 1883 Basetopic: zigbee2mqtt

If I change the ip-address to something else like my another server I just get "Connection refused" so it reads the config.

magpern commented 4 years ago

@wisekki at the moment I cannot think of anything. The error message

MQTTnet.Exceptions.MqttCommunicationException: Cannot access a disposed object. Is wierd. This indicates an error for real. If it can be recreated on demand, it could be fixed, probably easy.. anyway

Your settings look ok. Try with single quotes around the user and pwd Is your mqtt server really 192.168.1.1? 192.168.1.1 usually is the router in the ethernet network And finally, is 1883 the correct port?

Have you tried MQTT Explorer from a workstation? It is available for most OS. Can you connect with that? Please confirm that.

settings:
  MqttServer: 192.168.1.1
  MqttUsername: 'antti'
  MqttPassword: 'pwd'
  MqttPort: 1883
  Basetopic: zigbee2mqtt

I don't have password enabled on my mqtt server. I will install one and try with user and password. My current server accepts anything in user/password (for some reason)

wisekki commented 4 years ago

I've tried with single and double quotes but every time I press "save" they get removed. And yes my mqtt-server is at 192.168.1.1:1883.. confirmed and works from every other instance. Like from four different RPI's. I tried MQTT explorer and it works nicely. So it's not completely about my setup :)

tcp 0 0 0.0.0.0:1883 0.0.0.0:* LISTEN 1130/mosquitto

br0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500 inet 192.168.1.1

wisekki commented 4 years ago

@magpern did you have time to test it with username/pwd?

magpern commented 4 years ago

@wisekki yes! Eventually! I updated mosquitto to accept only user/password and updated my server too. After a reboot, by controller refused to start, but yesterday I got it back online.

So, mosquitto is confirmed to only accept user/password and when I put the user/password in the docker-compose file it works fine

  zigbee2mqttAssistant:
    image: carldebilly/zigbee2mqttassistant:dev
    container_name: zigbee2mqttAssistant
    environment:
      - Z2MA_SETTINGS__MQTTSERVER=192.168.1.224
      - Z2MA_SETTINGS__MQTTUSERNAME=myuser
      - Z2MA_SETTINGS__MQTTPASSWORD=mypassword
      - Z2MA_SETTINGS__MQTTPORT=1883
      - Z2MA_SETTINGS__BASETOPIC=zigbee2mqtt
      - TZ=Europe/Stockholm
    ports:
      - 8880:80
    restart: unless-stopped

And in my hassio (home assistant) as a plug in this worked fine

settings:
  MqttServer: 192.168.1.224
  MqttUsername: myuser
  MqttPassword: mypassword
  MqttPort: 1883
  Basetopic: zigbee2mqtt

If you want to run it on a separate machine, but still see it in home assistant, you can add this to your configuration.yaml in HA

panel_iframe:
  zigbee2mqtt:
    title: Zigbee2MQTT
    icon: mdi:zigbee
    url: http://192.168.1.220:8880

192.168.1.224 is my mqtt and home assistant server 192.168.1.220 is am Orange Pi 3 running Z2MA in docker

wisekki commented 4 years ago

For some reason the carldebilly/zigbee2mqttassistant:dev docker or the hassio plugin does not see the mqtt-server running on the same server. I did DNAT from my another computer to the 192.168.1.1:1883 and added that IP to the hassio plugin it started to work. With that I can see all the devices on my network if I route the traffic... very strange.

--dport 8888 -j DNAT --to-destination 192.168.1.1:1883
magpern commented 4 years ago

@wisekki I have no clue what could be the problem. The only thing I could guess is the somewhat strange ip of your server (x.x.x.1 is usually the gateway in a network), but again, that should not matter at all, not for Z2MA. Glad to hear it started working, though a work-around

wisekki commented 4 years ago

I tried it with virtual eth and that did not work either.. something odd either in my docker configuration or in the zigbee2qttassistant.

chrzr commented 4 years ago

@wisekki yes! Eventually! I updated mosquitto to accept only user/password and updated my server too. After a reboot, by controller refused to start, but yesterday I got it back online.

So, mosquitto is confirmed to only accept user/password and when I put the user/password in the docker-compose file it works fine

  zigbee2mqttAssistant:
    image: carldebilly/zigbee2mqttassistant:dev
    container_name: zigbee2mqttAssistant
    environment:
      - Z2MA_SETTINGS__MQTTSERVER=192.168.1.224
      - Z2MA_SETTINGS__MQTTUSERNAME=myuser
      - Z2MA_SETTINGS__MQTTPASSWORD=mypassword
      - Z2MA_SETTINGS__MQTTPORT=1883
      - Z2MA_SETTINGS__BASETOPIC=zigbee2mqtt
      - TZ=Europe/Stockholm
    ports:
      - 8880:80
    restart: unless-stopped

And in my hassio (home assistant) as a plug in this worked fine

settings:
  MqttServer: 192.168.1.224
  MqttUsername: myuser
  MqttPassword: mypassword
  MqttPort: 1883
  Basetopic: zigbee2mqtt

If you want to run it on a separate machine, but still see it in home assistant, you can add this to your configuration.yaml in HA

panel_iframe:
  zigbee2mqtt:
    title: Zigbee2MQTT
    icon: mdi:zigbee
    url: http://192.168.1.220:8880

192.168.1.224 is my mqtt and home assistant server 192.168.1.220 is am Orange Pi 3 running Z2MA in docker

I have a question regarding the iframe setup in HA. I have zigbee2mqttassistant running in Docker on a Debian VM. I also wanted to link it in HA, but I can only see the devices list. Clicking on either Map or Status results in a browser error. In my case "Blocked by X-Frame-Options Policy", as I am using Firefox, but it also happens in Chrome and also using the Home Assistant Companion app. When directly accessed it works just fine. Seems like it is a server-side protection against embedding the page. Did you make any special configurations to be able to use it in an iframe?

carldebilly commented 4 years ago

@chrzr There's no "X-Frame-Options" policy defined. That would mean you have one in your browser and it's too restrictive. Please open a new issue for this problem and document it.