• Sure, here you go. The Web output is like a REST/JSON response that gives a sliding window of readings:

    {
    "current" : 41.4,
    "log" : [
    	{ "1514836500" : 47.9 },
    	{ "1514836800" : 41.4 },
    	{ "1514837100" : 39.5 },
    	{ "1514837400" : 39.3 },
    	{ "1514837700" : 39.1 },
    	{ "1514838000" : 39.1 },
    	{ "1514838300" : 38.9 },
        . . . .
    	{ "1515066900" : 41.4 },
    	{ "1515067200" : 41.4 },
    	{ "1515067500" : 41.4 }
    ]
    }
    

    I also send the readings to Graphite so I have a historical view (attached).

    // Wifi setup
    var WIFI_NAME = "myssid";
    var WIFI_OPTIONS = { password : "mypassword" };
    var wifi;
    
    // Where we are in the array.  Once full we'll shift and add to the end
    var arr_ptr = 0;
    
    // Max samples.  
    //    672 is 30 days of 15m samples
    //    4320 is 30 days of 5m samples
    var maxSamples = 4320;
    
    // How often to take a sample in seconds
    var sampleSec = 300;
    
    // Our sample arrays
    var ts_arr = Uint32Array(maxSamples);
    var temp_arr = Float32Array(maxSamples);
    
    // Array elements filled
    var arr_elements = 0;
    
    // Initial temperature setting
    var tempF = 32;
    
    // One wire object
    var ow = new OneWire(B0);
    
    // HTTP object
    var http = require("http");
    
    // Sensor object
    var sensor = require("DS18B20").connect(ow);
    
    // Fahrenheit?
    var fahr = true;
    
    // Graphite server
    graphite_host = "192.168.0.48";
    graphite_port = 2003;
    
    
    // Connect to WIfi
    function onInit() {
      wifi = require("EspruinoWiFi");
      wifi.connect(WIFI_NAME, WIFI_OPTIONS, function(err) {
        if (err) {
          console.log("Connection error: "+err);
          return;
        }
        console.log("Connected!");
        setTimeout(initProcess, 1000);
      });
    }
    
    // Get the temperature
    function updateTempF() {
      sensor.getTemp(function (sensor_temp) {
        tempF = ((sensor_temp * 9) / 5) + 32;
      });
    }
    
    
    // Initialization
    function initProcess() {
      // Get a page so we can get the current date from the header
      http.get("http://www.pur3.co.uk/hello.tx­t", function(res) {
        // Set our time bsaed on the header date
        now = res.headers.Date;
        epoch = Date.parse(now);
        setTime(epoch/1000);
        date = new Date();
        console.log("Init: ", date.toUTCString());
    
        // Set logging and the server to start after a few seconds
        updateTempF();
        setTimeout(startLogging, 3000);
        setTimeout(startServer, 5000);
      });
    }
    
    
    // Start our Web server
    function startServer() {
      console.log("Starting server");
      http.createServer(function (req, res) {
        updateTempF();
        res.writeHead(200, {'Content-Type': 'application/json'});
        if (arr_elements === 0) {
          res.end("{\n\"current\" : " + tempF.toFixed(1) + ",\n\"log\" : []\n}");
          return();
        }
        var i = 0; 
        res.on('drain', function() {
          for (var n = 0; n < 10; n++) {
            sample = "\n\t{ \"" + ts_arr[i] + "\" : " + temp_arr[i].toFixed(1)+ " }";
            res.write(sample);
            if (++i == arr_elements) {
              res.end("\n]\n}");
              break;
            }
            else {
              res.write(",");
            }
          }
        });
    
        page = "{\n\"current\" : " + tempF.toFixed(1) + ",\n\"log\" : [";
        res.write(page);
      }).listen(80);
    }
    
    
    // Start logging the temperature
    function startLogging() {
      console.log("Starting logging");
      setInterval(function() {
        sensor.getTemp(function (sensor_temp) {
          now = Date.now() / 1000;
          floor = parseInt(now);
          if (fahr)
            tempF = ((sensor_temp * 9) / 5) + 32;
          else
            tempf = sensor_temp;
          mod = floor % sampleSec;
          if (!mod) {
            if (arr_ptr == maxSamples) 
              arr_ptr = 0;
            ts_arr[arr_ptr] = parseInt(floor);
            temp_arr[arr_ptr++] = tempF;
            if (++arr_elements > maxSamples)
              arr_elements = maxSamples;
          }
          // Also send to Graphite
          socket = require("net").connect({"host" : graphite_host, "port" : graphite_port}, function() {
            msg = "espruino-attic-oaktreepeak-com.temperat­ure " + tempF + " " +  now + "\n";
            socket.write(msg);
            socket.end();
          });
        });
      }, 1000);
    }
    
    

    1 Attachment

    • graph.jpg
About

Avatar for user84292 @user84292 started