aboutsummaryrefslogtreecommitdiff
path: root/code/008-logging.js
blob: 971767b620b56510f9c079ae11db94da6b489c5d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
function draw_screen(data) {
  console.log("Drawing the screen with data");
  console.log(data);
  g.drawString("Espruino demo", 2, 2);
  if (data) {
    g.drawString("Temp: "+data.temp.toString(),2,10);
    g.drawString("  RH: "+data.rh.toString(),2,18);
  }
  g.flip();
}

function start() {
  dht = require("DHT22").connect(C11);
  I2C1.setup({scl:B6,sda:B7});
  g = require("SH1106").connect(I2C1, draw_screen);
  fs = require("fs");
}

E.on('init', start);

setWatch(function(e) {
  g.setContrast(255);
  dht.read(draw_screen);
  setTimeout(function () {
    g.setContrast(1);
  }, 4000);
}, BTN, { repeat: true, edge: 'rising' });

setInterval(function() {
  dht.read(function(data) {
    entry = "- {time: " +
      Date.now() +
      ", temp: " + 
      data.temp.toString() +
      ", rh: " +
      data.rh.toString();
    console.log("saving: " + entry);
    fs.appendFileSync("temp_rh.yaml", entry);
  });
}, 60000);

start();