ESP8266 ควบคุม i/o ผ่าน web (Access point)

16 Jul 2016,
Share: 

ในบทความนี้ผมจะอธิบายถึงวิธีการทําให้ ESP8266 เป็น Access point ให้อุปกรณ์อุปกรณ์อื่นๆ ที่เชื่อมต่อ wifi ได้ เช่น computer, smart phone ฯลฯ มาเชื่อมต่อกับ SEP8266 แล้วควบคุม i/o ปิด/เปิด LED ผ่านเว็บ ด้วยวิธีส่งข้อมูลผ่าน HTTP_GET เช่น

อุปกรณ์ที่ต้องใช้คือ

Source Code ตัวอย่าง

#include <ESP8266WiFi.h>
#include <WiFiClient.h>
#include <ESP8266WebServer.h>

/*-- Set SSID, Password and IP --*/
const char *ssid = "test-esp8266"; //config ssid
const char *password = "12345678"; //config password
const byte wifi_ip[4] = {192, 168, 1, 2}; //config ipaddress of access point
const byte wifi_subnet[4] = {255, 255, 255, 0}; //config subnet mask of access point
const byte wifi_gateway[4] = {192, 168, 1, 1}; //config gateway of access point

/*-- Create server port 80 --*/
ESP8266WebServer server(80);

/*-- http://ip_address/ --*/
void root() {
  server.send(200, "text/html", "<h1>Hello World :)</h1><br/>");
}

/*-- http://ip_address/led --*/
void led(){
  if(server.arg("set")=="1"){
    digitalWrite(BUILTIN_LED, LOW); // LED ON
    server.send(200, "text/html", "<h1>LED: ON</h1><br/>");
  }else if(server.arg("set")=="0"){
    digitalWrite(BUILTIN_LED, HIGH); // LED OFF
    server.send(200, "text/html", "<h1>LED: OFF</h1><br/>");
  }
}

void setup() {
  /*-- Initial pin output --*/
  pinMode(BUILTIN_LED, OUTPUT);


  /*-- Config wifi ---*/
  WiFi.softAP(ssid, password);
  WiFi.softAPConfig(
    IPAddress(wifi_ip[0], wifi_ip[1], wifi_ip[2], wifi_ip[3]),
    IPAddress(wifi_gateway[0], wifi_gateway[1], wifi_gateway[2], wifi_gateway[3]),
    IPAddress(wifi_subnet[0], wifi_subnet[1], wifi_subnet[2], wifi_subnet[3])
  );

  /*-- Handle url request --*/
  server.on("/", root); // http://ip_address/
  server.on("/led", HTTP_GET, led); // http://ip_address/led

  /*-- Server start --*/
  server.begin();
}

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

download source code จาก Source code ตัวอย่าง esp8266 จะทำงานเป็น Access point โดยมีรายละเอียดดังนี้

  • ssid : test-esp8266
  • password : 123456789
  • ip address : 192.168.1.2
  • subnet mask : 255.255.255.0
  • gateway : 192.168.1.1

สามารถเปลี่ยนได้ตรงบรรทัดที่ 6-10 ถ้าไม่ต้องการใส่ password สามารถแก้ตรงบรรทัดที่ 37 โดยลบพารามิเตอร์ password ออก function root จะทำงานเมื่อเรียก http://192.168.1.2/ โดยใน function นี้ผมเขียนให้แสดงคำว่า Hello World function led จะทำงานเมื่อเรียก http://192.168.1.2/led โดยใน function นี้ ผมเขียนให้รับค่าจาก HTTP_GET มาเพื่อควบคุมการ ปิด-เปิด LED  ตัวอย่างการเรียกคือ http://192.168.1.2/led?set=1 มีข้อสงสัยตรงไหน หรือผิดพลาดอะไรก็ comment ได้เลยนะครับ :) ดูวีดีโอตัวอย่าง

Suggestion blogs

Docker no space left on device error

สวัสดีครับ บทความนี้จะเป็นเรื่องเกี่ยวกับ Error ของ Docker ครับ คือ  "Docker no space left on device error" ซึ่งเกิดจาก Docker ไม่สามารถเขียนไฟล์ Docker.qcow2 ได้ เนื่องจาก ไฟล์ดังกล่าวมีขนาดใหญ่เกินไป

Flutter EP.2.1 Class และ Object ในภาษา Dart

สวัสดีครับ หลังจาก EP.2 เราได้เรียนรู้ Syntax ของภาษา Dart กันไปแล้ว สําหรับ EP นี้ก็จะเป็น เนื้อหาเพิ่มเติมซึ่งจะเกี่ยวข้องกับ Class และ Object ในภาษา Dart

Docker Swarm คืออะไร

Docker swarm เป็น Native Clustering ของ Docker คือเป็นเครื่องมือช่วยจัดการเครื่อง server ที่รัน Docker หลายๆเครื่องให้อยู่ในสภาพแวดล้อมเดียวกัน พูดง่ายๆก็คือ การนําเอาเครื่อง server หลายๆเครื่อง (Worker) มาช่วยกันทํางาน โดยจะถูกควบคุมการทํางานโดย Manager และยังมีระบบ IPVS ที่เป็น Load-balance ซึ่งจะทําให้เราสามารถเข้าถึง Website หรือ Application ต่างๆ ที่เรารันอยู่ได้จากเครื่องไหนก็ได้ใน Swarm โดย IPVS จะช่วยจัดการให้เราเองโดยอัตโนมัติ ศัพท์ที่เกี่ยวกับ Docker swarm ที่ควรรู้มีดังนี้


Copyright © 2019 - 2025 thiti.dev |  v1.55.1 |  Privacy policy | 

Build with ❤️ and Astro.

Github profile   Linkedin profile   Instagram   X profile   Nostr   Youtube channel   Telegram   Email contact   วงแหวนเว็บ