Avatar for MortenMoulder

MortenMoulder

Member since Nov 2017 • Last active Dec 2017
  • 3 conversations
  • 9 comments

Most recent activity

  • in ESP8266
    Avatar for MortenMoulder

    The problem wasn't the data being sent, the problem was WebSockets being slow as hell. I can do this probably faster than 20ms now using raw TCP socket.

    But yes, that's a good point.

  • in ESP8266
    Avatar for MortenMoulder

    Okay, so I've opted for the "net" module instead as a socket server. It's much, much, much faster! I can easily do 50ms now without issues.

    var esp8266 = require("ESP8266");
    
    function onInit() {
      var ssid = "SSID";
      var wifiOpts = {
        password: "PASSWORD"
      };
      var wifi = require("Wifi");
      wifi.connect(ssid, wifiOpts, function(err) {
        console.log("connected? err=", err, "info=", wifi.getIP());
        require("net").connect({host: "192.168.1.209", port: 8001}, function(socket) {
          console.log("Connected");
    
          var arr = "";
          socket.on("data", function(data) {
            arr += data;
            if(arr.slice(-2) == "]]") {
              data = JSON.parse(arr);
              esp8266.neopixelWrite(NodeMCU.D2, data);
              arr = "";
            }
          });
        });
      });
    }
    
    save();
    

    I had to hack it up a bit, because I send so many characters. I saw that the socket.on("data") got hit way too many times. Apparently it either receives or sends in chunks of ~260 characters, so I simply add those to a string, check if the last two characters are ]] (meaning my 2D array is complete) and then JSON.parse() it.

    Works amazingly fast!

  • in ESP8266
    Avatar for MortenMoulder

    I have about 150 LEDs in a strip. I want to change those colors, based on patterns I write externally (in a browser for example). I send the 2D array like this:

    [[255, 255, 255], [255, 0, 0], [255, 255, 255]]
    

    and so on from my NodeJS server running the "ws" module as a server. The ESP8266 NodeMCU then connects to the NodeJS server, and waits for it to send the RGB colors for all these LEDs.

    The problem occurs when I want to send the RGB codes for all 150 LEDs at once. This takes 1000-1500 milliseconds and it simply queues them up. If I keep it running for 30-60 seconds, then disconnect my NodeJS server, it will keep changing the colors on the LED strip, because it keeps the colors in a queue (because it's too slow?) .

    I am running this on a local network and here is my Espruino code:

    var esp8266 = require("ESP8266");
    
    function onInit() {
        var ssid = "SSID";
        var wifiOpts = {
            password: "PASSWORD"
        };
        var wifi = require("Wifi");
        wifi.connect(ssid, wifiOpts, function(err) {
            console.log("connected? err=", err, "info=", wifi.getIP());
            var ws = connect();
    
            ws.on('open', function() {
                console.log("Connected");
            });
    
            ws.on("message", function(msg) {
                msg = JSON.parse(msg);
                esp8266.neopixelWrite(NodeMCU.D2, msg);
            });
        });
    }
    
    function connect() {
        var WebSocket = require("ws");
        var host = "192.168.1.209";
        return new WebSocket(host, {
            path: '/',
            port: 8001,
            protocolVersion: 13,
            origin: 'Espruino',
            keepAlive: 60,
        });
    }
    
    save();
    

    Any clue what could be wrong? Is 1800 characters simply too much?

  • in ESP8266
    Avatar for MortenMoulder

    Turns out I have to run the code twice. The first time I run it, it doesn't make the colors just right, but the second time I run it, the colors are fine.

    Oh well.

  • in ESP8266
    Avatar for MortenMoulder

    Hi! I finally got Espruino running on my ESP8266 12-E NodeMCU, but I'm having some issues talking to my strip correctly. When I was controlling my strip using Arduino and C before, I hooked up the data pin to RX, which worked just fine. Now I'm (apparently) supposed to hook it up to D2, or any of the pins near it, but the colors are all wrong.

    When I do something like:

    require("neopixel").write(NodeMCU.D2, [255, 0, 0]);
    

    The first LED shines 70% yellow 30% green, if I do [0, 255, 0] it's 90% white 10% purple, and if I do [0, 0, 255] it's 80% white 20% blue (something like that).

    It's not red, green, or blue as it's supposed to be. Any clue why?

    Oh, and I also tried this:

    require("ESP8266").neopixelWrite(NodeMCU­.D2, [255,0,0]);
    

    but that yields the same result.


    EDIT: I found this code in another post, which actually works fine:

    E.setClock(80);
    var i = 0;
    var esp8266 = require("ESP8266");
    function colorLeds(red, green, blue) {
      var data = [];
      for (var i=0; i<3; i++) {
        data.push(green);
        data.push(red);
        data.push(blue);
      }
      console.log(data);
      esp8266.neopixelWrite(NodeMCU.D2, data);
    }
    setInterval(function() {
      i++;
      if ((i % 3) === 0) {
          colorLeds(255,0,0);
      } else if ((i%3) === 1) {
          colorLeds(0,255,0);
      } else {
          colorLeds(0,0, 255);
      }
    }, 1000);
    

    BUT! If I simply comment out the setInterval() and just write colorLeds(255, 0, 0) they all shine with a purple hue. What on earth???

  • in ESP8266
    Avatar for MortenMoulder

    FIXED!

    I flashed the espruino_1v94_esp8266_4mb_combined_4096.­bin firmware instead. I simply did:

    esptool --port COM4 --baud 115200 write_blash --verify --flash_freq 80m --flash_mode dio --flash_size 32m 0x0000 espruino_1v94_esp8266_4mb_combined_4096.­bin
    

    Could probably be shortened a bit, but it works now!

    Wifi also works now :-)

Actions