Introduction: IoT: Visualizing Light Sensor Data Using Node-RED

About: Electronics Engineer | Always in the phase of continuous learning

In this instructable, you’ll learn how to create an internet connected sensor! I’ll be using an ambient light sensor (TI OPT3001) for this demo, but any sensor of your choice (temperature, humidity, potentiometer, etc.) would work. The sensor values will be published to a cloud-based application using MQTT.

There are many tutorials out there that show how you can achieve this using an Arduino or a Raspberry Pi. We'll accomplish this demo using TI's (Texas Instruments) LaunchPad Ecosystem.

Step 1: Watch the Video!

Step 2: Hardware

Components used
- TI MSP432 LaunchPad – $19.99 (US)
[http://www.ti.com/tool/MSP-EXP432P401R]
- SimpleLink Wi-Fi CC3100 Module – $19.99 (US)
[http://www.ti.com/tool/CC3100BOOST]
- Educational BoosterPack MKII – $29.99 (US)
[http://www.ti.com/tool/BOOSTXL-EDUMKII]

Q> If you're wondering what an Educational BoosterPack MKII is 🤔?
A> It is an easy-to-use plug-in module that offers various analog and digital inputs and outputs at your disposal including an analog joystick, environmental and motion sensors, RGB LED, microphone, buzzer, color LCD display, and more.

- Power Bank and Micro USB Cable

Sensor
- TI OPT3001 – Ambient Light Sensor
[http://www.ti.com/product/OPT3001]

Step 3: Hardware Setup

Plug your CC3100 Wi-Fi module and the Educational BoosterPack MKII into your LaunchPad, then plug your LaunchPad to one of your computer’s USB ports.

Step 4: MQTT Basics

MQTT stands for Message Queuing Telemetry Transport.
It’s a lightweight publish/subscribe messaging protocol. It is useful for use with low power sensors but is applicable to many scenarios. The protocol consists of 3 main components: PUBLISHER, BROKER and SUBSCRIBER.

PUBLISHER: The MSP432 LaunchPad will be the PUBLISHER, as it will be publishing light sensor data under a specific topic.

BROKER: Acts as a simple, common interface for everything to connect to. It’s a server that handles the data transmission between the PUBLISHER and the SUBSCRIBER. In this example, we are using a publicly accessible MQTT broker, often useful for prototyping and testing.
Here’s a list of public brokers:
[https://github.com/mqtt/mqtt.github.io/wiki/public_brokers]

SUBSCRIBER: In order to subscribe to any data that is being sent by a PUBLISHER, the SUBSCRIBER must be connected to the same BROKER and subscribed to the same topic as the PUBLISHER. If these 2 conditions are met, the SUBSCRIBER will be able to receive messages from the PUBLISHER.

NOTE: With MQTT, multiple publishers and subscribers can be using the same Broker/Topic. In addition, a single publisher could be sending data to more than one subscriber.

Step 5: Energia

Energia is an open source & community-driven integrated development environment (IDE) & software framework that supports many TI processors, primarily those available in the LaunchPad development ecosystem.
Download: [https://energia.nu/download/]

E1. Open Energia IDE and select the correct Serial Port and Board by navigating to: Tools
E2. Energia comes preloaded with example programs for the Educational BoosterPack MKII. To verify that the light sensor is working, open and upload the example code for OPT3001 by going to: File > Examples > EducationalBP_MKII > OPT3001_Demo
E3. If everything is working, the light sensor readings should start streaming into the Serial Monitor. Vary the light exposure to see the sensor values change.
E4. The Energia version (0101E0017) I’m currently using comes preloaded with the library for MQTT PubSubClient. If you’re using a version of Energia that doesn’t have this library, you can get it from:
[https://github.com/energia/Energia/tree/master/libraries/PubSubClient]
E5. The sketch is a slight modification of the example that’s available in: File > Examples > PubSubClient > MQTTBasicWiFi
E6. The only thing we’ll need to modify is our “ssid” and “password” information for our Wi-Fi router.
E7. The public MQTT Server that is used in the sketch is [http://mqtt.eclipse.org/]. To change the TOPIC that our LaunchPad is publishing to, replace the string with your own in the client.publish() function call in the main loop().
E8. Upload this program to the LaunchPad by clicking on the Upload button.
E9.
Open the Serial Monitor. You should see sensor values flowing in as well as “Publish successful!!”.

Step 6: IBM Cloud

Now that we’re publishing light sensor data, let’s create a cloud-side application that can subscribe to our LaunchPad and visualize our sensor data. We’ll use Node-RED, which is available on the IBM Cloud platform as one of the Starter kits applications in the catalog.

What is Node-RED?
Node-RED is a programming tool for wiring together hardware devices, APIs and online services in new and interesting ways. Node-RED is built on Node.js, taking full advantage of its event-driven, non-blocking model. This makes it ideal to run at the edge of the network on low-cost hardware such as the Raspberry Pi as well as in the cloud.

C1. Sign up for an IBM Cloud account by using your existing IBMid or by creating a new IBMid.
C2. Once you’re logged into IBM Cloud, you will be taken to your Dashboard.
C3. Click on the Catalog tab and search for Node-RED App.
C4. Click on the Create app button to continue. This will create your new cloud-based application. This may take a few minutes to complete!
C5. Now that you’ve deployed your Node-RED application, open your IBM Cloud Resource list by selecting the sidebar menu and then selecting Resource List. You will see your newly created Node-RED Application listed under the Apps section.
C6. Click on the Cloud Foundry app entry to go to your deployed application’s details page. Click the Visit App URL link to access your Node-RED Starter application.

Step 7: Node-RED App

N1. The first time you open your Node-RED app, you’ll need to configure it and set up security.
N2.
Click the Go to your Node-RED flow editor button to open the editor.
N3.
The Node-RED editor opens showing the default flow.
N4. Drag the mqtt in block from the Node-RED palette into the empty sheet.
N5. Double-click the mqtt block and edit the properties with the same parameters that your LaunchPad is publishing to:
Server – mqtt.eclipse.org:1883
Topic – EDUMKII_IOT
Once configured, click Done.
N6. After wiring up the remaining nodes, click the Deploy button on the top-right. This will cause your application to start running.
N7. Click the debug tab to finally see sensor values from your LaunchPad streaming in!
N8. Click the link in the Layout tab of the dashboard to see sensor values in chart and gauge mode.
N9. Congratulations on making to the final step! You can now visualize real-world sensor data in the cloud!!

References

MQTT.ORG
[http://mqtt.org/]
Energia – MQTT Tutorial
[https://energia.nu/guide/tutorials/connectivity/tutorial_mqtt/]
Node-RED
[https://nodered.org/]
Running on IBM Cloud
[https://nodered.org/docs/getting-started/ibmcloud]
Create a Node-RED starter application
[https://developer.ibm.com/components/node-red/tutorials/how-to-create-a-node-red-starter-application/]