Javascript

From wikipost
Revision as of 10:07, 27 July 2020 by Admin (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

JSON things I used in my node-red home automation projects


Code to read multiple DS18B20 or DS18S20 temperature sensors from a Wemos D1 mini and feed them into a graph object in node-red

var obj = JSON.parse(msg.payload);

/* 
   example output from topic 'house/wemos2/tele/SENSOR'

    {
      "Time":"2020-07-26T10:38:21",
      "DS18S20-1":{"Id":"000802015ED5","Temperature":20.3},
      "DS18B20-2":{"Id":"02131E3901AA","Temperature":19.8},
      "TempUnit":"C"
    }
*/

if ("DS18S20-1" in obj)
{
  var msg1 = {
    payload : obj['DS18S20-1'].Temperature,
    topic : "Peltier-Cold"
  }
}

if ("DS18B20-2" in obj)
{
    var msg2 = {
      payload : obj['DS18B20-2'].Temperature,
      topic : "Inside Box"
    }
}

if ("DS18B20-3" in obj)
{
    var msg3 = {
      payload : obj['DS18B20-3'].Temperature,
      topic : "Peltier Hot"
    }
}

node.send([[msg1,msg2,msg3]]);