ทำ Data Logger ด้วย TTGO T4 (ESP32) และแสดงข้อมูลผ่านจอด้วย GPS Module

Pawat Saengduan
Chiang Mai Maker Club
10 min readMay 8, 2020
บอร์ด TTGO T4 ที่แสดงผลข้อมูล GPS และสถานะ SD Card

สวัสดีคร้าาบบบ ก่อนอื่นผมขอแนะนำตัวก่อนนะครับผม…… ผมชื่อนนนะครับ

ในบทความนี้ผมจะสอนวิธีการ ทำ Data Logger ด้วย TTGO T4 (ESP32) และแสดงข้อมูลผ่านจอด้วย GPS Module

เตรียมอุปกรณ์

ในบทความนี้ มีอุปกรณ์ที่ต้องใช้ดังนี้ครับ

  1. LILYGO® TTGO T4 (ESP32) x1
  2. SD Card x1 ( ขนาดเท่าไหรก็ได้ครับ )
  3. GPS Module x1 ( ที่รองรับ Library TinyGPS++ )

ในเมื่อมีอุปกรณ์พร้อมแล้ว ก็ไปลุยเขียนโค้ดต่อเลยดีกว่า คร้าบบผม..!?!!

ก่อนจะลุยโค้ดมาต่อเซ็นเซอร์กันก่อนครับ 5555+

                             CONNECTION
|--------------|---------------|--------------|--------------------|
| Device | Pin ( Device )| Pin (Sensor) | Sensor Module ||--------------|---------------|--------------|--------------------|
| TTGO T4 | 33 (TX) | RX | GPS Module |
|--------------|---------------|--------------|--------------------|
| TTGO T4 | 35 (RX) | TX | GPS Module |
|------------------------------------------------------------------|

ถึงเวลา… เขียนโค้ด

เอาละคร้บ ถึงเวลาเขียนโค้ดกันแล้ววว! ในวันนี้ IDE ที่ผมใช้เป็น Visual Studio Code ที่ใช้ PlatformIO นะครับผม

PlatformIO on Visual Studio Code

มาเริ่มกันเลยครับผม

!!! สามารถดาวน์โหลดโค้ดตัวเต็มจากด้านล่างของหัวข้อนี้ได้เลยครับ !!!

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

2. สร้างโปรเจกต์

2.1 เปิดมาก็สร้างโปรเจกต์ก่อนเลยสิครับ ….. รอไรละ!!

คลิกตรง [+] New Project เพื่อสร้างโปรเจกต์

2.2 หลังจากนั้นก็… ตั้งต่าโปรเจกต์ครับ ชื่ออะไร.. แต่ว่าบอร์ดให้ตั้งเป็น TTGO T1 ครับ เพราะว่าในบทความนี้บอร์ดที่เราใช้เป็น TTGO T4 แต่ว่าก็สามารถใช้ได้

ตั้งค่าการสร้างโปรเจกต์

หลักจากนั้นก็ต้อง รอ….. กันอีกเช่นเคยฮะ

กำลังสร้างโปรเจกต์…. รอไปเถอะครับ…

เมื่อสร้างเสร็จแล้ว.. เราก็เข้าไปที่ src/main.cpp เลยครับ

อธิบายโครงสร้างของ PlatformIO ( สำหรับใครที่ยังไม่รู้นะครับ )

  • .pio/ — เป็นโฟลเดอร์สำหรับเก็บไฟล์ที่ถูกคอมไฟล์ไปแล้วและไลบรารี่ที่ถูกคอมไฟล์ไปแล้ว
  • include/ — ไฟล์สำหรับเก็บโค้ดต่างๆ ที่แยกออกมาจากไฟล์หลัก (.h *cpp )
  • lib/ — โฟลเดอร์สำหรับเก็บไลบรารี่ต่างๆ ( ถ้าเราใส่ไลบรารี่อันเดียวกับไลบรารี่แบบ Global มันจะทำงานใน lib เท่านั้น )
  • src/ — โฟลเดอร์เก็บไฟล์โค้ดหลัก
  • src/main.cpp — main.cpp คือโค้ดหลักในการรันโปรแกรม
  • test/ — เป็นโฟลเดอร์สำหรับ test
  • .gitignore — ไม่เพิ่มไฟล์เข้า Git
  • .travis.yml — สำหรับ CI
  • platformio.ini — สำหรับตั้งค่าโปรเจ็ค แบบละเอียด

ตอนนี้เรารู้โครงสร้างของ PlatformIO… ถึงเวลาเขียนโค้ดและ รันโค้ด!

3. ดาวน์โหลดไลบรารี่

3.1 เราต้องใช้ไลบรารี่…. แน่นอนครับว่า.. เราใช้ Module GPS ด้วย เราก็ต้องโหลดไลบรารี่มา — ลิงก์ดาวน์โหลด

เว็บดาวน์โหลดไลบรารี่ TinyGPS++

พอโหลดมาเสร็จแล้ว.. ก็แตกไฟล์… พอแตกไฟล์เสร็จแล้ว ให้เอาไปใส่ใน

ROOT_PROJECT/lib
นำโฟลเดอร์ TInyGPSPlus-x.x.xb ใส่ไปใน ROOT_PROJECT/lib

ไลบรารี่ต่อไปครับ.. เป็นไลบรารี่ที่ผมดัดแปลงมานิดหน่อย… นั้นก็คือ TFT_eSPI ครับ

เพราะฉะนั้นผมเลยมีลิ้งก์ดาวน์โหลดให้ครับ

เว็บดาวน์โหลดไลบรารี่ TFT_eSPI ที่ผมดัดแปลง

แล้วไปใส่ใน lib/ เหมือนเดิมครับ

ในโฟลเดอร์ lib/

ต่อไปก็เอาไฟล์ 2 ตัวนี้ ไปใส่ใน

ROOT_PROJECT/include

T4_V12.h:

T4_V12.h

T4_V13.h:

T4_V13.h

ให้เอาไปใส่ใน

!!! ROOT_PROJECT ก็คือโฟลเดอร์ของโปรเจกต์ครับ !!!

T4_V12.h ---> ROOT_PROJECT/include/T4_V12.h
T4_V13.h ---> ROOT_PROJECT/include/T4_V13.h
ในโฟลเดอร์ ROOT_PROJECT/include

เอาละ…!!?! ถึงเวลาเขียนโค้ด!!!!

— — — — — — —

4. เขียนโค้ด

มาถึงก็เปิดไฟล์ main.cpp ก่อนเลยครับ

4.1 เปิดไฟล์มาแล้วสิ่งแรกก็คือ #include

/* นำไลบรารี่เข้า */
#include <Arduino.h>
#include <HardwareSerial.h>#include <TinyGPS++.h>#include <TFT_eSPI.h>#include <SPI.h>#include <SD.h>/* เลือกโมเดลบอร์ดโดย ปิดคอมเม้นท์ */
// #define T4_V12#define T4_V13
#if defined(T4_V12)#include "T4_V12.h"#elif defined(T4_V13)#include "T4_V13.h"#else#error "Please select board version."#endif

4.2 ตั้งค่า Pin ต่างๆ และ Config ทุกอย่าง

/* ตั้งค่า SD Card ด้วย SPI */
SPIClass sdSPI(VSPI);
#define IP5306_ADDR 0X75#define IP5306_REG_SYS_CTL0 0x00
/* กำหนดตัวแปรพินที่ต้องใช้กับ GPS Module และ Baudrate*/
#define RXPin (35)#define TXPin (33)
static const uint32_t GPSBaud = 9600;
/* กำหนดไลบรารี่ */
HardwareSerial hs(2);TinyGPSPlus gps;TFT_eSPI tft = TFT_eSPI();

4.3 ตั้งค่าฟังก์ชั่น

/* กำหนดฟังก์ชั่น */
static void smartDelay(unsigned long ms);
static void printFloat(float val, bool valid, int len, int prec);static void printInt(unsigned long val, bool valid, int len);static void printDate(TinyGPSDate &d);static void printTime(TinyGPSTime &t);String setFilename(TinyGPSDate &d);void writeRoot(fs::FS &fs);

4.4 กำหนดตัวแปร

