How to subscribe to a MQTT topic

Posted on
  • How to subscribe to a MQTT topic?
    ESP sends messages, but does not accept.
    var server = "";
    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.connect("INGRI", {password: "3hotswf3"}, function(err) { //
      //wifi.connect("AndroidAP", {password: "tssu7350"}, function(err) { //
        if(err)console.log(err);else console.log("connected!");
    wifi.getStatus(function(ap) {
    if(ap.station === 'connected'){
    mqtt.on('connected', function() {
           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() {
      print("mqtt connect");
     }, 2000);


  • 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

How to subscribe to a MQTT topic

Posted by Avatar for Alexander @Alexander