You are reading a single comment by @jfox and its replies. Click here to read the full conversation.
  • Here is the code that works without error:

    var
        led = NodeMCU.D7,
        relay = NodeMCU.D6;
    
    pinMode(led, "output");
    pinMode(relay, "output");
    
    
    
    function onInit() {
        setupWifi();
        setLed(led, 0);
    }
    
    
    
    function setupWifi() {
        var
            ssid = "ssid",
            options = {
                password: "password"
            },
            wifi = require("Wifi");
    
    
        wifi.connect(ssid, options, function() {
            print(wifi.getIP());
    
            server = "192.168.2.10";
            mqtt = require("MQTT").create(server);
            mqtt.connect();
    
            mqtt.on('connected', function() {
                mqtt.publish('esp8266/status', 'sonoff01 start');
                mqtt.subscribe("esp8266/sonoff01");
    
            });
    
            mqtt.on('publish', function(pub) {
                mqtt.publish('esp8266/status', 'sonoff01 ' + pub.message);
    
                switch (pub.message) {
                    case 'on':
                        setLed(led, 1);
                        setRelay(relay, 1);
                        break;
                    case 'off':
                        setLed(led, 0);
                        setRelay(relay, 0);
                        break;
                }
            });
    
        });
    }
    
    
    
    
    function setLed(led, state) {
        state = !state;
        digitalWrite(led, state);
    }
    
    function setRelay(relay, state) {
        digitalWrite(relay, state);
    }
    

    Here is what process.memory() shows:

    process.memory();
    ={ "free": 849, "usage": 851, "total": 1700, "history": 434 }
    

    Change line 31 from:

    mqtt.connect();
    

    to:

    mqtt.connect({client_id:"booger"});
    

    This is the result:

    Running onInit()...
    {
      "ip": "192.168.2.16",
      "netmask": "255.255.255.0",
      "gw": "192.168.2.1",
      "mac": "60:01:94:07:ab:1e"
     }
    Client connected
    Uncaught Error: Function "write" not found!
     at line 1 col 35
    ....log("Client connected");a.write(b.mqttConnect(b.clie­nt_id))...
                                  ^
    in function "e" called from line 3 col 353
    ..."close")});b.client=a};a?e():a=requir­e("net").connect({host:...
                                  ^
    in function "connect" called from line 1 col 109
    ...onnect({client_id:'booger'}),mqtt.on(­'connected',function(){...
                                  ^
    in function called from system
    

    Here is what process.memory() shows:

    process.memory();
    ={ "free": 1120, "usage": 580, "total": 1700, "history": 436 }
    
About

Avatar for jfox @jfox started