/* สำหรับ Multitask ทีใช้รูปแบบ millis() */
uint32_t last1 = 0;
uint32_t last2 = 0;
/* สำหรับเก็บชื่อไฟล์ */char filename[12];
/* สำหรับเก็บสถานะของ SD Card */bool writeOk = false;bool isReady = false;

4.5 เรียกฟังกชั่น setup() แล้ว setup ต่างๆ

void setup(){
/* Setup Serial และ GPS Module */
Serial.begin(115200);
hs.begin(GPSBaud, SERIAL_8N1, RXPin, TXPin, false);
/* Setup จอ TFT LCD */
tft.init(); tft.setRotation(1); tft.fillScreen(TFT_BLACK); tft.setTextSize(3); tft.setTextColor(TFT_WHITE);
if (TFT_BL > 0) { pinMode(TFT_BL, OUTPUT); digitalWrite(TFT_BL, HIGH); }
/* Setup SD Card */
Serial.print("Initializing SD Card... "); if (SD_CS > 0) { sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS); if (!SD.begin(SD_CS, sdSPI)) { Serial.println("Failed!"); tft.setCursor(0, 0); tft.println("Mount failed!");
delay(1000);
isReady = false; } else { Serial.println("Success!");
tft.setCursor(0, 0); tft.println("Mount Success!"); delay(1000); isReady = true; }
}
/* Set ขนาด Text ที่ 2 */tft.setTextSize(2);}

4.6 เรียกฟังก์ชั่น loop() เพื่อเก็บข้อมูลและ แสดงผลแบบวนลูป

void loop(){
/* Multitask ที่ 1 ทำงานทุกๆ 1 วิ */
if (millis() - last1 >= 1000L) { last1 = millis(); /* ให้จอลบล้างข้อมูลที่แสดงทั้งหมด */ tft.fillScreen(TFT_BLACK); tft.setTextColor(TFT_WHITE); /* ถ้าไม่เชื่อมต่อกับ GPS Module ให้แสดงบนจอ if (millis() > 5000 && gps.charsProcessed() < 10) { tft.print(F("No GPS data received: check wiring")); } /* แสดงผลข้อมูลเกี่ยวกับ GPS */ tft.setCursor(0, 0); tft.print("Satellites: "); printInt(gps.satellites.value(), gps.satellites.isValid(), 5); tft.print("\nHDOP: "); printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1); tft.print("\nLatitude: "); printFloat(gps.location.lat(), gps.location.isValid(), 11, 6); tft.print("\nLongitude: "); printFloat(gps.location.lng(), gps.location.isValid(), 12, 6); tft.print("\nFix (Age): "); printInt(gps.location.age(), gps.location.isValid(), 5); tft.print("\nDate: "); printDate(gps.date); tft.print("\nTime: "); printTime(gps.time); tft.print("\nDate (Age): "); printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2); tft.print("\nAltitude (m): "); printFloat(gps.course.deg(), gps.course.isValid(), 7, 2); tft.print("\nCourse Speed Card: "); printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2); tft.print("\nChars: "); printInt(gps.charsProcessed(), true, 6); tft.print("\nSentences: "); printInt(gps.sentencesWithFix(), true, 10); tft.print("\nChecksum: "); printInt(gps.failedChecksum(), true, 9);
/* แสดงผลสถานะของ SD Card */
if (writeOk == true && isReady == true) { tft.setTextSize(1.3); tft.setCursor(0, 223); tft.print("SD Card: Writing"); tft.setTextSize(2); } else if (writeOk == false && isReady == true) { tft.setTextSize(1.3); tft.setCursor(0, 223); tft.print("SD Card: Attempting to write"); tft.setTextSize(2); } else if (writeOk == false && isReady == false) {
tft.setTextSize(1.3);
tft.setCursor(0, 223); tft.println("SD Card: Mount failed!"); tft.setTextSize(2); }
smartDelay(1800);}/* นำข้อมูล GPS เขียนลง SD Card ถ้า SD Card ทำงานได้*/if ((millis() - last2) >= 20000L && isReady == true){ writeRoot(SD); last2 = millis();
}
}

4.7 เขียนให้ฟังก์ชั่นทำอะไร

