สอน NETPIE2020 บทที่ 3 ESP32 สั่งงาน Servo Motor
📺 📟 🌏ในบทนี้จะนำเสนอการใช้ NETPIE2020 สั่งงาน Sevo Motor หมุน 0–180 องศา ด้วย Widget Slider
สอน NETPIE2020 บทที่ 3 ESP32 สั่งงาน Servo Motor
📺 📟 🌏ในบทนี้จะนำเสนอการใช้ NETPIE2020 สั่งงาน Sevo Motor หมุน 0–180 องศา ด้วย Widget Slider

- เริ่มต้นให้ Create Device โดยเลือกเมนูดังภาพด้านล่าง หรือใช้ Device ที่สร้างไว้ในบทเรียนก่อนหน้าก็สามารถข้ามขั้นตอนนี้ได้เลย

กรอกชื่อ Device แล้วกด Save

สถานะจะยังเป็น Offline อยู่ เพราะยังไม่ได้เชื่อมต่อบอร์ด ESP32 ต่อไปให้กดที่ ID

ค่าที่จะต้องใช้กรอกใน Arduino ได้แก่ Client ID, Token, Secret ในขั้นตอนต่อไป

- เปิดโปรแกรม Arduino IDE ถ้าติดตั้งไลบรารี่ PubSubClient แล้ว ก็ข้ามขั้นตอนนี้ได้เลย การติดตั้งไลบรารี่ PubSubClient โดยไปที่เมนู Sketch เลือก Include Library จากนั้นกดที่เมนู Manage Libraries

พิมพ์ PubSubClient ในช่อง และหาไลบรารี่ PubSubClient จากนั้นกดปุ่ม Install

5.ต่อไปติดตั้ง ไลบรารี่ Servo Motor โดยพิมพ์ esp32servo ในช่อง และหาไลบรารี่ ESP32Servo จากนั้นกดปุ่ม Install

- เมื่อติดตั้งเสร็จจะแสดงข้อความ Installed ให้กดปุ่ม close ได้เลย

ต่อไปมาเริ่มเขียนโปรแกรมกันเลย ให้ทำการแก้ไขบรรทัดที่มี Comment ห้อยท้ายนะครับ จำนวน 6 บรรทัด
#include <WiFi.h>
#include <PubSubClient.h>
#include <ESP32Servo.h>
#define SERVO 5//ระบุขา Pin ที่เชื่อมต่อ Servo Motor ตัวอย่างใช้ขา 5
const char* ssid = "xxxxxxxxxxxx"; //กรอกชื่อ Wifi
const char* password = "xxxxxxxxxx"; //กรอกรหัส Wifi
const char* mqtt_server = "broker.netpie.io";
const int mqtt_port = 1883;
const char* mqtt_Client = "xxxxxxxxxxxxx"; //กรอก Client ID
const char* mqtt_username = "xxxxxxxxxxxxxx"; //กรอก Token
const char* mqtt_password = "xxxxxxxxxxxxxx"; //กรอก Secret
WiFiClient espClient;
PubSubClient client(espClient);
Servo myservo;
int ser = 0;
char msg[150];
String datastr;
void reconnect() {
while (!client.connected()) {
Serial.print("Attempting MQTT connection…");
if (client.connect(mqtt_Client, mqtt_username, mqtt_password)) {
Serial.println("connected");
client.subscribe("@msg/#");
}
else {
Serial.print("failed, rc=");
Serial.print(client.state());
Serial.println("try again in 5 seconds");
delay(5000);
}
}
}
void callback(char* topic, byte* payload, unsigned int length) {
Serial.print("Message arrived [");
Serial.print(topic);
Serial.print("] ");
String message;
for (int i = 0; i < length; i++) {
message = message + (char)payload[i];
}
Serial.println(message);
if(String(topic) == "@msg/servo") {
ser = message.toInt();
datastr = "{\"data\": {\"servo\":" + String(ser)+"}}";
datastr.toCharArray(msg, (datastr.length() + 1));
client.publish("@shadow/data/update", msg);
Serial.print("Servo set to ");
Serial.print(ser);
Serial.println(datastr);
}
}
void setup() {
Serial.begin(9600);
myservo.attach(SERVO);
Serial.println();
Serial.print("Connecting to ");
Serial.println(ssid);
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
client.setServer(mqtt_server, mqtt_port);
client.setCallback(callback);
}
void loop() {
if (!client.connected()) {
reconnect();
}
client.loop();
myservo.write(ser);
delay(1000);
}
ทำการ Upload โค้ดลงบอร์ด ESP32

ตรวจสอบสถานะจะแสดงข้อความ Online

ต่อไป ไปที่หน้า Dashboard แล้วกดปุ่ม +Create

ตั้งชื่อ Dashboard แล้วกด SAVE

คลิกที่ชื่อ Dashboard ที่สร้างขึ้น

ไปที่เมนู Setting กด +Add source

เลือก Device ในส่วนของ Privileges ให้เพิ่มทุกตัวที่มี แล้วกด SAVE

กลับมาที่หน้า Dashboard แล้วกด Edit

กด +Add Panel

กดเครื่องหมาย + เพื่อเลือก Widget ที่ต้องการ

ตรง Widget Type ให้เลือก Slider เพื่อใช้สั่ง Servo Motor โดยตั้งค่า MIN = 0 และ MAX = 180

ตั้งค่า ONSTOP ACTION ด้วยคำสั่ง
#["KruRin"].publishMsg("servo",String(value))

แล้วกดปุ่ม Done

จากนั้นกด Save

ทดสอบสั่งหมุน Servo Motor

ดูเล่นกันดูนะครับ

ขอบคุณที่รับชมครับผม

สนันสนุน KruRin ในการเขียนบทความ เลี้ยงกาแฟซักแก้วครับ กดที่นี่

สนใจบอร์ด IoT Education Kit V1.1

สั่งซื้อใน Shopee ที่ลิ้งค์ด้านล่างได้เลยครับผม
หรือ กด ที่นี่
KruRin
บทต่อไป กดลิ้งค์ด้านล่างเลยครับ
메타데이터
- post_id
- f932e0b784ee
- slug
- สอน-netpie2020-บทที่-3-esp32-สั่งงาน-servo-motor-f932e0b784ee
- url
- https://medium.com/@KruRin/%E0%B8%AA%E0%B8%AD%E0%B8%99-netpie2020-%E0%B8%9A%E0%B8%97%E0%B8%97%E0%B8%B5%E0%B9%88-3-esp32-%E0%B8%AA%E0%B8%B1%E0%B9%88%E0%B8%87%E0%B8%87%E0%B8%B2%E0%B8%99-servo-motor-f932e0b784ee
- canonical_url
- https://medium.com/@KruRin/%E0%B8%AA%E0%B8%AD%E0%B8%99-netpie2020-%E0%B8%9A%E0%B8%97%E0%B8%97%E0%B8%B5%E0%B9%88-3-esp32-%E0%B8%AA%E0%B8%B1%E0%B9%88%E0%B8%87%E0%B8%87%E0%B8%B2%E0%B8%99-servo-motor-f932e0b784ee
- author_url
- https://medium.com/@KruRin
- status
- ok
- fetched_at
- 2026-06-13 09:42:26