# ESP32

1. **Kết nối tới Innoway sử dụng WiFi**

**WIFI INITIAL STATION**

Khởi tạo WiFi ở chế độ station. Thay đổi ssid, password cho phù hợp với WiFi đang sử dụng

```
wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .password = EXAMPLE_ESP_WIFI_PASS,
	    .threshold.authmode = WIFI_AUTH_WPA2_PSK,
        },
};
```

**MQTT INNOWAY START**

Hàm sử dụng để kết nối tới Innoway và sẽ trả về MQTT client khi kết nối thành công. Hàm này được sử dụng sau khi khởi tạo WiFi thành công, gồm có các parameter sau:

```c
esp_mqtt_client_handle_t mqtt_innoway_start(char *client_id, char *passowrd,  mqtt_innoway_callback user_callback)
```

1. Client Id: Id của client sử dụng khi kết nối tới Innoway
2. Password: Lấy từ device Token trên Innoway
3. MQTT Innoway Callback: Hàm callback được gọi tới khi có các sự kiện của Innoway

Ví dụ:

<pre class="language-c"><code class="lang-c"><strong>esp_mqtt_client_handle_t Innoway_Mqtt_client;
</strong>Innoway_Mqtt_client = mqtt_innoway_start(deviceID, deviceToken, Innoway_MQTT_event_callback);
</code></pre>

**MQTT INNOWAY PUBLISH**

Được sử dụng để publish data tới Innoway theo Topic mong muốn:

```c
mqtt_innoway_publish(esp_mqtt_client_handle_t client, const char *topic, const char *data, int len, int qos, int retain)
```

**MQTT INNOWAY SUBCRIBE**

Subcribe topic tới Innoway và bắt đầu nhận bản tin theo topic này

```c
int mqtt_innoway_subscribe(esp_mqtt_client_handle_t client, const char *topic, int qos);
```

**MQTT INNOWAY UNSUBCRIBE**

Unsubcribe 1 topic đã subcribe từ trước, dừng việc nhận bản tin theo topic này

```c
int mqtt_innoway_unsubscribe(esp_mqtt_client_handle_t client, const char *topic);
```

**MQTT INNOWAY DISCONNECT**

Sử dụng để disconnect tới Innoway&#x20;

```c
esp_err_t mqtt_innoway_disconnect(esp_mqtt_client_handle_t client);
```

**MQTT INNOWAY RECONNECT**

Reconnect lại tới innoway khi chủ động ngắt kết nối bằng mqtt\_innoway\_disconnect, tuy nhiên vẫn chưa sử dụng mqtt\_innoway\_stop. Sử dụng để reconnect nếu mất kết nối với Innoway mà không cần cấu hình lại các tham số của client.

```c
esp_err_t mqtt_innoway_reconnect(esp_mqtt_client_handle_t client);
```

**MQTT INNOWAY STOP**

Dừng kết nối tới Innoway, tuy nhiên cần phải sử dụng mqtt\_innoway\_disconnect để ngắt kết nối trước, nếu không có thể sẽ xảy ra lỗi.

```c
esp_err_t mqtt_innoway_stop(esp_mqtt_client_handle_t client);
```

2. **Kết nối tới Innoway sử dụng module sim4G (SIM7600)**

**SIMCOM INNOWAY INIT**

Khởi tạo cấu hình kết nối tới module sim của ESP32, lựa chọn UART, chân RX, TX

```c
esp_err_t simcom_innoway_init(uart_port_t uart_num, int tx_io_num, int rx_io_num);
```

**SIMCOM INNOWAY REGISTERED**

Kiểm tra module sim đã có kết nối mạng chưa, nếu đã có kết nối thì trả về True, còn nếu chưa thì trả về là FLASE

```c
bool simcom_is_registered();
```

**SIMCOM INNOWAY START**

Hàm sử dụng để kết nối tới Innoway và sẽ trả về True khi kết nối thành công và False khi kết nối thất bại. Hàm này được sử dụng sau khi module sim đã kết nối vào mạng, bên cạnh đó là khởi tạo cấu hình cho client MQTT

```c
simcom_innoway_mqtt_client_t client = {
	.client_id = DEVICE_ID,
	.host = BROKER,
	.keepalive = 60,
	.index = 0,
	.msg_id = 1,
	.password = DEVICE_TOKEN,
	.port = 1883,
	.tcp_connect_id = 1,
	.username = USERNAME,
	.sim_mqtt_innoway_event_handler = simcom_innoway_mqtt_event_handler,
};
```

Trong đó password sẽ là DEVICE\_TOKEN lấy từ Innoway. Simcom\_innoway\_mqtt\_event\_handler là hàm handler sự kiện của MQTT innoway.&#x20;

```c
bool simcom_innoway_mqtt_start(simcom_innoway_mqtt_client_t client);
```

Ví du:&#x20;

```c
simcom_innoway_mqtt_client_t client = {
	.client_id = DEVICE_ID,
	.host = BROKER,
	.keepalive = 60,
	.index = 0,
	.msg_id = 1,
	.password = DEVICE_TOKEN,
	.port = 1883,
	.tcp_connect_id = 1,
	.username = USERNAME,
	.sim_mqtt_innoway_event_handler = simcom_innoway_mqtt_event_handler,
};
simcom_innoway_mqtt_start(client);
```

**SIMCOM INNOWAY STOP**

Sử dụng đóng kết nối với innoway, trong hàm này đã bao gồm việc chủ động dừng kết nối với innoway trước khi đóng kết nối. Do việc đóng kết nối cần phải bắt đầu bằng việc disconnect, sau đó mới tới stop.

```c
bool simcom_innoway_mqtt_stop(simcom_innoway_mqtt_client_t client);
```

**SIMCOM INNOWAY SUBCRIBE**

Subcribe topic tới Innoway và bắt đầu nhận bản tin theo topic này

```c
bool simcom_innoway_mqtt_subscribe(simcom_innoway_mqtt_client_t client, const char* topic, uint8_t qos);
```

**SIMCOM INNOWAY UNSUBCRIBE**

Unsubcribe 1 topic đã subcribe từ trước, dừng việc nhận bản tin theo topic này

```c
bool simcom_innoway_mqtt_unsubscribe(simcom_innoway_mqtt_client_t client, const char* topic);
```

**SIMCOM INNOWAY PUBLISH**

Được sử dụng để publish data tới Innoway theo Topic mong muốn:

```c
bool simcom_innoway_mqtt_publish(simcom_innoway_mqtt_client_t client, const char* topic, const char* data, int len, uint8_t qos, int retain);
```

**SIMCOM INNOWAY DISCONNECT**

Sử dụng để disconnect tới Innoway trả về True nếu disconnect thành công, ngược lại là false

```c
bool simcom_innoway_mqtt_disconnect(simcom_innoway_mqtt_client_t client);
```

**SIMCOM INNOWAY RECONNECT**

Sử dụng để reconnect nếu mất kết nối với Innoway mà không cần cấu hình lại các tham số của client

```c
bool simcom_innoway_mqtt_reconnect(simcom_innoway_mqtt_client_t client);
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://innoway.gitbook.io/innoway/tai-lieu/sdk/esp32.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
