Created
September 27, 2024 06:06
-
-
Save maxpromer/15e3bcb1e786eb726d7e4c5876afe978 to your computer and use it in GitHub Desktop.
IOXESP32 VC-02 Serial communication
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
#define RX_PIN (26) // กำหนดขาต่อ RX VC-02-Kit | |
#define TX_PIN (27) // กำหนดขาต่อ TX VC-02-Kit | |
#define LED_PIN (5) // กำหนดขาต่อ LED | |
void setup() { | |
pinMode(LED_PIN, OUTPUT); // กำหนดขา LED เป็น Digital Output | |
digitalWrite(LED_PIN, HIGH); // กำหนดลอจิก LED เป็น HIGH (ปิด LED) | |
Serial.begin(115200); // ใช้งาน Serial Monitor | |
Serial2.begin(115200, SERIAL_8N1, RX_PIN, TX_PIN); // ใช้งาน Serial2 | |
Serial2.setTimeout(50); // กำหนดเวลา Timeout เป็น 50 mS | |
Serial.println("Ready !"); // แสดงข้อความ Ready ! ใน Serial Monitor | |
} | |
void loop() { | |
if (Serial2.available()) { // ถ้ามีข้อมูลเข้ามาที่ Serial2 | |
String command_code = Serial2.readString(); // อ่านค่าจาก Serial2 | |
if (command_code == "\x5A\x00\x00\x00\x5A") { // ถ้าได้รับคำสั่ง Wake Up | |
Serial.println("Wake Up"); // แสดงข้อความ Wake Up ใน Serial Monitor | |
} else if (command_code == "\x5A\x01\x00\x00\x5B") { // ถ้าได้รับคำสั่ง Wake Up | |
Serial.println("Standby"); // แสดงข้อความ Wake Up ใน Serial Monitor | |
} else if (command_code == "\x5A\x27\x00\x00\x81") { // ถ้าได้รับคำสั่ง turn on the light | |
Serial.println("LED ON"); // แสดงข้อความ LED ON ใน Serial Monitor | |
digitalWrite(LED_PIN, LOW); // กำหนดลอจิก LED เป็น LOW (เปิด LED) | |
} else if (command_code == "\x5A\x28\x00\x00\x82") { // ถ้าได้รับคำสั่ง turn off the light | |
Serial.println("LED OFF"); // แสดงข้อความ LED OFF ใน Serial Monitor | |
digitalWrite(LED_PIN, HIGH); // กำหนดลอจิก LED เป็น HIGH (ปิด LED) | |
} else { // ถ้าไม่ตรงกับ if ใดเลยก่อนหน้านี้ | |
// แสดงข้อมูลที่ได้รับจาก Serial2 ในรูปเลขฐาน 16 ใน Serial Monitor | |
Serial.print("Unknow command : "); | |
for (uint8_t i=0;i<command_code.length();i++) { | |
Serial.print("0x"); | |
Serial.print((int) command_code.charAt(i), HEX); | |
Serial.print(" "); | |
} | |
Serial.println(); | |
} | |
} | |
delay(10); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment