• @allObjects

    Thank you for the reply, the low-level code above was taken from another post that Gordon commented on, he wanted to see what the wifi module reported back, so just to save time I decided to do the same.

    The code I am actually running is as follows:

    const WIFI_NAME = "<REDACTED>";
    const WIFI_OPTIONS = { password : "<REDACTED>" };
    const wifi = require("Wifi");
    let mqtt;
    
    const server = "172.24.0.2"; // the ip of your MQTT broker
    const options = {
      // ALL OPTIONAL - the defaults are below
      client_id: "random", // the client ID sent to MQTT - it's a good idea to define your own static one based on `getSerial()`
      keep_alive: 60, // keep alive time in seconds
      port: 1883, // port number
      clean_session: true,
      username: "username", // default is undefined
      password: "password", // default is undefined
      protocol_name: "MQTT", // or MQIsdp, etc..
      protocol_level: 4, // protocol level
    };
    
    let id = null;
    let temp = 0;
    let running = false;
    let speed = 50;
    let rgb = {r: 128, g: 128, b: 128};
    const leds = 12;
    let isActive = false;
    
    const onInit = () => {
      lightsOff();
      lightsOn(rgb, 300);
      wifi.on('disconnected', connect);
      connect();
    };
    
    const connect = () => {
      console.log("wifi connecting...");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, (err) => {
        if (err) {
          console.log("wifi error", err);
          return;
        }
        console.log("wifi connected");
        connectMQTT();
      });
    };
    
    const connectMQTT = () => {
      console.log("connecting to mqtt");
      mqtt = require("MQTT").create(server, options);
    
      mqtt.on("connected", () => {
        console.log("mqtt connected");
        lightsOff();
        lightsOn({r: 0, g: 128, b: 0}, speed);
        mqtt.subscribe("espruino/rgb");
        mqtt.subscribe("espruino/speed");
       // mqtt.subscribe("outdoor-motion-sensor");­
      });
    
      mqtt.on("publish", (pub) => {
        switch (pub.topic) {
          case "espruino/rgb":
            rgb = JSON.parse(pub.message);
            lightsOff();
            lightsOn(rgb, speed);
            break;
    
          case "espruino/speed":
            speed = pub.message;
            lightsOff();
            lightsOn(rgb, speed);
            break;
    
          case "outdoor-motion-sensor":
            const activity = JSON.parse(pub.message).activity;
            if(isActive === activity){
              return;
            }else{ 
              isActive = activity;
            }
            const color = isActive ? {r: 255, g: 0, b: 0} : {r: 0, g: 255, b: 0};
            speed = isActive ? 200 : 1000;
            lightsOff();
            lightsOn(color, speed);
            break;
        }
      });
      
      mqtt.on("error", (err) => console.log("error", err));
    
      mqtt.connect();
    };
    
    const lightsOn = (color, speed) => {
      if (running) return;
    
      let rgb = new Uint8ClampedArray(leds * 3);
      let pos = 0;
    
      const getPattern = () => {
        pos = (pos + 1) % leds;
    
        rgb[pos * 3 + 0] = color.g;
        rgb[pos * 3 + 1] = color.r;
        rgb[pos * 3 + 2] = color.b;
    
        for (var i = 0; i < leds * 3; i++) rgb[i] *= 8 / leds;
    
        return rgb;
      };
    
      id = setInterval(() => {
        A15.set();
        require("neopixel").write(B15, getPattern());
        A15.reset();
      }, speed);
      running = true;
    };
    
    const lightsOff = () => {
      clearInterval(id);
      let rgb = new Uint8ClampedArray(leds * 3);
    
      const getReset = () => {
        for (let i = 0; i < rgb.length; ) {
          rgb[i++] = 0;
          rgb[i++] = 0;
          rgb[i++] = 0;
        }
        return rgb;
      };
    
      A15.set();
      require("neopixel").write(B15, getReset());
      A15.reset();
      running = false;
    };
    
    const getRndColor = () => {
      let color = {
        r: Math.round(Math.random() * 255 + 25),
        g: Math.round(Math.random() * 255 + 25),
        b: Math.round(Math.random() * 255 + 25),
      };
      return color;
    };
    

    The error message is returned here:

    if (err) {
          console.log("wifi error", err);
          return;
        }
    
    

    ```

About

Avatar for Coder2012 @Coder2012 started