🧩 1. ทำความเข้าใจ ESP32 ในฐานะ IoT Device

ESP32 คือ ไมโครคอนโทรลเลอร์ที่มี Wi-Fi และ Bluetooth ในตัว ทำให้สามารถเชื่อมต่อ Network โดยไม่ต้องมีโมดูลเสริมเหมือน Arduino UNO + ESP8266

ฟังก์ชันคำอธิบาย
Wi-Fi Stationเชื่อมต่อกับ Router เหมือนมือถือ (โหมด STA)
Wi-Fi Access Pointสร้าง Hotspot ให้คนอื่นเชื่อมต่อ (โหมด AP)
Wi-Fi STA + APโหมดผสม เชื่อมต่อ Wi-Fi พร้อมปล่อย Hotspot
HTTP Client / Serverส่งข้อมูลไปยังเว็บ API หรือเปิดเว็บให้เข้าดูได้
MQTT Clientใช้สื่อสารกับ IoT Broker แบบ publish / subscribe

⚙️ 2. เริ่มต้นการเชื่อมต่อ Wi-Fi (Station Mode)

📘 ตัวอย่างโค้ดพื้นฐาน

C++
#include <WiFi.h>

const char* ssid     = "LIC-WiFi";
const char* password = "12345678";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  Serial.print("Connecting");
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("\nWiFi connected!");
  Serial.print("IP Address: ");
  Serial.println(WiFi.localIP());
}

void loop() {}

แสดง IP Address ของ ESP32 ที่ได้รับจาก Router สามารถใช้ IP นี้เข้าผ่าน Browser ถ้ามีเว็บ server รันอยู่ในบอร์ด


🔹 3. โหมด Access Point (AP Mode)

📘 ตัวอย่าง:

C++
#include <WiFi.h>

void setup() {
  Serial.begin(115200);
  WiFi.softAP("ESP32-AP", "12345678");
  Serial.print("AP IP: ");
  Serial.println(WiFi.softAPIP());
}

void loop() {}

เมื่ออัปโหลดโค้ดนี้ ESP32 จะปล่อยสัญญาณ Wi-Fi ชื่อ ESP32-AP โทรศัพท์สามารถเชื่อมต่อได้ทันที


🔹 4. สร้าง Web Server ภายใน ESP32

📘 โค้ดตัวอย่างง่าย ๆ:

C++
#include <WiFi.h>
#include <WebServer.h>

const char* ssid = "LIC-WiFi";
const char* password = "12345678";
WebServer server(80);

void handleRoot() {
  server.send(200, "text/html", "<h1>Hello from ESP32!</h1>");
}

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);
  server.begin();
}

void loop() {
  server.handleClient();
}

พิมพ์ IP ใน Browser เช่น http://192.168.1.25 จะเห็นหน้าเว็บจาก ESP32


🔹 5. สร้าง Web Server แบบควบคุม LED

📘 ตัวอย่าง:

C++
#include <WiFi.h>
#include <WebServer.h>

const char* ssid = "LIC-WiFi";
const char* password = "12345678";
WebServer server(80);
const int led = 2;

void handleRoot() {
  String html = "<h2>LED Control</h2>";
  html += "<a href='/on'>Turn ON</a><br>";
  html += "<a href='/off'>Turn OFF</a>";
  server.send(200, "text/html", html);
}

void handleOn()  { digitalWrite(led, HIGH); server.send(200, "text/html", "LED ON"); }
void handleOff() { digitalWrite(led, LOW);  server.send(200, "text/html", "LED OFF"); }

void setup() {
  pinMode(led, OUTPUT);
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.println(WiFi.localIP());

  server.on("/", handleRoot);
  server.on("/on", handleOn);
  server.on("/off", handleOff);
  server.begin();
}

void loop() { server.handleClient(); }

สามารถเปิด/ปิด LED จาก Browser ได้เลย


🔹 6. ESP32 เป็น Web Client (ส่งข้อมูลไปยัง API)

📘 ตัวอย่าง:

C++
#include <WiFi.h>
#include <HTTPClient.h>

