W07- Huzzah32- MQTT Send / Receive

Explorations this week began with review and continuation of W06 code samples.

Demo of a Node server ( W11 ) was also shown.

New examples this week:

Multi-topic MQTT :: Huzzah32 Send, p5js Receive

There are several examples in this folder that show how different approaches to sending MQTT data from a Huzzah32. In all examples the p5js sketches are receiving multiple topics. Check the onMqttMessageArrived(message) function in (mqtt.js) to see how it’s done.

Required Circuits

All examples require two basic circuits — a photocell and a button.

See how to set those circuits up in this post. Find source code here. [download]

Huzzah32-mqttsend-simple

1-Huzzah32-mqttsend-simple [download]

This example reads the above circuits and sends their states and MQTT from the Huzzah to p5js.

Huzzah32

On the Huzzah32 side, the message build and message send functions are split apart. The buildButtonMessage(int) function uses the glue library. The buildPhotocellMessage(int) function builds Strings directly.

Sending is done in publishMqttMessage(buttonTopic, payload) — found at the bottom of MQTT.ino.

p5js

The p5js side uses simple graphical elements to display the changes in sensor state.

flappy-huzzahController

2-flappy-huzzahController [download]

This example reads the sensors from the circuits above, but controls images rather than graphical primitives.

Huzzah32

On the microcontroller side — use the same code as in the example above.

p5js

In this example images are used instead of simple graphics. Compare the p5js code to understand the differences in how the graphics are constructed.

You can in fact have both of the above p5js examples open and receiving data at the same time. Give it a try.

flappy-p5js-Only

3-flappy-p5js-Only [download]

This is a p5js only example of the character from example two (2) above. It uses key commands to move the character rather than the photocell.

Multi-topic MQTT :: Huzzah32 Receive

Huzzah32-MqttRecMultiTopic [download]

Receiving multiple topics in MQTT requires a slight change to how we listen for MQTT messages. Where earlier examples listened for a single topic by polling in loop — here we use the event function of the arduinoMQTTClient library.

Within this function we determine which topic arrived and THEN use the Scissors library to parse the messages.

Check the mqttMessageReceive(int messageSize) function in mqttMultiTopic-ard.ino to see how its is woven together.