Electrical engineer/inventor Guglielmo Marconi with the spark-gap transmitter (right) and coherer receiver (left) he used in some of his first long distance radiotelegraphy transmissions during the 1890s. (source)
Sensors: BLE and MQTT (google slides)
We are exploring shiftr.io as our mqtt broker. You can view a live visualization of their public broker here (brace yourself): https://www.shiftr.io/try/
There are many other brokers available, in addition to ones you can run on your own devices (for instance Moqsuitto MQTT on Raspberry Pi).
We will run these examples on the ESP32 devboard, connected to an MQTT broker on shiftr.io.
When you upload the arduino code below, you wil specify the “topic” it is publishing/subscribing to. You will want to name this topic to correspond to your device.
For instance, by default the example will say /smartenv/devboard1
. We don’t want 15 of those boards with the same name, so try renaming it to something individualized (f.ex. /smartenv/robertservo
).
Source code link: mqtt-stored-credentials.zip
Install the PubSubClient with the Arduino Tools->Manage Libraries
saveWifiCredentials()
and add your UCSD username and password.loadWifiCredentials()
function any other time you need to login with username and password.This program logs into your wifi, connects to an MQTT server at shiftr.io (https://public.cloud.shiftr.io/), and then publishes info on a topic. It also subscribes to that topic, listening for information.
In the serial port you should be able to see debug information when it first connects to the wifi, connects to the MQTT server, and when it receives any updates on the topic.
To test out the MQTT connectivity, we can use a simple p5 sketch mqttPublisher to read or write info from the topic. (edit link)
By default, the topic for your device is /smartenv/devboard1
, defined both in the Arduino code and the p5 sketch.
NOTE: You will want to change this topic to give your own devboard a name, so f.ex. /smartenv/robert1/
. For now, let’s keep the /smartenv/
base the same for everyone, so we can see all of our messages in the shiftr online graph. You will need to change the topic in both the arduino code and the p5 sketch for thit to work.
Change the topics (line 166, 168) to reflect your device name. So go from:
if (digitalRead(SW1)) {
client.publish("/smartenv/devboard1/sw1", "1");
} else {
client.publish("/smartenv/devboard1/sw1", "0");
}
to
if (digitalRead(SW1)) {
client.publish("/smartenv/robert1/sw1", "1");
} else {
client.publish("/smartenv/robert1/sw1", "0");
}
We can also use the public shiftr.io interface to view all of the MQTT messages being sent through their server.
Open https://public.cloud.shiftr.io/ and look for the /smartenv/
topic, so for instance /smartenv/devboard1
as the base code is configured.
Our source code built in a way to control LED2 (pin 18)using the topic address /smartenv/devboard1/
:
on
to this topic (from the p5 sketch) turns the orange light // LED2
off
turns off LED2
Adapt the source code to move a servo motor in addition to controlling the LED.
Topic: /smartenv/devboard1
(choose the correct topic name for your device)
Messages
Add code to control the movement. Assuming the servo is attached to pin D0, you want to check for data such as:
left
to move all the way to the leftright
to move all the way to the rightmove 90
to move some number of degrees. you can parse this message and decode the “move” and the number each separately.
Test It Again, we can use the p5 mqttPublisher to test it out. (edit link)
Adapt the source code to control a relay instead of the built-in LED.
Topic: /smartenv/devboard1
(NOTE: you will want to change this topic to give your own devboard a name, so f.ex. /smartenv/robert1/
)
Messages
Relay control:
on
turns relay on
off
turns relay off
Test It Again, we can use the p5 mqttPublisher to test it out. (edit link)
Let’s all wire up some different sensors/actuators and assign them different MQTT topics.
Play around with interoperability! (E.A.T)