Uncaught Error: Module "MQTT" not found

Posted on
  • Did not see the MQTT module. More can that where to disable that would work MQTT.

    
    |   __|___ ___ ___ _ _|_|___ ___
    |   __|_ -| . |  _| | | |   | . |
    |_____|___|  _|_| |___|_|_|_|___|
              |_| http://espruino.com
     1v94 Copyright 2016 G.Williams
    Espruino is Open Source. Our work is supported
    only by sales of official boards and donations:
    http://espruino.com/Donate
    Flash map 4MB:512/512, manuf 0xef chip 0x4016
    >var server = "m14.cloudmqtt.com";
    ="m14.cloudmqtt.com"
    ={
      "client_id": "random",
      "keep_alive": 60, "port": 15577, "clean_session": true,
      "username": "",
      "password": "",
      "protocol_name": "MQTT",
      "protocol_level": 4 }
    >var mqtt = require("MQTT").create(server, options);
    Uncaught Error: Module "MQTT" not found
     at line 1 col 26
    var mqtt = require("MQTT").create(server, options);
                             ^
    
  • You need to upload the code in the right side of the ide, it appears you are entering the code on the left.

  • Can't connect to https://api.cloudmqtt.com. What's wrong? Is this even possible?

  • This is my code:

    var server = "m14.cloudmqtt.com";
     var options = {
        client_id : "random",
        keep_alive: 60,
        port: 35577,
        clean_session: true,
        username: "ciugi**",
        password: "eYV1i*****",
        protocol_name: "MQTT",
        protocol_level: 4,
      };
      var mqtt = require("MQTT").create(server, options);
      mqtt.on('connected', function() {
        mqtt.subscribe("test");
      });
      mqtt.on('publish', function (pub) {
        console.log("topic: "+pub.topic);
        console.log("message: "+pub.message);
      });
    var wifi = require("Wifi");
    function onInit() {
       wifi.stopAP();
       wifi.connect("INGRIFAB", {password: "B73vFeX6se2"}, function(err) {
            if(err)console.log(err);else console.log("connected!");
            console.log(wifi.getIP().ip);
            });
    }
    var server = "m14.cloudmqtt.com";
     var options = {
        client_id : "random",
        keep_alive: 60,
        port: 35577,
        clean_session: true,
        username: "ciugi**",
        password: "eYV1i*****",
        protocol_name: "MQTT",
        protocol_level: 4,
      };
      var mqtt = require("MQTT").create(server, options);
      mqtt.on('connected', function() {
        mqtt.subscribe("test");
      });
      mqtt.on('publish', function (pub) {
        console.log("topic: "+pub.topic);
        console.log("message: "+pub.message);
      });
    var wifi = require("Wifi");
    function onInit() {
       wifi.stopAP();
       wifi.connect("INGRIFAB", {password: "B73vFeX6se2"}, function(err) {
            if(err)console.log(err);else console.log("connected!");
            console.log(wifi.getIP().ip);
            });
    }
    wifi.save();
    save();
    
  • Maybe try and connect with an MQTT client on a local computer first to check it works?

    It's also worth noting that Espruino on ESP8266 won't do TLS (needed for secure MQTT), which I guess could be causing you problems?

  • Understood! It works!!! Just the ports were closed

  • Here is the working code! Whom it is necessary.

     var server = "m14.cloudmqtt.com";
     var options = {
     keep_alive: 60,
      port: 15577,
    username: "ciug****",
    password: "eYV1ivU****",
    protocol_name: "MQTT",
    protocol_level: 4
    };
    var mqtt = require("MQTT").create(server, options);
    var wifi = require("Wifi");
    function onInit() {
     wifi.stopAP();  
          wifi.connect("AndroidAP", {password: "tssu7350"}, function(err) { 
            if(err)console.log(err);else console.log("connected!");
            console.log(wifi.getIP().ip);
          wifi.getStatus(function(ap) {
             if(ap.station === 'connected'){
        print("START...");
        mqtt.on('connected', function() {
         print("connect!!!");
        mqtt.subscribe("test");
        });
        mqtt.on('publish', function (pub) {
          console.log("topic: "+pub.topic);
          console.log("message: "+pub.message);
        });
        mqtt.on('disconnected', function() {
          print("disconnected!!!");
         console.log("MQTT disconnected... reconnecting.");
         setTimeout(function() {
          mqtt.connect();
          print("mqtt connect");
         }, 2000);
         });
    mqtt.connect();
          }
        });
      });
    }
    wifi.save();
    save();
    
  • Are only outgoing messages. Why? The function mqtt.on('publish', mqttMessage); - not working

  • Have you subscribed to what you are interested in? Usually topics start with a /, so you might need mqtt.subscribe("/test");

  • PS... on a side note: @Alexander, when you include code in posts, place it between two line that have only three (3) 'back-ticks' at the beginning and make sure there is an empty line before and after the lines with the back-ticks. This way all your code is furnished with line numbers, shows in one block, and is much more legible.

  • Is that better? :)

  • Perfect! ;-)

    Btw, now I noticed, that save() is part of your code you upload... is it?

    I'm not sure if it is bad, but it kind of tickles my guts the wrong way... @Gordon will for sure know. I keep the save() out of upload business, and even the direct onInit() or any other heavy activity in level 0...

  • Yes, as @allObjects says I'd avoid using save() right at the end of code you upload, and would do it manually.

    If you want to save immediately when you send, you're probably better off using save on send in the IDE options.

  • Thank you! Very useful tips.

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

Uncaught Error: Module "MQTT" not found

Posted by Avatar for Alexander @Alexander

Actions