gausby / tortoise

A MQTT Client written in Elixir
Apache License 2.0
314 stars 54 forks source link

AWS IoT and publish freeze on Nerves #127

Open MAkcanca opened 3 years ago

MAkcanca commented 3 years ago

I'm trying to write an example project for Aws IoT core MQTT connection with Tortoise, but whenever I try to publish a message, application hangs. It's running on Rpi0 I'm fairly new to Elixir and can't say that this is the right approach, but the lack of example projects does not make things easier to learn either. Here is what I do to send a message

        client_opts = [ 
            client_id: "testdev01",
            handler: {Tortoise.Handler.Logger, []},
            server: {Tortoise.Transport.SSL,
            cacerts: :certifi.cacerts(),
            certfile: "/srv/erlang/lib/network_led-0.1.0/priv/testdev01.cert",
            keyfile: "/srv/erlang/lib/network_led-0.1.0/priv/testdev01.private.key",
            depth: 99,
            host: 'axxxxxxxxxx-ats.iot.eu-central-1.amazonaws.com',
            port: 8883,
            customize_hostname_check: [match_fun: :public_key.pkix_verify_hostname_match_fun(:https)],
            versions: [:'tlsv1.2'],
            server_name_indication: '*.iot.eu-central-1.amazonaws.com',
            },
            subscriptions: [{"#", 0}]
        ]
        Tortoise.Connection.start_link(client_opts)
        Tortoise.publish("testdev01", "sensor/test", "hello")

When I insert some "debug console log's" between the functions, I can see that it runs until publish, then it hangs. I believe this happened before in https://github.com/gausby/tortoise/issues/62 https://github.com/gausby/tortoise/issues/95 Nobody tackled this problem for 2 years? I'm using Nerves, Elixir 1.11.2-otp-23, erlang 23.1.4 Any help appreciated, thanks!

Update: When I remove the depth option, error is thrown. Fatal - Handshake Failure\n {bad_cert,max_path_length_reached}'}}

Update 2: I switched the certifi with Amazon Root CA1 I found from here but it gives Unknown CA error now. I guess I need the full chain to verify it? I appended an intermediate cert from https://good.sca1a.amazontrust.com/ here to the same pem file but the result is the same, unknown CA.

dunyakirkali commented 3 years ago

@MAkcanca How did you solve this at the end?

MAkcanca commented 3 years ago

@dunyakirkali They forgot about this issue, probably not in their scope. I ended up not using nerves, currently trying to reach out to some Japanese folks about this. If you do solve this however, please ping me here.

taun commented 3 years ago

I just had this or an issue with the same symptoms. It was due to not specifying a timeout option and so the call was never returning or timing out.

Tortoise.publish("testdev01", "sensor/test", "hello", dos: 0, timeout: 500)

Returns after 500mS with a clause matching error due to {:error, :timeout} not being handled. But that's progress compared to hanging.