1-Making the project on the Breadboard,
Smart home systems are technological solutions that provide homeowners with the ability to remotely control and monitor various devices. In this project, we will build a basic smart home project using an ESP32-S2 microcontroller. In our project, we will integrate light control and temperature measurement features.
Materials:
ESP32-S2 development board
Light sensor
Temperature sensor
Breadboard and jumper cables
USB cable and power supply
Step 1: Hardware Connections:
Place the ESP32-S2 on the breadboard.
Connect the light sensor to the ESP32-S2 with a specific pin.
Connect the temperature sensor to the ESP32-S2 with a specific pin.
Step 2: Arduino IDE and Libraries:
Install the Arduino IDE on your computer.
Install the ESP32 support package.
Add the necessary libraries to read light and temperature sensors.
Step 3: Reading Sensor Data:
Write a simple program to the ESP32-S2 via Arduino IDE.
Read the data from the light and temperature sensors.
Display this data on the serial monitor.
Step 4: Wi-Fi Connection:
Add the code to connect the ESP32-S2 to the Wi-Fi network.
Check the connection status to determine if you have successfully connected.
Step 5: Remote Access:
Send the sensor data read from the ESP32-S2 to a server via an MQTT message.
Set up the MQTT server on a Raspberry Pi or other device and receive the data.
Step 6: User Interface:
You can control sensors remotely by creating a web or mobile app.
For example, add options to turn lights on/off and monitor temperature readings.
Below you can find a detailed explanation about the programming of several sensors used on the ESP32-S2. In this example we will use Arduino IDE and C++ language.
Light Sensor Programming:
Sensor Connection:
Connect the light sensor to the ESP32-S2. For example, you can use an analog pin.
Adding a Library:
Open the Arduino IDE and add the library for the light sensor by selecting "Include Library" -> "Adafruit TSL2591" from the "Sketch" menu.
Code Example:
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include "Adafruit_TSL2591.h"
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
void setup() {
Serial.begin(115200);
if (!tsl.begin()) {
Serial.println("Işık sensörü başlatılamadı. Bağlantıları kontrol edin!");
while (1);
}
Serial.println("Işık sensörü başarıyla başlatıldı!");
}
void loop() {
uint16_t luminosity = tsl.getLuminosity(TSL2591_VISIBLE);
Serial.print("Luminosity: "); Serial.println(luminosity);
delay(1000);
}
Temperature Sensor Programming:
Sensor Connection:
Connect the temperature sensor to the ESP32-S2. For example, you can use a digital pin.
Adding a Library:
Open the Arduino IDE and add the necessary libraries for the temperature sensor by selecting "Include Library" -> "OneWire" and "DallasTemperature" from the "Sketch" menu.
Code Example:
#include <OneWire.h>
#include <DallasTemperature.h>
#define ONE_WIRE_BUS 4 // Örnek olarak D4 pinini kullanıyoruz.
OneWire oneWire(ONE_WIRE_BUS);
DallasTemperature sensors(&oneWire);
void setup() {
Serial.begin(115200);
sensors.begin();
}
void loop() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print("Sıcaklık: ");
Serial.print(temperature);
Serial.println(" °C");
delay(1000);
}
PIR Sensor Programming:
Sensor Connection:
Connect the PIR sensor to the ESP32-S2. For example, you can use a digital pin.
Code Example:
int pirPin = 2; // Örnek olarak D2 pinini kullanıyoruz.
void setup() {
Serial.begin(115200);
pinMode(pirPin, INPUT);
}
void loop() {
int motion = digitalRead(pirPin);
if (motion == HIGH) {
Serial.println("Hareket Algılandı!");
// Burada röle modülünü aktive etme veya istediğiniz diğer eylemleri gerçekleştirme kodunu ekleyebilirsiniz.
}
delay(1000);
}
Relay Module Programming:
Sensor Connection:
Connect the relay module to the ESP32-S2. For example, you can use a digital pin.
Code Example:
int relayPin = 5; // Örnek olarak D5 pinini kullanıyoruz.
void setup() {
Serial.begin(115200);
pinMode(relayPin, OUTPUT);
}
void loop() {
// Röle modülünü kontrol etmek için gerekli kodu buraya ekleyin.
// Örneğin, belirli bir koşul sağlandığında röle modülünü açabilir veya kapatabilirsiniz.
delay(1000);
}
DHT11 Temperature and Humidity Sensor Arduino Programming:
To use the DHT11 sensor with Arduino, you first need to add the DHT library. This library provides the necessary functions to communicate with the sensor and read the data.
Adding the Library:
Open the Arduino IDE and add the DHT library by following these steps:
Sketch -> Include Library -> Manage Libraries...
Type "DHT" in the search bar.
Find the DHT Sensor Library and install it.
Code Example:
#include <DHT.h>
#define DHT_PIN 2 // DHT11 sensörünün veri pini
DHT dht(DHT_PIN, DHT11);
void setup() {
Serial.begin(9600);
dht.begin();
}
void loop() {
delay(2000); // Ölçüm aralığı
float humidity = dht.readHumidity();
float temperature = dht.readTemperature();
if (isnan(humidity) || isnan(temperature)) {
Serial.println("DHT11 sensöründen veri okunamadı!");
return;
}
Serial.print("Sıcaklık: ");
Serial.print(temperature);
Serial.print(" °C, Nem: ");
Serial.print(humidity);
Serial.println(" %");
}
Use and code all of all sensors:
A brief description of this code:
This is Arduino code for a smart home project based on ESP32-S2 and Raspberry Pi Zero W. It reads data from various sensors and sends it to a broker over MQTT protocol. The sensors used in the project include a light sensor, temperature sensor, motion sensor (PIR), humidity sensor and DHT11 temperature sensor. The data received from the sensors can be published to MQTT topics at a certain interval and monitored by devices or services that subscribe to this data. In addition, the connection status control mechanisms in the code enable continuous monitoring of WiFi and MQTT connections and automatic reconnection in case of connection loss. This ensures reliable and continuous IoT (Internet of Things) communication and increases home automation stability.
#include <Wire.h>
#include "Adafruit_Sensor.h"
#include "Adafruit_TSL2591.h"
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_Sensor.h>
#include <DHT.h>
#include <WiFi.h>
#include <PubSubClient.h>
#define PIR_PIN 2 // PIR sensörü için D2 pinini kullanıyoruz.
#define RELAY_PIN 5 // Röle modülü için D5 pinini kullanıyoruz.
// WiFi bilgilerini girin
const char *ssid = "WiFi_SSID";
const char *password = "WiFi_PASSWORD";
// MQTT Broker bilgilerini girin
const char *mqtt_server = "MQTT_BROKER_IP";
const char *mqtt_user = "MQTT_USERNAME";
const char *mqtt_password = "MQTT_PASSWORD";
const char *mqtt_client_id = "ESP32_Client";
WiFiClient espClient;
PubSubClient client(espClient);
Adafruit_TSL2591 tsl = Adafruit_TSL2591(2591);
OneWire oneWire(4); // Sıcaklık sensörü için D4 pinini kullanıyoruz.
DallasTemperature sensors(&oneWire);
DHT dht(D2, DHT11); // DHT11 sensörü için D2 pinini kullanıyoruz.
unsigned long previousMillis = 0;
const long interval = 10000; // Veri yayını aralığı (10 saniye)
void setup() {
Serial.begin(115200);
// WiFi Bağlantısı
connectWiFi();
// MQTT Bağlantısı
connectMQTT();
// Sensör ve Pin Ayarları
if (!tsl.begin()) {
Serial.println("Işık sensörü başlatılamadı. Bağlantıları kontrol edin!");
while (1);
}
sensors.begin();
pinMode(PIR_PIN, INPUT);
pinMode(RELAY_PIN, OUTPUT);
}
void loop() {
// WiFi ve MQTT Durumu
if (WiFi.status() != WL_CONNECTED) {
Serial.println("WiFi bağlantısı kayboldu, tekrar bağlanıyor...");
connectWiFi();
}
if (!client.connected()) {
Serial.println("MQTT Broker'a bağlanılıyor...");
connectMQTT();
}
// Zaman sayacı kontrolü
unsigned long currentMillis = millis();
if (currentMillis - previousMillis >= interval) {
previousMillis = currentMillis;
// Sensör Okumaları
float luminosity = readLuminosity();
float temperature = readTemperature();
int motion = readMotion();
float humidity = readHumidity();
float dhtTemperature = readDHTTemperature();
// MQTT üzerinden veri gönderimi
publishData("ev/sensor", luminosity);
publishData("ev/temperature", temperature);
publishData("ev/motion", motion);
publishData("ev/humidity", humidity);
publishData("ev/dhtTemperature", dhtTemperature);
}
// MQTT işlemleri
client.loop();
delay(1000); // MQTT hizmetlerine ağırlık vermek için küçük bir gecikme
}
void connectWiFi() {
Serial.println("WiFi bağlantısı kuruluyor...");
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("WiFi bağlantısı başarısız, tekrar deniyor...");
}
Serial.println("WiFi bağlantısı başarıyla kuruldu");
}
void connectMQTT() {
client.setServer(mqtt_server, 1883);
client.setCallback(callback);
while (!client.connected()) {
Serial.println("MQTT Broker'a bağlanılıyor...");
if (client.connect(mqtt_client_id, mqtt_user, mqtt_password)) {
Serial.println("MQTT Broker'a bağlanıldı");
} else {
Serial.print("Bağlantı başarısız, tekrar deniyor: ");
Serial.print(client.state());
delay(2000);
}
}
}
float readLuminosity() {
uint16_t luminosity = tsl.getLuminosity(TSL2591_VISIBLE);
Serial.print("Luminosity: "); Serial.println(luminosity);
return luminosity;
}
float readTemperature() {
sensors.requestTemperatures();
float temperature = sensors.getTempCByIndex(0);
Serial.print("Sıcaklık: "); Serial.print(temperature); Serial.println(" °C");
return temperature;
}
int readMotion() {
int motion = digitalRead(PIR_PIN);
Serial.print("Motion: "); Serial.println(motion);
return motion;
}
float readHumidity() {
float humidity = dht.readHumidity();
Serial.print("Nem: "); Serial.println(humidity);
return humidity;
}
float readDHTTemperature() {
float dhtTemperature = dht.readTemperature();
Serial.print("DHT11 Sıcaklık: "); Serial.println(dhtTemperature);
return dhtTemperature;
}
void publishData(const char *topic, float data) {
char payload[50];
sprintf(payload, "%.2f", data);
client.publish(topic, payload);
Serial.print("Published to "); Serial.print(topic); Serial.print(", Data: "); Serial.println(data);
}
Why I prefer JLCPCB:High Quality: JLCPCB manufactures PCBs using quality materials and keeps any manufacturing margin of error to a minimum. This increases the reliability of your projects.
Fast Delivery: JLCPCB quickly processes your orders and offers fast delivery options to customers worldwide.
Low Cost: JLCPCB offers competitive prices and provides economical options even for low quantity PCB production.
User Friendly Platform: JLCPCB's website offers a user-friendly platform for you to upload, customise and order your PCB design.
Coupons and gifts:Every time you made a payment, you will receive a coupon In box, an extra Item like a pen or batch is for you as a free gift from JLCPCB.
More services:PCB assembly service starting from just $8. SMT stencil service is available from $7. PCB 2, 4 and 6-layer PCB staring from just $2.
1) First, Due to a very high competition in the market Only JLCPCB is the PCB manufacturing company offering all colors PCBs in same price and they introduce the purple solder mask also this year.
2) Industrial 3d printing service is now available in very low cost and with professional machinery.
3) Parts availability in JLCPCB component store and Pre-ordering parts for your assembly service is also there.
User Interface:Because
JLCPCB
is with LCSC and EasyEDA as a Partner. Which is making this a group of electronic companies and a very right place of electronics hobbyists, students, engineers and Embedded designers.The process starts from making the circuit in EasyEDA and then Gerber files goes to JLCPCB for manufacturing the boards, components requirements are fulfilled by LCSC. Which makes the ordering process very easy. Software Itself detects the size, type and assembly cost of PCB. Then a team of Engineers go through your order and confirm the files.
If there is any error or something different thing they found in the board, they will send a confirmation mail to proceed or re-upload the Gerbers.
After-sales:Let’s talk about after sales and quality complains, in 99.99% cases there will be no complain in the boards. But if something like this happens, they will give you a coupon to place the order again in free.
Your sales representative is there to take queries related the PCBs. I found the Online chat option very good in this case. If you have any query related, service, cost, product and shipment just drop a message and the team of supporting agent is live for you(in business hours).
How to Order from JLCPCB?
Placing an order from JLCPCB is quite simple. Here is a step-by-step guide explaining how to place an order:
Create an Account: Go to the official website of JLCPCB https://jlcpcb.com/HAR and create an account. Creating an account is free.
Upload Project: After logging into your account, find "Place Order" or similar option to upload your PCB design. Upload your PCB design in Gerber file format.
Customise Parameters: You can customise the specifications of your PCB. Set details such as number of layers, dimensions, material selection and other features.
Order Confirmation: After customising your design, review and confirm your order. Check the total cost and estimated delivery time.
Make Payment: Make payment to finalise your order. JLCPCB offers different payment options, so you can choose your preferred payment method.
Enter Delivery Information: Specify where your PCBs will be shipped and which delivery method you will use. Enter your address details and other necessary information.
Finalise Order: Finally, finalise and confirm your order. JLCPCB will process your order and send you an order confirmation.
After you place your order, JLCPCB will manufacture the PCBs for you and ship them to the address you specify. Production and delivery time may vary depending on your preferred options.
Thank you very much JLCPCB for supporting this project
Comments