const char* ssid = "LIC-WiFi";
const char* password = "12345678";

void setup() {
  Serial.begin(115200);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);

  HTTPClient http;
  http.begin("https://api.thingspeak.com/update?api_key=ABCDEFG123&field1=25.5");
  int code = http.GET();
  Serial.printf("HTTP Response code: %d\n", code);
  http.end();
}

void loop() {}

ส่งข้อมูลอุณหภูมิไปยัง ThingSpeak หรือ API อื่นได้


🔹 7. การใช้ MQTT Protocol (มาตรฐาน IoT)

  • ใช้รูปแบบ Publisher / Subscriber
  • มีตัวกลางคือ MQTT Broker เช่น test.mosquitto.org, broker.hivemq.com

📘 ตัวอย่าง:

C++
#include <WiFi.h>
#include <PubSubClient.h>

const char* ssid = "LIC-WiFi";
const char* password = "12345678";
const char* mqtt_server = "broker.hivemq.com";

WiFiClient espClient;
PubSubClient client(espClient);

void setup_wifi() {
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) delay(500);
  Serial.println(WiFi.localIP());
}

void callback(char* topic, byte* payload, unsigned int length) {
  Serial.print("Message ["); Serial.print(topic); Serial.print("]: ");
  for (int i=0;i<length;i++) Serial.print((char)payload[i]);
  Serial.println();
}

void reconnect() {
  while (!client.connected()) {
    if (client.connect("ESP32Client")) {
      client.subscribe("lic/led");
    } else delay(2000);
  }
}

void setup() {
  Serial.begin(115200);
  setup_wifi();
  client.setServer(mqtt_server, 1883);
  client.setCallback(callback);
}

void loop() {
  if (!client.connected()) reconnect();
  client.loop();
  client.publish("lic/temp", "28.5");  // ส่งค่าอุณหภูมิ
  delay(2000);
}

ใช้ส่งค่าขึ้น Dashboard หรือ Node-RED ได้เลย


🔹 8. การเชื่อมต่อกับ Blynk (แอป IoT ยอดนิยม)

  • ใช้ไลบรารี Blynk สำหรับ ESP32
  • สร้าง Project ใน แอป Blynk แล้วรับ Auth Token
  • ใช้โค้ดเชื่อมต่อผ่าน Wi-Fi และส่งค่าจาก Sensor ไปแสดงใน มือถือ

📚 9. แบบฝึกหัดท้ายบท

1️⃣ เชื่อมต่อ Wi-Fi และแสดง IP Address ใน Serial Monitor
2️⃣ สร้าง Web Server แสดงค่าอุณหภูมิจาก DHT11
3️⃣ สร้างหน้าเว็บควบคุม LED 2 ดวง
4️⃣ ส่งข้อมูลขึ้น ThingSpeak ทุก 10 วินาที
5️⃣ สร้างระบบ ESP32 MQTT → Node-RED → Dashboard


🧾 10. สรุปแนวคิดสำคัญ

หัวข้อคำอธิบาย
Wi-Fi Stationเชื่อมต่อเข้ากับ Router ที่มีอยู่
Access Pointสร้าง Hotspot ให้เชื่อมต่อโดยตรง
Web Serverเปิดหน้าเว็บให้ควบคุมหรือดูค่าจาก Sensor
Web Clientส่งข้อมูลออกไปยัง API / Cloud
HTTP vs MQTTHTTP เหมาะกับ REST / Web, MQTT เหมาะกับ IoT เรียลไทม์
ThingSpeak / BlynkCloud ยอดนิยมสำหรับ IoT
Node-RED / MQTTใช้สร้าง Dashboard หรือ ระบบ Smart Home

🌐 ภาพรวมแนวคิดระบบ IoT

C++
 [Sensor] → ESP32 (Wi-Fi) → Internet → [Server / Cloud] → [Dashboard / App]

เช่น

  • DHT22 → ESP32 → ThingSpeak → กราฟ อุณหภูมิ
  • Ultrasonic → ESP32 → Node-RED → แจ้งเตือน Line