Skip to content

Instantly share code, notes, and snippets.

@cmoulliard
Last active November 30, 2021 17:08
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cmoulliard/6df2c724c3a61243650ddb5364f0341a to your computer and use it in GitHub Desktop.
Save cmoulliard/6df2c724c3a61243650ddb5364f0341a to your computer and use it in GitHub Desktop.

Intro

The data collected by the ispindle IoT can be forwarded to a server, MQTT broker, brewery web site, ...

The following node-red flow has been designed to collect from a MQTT broker running on a laptop (or raspberry pi) the data which are then enriched in order to post them on ubidots

Before to start the flow locally, install node-red and mosquitto as MQTT broker

Edit the mosquitto config file (e.g: /usr/local/etc/mosquitto/mosquitto.conf) to append the following 2 parameters supporting to access the broker without user/pwd and to use non localhost as hostname

listener 1883
allow_anonymous true

Open a terminal, start the MQTT broker (e.g: `brew services start mosquitto)

To be able to work with the ubidots_out node flow, it is needed to specify as token your own API Token that you can find here within the ubidots_dots config

  {
    "id": "9882cb140721248b",
    "type": "ubidots_out",
    "z": "a490cb4b563e3faf",
    "name": "",
    "token": "BBFF-xxxxxxxxTO_BE_CHANGEDxxxxxx",
...

Change also the mqtt-broker object to specify the IP address (see broker) of yourpi, laptop where the MQTT broker is running like also thename to be used (e.g mosquitto-mac`)

  {
    "id": "e7a90bd264380835",
    "type": "mqtt-broker",
    "name": "mosquitto-mac",
    "broker": "192.168.1.90",
    "port": "1883",
...

When done, you can launch node-red using the following flow

export DEVICE_NAME="ispindle001"
node-red ./flows/ispindle-mqtt-ubidots.json

NOTE: Don't forget to pass as parameter the device name to be used to report the data under https://stem.ubidots.com/app/devices/ web page

Usefull links

node-red and ubidots: https://help.ubidots.com/en/articles/1440402-connect-node-red-with-ubidots

Flow

from mqtt topics created by ispindle -> to ubidots_out (= another mqtt broker) where density is calculated from gravity

[
  {
    "id": "a490cb4b563e3faf",
    "type": "tab",
    "label": "ispindle-mqtt-ubidots",
    "disabled": false,
    "info": "",
    "env": []
  },
  {
    "id": "9882cb140721248b",
    "type": "ubidots_out",
    "z": "a490cb4b563e3faf",
    "name": "",
    "token": "BBFF-xxxxxx",
    "label_device": "",
    "device_label": "MacDabou_x",
    "tier": "educational",
    "tls_checkbox": true,
    "x": 890,
    "y": 160,
    "wires": []
  },
  {
    "id": "bf0fb6917ad986ab",
    "type": "mqtt in",
    "z": "a490cb4b563e3faf",
    "name": "Ispindel001",
    "topic": "ispindel/Ispindel001/#",
    "qos": "2",
    "datatype": "auto",
    "broker": "e7a90bd264380835",
    "nl": false,
    "rap": true,
    "rh": 0,
    "inputs": 0,
    "x": 100,
    "y": 80,
    "wires": [
      [
        "189e3da5981418b1"
      ]
    ]
  },
  {
    "id": "d1d642a53b8169ed",
    "type": "debug",
    "z": "a490cb4b563e3faf",
    "name": "",
    "active": true,
    "tosidebar": true,
    "console": false,
    "tostatus": false,
    "complete": "payload",
    "targetType": "msg",
    "statusVal": "",
    "statusType": "auto",
    "x": 890,
    "y": 80,
    "wires": []
  },
  {
    "id": "189e3da5981418b1",
    "type": "function",
    "z": "a490cb4b563e3faf",
    "name": "enrich_messages",
    "func": "var deviceLabel = env.get('DEVICE_NAME')\nvar label, new_payload\nvar topic_prefix = \"ispindel/Ispindel001\"\n\nswitch (msg.topic) {\n    case topic_prefix + \"/tilt\":\n      label = \"tilt\";\n      new_payload = {[label]: parseFloat(msg.payload)};\n    break;\n    \n    case topic_prefix + \"/battery\":\n      label = \"battery\";\n      new_payload = {[label]: parseFloat(msg.payload)};\n    break;\n    \n    case topic_prefix + \"/temperature\":\n      label = \"temperature\";\n      new_payload = {[label]: parseFloat(msg.payload)};\n    break;\n    \n    case topic_prefix + \"/gravity\":\n      label = \"gravity\";\n      new_payload = {[label]: parseFloat(msg.payload)};\n    break;\n    \n    case topic_prefix + \"/RSSI\":\n      label = \"RSSI\";\n      new_payload = {[label]: parseFloat(msg.payload)};\n    break;\n\n    case topic_prefix + \"/interval\":\n      label = \"interval\";\n      new_payload = {[label]: parseFloat(msg.payload)};\n    break; \n    \n    case topic_prefix + \"/temp_units\":\n      label = \"temp_units\";\n      // new_payload = {[label]: {value: msg.payload}};\n      new_payload = {[label]: {value: 1, context: {units: \"°C\"}}}\n    break;     \n}\n\n\nmsg.topic = label      \nmsg.payload = new_payload\nmsg.payload.ubidotsDeviceLabel = deviceLabel\n\n// node.log(\"Device name: \" + env.get('DEVICE_NAME'))\n\nnode.log(\"Payload: \" + JSON.stringify(msg.payload))\nnode.log(\"Topic : \" + msg.topic)\n      \nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 330,
    "y": 80,
    "wires": [
      [
        "05c0f15d683d6d25"
      ]
    ]
  },
  {
    "id": "d1a744cf2741bc63",
    "type": "comment",
    "z": "a490cb4b563e3faf",
    "name": "ispindle name",
    "info": "The device name defined part of the ispindle configuration should match exactly the `DEVICE_NAME` of the topic\n\nexample:\n\nDEVICENAME=\"Ispindle001\"\n\n->\n\nTOPIC=\"ispindle/$DEVICENAME/#\"",
    "x": 210,
    "y": 300,
    "wires": []
  },
  {
    "id": "05c0f15d683d6d25",
    "type": "join",
    "z": "a490cb4b563e3faf",
    "name": "",
    "mode": "custom",
    "build": "merged",
    "property": "payload",
    "propertyType": "msg",
    "key": "topic",
    "joiner": "\\n",
    "joinerType": "str",
    "accumulate": false,
    "timeout": "5",
    "count": "",
    "reduceRight": false,
    "reduceExp": "",
    "reduceInit": "",
    "reduceInitType": "",
    "reduceFixup": "",
    "x": 530,
    "y": 80,
    "wires": [
      [
        "5cb2a16a39de19ee"
      ]
    ]
  },
  {
    "id": "a9d9ffa86e798a29",
    "type": "mqtt out",
    "z": "a490cb4b563e3faf",
    "name": "",
    "topic": "ispindel/Ispindel001/tilt",
    "qos": "2",
    "retain": "",
    "respTopic": "",
    "contentType": "application/json",
    "userProps": "",
    "correl": "",
    "expiry": "",
    "broker": "e7a90bd264380835",
    "x": 330,
    "y": 160,
    "wires": []
  },
  {
    "id": "2e8bc76c70ad6df3",
    "type": "inject",
    "z": "a490cb4b563e3faf",
    "name": "tilt",
    "props": [
      {
        "p": "payload"
      },
      {
        "p": "topic",
        "vt": "str"
      }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "tilt",
    "payload": "23.24",
    "payloadType": "num",
    "x": 110,
    "y": 160,
    "wires": [
      [
        "a9d9ffa86e798a29"
      ]
    ]
  },
  {
    "id": "cf148097a0c4e7a4",
    "type": "mqtt out",
    "z": "a490cb4b563e3faf",
    "name": "",
    "topic": "ispindel/Ispindel001/gravity",
    "qos": "2",
    "retain": "",
    "respTopic": "",
    "contentType": "application/json",
    "userProps": "",
    "correl": "",
    "expiry": "",
    "broker": "e7a90bd264380835",
    "x": 340,
    "y": 220,
    "wires": []
  },
  {
    "id": "abb5f2116cea26f5",
    "type": "inject",
    "z": "a490cb4b563e3faf",
    "name": "gravity",
    "props": [
      {
        "p": "payload"
      },
      {
        "p": "topic",
        "vt": "str"
      }
    ],
    "repeat": "",
    "crontab": "",
    "once": false,
    "onceDelay": 0.1,
    "topic": "gravity",
    "payload": "0.59",
    "payloadType": "num",
    "x": 110,
    "y": 220,
    "wires": [
      [
        "cf148097a0c4e7a4"
      ]
    ]
  },
  {
    "id": "5cb2a16a39de19ee",
    "type": "function",
    "z": "a490cb4b563e3faf",
    "name": "gravity-to-sg",
    "func": "var density = 1+(msg.payload.gravity/(258.6-((msg.payload.gravity/258.2)*227.1)))\n\nnode.log(\"#### Density: \" + density)\n\nmsg.payload.density = density\nreturn msg;",
    "outputs": 1,
    "noerr": 0,
    "initialize": "",
    "finalize": "",
    "libs": [],
    "x": 710,
    "y": 80,
    "wires": [
      [
        "d1d642a53b8169ed",
        "9882cb140721248b"
      ]
    ]
  },
  {
    "id": "e7a90bd264380835",
    "type": "mqtt-broker",
    "name": "mosquitto-mac",
    "broker": "192.168.1.90",
    "port": "1883",
    "clientid": "",
    "autoConnect": true,
    "usetls": false,
    "protocolVersion": "4",
    "keepalive": "60",
    "cleansession": true,
    "birthTopic": "",
    "birthQos": "0",
    "birthPayload": "",
    "birthMsg": {},
    "closeTopic": "",
    "closeQos": "0",
    "closePayload": "",
    "closeMsg": {},
    "willTopic": "",
    "willQos": "0",
    "willPayload": "",
    "willMsg": {},
    "sessionExpiry": ""
  }
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment