node.jsxivelynode-red

Xively and Node-Red


I'm fairly new at all this but have muddled my way to getting my Arduino to post values to a Xively stream I've named "Lux and Temp." Three values; count, lux, and temperature.

Now what I want to do is take those values and do something with them using Node-Red. http://nodered.org

I have Node-Red up and running, but I'll be danged if I can figure out how to parse the data from the Xively API feed. https://api.xively.com/v2/feeds/1823833592

Sadly, I don't have enough reputation points to be able to actually post the data it returns to here, since it has more than 3 URLs embedded in the data. It's a LONG string of data too. ;)

I'm just stumped though as to how to write the function to extract the parts I want.

My initial want is to make a simple Twitter feed out of it. Something like;

"Count 40, Lux 30, Temp 78.3"

I'll eventually want to recycle the code for other things like making my RasPi do something; maybe a display or some LEDs. In either case I need to parse the data and build various messages with it.

Anybody have any experience with the Node-Red functions that can walk me through a solution? The Node-Red site is pretty awesome, but I think it assumes I'm a MUCH more experienced user than I really am. It gives hints, but frankly about all I know is fairly basic Arduino and trivial level Python.


Solution

  • OK, it shouldn't be too tricky, but try putting this in a function block:

    var newPayload = "";
    var count, lux, temp;
    
    var data = msg.payload.datastreams;
    
    for (var i = 0 ; i< data.length; i++) {
      if (data[i].id === 'Count') {
        count = data[i].current_value;
      }else if (data[i].id === 'Lux') {
        lux = data[i].current_value;
      } else if (data[i].id === 'Temp') {
        temp = data[i].current_value;
      }
    }
    
    newPayload = util.format('Count: %s, Lux: %s, Temp: %s', count, lux, temp);
    
    msg.payload = newPayload;
    
    return msg;
    

    You may need to add a msg.payload = JSON.parse(msg.payload); to the start if however your getting the feed from xively is not already considered to be json. [edit] You could also just run the flow through a JSON parse node. (I always forget the converter nodes)

    You should be able to wire that to a twitter output node.