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.
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.
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
neopixel/color and add three ports to the payload -
Send on Change so messages are sent when inputs are changed, and enable
Rate Limit and set it to at
10 messages / second.
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
Noodl will send a MQTT message to the broker (shiftr.io) with the payload encoded as stringified JSON. A message with
blue set to
20, will encode the payload as
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.
Writing the Arduino code
The Ardunio code consists of the following steps:
- Connect to Wifi
- Connect to shiftr.io
- Subscribe to the topic
- Wait for an incoming message
- Parse the payload as JSON and send the color to the NeoPixel ring
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.