NguyenKhacSon 231432336 — ESP32 simulation

An interactive ESP32 circuit simulation you can run free in your browser on Velxio, by nks260805.

Components

Sketch code

#include <Arduino.h>
#include <WiFi.h>
#include <HTTPClient.h>

// --- CẤU HÌNH CHÂN UART2 ---
#define RX2_PIN 16
#define TX2_PIN 17

// --- CẤU HÌNH WIFI & THINGSBOARD CLOUD ---
const char* WIFI_SSID = "Velxio-GUEST";
const char* WIFI_PASS = "";
const char* TB_TOKEN = "6drdNfDYaLxz6pBV9OR0";
String tbUrl = "http://thingsboard.cloud/api/v1/" + String(TB_TOKEN) + "/telemetry";

// --- MÁY TRẠNG THÁI BỘ ĐỌC KHUNG UART ---
enum RxParserState {
  WAIT_HEADER_1, // Chờ byte 0xFD
  WAIT_HEADER_2, // Chờ byte 0xAD
  READ_PAYLOAD   // Đọc 8 byte dữ liệu float
};

RxParserState rxState = WAIT_HEADER_1;
uint8_t payloadBuffer[8];
int payloadIndex = 0;

// Cờ báo có dữ liệu mới cần đẩy lên ThingsBoard
bool hasNewTelemetry = false;
float latestTemp = 0.0;
float latestHum = 0.0;

// --- HÀM PHẢN HỒI GÓI BÁO NHẬN ACK (0xFD, 0xDE) ---
void sendAckPacket() {
  uint8_t ack[2] = {0xFD, 0xDE};
  Serial2.write(ack, 2);
  Serial2.flush(); // Đẩy ngay lập tức 2 byte ra khỏi bộ đệm phần cứng
  Serial.println("[Gateway B] -> Da gui phan hoi ACK (0xFD, 0xDE) ve Thiet bi A!");
}

// --- HÀM ĐẨY DỮ LIỆU LÊN THINGSBOARD CLOUD ---
void uploadToThingsBoard(float temperature, float humidity) {
  if (WiFi.status() != WL_CONNECTED) {
    Serial.println("[ThingsBoard] Chua co WiFi! Dang thu ket noi lai WiFi...");
    return;
  }

  HTTPClient http;
  http.begin(tbUrl);
  http.addHeader("Content-Type", "application/json");

  String jsonPayload = "{\"temperature\":" + String(temperature, 2) + 
                       ",\"humidity\":" + String(humidity, 2) + "}";

  Serial.println("[ThingsBoard] Dang day du lieu len Cloud: " + jsonPayload);
  int httpResponseCode = http.POST(jsonPayload);

  if (httpResponseCode == 200) {
    Serial.printf("[ThingsBoard] Gui THANH CONG! (HTTP Code: %d)\n\n", httpResponseCode);
  } else {
    Serial.printf("[ThingsBoard] Loi HTTP: %d (%s)\n\n", 
                  httpResponseCode, http.errorToString(httpResponseCode).c_str());
  }
  http.end();
}

void setup() {
  Serial.begin(115200);
  Serial2.begin(115200, SERIAL_8N1, RX2_PIN, TX2_PIN);

  Serial.println("=== THIET BI B (GATEWAY) SAN SANG NHAN UART ===");
  
  // Khởi động kết nối Wi-Fi ở chế độ chạy ngầm (Không dùng while chặn luồng)
  WiFi.mode(WIFI_STA);
  WiFi.begin(WIFI_SSID, WIFI_PASS);
}

void loop() {
  // 1. ƯU TIÊN SỐ 1: Đọc dữ liệu UART từ Thiết bị A và phản hồi ACK ngay lập tức
  while (Serial2.available() > 0) {
    uint8_t incomingByte = Serial2.read();

    switch (rxState) {
      case WAIT_HEADER_1:
        if (incomingByte == 0xFD) {
          rxState = WAIT_HEADER_2;
        }
        break;

      case WAIT_HEADER_2:
        if (incomingByte == 0xAD) {
          payloadIndex = 0;
          rxState = READ_PAYLOAD;
        } else if (incomingByte == 0xFD) {
          rxState = WAIT_HEADER_2;
        } else {
          rxState = WAIT_HEADER_1;
        }
        break;

      case READ_PAYLOAD:
        payloadBuffer[payloadIndex++] = incomingByte;
        
        if (payloadIndex >

More projects by nks260805