dev-resources.site
for different kinds of informations.
ESP32 WiFiManager MQTT Spiffs config ArduinoJson 7
Published at
5/9/2024
Categories
esp32
cpp
Author
komsan21
Author
8 person written this
komsan21
open
#include <WiFi.h>
#include <WiFiManager.h>
#include <ArduinoJson.h>
#include <PubSubClient.h>
#include <SPIFFS.h>
// ตั้งค่า WiFiManager
WiFiManager wm;
// ตั้งค่า MQTT
WiFiClient espClient;
PubSubClient client(espClient);
// ตั้งค่า JSON document
StaticJsonDocument<200> doc;
void setup() {
Serial.begin(115200);
// เริ่มต้น SPIFFS
if (!SPIFFS.begin(true)) {
Serial.println("An Error has occurred while mounting SPIFFS");
return;
}
// รีเซ็ตการตั้งค่า WiFi เมื่อกดปุ่มรีเซ็ตนาน 3 วินาที
wm.resetSettings();
// กำหนดค่าเริ่มต้นสำหรับการกำหนดค่าผ่าน WiFiManager
WiFiManagerParameter custom_mqtt_server("server", "MQTT Server", "mqtt.example.com", 40);
WiFiManagerParameter custom_mqtt_port("port", "MQTT Port", "1883", 6);
wm.addParameter(&custom_mqtt_server);
wm.addParameter(&custom_mqtt_port);
bool res = wm.autoConnect("ESP32-AP");
if (!res) {
Serial.println("Failed to connect");
// ไม่สามารถเชื่อมต่อ WiFi ให้เข้าสู่โหมด Deep Sleep
} else {
Serial.println("Connected to WiFi");
// เก็บค่า MQTT Server และ Port ลงใน JSON document
doc["mqtt_server"] = custom_mqtt_server.getValue();
doc["mqtt_port"] = custom_mqtt_port.getValue();
// เขียนข้อมูล JSON ลงไฟล์ /data.json ใน SPIFFS
File file = SPIFFS.open("/data.json", "w");
if (file) {
serializeJson(doc, file);
file.close();
} else {
Serial.println("Failed to open /data.json for writing");
}
// เชื่อมต่อ MQTT
client.setServer(doc["mqtt_server"], doc["mqtt_port"].as<int>());
client.connect("ESP32-Client");
}
}
void loop() {
// คงการเชื่อมต่อ MQTT
client.loop();
}
esp32 Article's
30 articles in total
ESP32 Weather Dashboard with a WiFi Menu
read article
Prototipos rápidos con Wokwi, ESP32 y AWS IoT Core
read article
Smart Home Security: Advanced Motion Detection with CCTV
read article
Matter protocol on a budget
read article
Ulanzi TC001 - ESP32 Programming / Custom Arduino firmware
read article
Rust on a $2 dev board
read article
Sliding Puzzle Next Move Suggesting Simple DL Model with ESP32 TensorFlow Lite
read article
Time-for-space reluctance for the Wi-Fi AP scan for ESP8266
read article
I created a Realtime Voice Assistant for my ESP-32, here is my journey - Part 2 : Node, OpenAI, Langchain
read article
I created a Realtime Voice Assistant for my ESP-32, here is my journey - Part 1 : Hardware, PlatformIO & C++
read article
ESP32 Smart Garden: Automating Plant Care with IoT
read article
Smart Connectivity: Leveraging Wi-Fi and Bluetooth Coexistence in ESP32
read article
Building a DIY Smart Home Device: Get S**t Done
read article
Top ESP32 Projects To Start in 2024
read article
Why You Should Choose MicroPython for Prototyping and Research Work
read article
How to build a smart home system with ESP32
read article
Advanced ESP32: Exploring Key Features and Versatile Uses
read article
Developing IoT Application with ESP32
read article
Simple Arduino Framework Photo Frame Implementation with Photos Downloaded from the Internet via DumbDisplay
read article
DHT22 with MicroPython on ESP32
read article
MicroPython ESP32: Blink LED
read article
Using an OLED Display with MicroPython on ESP32
read article
Interfacing HC-SR04 Ultrasonic Sensor with ESP32 Using MicroPython
read article
Configurar Arduino IDE para ESP32 en Windows 10 💻
read article
Arduino IDE 2 上傳檔案到 ESP32/ESP8266 的外掛
read article
ESP32 WiFiManager MQTT ArduinoJson 7
read article
How to Code with Lua on ESP32 with XEdge32
read article
Tiny E-Ink Picture Display
read article
Esp32 Rust Board on Macos M-chip in Docker
read article
ESP32 WiFiManager MQTT Spiffs config ArduinoJson 7
currently reading
Featured ones: