Communicating with an Arduino

Noodl can connect to external hardware using MQTT, a standardized protocol that's commonly used in IoT solutions. MQTT is a lightweight protocol where multiple clients communicate via a broker.

This guide will go through the steps of signing up to a free online MQTT broker and using Noodl to control a NeoPixel Ring, connected to an Arduino MKR1000. It can also be used as a reference on how to connect other types of devices that can communicate with MQTT, the steps in Noodl are the same.

 

Using shiftr.io

There are many MQTT brokers available. Some solutions you can host yourself, others are hosted for you, some have paid plans, and some are free. We're going to use shiftr.io.

Start by going to shiftr.io. Sign up and log in to your dashboard. Now you need to create a namespace. A namespace is used to connect multiple devices and devices connected to the same namespace can receive and send messages to each other. You'll probably want to create one namespace per project, or reuse the same namespace across multiple projects.

Once you've created a namespace, you need to add a token. A token is used to connect to the namespace and allows different devices to have different permissions. The easiest way to get started is to only create one token, with full access and use it on all your devices.

Once you have your token, the broker is ready to be used.

To recap, a namespace is a shared space for all your devices and Noodl projects to talk to each other. You can have multiple tokens, basically a username and a password, to connect to one namespace. You can either have one token for all devices, or different tokens with different permissions for each device.

 

Creating the Noodl project

Create a new Noodl project and go to project settings. Copy the mqtt:// url from shiftr to the Broker Url in the project.

Replace the Broker URL in this example with your own broker url

Replace the Broker URL in this example with your own broker url

Noodl will use this broker for all messages that are sent and received with the Send Message and Receive Message nodes. This includes communication between multiple devices running Noodl and is not limited to external hardware.

Add three sliders from the library in Noodl and create a Send Message node. Add the sliders to a Stack Layout and set the Max Value of the sliders to 255. The Ardunio will be able to set the color of the NeoPixel using three values between 0 (no light) and 255 (maximum brightness), one value for red, green and blue. These three colors can be combined to achieve almost any color.

Select the Send Message node and set the Topic to neopixel/color and add three ports to the payload - red, green and blue. Enable Send on Change so messages are sent when inputs are changed, and enable Rate Limit and set it to at 10 messages / second.

If Rate Limit is disabled, Noodl will send around 60 messages per second when adjusting a slider. This is a bit too fast for the rest of the system to process and will introduce delays. Around 10 seems resonable, but feel free to experiment.

Connect the first slider to red, the second to green and the third to blue.

You can also create three sliders to control the red, green and blue color components individually

Noodl will send a MQTT message to the broker (shiftr.io) with the payload encoded as stringified JSON. A message with red, green and blue set to 20, will encode the payload as {"red":20,"green":20,"blue":20}.

The next step is to write the code for the Arduino MKR1000. We'll connect to the same broker, subscribe to the neopixel/color topic, parse the payload, and send the color to the NeoPixel ring.

Download the Noodl Project

Writing the Arduino code

The Ardunio code consists of the following steps:

  1. Connect to Wifi
  2. Connect to shiftr.io
  3. Subscribe to the topic neopixel/color
  4. Wait for an incoming message
  5. Parse the payload as JSON and send the color to the NeoPixel ring

Download the code for the Ardunio

You need to install the following libraries:

Instructions on how to install libraries can be found here.

Tip: If you get the error "No device found" when trying to upload your code, press the reset button twice and try again.