poblabs / weewx-belchertown

A clean and modern weewx skin with real time streaming updates, forecast data and interactive charts. View it in action at BelchertownWeather.com
https://belchertownweather.com
GNU General Public License v3.0
211 stars 112 forks source link

How to config Websocket #64

Closed HoracioDos closed 5 years ago

HoracioDos commented 5 years ago

Hello! I've installed your extension without problems but I can't make it work with websockets without SSL. It seems to be a common problem. Weewx V3.9.1 is running in a Raspberry Pi with Raspbian Stretch 9.6 Mosquitto Server is v3.1.1 and client V1.5.6

This is my config: weewx.conf

[StdRESTful]
    [[MQTT]]
        server_url = mqtt://myuser:mypassword@localhost:1883/
        topic = weather
        unit_system = METRIC
        binding = archive, loop
        aggregation = aggregate

[StdReport]
    [[Belchertown]]
        HTML_ROOT = /var/www/html/weewx/belchertown
        skin = Belchertown
        enable = true
        [[[Extras]]]
            mqtt_enabled = 1
            mqtt_host = "localhost"
            mqtt_port = 9001
            mqtt_ssl = 0
            mqtt_topic = "weather/loop/#"
            disconnect_live_website_visitor = 900000

Nginx Site config.

# Mosquitto websocket upstream
upstream mqtt-ws {
        server localhost:9001;
}

# configuration of the server
server {
    # the port your site will be served on
    listen      80;
    charset     utf-8;
    root /var/www/html/weewx/belchertown;
    index index.html;

    # forward MQTT-via-Websocket to Mosquitto
    location /mqtt-ws {
        proxy_http_version 1.1;
        proxy_cache_bypass 1;
        proxy_no_cache 1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_pass http://mqtt-ws/;
    }
}

sudo netstat -tulpn | grep -E '1883|9001'

tcp        0      0 0.0.0.0:1883            0.0.0.0:*               LISTEN      290/mosquitto       
tcp        0      0 0.0.0.0:9001            0.0.0.0:*               LISTEN      290/mosquitto       
tcp6       0      0 :::1883                 :::*                    LISTEN      290/mosquitto 

Mosquitto Config

persistence false

allow_anonymous true

password_file /etc/mosquitto/passwd
acl_file /etc/mosquitto/acl
listener 1883
protocol mqtt

# websockets
listener 9001
protocol websockets

mosquitto_sub -h localhost -t weather/loop/# -u "user" -P "pwd" (output) {"UV": "13.4687140864", "cloudbase_meter": "404.057634127", "outHumidity": "79.9993663432", "pressure_mbar": "1053.16600212", "rain_cm": "0.0", "barometer_mbar": "1053.16600212", "consBatteryVoltage_volt": "11.4897605947", "dewpoint_C": "-2.73389218279", "inTempBatteryStatus": "0.0", "rainBatteryStatus": "0.0", "windGust_kph": "0.000203954349963", "referenceVoltage_volt": "12.8755590251", "heatindex_C": "0.306093482847", "dayRain_cm": "0.508", "inDewpoint_C": "-0.511552874707", "outTempBatteryStatus": "0.0", "altimeter_mbar": "1055.96899606", "windchill_C": "0.306093482847", "appTemp_C": "-2.04603501469", "outTemp_C": "0.306093482847", "windGustDir": "359.996198059", "heatingVoltage_volt": "10.8612010464", "humidex_C": "0.306093482847", "rain24_cm": "0.508", "rxCheckPercent": "19.8511750505", "hourRain_cm": "0.0", "inTemp_C": "17.2224569074", "windSpeed_kph": "0.000169961958303", "usUnits": "16.0", "txBatteryStatus": "1.0", "windBatteryStatus": "0.0", "supplyVoltage_volt": "13.101027417", "rainRate_cm_per_hour": "0.0", "dateTime": "1550072698.0", "windDir": "359.996198059", "inHumidity": "29.9991551332", "radiation_Wpm2": "962.051006173"}

poblabs commented 5 years ago

What do you mean common problem? In that everyone is experiencing it or just you can't get past it?

You have told the skin to NOT use SSL. Update your skin option to use SSL.

mqtt_ssl = 1

poblabs commented 5 years ago

You also should not subscribe to #. Change that to be just the loop topic.

HoracioDos commented 5 years ago

Hi. Please don't be mad at me. When I googled how to configure it with websockets I found that many people without enough knowledge is having trouble to make it work. Yes, As I can't get past it I humbly asked for help. There are not second intentions. Please keep in mind that english is not my mother language. Thanks for your help

poblabs commented 5 years ago

Not mad at all.

I just wasn't sure if you meant the skin had a problem. I use it daily with websockets and don't have any problems. That's why I think the option you had set was wrong.

Hopefully you are working now!

HoracioDos commented 5 years ago

Hello! I should have explained myself differently. I changed mqtt_topic = "weather/loop" and mqtt_ssl = 1 but I still can't connect. If I setup mosquitto to use plain websockets without TLS/SSL why should I set mqtt_ssl to 1? What I'm missing?

PS: I've added a port forward rule for 80 and 9001 in my router. Now I'm testing websockets with HiveMq test socket tool and it works fine but the skin still can't connect. I'm lost now. image

poblabs commented 5 years ago

Yes I think I am confused now.

If you wish to use HiveMQ as your broker, look at this post on the forums from someone else using HiveMQ. It may help you.

If you wish to use localhost as your MQTT Websockets broker, download and install mqttadmin website and use that to connect to your websockets. Once you get that tool to work with your websockets, then the skin should start to work too.

poblabs commented 5 years ago

Ok I think I understand more.

Your configuration is telling your Chrome browser to go to localhost for MQTT.

Chrome see's localhost is your PC and not the server.

You need this to be your server.

Also your Websockets is on port 80 now?

Change:

        [[[Extras]]]
            mqtt_enabled = 1
            mqtt_host = "IP ADDRESS OF YOUR PI. LIKE 192.168.1.100"
            mqtt_port = 80
HoracioDos commented 5 years ago

Yes I think I am confused now.

  • If you are using SSL/TLS. You need mqtt_ssl = 1.
  • If you are not using SSL/TLS, set it to 0.

If you wish to use HiveMQ as your broker, look at this post on the forums from someone else using HiveMQ. It may help you.

I used HiveMq because it was quicker for me to forward ports and try a small test. I already have a mosquitto broker running in a raspberry pi with some arduino clients doing silly stuff. By the way, your mosquitto install guide is excellent!

If you wish to use localhost as your MQTT Websockets broker, download and install mqttadmin website and use that to connect to your websockets. Once you get that tool to work with your websockets, then the skin should start to work too.

Good to know!!

HoracioDos commented 5 years ago

Finally! You are the man!!! I've been chasing my tail for hours.
I swaped "localhost" for the internal ip address (192.x.x.x) in nginx site configuration and in weewx.conf (both places) and it works now. As I tried so many combinations I lost the track of them. mqtt_port is still 9001 just like mosquitto websockets setup. I don't know if it's ok for me to tell you that I made a small donation for coffee last night. Thanks for all inconvenience

poblabs commented 5 years ago

Thanks for the coffee! And I'm glad you got it working. Sorry I was confused earlier but I am glad you are working now!