Local MQTT broker setup and working with Node-RED

Pure-javascript-MQTT-broker

In my previous tutorials we talked to Arduino device using gcloud and now lets try by running mqtt broker on the local network. Here are all the steps needed for the integration.

  1. Installing Node-RED
  2. Connecting with Mos
  3. Circuits on Node-RED
  4. Installing Mosquitto
  5. Button, LED, Temperature sensor tutorials

Installing Node-RED:

A programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. It provides a browser-based editor that makes it easy to wire together flows using the wide range of nodes in the palette that can be deployed to its runtime in a single-click.

Steps to install on macOS: 

  • Download installer package for node.js macOS 64-bit from here.

nodered.png

  • Check the version of node.js installed using node -v

1

  • Try to install using
    sudo npm install -g --unsafe-perm node-red

installnodered

  • Open node-red using the command node-red in terminal

2

  • node-red starts running on the local server and we can open http://127.0.0.1:1880/  on any of the browsers and the UI pops-up

3

It looks all good so far. But now how do we make our device talk to Node-RED ? We have to make sure that they are both talking through the local server.

Connecting to mos UI :

Remember how we start our mos UI ? repeat the same steps and let the init.js run

10

  • Go to Device Config  and update the MQTT server url with the systems IP address. Click on Save configuration

1519075244339

Mac users this is how you do it. I understand that first time users will struggle to figure this out, as it happened to me while doing it. Credits to professor Mr. Paul Schragger for helping me with this part. Here are the steps on how to set the local network open to connected devices on Mac,

Network- WiFi(Internet) sharing and IP address :

  • Go to System Preferences

11

  • Open Sharing

12

  • Enable internet sharing

13

  • Go to System Preferences –> Network –> TCP/IP and get the IPv4 Address

1519075162450

Once that is done, we now get back to the Node-RED page and start the circuit connections, I am adding input Inject, mqtt and output debug nodes to the flow.

Circuits on Node-RED:

  • Edit the debug node by clicking on it.

8

  • Edit the mqtt-broker node. Add the server as localhost:1883 , topic as iot-topic and give it some name.

6

  • Click on the “Pencil” symbol next to server name to make more changes.

7

  • Edit the inject node. Give it the timestamp payload and set the repeat intervals as needed.

5

  • Below is the completed circuit. You can connect them by dragging the lines and joining the nodes (Grey lines) and deploy.

4

We see the tick-tock up times and messages displayed under debug section after deploying.

Now what?

How do I make my local mqtt server talk to my Arduino device? Also I have realized I have a problem with my connection. Because if you have observed in the above picture it shows that it is still connecting to my mqtt-broker. After searching a good amount of urls, I have understood that I have to Install Mosquitto on my system and that’s how they both talk. Eclipse Mosquitto is an open source (EPL/EDL licensed) message broker that implements the MQTT protocol versions 3.1 and 3.1.1. It is lightweight and is suitable for use on all devices from low power single board computers to full servers. We were missing a critical requirement may be!! I thought as I have mqtt-broker on Node-RED I might not need an other installation, but looks like I need one.

Mosquitto installation:

  • Mac users should install using Home-brew –$ brew install mosquitto

17

15

  • Prepare mosquitto for launchd$ ln -sfv /usr/local/opt/mosquitto/*.plist ~/Library/LaunchAgents
  • Run mosquitto$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mosquitto.plist
  • Now let’s try the broker, in the terminal launch the subscriber client$ mosquitto_sub -h 127.0.0.1 -t topic
  • In another terminal launch the publisher client$ mosquitto_pub -h 127.0.0.1 -t topic -m "Hello world!"
  • In the first terminal you should see the Hello world! message.

18

Now my MQTT server is up and connected. This reflects on Node-Red when we deploy. Below we will look at couple of tutorials and see how our device is integrated to the local server.

Internet Button Tutorial:

Please look here for the connections on breadboard and the setup on mongoose OS for this tutorial . We will directly talk about the circuit on Node-RED.

  • Take one mqtt input node and one debug output node like we did above for init.js file, connect them and deploy

28

  • We see the time when the button is pressed in Node-RED and mos

29

Now we will try to make mqtt as our output and give commands using Node-Red to the device.

LED Tutorial (ON and OFF) :

  • Update the code for LED tutorial in mos ui as mentioned in previousl tutorial
  • Drag two inject nodes and edit them as shown below. Change the payload to {“on”:true} for one and

22

  • {“on”: false} for the other inject node. Topic should be /devices/”device-id”/events.

23

  • Take a debug node and mqtt output node and edit that as below.

24

  • Connect all the nodes as shown below and Deploy 

20

  • When you click on the inject buttons you see the message payloads as below

27

  • When the on inject button is pressed, LED turns on

IMG_3334

  • When the OFF inject button is pressed, LED turns off

IMG_3335

Finally, after a week of effort I could make my local mqtt-broker talk to my device.

Temperature & Humidity Sensor:

  • Make the connections like we did in earlier tutorial
  • Reboot the device and update device config in mos

31

  • Make the connections similar to that of Internet button and deploy. You see the messages flowing from the sensor to the debug panel as below.

30.png

Controlling LED using Internet Button

It is similar to what we did above for Internet Button and LED individually, but we have to make some modifications in code and on Node-Red sides.

  • Make the connections, I am using GPIO Pin 26 for Button and Pin 21 for LED

IMG_3612.JPG

  • Invoke mos and update the init.js file with below code. What we are doing here is to communicate with the device using two different pins
load('api_config.js');
load('api_gpio.js');
load('api_mqtt.js');

let topic2 = '/devices/' + Cfg.get('device.id') + '/config';
let pin2 = 21;

GPIO.set_mode(pin2, GPIO.MODE_OUTPUT);

MQTT.sub(topic2, function(conn, top, msg) {
 print('Got config update:', msg.slice(0, 100));
 let obj = JSON.parse(msg);
 if (obj) GPIO.write(pin2, obj.on);
}, null);

let topic = '/devices/' + Cfg.get('device.id') + '/events';
let pin = 26;
GPIO.set_mode(pin, GPIO.MODE_INPUT);
GPIO.set_pull(pin, GPIO.PULL_DOWN);
GPIO.set_button_handler(pin, GPIO.PULL_DOWN, GPIO.INT_EDGE_NEG, 200, function() {
 let msg = JSON.stringify({ on: GPIO.toggle(pin) });
 let ok = MQTT.pub(topic, msg, 1);
 print(ok, msg);
}, null);
  • Update the device configuration like we did above with the IPv4 address, enable MQTT, save and reboot the device
  • Add two MQTT nodes, one In & other Out to the flow on Node-Red.

Picture2.png

  • Edit the nodes and update the topics as shown below.

4 .     3

  • Update and deploy the flow on Node-Red
  • Now on press of the Internet Button the LED goes On and Off. You can see the device logs on mos UI as the action is performed

Picture1

The same is captured in this video

Here are a few URLs that I have gone through to make this all work and to do the tutorials. Next time we will try to connect the device to a database and store the activities.

Advertisements

One thought on “Local MQTT broker setup and working with Node-RED

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s