/* ดีเลย์พร้อมรับข้อมูล GPS */
static void smartDelay(unsigned long ms)
{ unsigned long start = millis(); do { while (hs.available()) gps.encode(hs.read()); } while (millis() - start < ms);}
/* แสดงผลข้อมูลแบบทศนิยมขึ้นจอแบบพิเศษ */
static void printFloat(float val, bool valid, int len, int prec){ if (!valid) { while (len-- > 1) tft.print('*');
tft.print(' '); } else { tft.print(val, prec);
int vi = abs((int)val); int flen = prec + (val < 0.0 ? 2 : 1); // . and - flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i = flen; i < len; ++i)
tft.print(' '); } smartDelay(0);}
/* แสดงผลข้อมูลจำนวนเต็มแบบพิเศษ */
static void printInt(unsigned long val, bool valid, int len){ char sz[32] = "*****************"; if (valid) sprintf(sz, "%ld", val);
sz[len] = 0; for (int i = strlen(sz); i < len; ++i)
sz[i] = ' ';
if (len > 0) sz[len - 1] = ' ';
tft.print(sz);
smartDelay(0);}
/* แสดงผลข้อมูลวันเดือนปีแบบพิเศษ */static void printDate(TinyGPSDate &d){ if (!d.isValid()) { tft.print(F("********** ")); } else { char sz[32];
sprintf(sz, "%02d/%02d/%04d ", d.month(), d.day(), d.year()); tft.print(sz); }printInt(d.age(), d.isValid(), 5);
smartDelay(0);}
/* แสดงผลข้อมูลเวลาแบบพิเศษ */static void printTime(TinyGPSTime &t){ char sz[32]; if (t.isValid()) { sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second()); } printInt(t.age(), t.isValid(), 5); smartDelay(0);}/* ฟังก์ชั่นสำหรับต้งชื่อไฟลใน SD Card */String setFilename(TinyGPSDate &d){ if (!d.isValid()) { sprintf(filename, "/NULLFiles.csv"); } else { sprintf(filename, "/%02d%02d%04d.csv", d.day(), d.month(), d.year()); } return (String)filename;}
/* เขียนข้อมูลลง SD Card */
void writeRoot(fs::FS &fs){
/* เปิดไฟล์ */
File root = fs.open(setFilename(gps.date), FILE_APPEND);
/* แสดงสถานะบน Serial */ Serial.print("SD Card Write... "); if (root) {
/* ตั้งสถานะ SD Card */
writeOk = true;
/* แสดงสถานะบน Serial */ Serial.println("Success");
/* แสดงผลข้อมูล GPS บนจอ */
root.print(gps.satellites.isValid()); root.print(","); root.print(gps.satellites.value()); root.print(","); root.print(gps.hdop.hdop()); root.print(","); root.print(gps.location.lat()); root.print(","); root.print(gps.location.lng()); root.print(","); root.print(gps.location.age()); root.print(","); root.print(gps.date.month()); root.print("/"); root.print(gps.date.day()); root.print("/"); root.print(gps.date.year()); root.print(","); root.print(gps.time.hour()); root.print(":"); root.print(gps.time.minute()); root.print(":"); root.print(gps.time.second()); root.print(","); root.print(gps.altitude.meters()); root.print(","); root.print(gps.course.deg()); root.print(","); root.print(gps.speed.kmph()); root.print(","); root.print(gps.charsProcessed()); root.print(","); root.print(gps.sentencesWithFix()); root.print(","); root.println(gps.failedChecksum());
/* ปิดไฟล์ */
root.close(); } else {
/* เปิดไฟล์ */
writeOk = false;
/* แสดงสถานะบน Serial */ Serial.println("Failed"); }}

โค้ดทั้งหมด:

