Cirkit Designer LogoCirkit DesignerYour all-in-one circuit design IDE Launch Cirkit Designer App

Raspberry Pi Pico W-Based Smart Environmental Control System with MQTT and Sensor Integration

View and Edit Project In Cirkit DesignerImage of Raspberry Pi Pico W-Based Smart Environmental Control System with MQTT and Sensor Integration

Circuit Documentation

Summary

The circuit is designed to operate in two modes: IoT mode and Sensor mode. In IoT mode, the fan and light are controlled via MQTT messages. In Sensor mode, the fan and light are controlled automatically based on sensor inputs from a DHT11 temperature and humidity sensor, an LDR (Light Dependent Resistor), and a Microwave Radar Motion Sensor. The Raspberry Pi Pico W serves as the microcontroller, interfacing with various sensors and actuators, and managing the logic for both modes. The circuit also includes a two-channel relay to control the fan and light, pushbuttons for manual mode switching, and an I2C module connected to an LCD display for user feedback.

Component List

Wiring Details

Raspberry Pi Pico W

DHT11

Two Channel Relay 5V

Photocell (LDR)

Resistor

RCWL-0516 Microwave Radar Motion Sensor Module

Pushbutton

BULB

Power Supply

Fan

I2C Module

LCD Display (16 pin)

Documented Code

/*
 * This Arduino Sketch is designed for a Raspberry Pi Pico W microcontroller.
 * The circuit has two modes: IoT mode and Sensor mode.
 * In IoT mode, the fan and light are controlled via MQTT messages.
 * In Sensor mode, the fan and light are controlled automatically based on
 * sensor inputs (DHT11, LDR, and Microwave Radar Motion Sensor).
 */

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

#define DHTPIN 4
#define DHTTYPE DHT11
#define LDRPIN 26
#define MICROWAVEPIN 16
#define LIGHTPIN 6
#define FANPIN 7
#define BUTTON1PIN 8
#define BUTTON2PIN 9

const char* ssid = "project";
const char* password = "12345678";
const char* mqtt_server = "test.mosquitto.org";
const char* mqtt_topic = "JN_17102024";

WiFiClient espClient;
PubSubClient client(espClient);
DHT dht(DHTPIN, DHTTYPE);

void setup() {
  pinMode(LIGHTPIN, OUTPUT);
  pinMode(FANPIN, OUTPUT);
  pinMode(MICROWAVEPIN, INPUT);
  pinMode(BUTTON1PIN, INPUT);
  pinMode(BUTTON2PIN, INPUT);
  digitalWrite(LIGHTPIN, HIGH);
  digitalWrite(FANPIN, HIGH);
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(mqtt_callback);
  dht.begin();
}

void setup_wifi() {
  delay(10);
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected: ");
  Serial.println(WiFi.localIP());
}

void mqtt_callback(char* topic, byte* payload, unsigned int length) {
  String msg = "";
  for (unsigned int i = 0; i < length; i++) {
    msg += (char)payload[i];
  }
  if (msg == "a") {
    digitalWrite(LIGHTPIN, LOW);
  } else if (msg == "b") {
    digitalWrite(LIGHTPIN, HIGH);
  } else if (msg == "c") {
    digitalWrite(FANPIN, LOW);
  } else if (msg == "d") {
    digitalWrite(FANPIN, HIGH);
  }
}

void reconnect() {
  while (!client.connected()) {
    Serial.print("Attempting MQTT connection...");
    if (client.connect("raspberry_pi_pico")) {
      Serial.println("connected");
      client.subscribe(mqtt_topic);
    } else {
      Serial.print("failed, rc=");
      Serial.print(client.state());
      Serial.println(" try again in 5 seconds");
      delay(5000);
    }
  }
}

void sensor_control() {
  while (true) {
    int movement = digitalRead(MICROWAVEPIN);
    dht.read();
    float temp = dht.readTemperature();
    float hum = dht.readHumidity();
    int ldr_value = analogRead(LDRPIN);
    if (movement == HIGH) {
      for (int i = 60; i > 0; i--) {
        if (i >= 40) {
          digitalWrite(FANPIN, LOW);
        }
        if (ldr_value <= 10000) {
          digitalWrite(LIGHTPIN, LOW);
        } else if (ldr_value >= 20000) {
          digitalWrite(LIGHTPIN, HIGH);
        }
        if (temp >= 35) {
          digitalWrite(FANPIN, LOW);
        } else if (temp <= 34) {
          digitalWrite(FANPIN, HIGH);