-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathesp_sara_at.c
275 lines (240 loc) · 7.57 KB
/
esp_sara_at.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
#include "esp_sara_config.h"
#include "esp_sara_at.h"
#include "string.h"
#include "esp_err.h"
static const char *TAG = "SARA_AT";
static void esp_sara_read_at_response(void *param)
{
const char *TAG = "SARA_RX";
vTaskDelay(1000);
TickType_t start = xTaskGetTickCount();
while (1)
{
if (xSemaphoreTake(uart_semaphore, 1000) == pdTRUE)
{
int len = 0;
char message[SARA_BUFFER_SIZE] = {0};
while (1)
{
int length = 0;
ESP_ERROR_CHECK(uart_get_buffered_data_len(SARA_UART_NUM, (size_t *)&length));
if (length > 0)
{
char data[length + 1];
memset(data, 0, length + 2);
uart_read_bytes(SARA_UART_NUM, (uint8_t *)data, length, 10);
strcat(message, data);
len = strlen(message);
if ((message[len - 1] == '\n'))
{
break;
}
}
TickType_t now;
vTaskDelay(10 / portTICK_PERIOD_MS);
if ((now = xTaskGetTickCount()) - start > (1000 / portTICK_PERIOD_MS))
{
start = now;
break;
}
}
xSemaphoreGive(uart_semaphore);
if (len > 2)
{
SARA_AT_INFO("%d %s\n", len, message);
if (xQueueSendToBack(at_queue, (void *)&message, 1000) == pdFAIL)
{
SARA_AT_ERROR("Send to queue failed\n");
}
}
}
vTaskDelay(1 / portTICK_PERIOD_MS);
}
}
static esp_err_t esp_sara_wait_irc(int timeout)
{
const char *TAG = "SARA_IRC";
esp_err_t err = ESP_ERR_TIMEOUT;
int length = 0;
char message[SARA_BUFFER_SIZE];
memset(message, '\0', SARA_BUFFER_SIZE);
TickType_t now, start = xTaskGetTickCount();
while (1)
{
uart_get_buffered_data_len(SARA_UART_NUM, (size_t *)&length);
if (length > 0)
{
uint8_t data[64] = {'\0'};
uart_read_bytes(SARA_UART_NUM, (uint8_t *)data, length, timeout / portTICK_PERIOD_MS);
strcat(message, (const char *)data);
SARA_AT_INFO("%s", data);
if (message[strlen(message) - 1] == '\n')
{
if (strstr(message, "OK") != NULL)
{
err = ESP_OK;
break;
}
else if (strstr(message, "ERROR") != NULL)
{
err = ESP_FAIL;
break;
}
}
}
if ((now = xTaskGetTickCount()) - start > (timeout / portTICK_PERIOD_MS))
{
err = ESP_ERR_TIMEOUT;
break;
};
vTaskDelay(10 / portTICK_PERIOD_MS);
}
xSemaphoreGive(uart_semaphore);
message[strlen(message)] = '\0';
char *ch = strtok(message, "OK");
while (ch != NULL)
{
char rc[SARA_BUFFER_SIZE];
strcpy(rc, ch);
int len = strlen(rc);
if (len > 2)
{
rc[len] = '\0';
if (xQueueSendToBack(at_queue, (void *)&rc, 1000 / portTICK_PERIOD_MS) == pdFAIL)
{
SARA_AT_ERROR("Send to queue failed\n");
}
}
ch = strtok(NULL, "OK");
}
if (err != ESP_OK)
{
SARA_AT_ERROR("AT Command Error\n");
}
return err;
}
esp_err_t esp_sara_send_at_command(const char *command, int len, int timeout)
{
esp_err_t err = ESP_ERR_TIMEOUT;
if (xSemaphoreTake(uart_semaphore, timeout / portTICK_PERIOD_MS))
{
char *TAG = "SARA_TX";
uart_wait_tx_done(SARA_UART_NUM, timeout / portTICK_PERIOD_MS);
uart_write_bytes(SARA_UART_NUM, command, len);
SARA_AT_INFO("%s", command);
err = esp_sara_wait_irc(timeout);
}
return err;
}
void esp_sara_uart_init()
{
uart_config_t uart_config = {
.baud_rate = SARA_UART_BAUD,
.data_bits = UART_DATA_8_BITS,
.parity = UART_PARITY_DISABLE,
.stop_bits = UART_STOP_BITS_1,
.flow_ctrl = SARA_UART_HW_CONTROL,
.rx_flow_ctrl_thresh = 122,
};
ESP_ERROR_CHECK(uart_param_config(SARA_UART_NUM, &uart_config));
ESP_ERROR_CHECK(uart_set_pin(SARA_UART_NUM, SARA_UART_TX_PIN, SARA_UART_RX_PIN, SARA_UART_RTS_PIN, SARA_UART_DTR_PIN));
ESP_ERROR_CHECK(uart_driver_install(SARA_UART_NUM, SARA_BUFFER_SIZE * 2, SARA_BUFFER_SIZE, SARA_BUFFER_SIZE, NULL, 0));
at_queue = xQueueCreate(10, SARA_BUFFER_SIZE);
uart_semaphore = xSemaphoreCreateMutex();
xTaskCreate(esp_sara_read_at_response, "sara_rx_task", 4096, NULL, 4, &uart_task_handle);
SARA_AT_INFO("esp_sara_uart_client_handle_t init");
}
esp_err_t esp_sara_disable_echo()
{
return esp_sara_send_at_command("ATE0\r\n", 7, 1000);
}
esp_err_t esp_sara_req_attach(bool attach)
{
char cmd[32];
int len = sprintf(cmd, "AT+CGATT=%d\r\n", attach);
return esp_sara_send_at_command(cmd, len, 60000);
}
esp_err_t esp_sara_check_signal()
{
const char *cmd = "AT+CSQ\r\n";
return esp_sara_send_at_command(cmd, strlen(cmd), 1000);
}
esp_err_t esp_sara_check_modem()
{
const char *cmd = "AT\r\n";
return esp_sara_send_at_command(cmd, strlen(cmd), 1000);
}
esp_err_t esp_sara_check_sim()
{
const char *cmd = "AT+CPIN?\r\n";
return esp_sara_send_at_command(cmd, strlen(cmd), 1000);
}
esp_err_t esp_sara_is_connected()
{
const char *cmd = "AT+CGATT?\r\n";
return esp_sara_send_at_command(cmd, strlen(cmd), 1000);
}
esp_err_t esp_sara_set_apn(const char *apn)
{
char cmd[64];
int len = sprintf(cmd, "AT+CGDCONT=1,\"IP\",\"%s\"\r\n", apn);
return esp_sara_send_at_command(cmd, len, 1000);
}
esp_err_t esp_sara_set_rat(int rat)
{
char cmd[32];
int len = sprintf(cmd, "AT+URAT=%d\r\n", rat);
return esp_sara_send_at_command(cmd, len, 1000);
}
esp_err_t esp_sara_set_function(int fun)
{
char cmd[32];
int len = sprintf(cmd, "AT+CFUN=%d\r\n", fun);
return esp_sara_send_at_command(cmd, len, 1000);
}
esp_err_t esp_sara_set_mqtt_client_id(const char *client_id)
{
char cmd[64];
int len = sprintf(cmd, "AT+UMQTT=0,\"%s\"\r\n", client_id);
return esp_sara_send_at_command(cmd, len, 1000);
}
esp_err_t esp_sara_set_mqtt_server(const char *server, int port)
{
char cmd[64];
int len = sprintf(cmd, "AT+UMQTT=3,\"%s\",%d\r\n", server, port);
return esp_sara_send_at_command(cmd, len, 1000);
}
esp_err_t esp_sara_set_mqtt_timeout(uint16_t timeout)
{
char cmd[64];
int len = sprintf(cmd, "AT+UMQTT=10,%d\r\n", timeout);
return esp_sara_send_at_command(cmd, len, 1000);
}
esp_err_t esp_sara_set_clean_session(bool session)
{
char cmd[64];
int len = sprintf(cmd, "AT+UMQTT=12,%d\r\n", session);
return esp_sara_send_at_command(cmd, len, 1000);
}
esp_err_t esp_sara_set_mqtt_auth(const char *username, const char *password)
{
char cmd[128];
int len = sprintf(cmd, "AT+UMQTT=4,\"%s\",\"%s\"\r\n", username, password);
return esp_sara_send_at_command(cmd, len, 1000);
}
esp_err_t esp_sara_ping_mqtt_server(const char *server)
{
char cmd[64];
int len = sprintf(cmd, "AT+UMQTTC=8,\"%s\"\r\n", server);
return esp_sara_send_at_command(cmd, len, 60000);
}
esp_err_t esp_sara_mqtt_read_message()
{
const char *cmd = "AT+UMQTTC=6\r\n";
return esp_sara_send_at_command(cmd, strlen(cmd), 1000);
}
esp_err_t esp_sara_get_mqtt_error()
{
const char *cmd = "AT+UMQTTER\r\n";
return esp_sara_send_at_command(cmd, strlen(cmd), 1000);
}