Skip to main content

code test c3 - A7600C


#include <HardwareSerial.h>
#define simSerial Serial0  // Sử dụng UART1 cho module SIM
#define MCU_SIM_BAUDRATE 115200
#define MCU_SIM_TX_PIN 21
#define MCU_SIM_RX_PIN 20
#define MCU_SIM_EN_PIN 9
#define MCU_SIM_LED_PIN 10
#define PHONE_NUMBER  "sđt"//sđt nhận

void sim_at_wait()
{
    delay(100);
    while (simSerial.available()) {
        Serial.write(simSerial.read());
    }
}
bool sim_at_cmd(String cmd){
    simSerial.println(cmd);
    sim_at_wait();
    return true;
}
bool sim_at_send(char c){
    simSerial.write(c);
    return true;
}
void sent_sms()
{
    sim_at_cmd("AT+CMGF=1");
    String temp = "AT+CMGS=\"";
    temp += (String)PHONE_NUMBER;
    temp += "\"";
    sim_at_cmd(temp);
    sim_at_cmd("Hi from LKTD");

    // End charactor for SMS
    sim_at_send(0x1A);
}

void call()
{
    String temp = "ATD";
    temp += PHONE_NUMBER;
    temp += ";";
    sim_at_cmd(temp); 

    delay(20000);

    // Hang up
    sim_at_cmd("ATH"); 
}

void setup() {
  // Kích hoạt nguồn cho module SIM
  pinMode(MCU_SIM_EN_PIN, OUTPUT);
  digitalWrite(MCU_SIM_EN_PIN, LOW);  // Bật nguồn cho module SIM
  pinMode(MCU_SIM_LED_PIN, OUTPUT);
  digitalWrite(MCU_SIM_LED_PIN, HIGH);  // Bật LED

  delay(20);
  Serial.begin(115200);  // UART0 cho giao tiếp với máy tính (debug và AT command)
  Serial.println("\n\n\n\n-----------------------\nSystem started!!!!");
  delay(12000);
  simSerial.begin(MCU_SIM_BAUDRATE, SERIAL_8N1, MCU_SIM_RX_PIN, MCU_SIM_TX_PIN);  // UART1 cho module SIM

  Serial.println("UART Bypass Mode: ESP32-C3 đang làm cầu nối giữa PC và module SIM");
  Serial.println("Gửi lệnh AT từ PC để giao tiếp trực tiếp với module SIM");
  // Check AT Command
    sim_at_cmd("AT");

    // Product infor
    sim_at_cmd("ATI");

    // Check SIM Slot
    sim_at_cmd("AT+CPIN?");

    // Check Signal Quality
    sim_at_cmd("AT+CSQ");

    sim_at_cmd("AT+CIMI");
   // sent_sms();

    // Delay 5s
    delay(15000);   

    call();
}

void loop() {
  // Chuyển tiếp dữ liệu từ máy tính tới module SIM
  if (Serial.available()) {
    char c = Serial.read();
    simSerial.write(c);  // Gửi dữ liệu từ PC đến module SIM
  }
  sim_at_wait();

  // Chuyển tiếp dữ liệu từ module SIM tới máy tính
  if (simSerial.available()) {
    char c = simSerial.read();
    Serial.write(c);  // Gửi dữ liệu từ module SIM về PC
  }
}