Projects / Sensor to phone over MQTT

Sensor to phone over MQTT

Advanced1.3 hGuided buildReviewed
ESP32 DevKit3V3GPIO21GPIO22GNDBME280MQTT brokerroomt 25 °Ch 50 %every 10 sPhone app
publish electrolark/…/room {"t":25,"h":50}

Tap a part to see what it does.

Tap the parts to see what each one does. A teaching simulation, not a circuit simulator.

Build it step by step

0 of 5 done
  1. ESP32BME28026.0°C

    Wire the BME280

    VIN to 3V3, GND to GND, SDA to GPIO21, SCL to GPIO22.

  2. ESP32

    Pick a unique topic

    Change TOPIC to include your name and a random number so it does not clash with others.

  3. ESP32

    Add Wi-Fi and upload

    Put in your 2.4 GHz Wi-Fi, upload, and watch for "MQTT connected".

  4. ESP32

    Set up the phone

    In an MQTT dashboard app, add the same broker, port 1883, and subscribe to your topic.

  5. ESP32

    Add a gauge

    Make a gauge from the JSON field t, and another from h.

In short

How do you build a sensor to phone over MQTT?

An ESP32 publishes BME280 readings to an MQTT broker every 10 s, and a phone dashboard app shows them anywhere. You need ESP32 DevKit V1, BME280 Environmental Sensor and Breadboard 830 Tie Points. It takes 5 steps and about 75 minutes.

Level
Advanced
Time
About 75 min
Parts
3

Parts list

Tick what you already own. It saves to My Garage, so “Build with what I have” knows.

Wiring

FromToWhy
BME280 VINESP32 3V3Power
BME280 GNDESP32 GNDGround
BME280 SDAESP32 GPIO21I2C data
BME280 SCLESP32 GPIO22I2C clock

Code

Arduino IDE with the ESP32 board package. Install "PubSubClient" and "Adafruit BME280". A public test broker is fine for learning only: anyone can read it, so never send private data. Use your own broker with a password for real projects.

Runs on a virtual ESP32 Dev Module in your browser, with sliders for the sensors. Happy with it? Verify and upload to your real board from the same page (Chrome or Edge on a computer).

#include <WiFi.h>
#include <PubSubClient.h>
#include <Adafruit_BME280.h>

const char* SSID = "your-wifi";
const char* PASS = "your-password";
const char* BROKER = "test.mosquitto.org";   // public test broker: use your own for real data
const char* TOPIC = "electrolark/yourname-1234/room";

WiFiClient net;
PubSubClient mqtt(net);
Adafruit_BME280 bme;
unsigned long last = 0;

void connectMqtt() {
  while (!mqtt.connected()) {
    String id = "el-" + String((uint32_t)ESP.getEfuseMac(), HEX);
    if (mqtt.connect(id.c_str())) Serial.println("MQTT connected");
    else { Serial.print("MQTT failed, state "); Serial.println(mqtt.state()); delay(2000); }
  }
}

void setup() {
  Serial.begin(115200);
  Wire.begin(21, 22);
  if (!bme.begin(0x76)) Serial.println("BME280 not found at 0x76. Try 0x77.");
  WiFi.begin(SSID, PASS);
  while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print('.'); }
  mqtt.setServer(BROKER, 1883);
}

void loop() {
  if (!mqtt.connected()) connectMqtt();
  mqtt.loop();
  if (millis() - last > 10000) {
    last = millis();
    char msg[80];
    snprintf(msg, sizeof msg, "{\"t\":%.1f,\"h\":%.0f,\"p\":%.0f}", bme.readTemperature(), bme.readHumidity(), bme.readPressure() / 100.0);
    mqtt.publish(TOPIC, msg);
    Serial.println(msg);
  }
}

What the serial monitor should show

Open the serial monitor at the baud rate in the code. This is what our simulator prints for a virtual ESP32 Dev Module with its sensors at their default values. Your numbers will follow your real readings, but the format should match.

....MQTT connected

Test checklist

What you should see at each check. If a reading is far off, stop and use the troubleshooting list.

CheckExpected
Serial every 10 sA JSON line like {"t":24.8,"h":51,"p":1009}
Phone appThe same values arrive within a few seconds
Turn the router off and on"MQTT failed" lines, then it reconnects on its own

If it does not work

"MQTT failed, state -2"

The board cannot reach the broker: check Wi-Fi, and that the network allows port 1883.

Phone shows nothing

The topic must match exactly, including capitals and slashes.

Humidity reads 0

Your board is a BMP280, which has no humidity sensor.

Still stuck? Find the fault step by step with your multimeter →

Ask Lark AI

Common questions

What parts do I need for the sensor to phone over MQTT?

ESP32 DevKit V1, BME280 Environmental Sensor and Breadboard 830 Tie Points.

How long does the sensor to phone over MQTT take to build?

About 75 minutes for a advanced maker, in 5 steps.

How do I know the sensor to phone over MQTT works?

Serial every 10 s: A JSON line like {"t":24.8,"h":51,"p":1009}. Phone app: The same values arrive within a few seconds. Turn the router off and on: "MQTT failed" lines, then it reconnects on its own.

MQTT failed, state -2: what should I check?

The board cannot reach the broker: check Wi-Fi, and that the network allows port 1883.

Phone shows nothing: what should I check?

The topic must match exactly, including capitals and slashes.

Humidity reads 0: what should I check?

Your board is a BMP280, which has no humidity sensor.