You are reading a single comment by @MrTimcakes and its replies. Click here to read the full conversation.
  • I've often wanted to know the CPU utilization of my home server to help debugging or just seeing general usage, I'm aware of the Pico CPU Monitor but that uses USB, seeing as though it's a server I wanted something with WiFi. Oh and fancy WebSockets.

    First of all, we need some software on the server to broadcast its utilization. I chose WebSocketd, it takes any regular command line script and broadcasts it over WebSockets. Just remember to add it to your $PATH so you can use it from anywhere. I thought this was easier than making Node script to do essentially the same thing, this project is about Espruino after all. I have this run in a screen at startup on my server:

    websocketd --port=9231 /usr/bin/vmstat -n 1
    

    Now for the Espruino portion, while I had originally planned to run this on an Original Espruino I ended up using an ESP8266 I'll explain why later. The Espruino has to connect to the server, parse the information then display it on a graph. The information from the WebSocket comes in a format structured for columns on a command line using multiple spaces to separate it. Since Espruino doesn't support RegEx yet I had to use some fidgeting with indexOf to get the data into a usable format, specifically the idle time of the CPU. I then created a simple graph library to convert the CPU utilization into a graph and plot it. I used an SSD1306 128*64 OLED, here's the code and how it looks:

    require("Font8x12").add(Graphics);
    I2C1.setup({scl:NodeMCU.D4,sda:NodeMCU.D­3});
    var Wifi = require("Wifi");
    var Graph = require("http://dev.mrtimcakes.com/espru­ino/graph.js");
    var CPU = new Graph(90, 48);
    var WebSocket = require("ws");
    var ws = new WebSocket("<YOUR SERVER HERE>",{port: 9231, origin: 'http://' + Wifi.getIP().ip});
    var column = ["r", "b", "swpd", "free", "buff", "cache", "si", "so", "bi", "bo", "in", "cs", "us", "sy", "id", "wa", "st"];
    var lastUpdate = 0;
    
    ws.on('message', function(e) {
      while(e.indexOf('  ')!=-1)e=e.replace('  ',' ');
      var raw = e.trim().split(" ");
      var data = {};
      for (var i = 0; i < column.length; i++) {
        data[column[i]] = parseInt(raw[i]);
      }
    
      CPU.update(100 - data.id);
      lastUpdate = getTime();
    });
    
    function draw(){
      g.clear();
      g.setFont8x12(); 
      g.drawString("CPU", 7.5, 27);
      CPU.drawGraph(g, 33, 5.5);
      g.setFontBitmap();
      g.drawString("0", 29, 48);
      g.drawString("100", 21, 6);
      g.drawString("0", 33, 55);
      g.drawString("90", 115, 55);
      g.drawString("Seconds", 64, 55);
      
      if( (getTime() - lastUpdate) > 1.5){
        g.drawString("!", 0, 0);
      }
      
      g.flip();
    }
    
    var g = require("SSD1306").connect(I2C1, function(){
      draw();
      setInterval(draw,1000);
    });
    

    2 Attachments

    • IMG_20170625_041359.jpg
    • IMG_20170625_041518.jpg
About

Avatar for MrTimcakes @MrTimcakes started