/* นำไลบรารี่เข้า */
#include <Arduino.h>
#include <HardwareSerial.h>
#include <TinyGPS++.h>
#include <TFT_eSPI.h>
#include <SPI.h>
#include <SD.h>
/* เลือกโมเดลบอร์ดโดย ปิดคอมเม้นท์ */// #define T4_V12
#define T4_V13
#if defined(T4_V12)
#include "T4_V12.h"
#elif defined(T4_V13)
#include "T4_V13.h"
#else
#error "Please select board version."
#endif
/* ตั้งค่า SD Card ด้วย SPI */
SPIClass sdSPI(VSPI);
#define IP5306_ADDR 0X75
#define IP5306_REG_SYS_CTL0 0x00
/* กำหนดตัวแปรพินที่ต้องใช้กับ GPS Module และ Baudrate*/
#define RXPin (35)
#define TXPin (33)
static const uint32_t GPSBaud = 9600;/* กำหนดไลบรารี่ */
HardwareSerial hs(2);
TinyGPSPlus gps;
TFT_eSPI tft = TFT_eSPI();
/* กำหนดฟังก์ชั่น */
static void smartDelay(unsigned long ms);
static void printFloat(float val, bool valid, int len, int prec);
static void printInt(unsigned long val, bool valid, int len);
static void printDate(TinyGPSDate &d);
static void printTime(TinyGPSTime &t);
String setFilename(TinyGPSDate &d);
void writeRoot(fs::FS &fs);
/* สำหรับ Multitask ทีใช้รูปแบบ millis() */
uint32_t last1 = 0;
uint32_t last2 = 0;
/* สำหรับเก็บชื่อไฟล์ */
char filename[12];
/* สำหรับเก็บสถานะของ SD Card */
bool writeOk = false;
bool isReady = false;
void setup()
{
/* Setup Serial และ GPS Module */
Serial.begin(115200);
hs.begin(GPSBaud, SERIAL_8N1, RXPin, TXPin, false);

/* Setup จอ TFT LCD */
tft.init();
tft.setRotation(1);
tft.fillScreen(TFT_BLACK);
tft.setTextSize(3);
tft.setTextColor(TFT_WHITE);
if (TFT_BL > 0)
{
pinMode(TFT_BL, OUTPUT);
digitalWrite(TFT_BL, HIGH);
}
/* Setup SD Card */
Serial.print("Initializing SD Card... ");
if (SD_CS > 0)
{
sdSPI.begin(SD_SCLK, SD_MISO, SD_MOSI, SD_CS);
if (!SD.begin(SD_CS, sdSPI))
{
Serial.println("Failed!");
tft.setCursor(0, 0);
tft.println("Mount failed!");

delay(1000);
isReady = false;
}
else
{
Serial.println("Success!");
tft.setCursor(0, 0);
tft.println("Mount Success!");

delay(1000);

isReady = true;
}
}
/* Set ขนาด Text ที่ 2 */
tft.setTextSize(2);
}
void loop()
{
/* Multitask ที่ 1 ทำงานทุกๆ 1 วิ */
if (millis() - last1 >= 1000L)
{
last1 = millis();
/* ให้จอลบล้างข้อมูลที่แสดงทั้งหมด */
tft.fillScreen(TFT_BLACK);
tft.setTextColor(TFT_WHITE);
/* ถ้าไม่เชื่อมต่อกับ GPS Module ให้แสดงบนจอ
if (millis() > 5000 && gps.charsProcessed() < 10)
{
tft.print(F("No GPS data received: check wiring"));
}
/* แสดงผลข้อมูลเกี่ยวกับ GPS */
tft.setCursor(0, 0);
tft.print("Satellites: ");
printInt(gps.satellites.value(), gps.satellites.isValid(), 5);
tft.print("\nHDOP: ");
printFloat(gps.hdop.hdop(), gps.hdop.isValid(), 6, 1);
tft.print("\nLatitude: ");
printFloat(gps.location.lat(), gps.location.isValid(), 11, 6);
tft.print("\nLongitude: ");
printFloat(gps.location.lng(), gps.location.isValid(), 12, 6);
tft.print("\nFix (Age): ");
printInt(gps.location.age(), gps.location.isValid(), 5);
tft.print("\nDate: ");
printDate(gps.date);
tft.print("\nTime: ");
printTime(gps.time);
tft.print("\nDate (Age): ");
printFloat(gps.altitude.meters(), gps.altitude.isValid(), 7, 2);
tft.print("\nAltitude (m): ");
printFloat(gps.course.deg(), gps.course.isValid(), 7, 2);
tft.print("\nCourse Speed Card: ");
printFloat(gps.speed.kmph(), gps.speed.isValid(), 6, 2);
tft.print("\nChars: ");
printInt(gps.charsProcessed(), true, 6);
tft.print("\nSentences: ");
printInt(gps.sentencesWithFix(), true, 10);
tft.print("\nChecksum: ");
printInt(gps.failedChecksum(), true, 9);
/* แสดงผลสถานะของ SD Card */
if (writeOk == true && isReady == true)
{
tft.setTextSize(1.3);
tft.setCursor(0, 223);
tft.print("SD Card: Writing");
tft.setTextSize(2);
}
else if (writeOk == false && isReady == true)
{
tft.setTextSize(1.3);
tft.setCursor(0, 223);
tft.print("SD Card: Attempting to write");
tft.setTextSize(2);
}
else if (writeOk == false && isReady == false)
{
tft.setTextSize(1.3);
tft.setCursor(0, 223);
tft.println("SD Card: Mount failed!");
tft.setTextSize(2);
}
smartDelay(1800);
}
/* นำข้อมูล GPS เขียนลง SD Card ถ้า SD Card ทำงานได้*/
if ((millis() - last2) >= 20000L && isReady == true)
{
writeRoot(SD);
last2 = millis();
}
}
/* ดีเลย์พร้อมรับข้อมูล GPS */
static void smartDelay(unsigned long ms)
{
unsigned long start = millis();
do
{
while (hs.available())
gps.encode(hs.read());
} while (millis() - start < ms);
}
/* แสดงผลข้อมูลแบบทศนิยมขึ้นจอแบบพิเศษ */
static void printFloat(float val, bool valid, int len, int prec)
{
if (!valid)
{
while (len-- > 1)
tft.print('*');
tft.print(' ');
}
else
{
tft.print(val, prec);
int vi = abs((int)val);
int flen = prec + (val < 0.0 ? 2 : 1); // . and -
flen += vi >= 1000 ? 4 : vi >= 100 ? 3 : vi >= 10 ? 2 : 1;
for (int i = flen; i < len; ++i)tft.print(' ');
}
smartDelay(0);
}
/* แสดงผลข้อมูลจำนวนเต็มแบบพิเศษ */
static void printInt(unsigned long val, bool valid, int len)
{
char sz[32] = "*****************";
if (valid)
sprintf(sz, "%ld", val);

sz[len] = 0;
for (int i = strlen(sz); i < len; ++i)
sz[i] = ' ';
if (len > 0)
sz[len - 1] = ' ';

tft.print(sz);
smartDelay(0);
}
/* แสดงผลข้อมูลวันเดือนปีแบบพิเศษ */
static void printDate(TinyGPSDate &d)
{
if (!d.isValid())
{
tft.print(F("********** "));
}
else
{
char sz[32];

sprintf(sz, "%02d/%02d/%04d ", d.month(), d.day(), d.year());
tft.print(sz);
}
printInt(d.age(), d.isValid(), 5);
smartDelay(0);
}
/* แสดงผลข้อมูลเวลาแบบพิเศษ */
static void printTime(TinyGPSTime &t)
{
char sz[32];
if (t.isValid())
{
sprintf(sz, "%02d:%02d:%02d ", t.hour(), t.minute(), t.second());
}
printInt(t.age(), t.isValid(), 5);smartDelay(0);
}
/* ฟังก์ชั่นสำหรับต้งชื่อไฟลใน SD Card */
String setFilename(TinyGPSDate &d)
{
if (!d.isValid())
{
sprintf(filename, "/NULLFiles.csv");
}
else
{
sprintf(filename, "/%02d%02d%04d.csv", d.day(), d.month(), d.year());
}
return (String)filename;
}
/* เขียนข้อมูลลง SD Card */
void writeRoot(fs::FS &fs)
{
/* เปิดไฟล์ */
File root = fs.open(setFilename(gps.date), FILE_APPEND);

/* แสดงสถานะบน Serial */
Serial.print("SD Card Write... ");
if (root)
{
/* ตั้งสถานะ SD Card */
writeOk = true;
/* แสดงสถานะบน Serial */
Serial.println("Success");

/* แสดงผลข้อมูล GPS บนจอ */
root.print(gps.satellites.isValid());
root.print(",");
root.print(gps.satellites.value());
root.print(",");
root.print(gps.hdop.hdop());
root.print(",");
root.print(gps.location.lat());
root.print(",");
root.print(gps.location.lng());
root.print(",");
root.print(gps.location.age());
root.print(",");
root.print(gps.date.month());
root.print("/");
root.print(gps.date.day());
root.print("/");
root.print(gps.date.year());
root.print(",");
root.print(gps.time.hour());
root.print(":");
root.print(gps.time.minute());
root.print(":");
root.print(gps.time.second());
root.print(",");
root.print(gps.altitude.meters());
root.print(",");
root.print(gps.course.deg());
root.print(",");
root.print(gps.speed.kmph());
root.print(",");
root.print(gps.charsProcessed());
root.print(",");
root.print(gps.sentencesWithFix());
root.print(",");
root.println(gps.failedChecksum());

/* ปิดไฟล์ */
root.close();
}
else
{
/* เปิดไฟล์ */
writeOk = false;

/* แสดงสถานะบน Serial */
Serial.println("Failed");
}
}

