Skip to content

MQTT

Warning

ESP32 WiFi ONLY SUPPORTS 2.4GHz. IPhone or Android can be 5GHz. Be sure to allow 2.4Ghz connectivity

MQTT Publish

Go to https://testclient-cloud.mqtt.cool/ connect and subscribe to test/random

Exercise

Run the code on a real ESP32

Hint

It is a simple as: 1) copy the code, 2) set the WiFi ssid and password with the parameters of your WiFi, 3) Install the PubSubClient library by Nick O'Leary, 4) compile and uplad the code in Arduino IDE, 5) test as in the simulatino via https://testclient-cloud.mqtt.cool/

Exercise

Modify the code to send readings from the potentiometer by the ADC

Exercise

Modify the code to recive commands on the ESP32, not simply send random numbers

MQTT Publish/Subscribe

Go to https://testclient-cloud.mqtt.cool/ connect and subscribe to avitaletti/feeds/potentiometer to receive the reading of the potentiometer while publish to avitaletti/feeds/threshold with a value from 0 to 4095 to change the value of the threshold to turn on the led

Exercise

Now connect two real ESP32!

Interact with a server

<!DOCTYPE html>
<html>
<head>
  <meta charset="UTF-8">
  <title>MQTT MP3 Player</title>

  <script src="https://unpkg.com/mqtt/dist/mqtt.min.js"></script>
</head>

<body>

<audio id="player" src="audio.mp3"></audio>
<button id="unlock">Enable audio</button>
<span id="status">Audio locked: click the button once</span>

<script>
const player = document.getElementById("player");
const statusEl = document.getElementById("status");
let audioUnlocked = false;

// Browsers block play() until the user interacts with the page.
// A single click "unlocks" the audio element for later programmatic play().
document.getElementById("unlock").addEventListener("click", async () => {
    try {
        player.muted = true;
        await player.play();
        player.pause();
        player.currentTime = 0;
        player.muted = false;
        audioUnlocked = true;
        statusEl.textContent = "Audio enabled: waiting for MQTT commands";
    } catch (err) {
        statusEl.textContent = "Could not enable audio: " + err.message;
    }
});

function safePlay() {
    player.play().catch(err => {
        console.warn("play() blocked:", err.message);
        statusEl.textContent = "Playback blocked: click 'Enable audio' first";
    });
}

// MQTT broker via WebSocket
const client = mqtt.connect("wss://broker.emqx.io:8084/mqtt");

client.on("connect", () => {
    console.log("MQTT connected");
    client.subscribe("mp3/command");
});

client.on("message", (topic, message) => {

    const command = message.toString().trim();

    console.log("Command:", command);

    if (command === "play") {
        safePlay();
    }

    if (command === "pause") {
        player.pause();
    }

    if (command === "stop") {
        player.pause();
        player.currentTime = 0;
    }
});
</script>

</body>
</html>

Place the above file in a folder with the audio file audio.mp3, Name the file audio.html and open it in Chrome.

Publish a message with topic mp3/command, value play or stop

Note

the broker is wss://broker.emqx.io:8084/mqtt and correnspond to mqtt://broker.emqx.io:1883, namely tcp://broker.emqx.io:1883 on https://testclient-cloud.mqtt.cool/