Create nguyenmanhviet2025 #201
Open
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
#include <WiFi.h>
#include <PubSubClient.h>
#include <WebServer.h>
#include <LittleFS.h>
const char* default_ssid = "ESP32_AP";
const char* default_pass = "12345678";
char mqtt_server[40] = "192.168.1.100";
char mqtt_user[20] = "esp32";
char mqtt_pass[20] = "12345678";
WiFiClient espClient;
PubSubClient client(espClient);
WebServer server(80);
#define FAN_PIN 2
#define LIGHT_PIN 4
#define LOCK_PIN 5
void setupWiFi() {
WiFi.softAP(default_ssid, default_pass);
Serial.println("ESP32 phát WiFi tại IP: " + WiFi.softAPIP().toString());
}
void mqttCallback(char* topic, byte* payload, unsigned int length) {
String msg;
for (uint i = 0; i < length; i++) msg += (char)payload[i];
msg.trim();
if (String(topic) == "home/esp32/fan/set") {
digitalWrite(FAN_PIN, msg == "ON");
client.publish("home/esp32/fan/state", msg.c_str(), true);
}
if (String(topic) == "home/esp32/light/set") {
digitalWrite(LIGHT_PIN, msg == "ON");
client.publish("home/esp32/light/state", msg.c_str(), true);
}
if (String(topic) == "home/esp32/lock/set") {
digitalWrite(LOCK_PIN, msg == "ON");
client.publish("home/esp32/lock/state", msg.c_str(), true);
}
}
void reconnectMQTT() {
while (!client.connected()) {
if (client.connect("ESP32Client", mqtt_user, mqtt_pass)) {
client.subscribe("home/esp32/fan/set");
client.subscribe("home/esp32/light/set");
client.subscribe("home/esp32/lock/set");
} else {
delay(5000);
}
}
}
String htmlPage() {
File file = LittleFS.open("/index.html", "r");
String html = file.readString();
file.close();
return html;
}
void setupServer() {
server.on("/", HTTP_GET, {
server.send(200, "text/html", htmlPage());
});
server.on("/api/device", HTTP_GET, {
String name = server.arg("name");
String action = server.arg("action");
int state = (action == "on") ? HIGH : LOW;
});
server.on("/api/status", HTTP_GET, {
String json = "{";
json += ""quat":" + String(digitalRead(FAN_PIN)) + ",";
json += ""den":" + String(digitalRead(LIGHT_PIN)) + ",";
json += ""khoa":" + String(digitalRead(LOCK_PIN));
json += "}";
server.send(200, "application/json", json);
});
server.begin();
}
void setup() {
Serial.begin(115200);
pinMode(FAN_PIN, OUTPUT);
pinMode(LIGHT_PIN, OUTPUT);
pinMode(LOCK_PIN, OUTPUT);
LittleFS.begin();
setupWiFi();
setupServer();
client.setServer(mqtt_server, 1883);
client.setCallback(mqttCallback);
}
void loop() {
if (!client.connected()) reconnectMQTT();
client.loop();
server.handleClient();
}