ดาวน์โหลดโค้ดเต็ม!

4.8 แก้ไข PlatformIO Serial Baudrate

ต่อไปเปิดไฟล์

platformio.ini

พอเปิดเสร็จแล้ว ให้เว้นบรรทัดหนึ่งบรรทัดแล้วพิมพ์คำว่า

monitor_speed = 115200

เพราะฉะนั้น (คนที่ใช้เลือกบอร์ด TTGO T1 ใน PlatformIO) ใน ไฟล์ platformio.ini จะมีข้อมูลแบบนี้

; PlatformIO Project Configuration File;;   Build options: build flags, source filter;   Upload options: custom upload port, speed and extra flags;   Library options: dependencies, extra library storages;   Advanced options: extra scripting;; Please visit documentation for the other options and examples; https://docs.platformio.org/page/projectconf.html[env:ttgo-t1]platform = espressif32board = ttgo-t1framework = arduinomonitor_speed = 115200

ใส่ moniter_speed เพื่อเลือกความเร็วในการแสดง Serial Monitor

5. รันและคอมไพล์โค้ด

เอาละครับ..!?!? เราเขียนโค้ดเสร็จแล้ว… ต่อไปเราต้อง Build หรือ Flash เลย

วิธีการ Verify หรือ Build หรือ Compile ( สำหรับใครที่ยังไม่รู้นะครับ )

  1. กด CTRL + ALT + B
  2. ให้กดปุ่ม CTRL + SHIFT + P มันจะขึ้น Command Palette
