This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

How can I connect the web socket of lte modem to amazone mqtt endpoint?

Hi Devzone!

I'm using 'nRF52840' as an edge device, and this device is going to continuously check the surrounding environment and send the value to mqtt broker of aws through lte modem. Every task is complete without lte connection and now I have a problem trying to open web sockets using at command to connect to mqtt broker.

First of all, the modem which I use is called 'lm5' of 'm2net' company. This model supports at command and allows tcp connection via web socket. But what I can use as a parameter is ip address and port, and I don't know what to write in this ip address. All I know is that mqtt's endpoint URL address and tcp port '443'.

Secondly, if the lte modem is connected to the mqtt server via webSockets, how should the device subscribe to and publish a specific topic? Is there any good example of or relating to library?

Finally, I want to know about the whole system. Does this mean that the device requires a library that subscribes to and publish the topic for mqtt with authentication, and also requires a web socket connection with the aws mqtt broker on the lte modem? Or is it not necessary to set up separately on the modem because end points and ports are defined in the device library?

Thank you for reading the long article.

Best regards,
Baek

Parents
  • Hi.

    First, we have a cellular IoT chip (the nRF9160), that can act as a serial modem and has an example on how to connect to AWS.

    If that is not an option, we also support MQTT in our nRF5 SDK (for the nRF52 series).

    The nRF5 SDK has a MQTT client that you can use to connect to AWS. The iot component let you implement your own transport layer, by implementing and defining the struct and functions declared in components/iot/socket/common/transport_if.h.

    I believe AWS also has a C library you could incorporate in your project that does the same.

    Regarding your specific questions:

    To convert a hostname/URL to an IP address, you should do a DNS query. Beware that AWS uses load-balancing, and therefore change IP address frequently. You therefore have to do a new DNS query for each time you want to connect to AWS.

     

    Secondly, if the lte modem is connected to the mqtt server via webSockets, how should the device subscribe to and publish a specific topic? Is there any good example of or relating to library?

     See our MQTT client.

     

    Finally, I want to know about the whole system. Does this mean that the device requires a library that subscribes to and publish the topic for mqtt with authentication, and also requires a web socket connection with the aws mqtt broker on the lte modem? Or is it not necessary to set up separately on the modem because end points and ports are defined in the device library?

     I am not quite shure what you mean here. For MQTT, you need a TLS connection to the MQTT broker. The messages that is sent over that connection will include what topic it belongs to, etc. You would typically have a library to handle the protocol specific things, and provide a simple mechanism for sending and reciving messages, without having to care about exactly how the TCP packet should look. A such library is typically called an MQTT client.

    Best reagrds,

    Didrik

Reply
  • Hi.

    First, we have a cellular IoT chip (the nRF9160), that can act as a serial modem and has an example on how to connect to AWS.

    If that is not an option, we also support MQTT in our nRF5 SDK (for the nRF52 series).

    The nRF5 SDK has a MQTT client that you can use to connect to AWS. The iot component let you implement your own transport layer, by implementing and defining the struct and functions declared in components/iot/socket/common/transport_if.h.

    I believe AWS also has a C library you could incorporate in your project that does the same.

    Regarding your specific questions:

    To convert a hostname/URL to an IP address, you should do a DNS query. Beware that AWS uses load-balancing, and therefore change IP address frequently. You therefore have to do a new DNS query for each time you want to connect to AWS.

     

    Secondly, if the lte modem is connected to the mqtt server via webSockets, how should the device subscribe to and publish a specific topic? Is there any good example of or relating to library?

     See our MQTT client.

     

    Finally, I want to know about the whole system. Does this mean that the device requires a library that subscribes to and publish the topic for mqtt with authentication, and also requires a web socket connection with the aws mqtt broker on the lte modem? Or is it not necessary to set up separately on the modem because end points and ports are defined in the device library?

     I am not quite shure what you mean here. For MQTT, you need a TLS connection to the MQTT broker. The messages that is sent over that connection will include what topic it belongs to, etc. You would typically have a library to handle the protocol specific things, and provide a simple mechanism for sending and reciving messages, without having to care about exactly how the TCP packet should look. A such library is typically called an MQTT client.

    Best reagrds,

    Didrik

