How to subscribe to a MQTT topic

Posted on
  • How to subscribe to a MQTT topic?
    ESP sends messages, but does not accept.
    code:
    var server = "m14.cloudmqtt.com";
    var options = {

    client_id : "random",
    keep_alive: 60,
    clean_session: true,
    port: 15577,
    username: "ciugiy**",
    password: "eYV1ivUWk***",
    protocol_name: "MQTT",
    protocol_level: 4,
    

    };

    var mqtt = require("MQTT").create(server, options);
    var wifi = require("Wifi");

    function onInit() {
    wifi.stopAP();

       wifi.connect("INGRI", {password: "3hotswf3"}, function(err) { // 192.168.83.196//
      //wifi.connect("AndroidAP", {password: "tssu7350"}, function(err) { // 192.168.83.196//
        if(err)console.log(err);else console.log("connected!");
        console.log(wifi.getIP().ip);
    
    wifi.getStatus(function(ap) {
    if(ap.station === 'connected'){
    
    mqtt.on('connected', function() {
    print("connect!!!");
           var topic = "test/espruino";
           var message = "hello, world";
           mqtt.publish(topic, message);
    });
    mqtt.on('publish', function (pub) {
      console.log("topic: "+pub.topic);
      console.log("message: "+pub.message);
    });
    mqtt.on('disconnected', function() {
      console.log("MQTT disconnected... reconnecting.");
      setTimeout(function() {
      mqtt.connect();
      print("mqtt connect");
     }, 2000);
    });
    mqtt.connect();
          }
        });
        });
    

    }

    wifi.save();
    save();

  • This is a subscription to the topic :) Learn English!:)

  • mqtt.subscribe("test/espruino");?

    Also, I've tended to put a leading slash on all the topics I use (eg mqtt.subscribe("/test/espruino");) which might help.

    If you want anything beginning with test then you need wildcards: mqtt.subscribe("test/#");

  • Is it possible to make a secure connection SSL or TLS?

  • Yes - if you buy an Espruino WiFi board, or potentially an ESP32. It isn't possible with the ESP8266 since it doesn't have enough RAM to handle TLS as well as Espruino

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

How to subscribe to a MQTT topic

Posted by Avatar for Alexander @Alexander

Actions