Command Palette

ต่อไปพิมพ์คำว่า

PlatformIO: Build

แล้ว Enter ไปเลย!!

PlatformIO: Build

— — — — — — — — — — — — — — — — — — — — — — — — —

วิธีการ Upload หรือ Flash ( สำหรับใครที่ยังไม่รู้นะครับ )

  1. กด CTRL + Alt + U
  2. กด CTRL + ALT + P มันจะเปิด Command Palette
Command Palette

แล้วพิมพ์คำว่า

PlatformIO: Upload

แล้ว Enter ไปเลย!!

— — — — — — — — — — — — — — — — — — — — — — — — — — — — — —

วิธีเปิด Serial Monitor ( สำหรับใครที่ยังไม่รู้นะครับ )

  1. กด CTRL + Alt + S
  2. กด CTRL + ALT + P มันจะเปิด Command Palette
Command Palette

แล้วพิมพ์คำว่า

PlatformIO: Serial Monitor

แล้ว Enter ไปเลย!!

PlatformIO: Serial Monitor

— — — — — — — — — — — — — — — — — — — — — — — — — — — — —

พอ Flash (Upload) เสร็จแล้วก็จะได้ผลลัพธ์แบบนี้คร้าบบ..!?!!

ผลลัพธ์

จบบทความนี้แล้วครับ แล้วเจอกันใหม่นะครับ สวัสดีคร้าบบ..!?

วิดีโอที่เกี่ยวข้อง:

--

--