Logo

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
Categories
2 categories in total
esp32
open
cpp
open
Author
8 person written this
komsan21
open
ESP32 WiFiManager MQTT Spiffs config ArduinoJson 7
#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();
}
Enter fullscreen mode Exit fullscreen mode
esp32 Article's
30 articles in total
Favicon
ESP32 Weather Dashboard with a WiFi Menu
Favicon
Prototipos rápidos con Wokwi, ESP32 y AWS IoT Core
Favicon
Smart Home Security: Advanced Motion Detection with CCTV
Favicon
Matter protocol on a budget
Favicon
Ulanzi TC001 - ESP32 Programming / Custom Arduino firmware
Favicon
Rust on a $2 dev board
Favicon
Sliding Puzzle Next Move Suggesting Simple DL Model with ESP32 TensorFlow Lite
Favicon
Time-for-space reluctance for the Wi-Fi AP scan for ESP8266
Favicon
I created a Realtime Voice Assistant for my ESP-32, here is my journey - Part 2 : Node, OpenAI, Langchain
Favicon
I created a Realtime Voice Assistant for my ESP-32, here is my journey - Part 1 : Hardware, PlatformIO & C++
Favicon
ESP32 Smart Garden: Automating Plant Care with IoT
Favicon
Smart Connectivity: Leveraging Wi-Fi and Bluetooth Coexistence in ESP32
Favicon
Building a DIY Smart Home Device: Get S**t Done
Favicon
Top ESP32 Projects To Start in 2024
Favicon
Why You Should Choose MicroPython for Prototyping and Research Work
Favicon
How to build a smart home system with ESP32
Favicon
Advanced ESP32: Exploring Key Features and Versatile Uses
Favicon
Developing IoT Application with ESP32
Favicon
Simple Arduino Framework Photo Frame Implementation with Photos Downloaded from the Internet via DumbDisplay
Favicon
DHT22 with MicroPython on ESP32
Favicon
MicroPython ESP32: Blink LED
Favicon
Using an OLED Display with MicroPython on ESP32
Favicon
Interfacing HC-SR04 Ultrasonic Sensor with ESP32 Using MicroPython
Favicon
Configurar Arduino IDE para ESP32 en Windows 10 💻
Favicon
Arduino IDE 2 上傳檔案到 ESP32/ESP8266 的外掛
Favicon
ESP32 WiFiManager MQTT ArduinoJson 7
Favicon
How to Code with Lua on ESP32 with XEdge32
Favicon
Tiny E-Ink Picture Display
Favicon
Esp32 Rust Board on Macos M-chip in Docker
Favicon
ESP32 WiFiManager MQTT Spiffs config ArduinoJson 7

Featured ones: