MQTT LCD Motion Receiver — ESP32 simulation

Raspberry Pi MQTT motion sensor receiver with I2C LCD display

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by tarazan88889.

Components

Sketch code

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

WiFiClient espClient;
PubSubClient client(espClient);

const int PIR_BATH = 33;
const int PIR_BED  = 25;
const int PIR_LIV  = 26;

void setup() {
  Serial.begin(115200);
  pinMode(PIR_BATH, INPUT);
  pinMode(PIR_BED, INPUT);
  pinMode(PIR_LIV, INPUT);

  WiFi.begin("Velxio-GUEST");
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.println("WiFi OK");

  client.setServer("35.157.128.15", 1883);
  while (!client.connected()) {
    if (client.connect("sensor_combined")) {
      Serial.println("MQTT OK");
    } else {
      delay(2000);
    }
  }
}

void loop() {
  client.loop();

  if (digitalRead(PIR_BATH) == HIGH) {
    client.publish("home/bathroom/motion", "Bathroom");
    Serial.println("Bathroom");
    delay(2000);
  }
  if (digitalRead(PIR_BED) == HIGH) {
    client.publish("home/bedroom/motion", "Bedroom");
    Serial.println("Bedroom");
    delay(2000);
  }
  if (digitalRead(PIR_LIV) == HIGH) {
    client.publish("home/living_room/motion", "Living room");
    Serial.println("Living room");
    delay(2000);
  }
}