• I seem to get additional characters now as per below:

    Here is my src, just incase there's anything else that could be contributing to this problem. Once again I don't see any issues in the MQTT traffic tool I use on Windows.

    const server = "192.168.1.64"; // 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;
    const leds = 12;
    
    function onInit() {
      lightsOff();
      lightsOn(getRndColor(), 300);
    
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function (err) {
        if (err) {
          return;
        }
        startMQTT();
      });
    }
    
    function startMQTT() {
      mqtt = require("MQTT").create(server, options);
    
      mqtt.on("connected", function () {
        mqtt.subscribe("door");
        mqtt.subscribe("occupancy");
        mqtt.subscribe("temperature");
      });
    
      mqtt.on("publish", function (pub) {
        console.log(pub.topic, pub.message);
        switch (pub.topic) {
          case "door":
            if (pub.message === "2") {
              lightsOff();
              lightsOn({ r: 80, g: 0, b: 0 }, 50);
            }
            break;
    
          case "occupancy":
            if (pub.message === "1") {
              lightsOff();
              lightsOn({ r: 0, g: 80, b: 0 }, 100);
            }
            break;
    
          case "temperature":
            temp = Number(pub.message);
            lightsOff();
            switch (true) {
              case temp <= 18:
                lightsOff();
                lightsOn({ r: 3, g: 171, b: 255 }, 200);
                break;
    
              case temp > 18 && temp < 20:
                lightsOff();
                lightsOn({ r: 255, g: 162, b: 0 }, 200);
                break;
    
              case temp >= 20:
                lightsOff();
                lightsOn({ r: 255, g: 0, b: 0 }, 200);
                break;
            }
            break;
        }
      });
    
      mqtt.connect();
    }
    
    function lightsOn(color, speed) {
      if (running) return;
    
      let rgb = new Uint8ClampedArray(leds * 3);
      let pos = 0;
    
      function 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(function () {
        A15.set();
        require("neopixel").write(B15, getPattern());
        A15.reset();
      }, speed);
      running = true;
    }
    
    function lightsOff() {
      clearInterval(id);
      let rgb = new Uint8ClampedArray(leds * 3);
    
      function 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;
    }
    
    function 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;
    }
    
    onInit();
    

    1 Attachment

    • espruino3.png
About

Avatar for Coder2012 @Coder2012 started