Children
  • Hi Didrik

    Thank you for detailed answers!!

    To convert a hostname/URL to an IP address, you should do a DNS query. Beware that AWS uses load-balancing, and therefore change IP address frequently. You therefore have to do a new DNS query for each time you want to connect to AWS.

    At the AWS guide, I found the URL for websocket protocol is wss://<endpoint>.iot.<region>.amazonaws.com/mqtt. But, when I did nslookup for this it couldn't find the DNS for it. So, I did nslookup for <endpoint>.iot.<region>.amazonaws.com and I found 6 IPv4 address and 3 IPv6 address. But, How can I check which is for mqtt?

    For MQTT, you need a TLS connection to the MQTT broker.

    Is this supported by MQTT library? Then does it mean that I don't need to do TLS connection setting from LTE modem with AT command? Sorry to bother you.. How is TLS connection established? When I publish message to topic, will the message have information of TLS connection automatically if I set the TLS connection at the MQTT library?

    Again, Thank you.

    Best regards,

    Baek

  • Is there a specific reason why you want to use MQTT over WebSockets and not just MQTT (over TCP/TLS) directly?

    From my understanding of WebSockets, you initiate a connection using an HTTP GET request. The TCP/TLS connection is only concerned with the host, while the request itself will specify which path (e.g. /mqtt) you are interested in.

    A hostname may resolve to multiple IP addresses. Any one of the addresses should be fine to use (although I believe it is most common to use the first one).

    Our MQTT client supports TLS. It is built on top of a BSD socket interface, where TLS can be set as the protocol when opening a socket (NOTE: these sockets are different from WebSockets). In order to be able to use the MQTT client, you will have to implement a glue layer between these BSD sockets and your modem. Other than setting a couple of settings different when opening the connection to the broker, the MQTT client does not see a difference between TLS and TCP.

  • Hi Didrik!

    I misunderstood the system of lte connection and mqtt.

    I thought if I wanna connect with mqtt broker via lte modem, I should connect the broker with web socket.

    But you mean, I don't need to connect it only with web socket. right?

    So, does below thing have to be done to send and receive data with the MQTT broker?

    First, at the modem level, open a socket which connects TCP to ipv6 (not web socket) on the aws IoT server.

    Secondly, should I use the mqtt library for TLS authentication and for issuing and subscribing messages to certain topics?

    I tried to connect to aws IoT using nordic's mqtt library.

    As I said above, I opend a socket which connets TCP to ipv6 of aws IoT server at the modem level and I modified ipv6 and topic location and port in mqtt lib.

    However, seeing that the device cannot connect to the aws IoT server, I think I need something more to implement.

    When a device tries to connect to the mqtt broker, should it be implemented so that the transmitted packets are passed to the modem and that the modem passes the transmitted packets to the server? And is this the glue layer that you mentioned?

    The modem is connected to the device by a uart and the modem has at commands to operate the tcp send and tcp recieve.

    I'm not sure how to create this glue layer.

    Do you have any good example?

    In addition, I needed several certificates such as the RootCA file to connect to aws IoT. Do I not need these authentication files when I connect to aws IoT with the library of nordic?

    And if I need them, I want to know how I'm going to place those certificates and send those info within packet.

    Thank you!

  • Would it be the solution?

    For example, in nordic's mqtt library, there are mqtt_transport_connect(), mqtt_transport_write(), mqtt_transport_read(), mqtt_transport_disconnect().

    and Do I need to revise those function to send UART message to lte modem to run AT command?

    Like, in case of mqtt_transport_write() -> AT+CISEND()

  • but If above solution is right way, In what order should I input the message, and how aws can parse this message? Is there any syntax of this?? because, in this library, the message includes topic, payload, message id, tls info such things..

Related