PlatformIO - Start Project Over-the-Air ESP8266
สวัสดีครับทุกท่าน คราวที่แล้วผมได้แนะนำให้รู้จักกับเจ้า PlatformIO IDE และวิธีการติดตั้งไปแล้ว คราวนี้มาพูดถึงวิธีการใช้งานเบี้องต้น พร้อมวิธีการอย่างละเอียดของโปรเจค OTA(Over The Air) ผ่าน ตัว PlatformIO IDE งั้นมาเริ่ม Project ของเรากันเลยดีกว่า
OTA(Over The Air) ถ้าแปลตรงตัว คือ การโปรแกรมผ่านมาทางอากาศ ขยายความนิดนึง ความหมายก็คือว่า คุณสามารถอัพเดทซอฟท์แวร์ไปยัง NodeMCU ผ่านทางสัญญาณเครือข่ายอินเตอร์เน็ตได้
OTA(Over The Air) ถ้าแปลตรงตัว คือ การโปรแกรมผ่านมาทางอากาศ ขยายความนิดนึง ความหมายก็คือว่า คุณสามารถอัพเดทซอฟท์แวร์ไปยัง NodeMCU ผ่านทางสัญญาณเครือข่ายอินเตอร์เน็ตได้
1 . เปิดโปรแกรม Atom
2. คลิกที่เมนู PlatformIO/Initialize new Project or update existing
3. เลือกบอร์ด และ เลือก ที่อยู่สำหรับ File Project
4. new file in Folder src
![]() |
เพิ่ม File main.cpp |
5. ใส่โค้ตตัวอย่างสำหรับ OTA (อย่าลืมเปลี่ยน ssid กับ Password นะครับ)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
#include <ArduinoOTA.h>
#define D1 5
#define D2 4
#define ledPin_right D1
#define ledPin_left D2
const char* ssid = "SSID WiFi";
const char* password = "WiFi Password";
//const char* host = "OTA-testing";
//const char* ota_pass = "ota";
void setup()
{
Serial.begin(9600);
Serial.println("Booting...");
WiFi.mode(WIFI_STA);
WiFi.begin(ssid, password);
while (WiFi.waitForConnectResult() != WL_CONNECTED) // check connection with router
{
Serial.println("Connection Router Failed! Rebooting...");
delay(500);
ESP.restart();
}
//ArduinoOTA.setPort(8266); // Port defaults to 8266
//ArduinoOTA.setHostname(host); // Hostname defaults to esp8266-[ChipID]
//ArduinoOTA.setPassword(ota_pass); // No authentication by default
ArduinoOTA.onStart([]()
{
Serial.println("Start");
});
ArduinoOTA.onEnd([]()
{
Serial.println("End");
});
ArduinoOTA.onProgress([](unsigned int progress, unsigned int total)
{
Serial.printf("Progress: %u%%\n", (progress / (total / 100)));
});
ArduinoOTA.onError([](ota_error_t error)
{
Serial.printf("Error[%u]: ", error);
if (error == OTA_AUTH_ERROR) Serial.println("Auth Failed");
else if (error == OTA_BEGIN_ERROR) Serial.println("Begin Failed");
else if (error == OTA_CONNECT_ERROR) Serial.println("Connect Failed");
else if (error == OTA_RECEIVE_ERROR) Serial.println("Receive Failed");
else if (error == OTA_END_ERROR) Serial.println("End Failed");
ESP.restart();
});
ArduinoOTA.begin(); // setup the OTA server
Serial.println("Ready!");
Serial.print("IP address: ");
Serial.println(WiFi.localIP());
pinMode(ledPin_right,OUTPUT);
pinMode(ledPin_left,OUTPUT);
digitalWrite(ledPin_right,LOW);
digitalWrite(ledPin_left,LOW);
}
void loop()
{
ArduinoOTA.handle();
digitalWrite(ledPin_right,!digitalRead(ledPin_right));
//digitalWrite(ledPin_left,!digitalRead(ledPin_left));
delay(250);
}
6. อัพโหลด Code ลงบอร์ด NodeMCU แบบ Serial ก่อนเพื่อจะทำการอัพโหลดแบบ OTA ในครั้งต่อไป
(ถ้าเสียบบอร์ด NodeMCU อยู่เพียงบอร์ดเดียว Platform IDE จะเลือกพอร์ทให้อัตโนมัติ ไม่ต้องทำการกำหนดพอร์ท) ในกรณีที่มีบอร์ดเสียบอยู่หลายๆบอร์ด สามารถเลือกได้ตามภาพด้านล่าง
7. กดอัพโหลดเลยครับ
8. ทำการกำหนด IP ที่จะอัพโหลดผ่าน OTA (ดูจาก Serial Monitor) แล้วกดอัพโหลดเลยครับ
c2c7cc3a7145f80d6d289bc85626b7e9faccc994
c2c7cc3a7145f80d6d289bc85626b7e9faccc994c2c7cc3a7145f80d6d289bc85626b7e9faccc994c2c7cc3a7145f80d6d289bc85626b7e9faccc994
l ibraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino
c2c7cc3a7145f80d6d289bc85626b7e9faccc994
libraries/ArduinoOTA/examples/BasicOTA/BasicOTA.inolibraries/ArduinoOTA/examples/BasicOTA/BasicOTA.ino
![]() |
คลิกที่ Serial Monitor |
![]() |
เลือก Port และ Baud rate ให้ตรงตาม Code |
![]() |
IP address ของ NodeMCU |
![]() |
ใส่ IP ในการ upload |
![]() |
Mission complete |
NOTE: จะเห็นได้ว่าการใช้ PlatformIO ในการอัพโหลด ผ่าน OTA จะไม่ต้องทำการโหลดบอร์ด โหลด Library รวมทั้ง espressif framework ในการติดต่อผ่าน OTA เอง PlatformIO จะทำการโหลดมาให้อัตโนมัติ
ขอบคุณทุกๆท่านที่ติดตามผลงานของผม ไว้เจอกันบทความหน้าครับ มีอะไรใหม่ๆผมจะรีบอัพเดทและแบ่งปันให้ทุกท่าน ตอนนี้ผมขออ่านหนังสือเตรียมสอบก่อนครับ ฮ่าๆ
คำเตือนนน
ให้ปิด Firewall ก่อนที่จะทำการโปรแกรม OTA
ให้ปิด Firewall ก่อนที่จะทำการโปรแกรม OTA
please contact us.
Facebook : https://www.facebook.com/ThaiEmbedded
Youtube Channel : http://www.youtube.com/tesrchannel
Youtube Channel : http://www.youtube.com/tesrchannel
Blog : http://tesrteam.blogspot.com/
Email : pongpiphat.tach@gmail.com
line id : pongpiphat
Tel. 083-1858526
Email : pongpiphat.tach@gmail.com
line id : pongpiphat
Tel. 083-1858526
ABOUT THE AUTHOR

Hello We are OddThemes, Our name came from the fact that we are UNIQUE. We specialize in designing premium looking fully customizable highly responsive blogger templates. We at OddThemes do carry a philosophy that: Nothing Is Impossible
0 ความคิดเห็น:
แสดงความคิดเห็น