Skip to content

Commit 38ea935

Browse files
committed
demo code upload
1 parent 5a3e5bf commit 38ea935

File tree

196 files changed

+26341
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

196 files changed

+26341
-0
lines changed

Projects/PubSub/B-U585I-IOT02A/.cproject

Lines changed: 198 additions & 0 deletions
Large diffs are not rendered by default.
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
/**
2+
******************************************************************************
3+
* @file : esp32.h
4+
* @version : v 1.0.0
5+
* @brief : This file implements esp32 mini driver header
6+
*/
7+
#include "main.h"
8+
9+
#ifndef ESP32_H_
10+
#define ESP32_H_
11+
12+
#define ESP32_BOOT_DELAY 3000
13+
#define ESP32_COMMAND_TIMEOUT 3000
14+
15+
16+
char* esp32_reset (void);
17+
char* esp32_mode (void);
18+
char* esp32_setwifi (char*, char*);
19+
char* esp32_sntp (char* sntp_server);
20+
char* esp32_mqtt_cfg (void);
21+
char* esp32_mqtt_username (char* mqtt_username);
22+
char* esp32_mqtt_password (void);
23+
char* esp32_mqtt_client_id (char* mqtt_client_id);
24+
char* esp32_mqtt_connect (char* mqtt_host);
25+
char* esp32_at (void);
26+
char* esp32_mqtt_clean (void);
27+
char* esp32_mqtt_conn (void);
28+
char* esp32_mqtt_pub (char* topic, char* data);
29+
char* esp32_mqtt_pub_raw (char* topic, uint8_t length);
30+
char* esp32_mqtt_send_raw_data (char* data);
31+
#endif /* ESP32_H_ */
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
/* USER CODE BEGIN Header */
2+
/**
3+
******************************************************************************
4+
* @file : main.h
5+
* @brief : Header for main.c file.
6+
* This file contains the common defines of the application.
7+
******************************************************************************
8+
* @attention
9+
*
10+
* Copyright (c) 2023 STMicroelectronics.
11+
* All rights reserved.
12+
*
13+
* This software is licensed under terms that can be found in the LICENSE file
14+
* in the root directory of this software component.
15+
* If no LICENSE file comes with this software, it is provided AS-IS.
16+
*
17+
******************************************************************************
18+
*/
19+
/* USER CODE END Header */
20+
21+
/* Define to prevent recursive inclusion -------------------------------------*/
22+
#ifndef __MAIN_H
23+
#define __MAIN_H
24+
25+
#ifdef __cplusplus
26+
extern "C" {
27+
#endif
28+
29+
/* Includes ------------------------------------------------------------------*/
30+
#include "stm32u5xx_hal.h"
31+
#include "stm32u5xx_ll_icache.h"
32+
#include "stm32u5xx_ll_pwr.h"
33+
#include "stm32u5xx_ll_crs.h"
34+
#include "stm32u5xx_ll_rcc.h"
35+
#include "stm32u5xx_ll_bus.h"
36+
#include "stm32u5xx_ll_system.h"
37+
#include "stm32u5xx_ll_exti.h"
38+
#include "stm32u5xx_ll_cortex.h"
39+
#include "stm32u5xx_ll_utils.h"
40+
#include "stm32u5xx_ll_dma.h"
41+
#include "stm32u5xx_ll_gpio.h"
42+
#include "stm32u5xx_ll_lpgpio.h"
43+
44+
/* Private includes ----------------------------------------------------------*/
45+
/* USER CODE BEGIN Includes */
46+
47+
/* USER CODE END Includes */
48+
49+
/* Exported types ------------------------------------------------------------*/
50+
/* USER CODE BEGIN ET */
51+
52+
/* USER CODE END ET */
53+
54+
/* Exported constants --------------------------------------------------------*/
55+
/* USER CODE BEGIN EC */
56+
57+
/* USER CODE END EC */
58+
59+
/* Exported macro ------------------------------------------------------------*/
60+
/* USER CODE BEGIN EM */
61+
62+
/* USER CODE END EM */
63+
64+
/* Exported functions prototypes ---------------------------------------------*/
65+
void Error_Handler(void);
66+
67+
/* USER CODE BEGIN EFP */
68+
69+
/* USER CODE END EFP */
70+
71+
/* Private defines -----------------------------------------------------------*/
72+
#define ExpressLink_EVENT_Pin LL_GPIO_PIN_15
73+
#define ExpressLink_EVENT_GPIO_Port GPIOD
74+
#define LD2_Pin LL_GPIO_PIN_13
75+
#define LD2_GPIO_Port GPIOE
76+
77+
#define ESP32_USART_HANDLER huart3
78+
79+
/* USER CODE BEGIN Private defines */
80+
81+
/* USER CODE END Private defines */
82+
83+
#ifdef __cplusplus
84+
}
85+
#endif
86+
87+
#endif /* __MAIN_H */
Lines changed: 205 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,205 @@
1+
/**
2+
******************************************************************************
3+
* @file : ExpressLink.c
4+
* @version : v 1.0.0
5+
* @brief : This file implements AWS ExpressLink driver
6+
******************************************************************************
7+
* @attention
8+
*
9+
* <h2><center>&copy; Copyright (c) 2019 STMicroelectronics.
10+
* All rights reserved.</center></h2>
11+
*
12+
* This software component is licensed by ST under BSD 3-Clause license,
13+
* the "License"; You may not use this file except in compliance with the
14+
* ththe "License"; You may not use this file except in compliance with the
15+
* opensource.org/licenses/BSD-3-Clause
16+
*
17+
******************************************************************************
18+
*/
19+
#include "main.h"
20+
#include "esp32.h"
21+
22+
#include <stdio.h>
23+
#include <string.h>
24+
#include <stdlib.h>
25+
#include <ctype.h>
26+
27+
#define ESP32_TX_BUFFER_SIZE 256
28+
#define ESP32_RX_BUFFER_SIZE 256
29+
30+
/*---------- ESP32_USART_HANDLER -----------*/
31+
extern UART_HandleTypeDef ESP32_USART_HANDLER;
32+
33+
/*---------- ESP32_DEBUG -----------*/
34+
#define ESP32_DEBUG 1
35+
36+
37+
38+
static char command[ESP32_TX_BUFFER_SIZE] = { 0 };
39+
static char response[ESP32_RX_BUFFER_SIZE] = { 0 };
40+
41+
42+
/******************************************************************************/
43+
/**
44+
* @brief Send a command to the module
45+
* @retval The ExpressLink Module response
46+
*/
47+
static char* esp32_execute_command(char *command, unsigned long timeout_ms)
48+
{
49+
int i = 0;
50+
int command_flag = 0;
51+
int command_size = strlen(command);
52+
if (command_size > 255) {
53+
printf("AT COMMAND is over size!\r\n");
54+
return response;
55+
}
56+
57+
if ((strcmp(command, "AT+RST\r\n") == 0)) {
58+
command_flag = 1; //RST command
59+
}
60+
else if ((strncmp(command, "AT+CIPSNTPCFG", 13) == 0)) {
61+
command_flag = 2; //SNTP command
62+
}
63+
else if ((strncmp(command, "AT+MQTTPUBRAW", 13) == 0)) {
64+
command_flag = 3;
65+
}
66+
67+
HAL_StatusTypeDef USART_STATUS = HAL_OK;
68+
69+
//flush UART
70+
while (HAL_TIMEOUT != HAL_UART_Receive(&ESP32_USART_HANDLER, (uint8_t* )&response[0], ESP32_TX_BUFFER_SIZE, 50)) {
71+
//do nothing
72+
}
73+
74+
memset(response, 0, ESP32_RX_BUFFER_SIZE);
75+
76+
#if 0
77+
printf("%s", command);
78+
#endif
79+
80+
HAL_UART_Transmit(&ESP32_USART_HANDLER, (uint8_t* )command, command_size, timeout_ms);
81+
82+
if (!command_flag) { //command flag is 0
83+
do
84+
{
85+
USART_STATUS = HAL_UART_Receive(&ESP32_USART_HANDLER, (uint8_t* )&response[i], 1, timeout_ms);
86+
i++;
87+
88+
if (i > 3) {
89+
if ((response[i - 1] == '\n') && (response[i - 2] == '\r') && (response[i - 3] == 'K') && (response[i - 4] == 'O')) {
90+
break; //\r\nOK\r\n
91+
}
92+
else if ((response[i - 1] == '\n') && (response[i - 2] == '\r') && (response[i - 3] == 'R') && (response[i - 4] == 'O')) {
93+
break; //\r\nERROR\r\n
94+
}
95+
}
96+
} while ((USART_STATUS != HAL_TIMEOUT));
97+
}
98+
else { //command_flag is 1 or 2 or 3
99+
do
100+
{
101+
USART_STATUS = HAL_UART_Receive(&ESP32_USART_HANDLER, (uint8_t* )&response[i], 1, timeout_ms);
102+
i++;
103+
104+
if (i > 3) {
105+
if ((response[i - 1] == '\n') && (response[i - 2] == '\r') && (response[i - 3] == 'P') && (response[i - 4] == 'I')) {
106+
if (command_flag == 1) {
107+
//printf("i is %d and GOT IP\r\n", i);
108+
break;
109+
}
110+
}
111+
else if ((response[i - 1] == '\n') && (response[i - 2] == '\r') && (response[i - 3] == 'D') && (response[i - 4] == 'E')) {
112+
if (command_flag == 2) {
113+
//printf("i is %d and UPDATED\r\n", i);
114+
break;
115+
}
116+
}
117+
else if ((response[i - 1] == '>') && (response[i - 2] == '\n') && (response[i - 3] == '\r')) {
118+
if (command_flag == 3) {
119+
//printf("i is %d and >>>>>\r\n", i);
120+
break;
121+
}
122+
}
123+
}
124+
} while ((USART_STATUS != HAL_TIMEOUT));
125+
}
126+
if(USART_STATUS == HAL_TIMEOUT)
127+
{
128+
memset (response, 0, ESP32_RX_BUFFER_SIZE);
129+
snprintf(response, ESP32_TX_BUFFER_SIZE, "ERROR\r\n");
130+
}
131+
132+
#if (ESP32_DEBUG)
133+
printf("%s", response);
134+
#endif
135+
return response;
136+
}
137+
138+
139+
//----------ESP32--------------
140+
char* esp32_at(void) {
141+
return esp32_execute_command("AT\r\n", ESP32_COMMAND_TIMEOUT);
142+
}
143+
char* esp32_reset(void) {
144+
return esp32_execute_command("AT+RST\r\n", 20000);
145+
}
146+
147+
char* esp32_mode(void) {
148+
return esp32_execute_command("AT+CWMODE=1\r\n", ESP32_COMMAND_TIMEOUT);
149+
}
150+
151+
char* esp32_setwifi(char* ssid, char* pw) {
152+
snprintf(command, ESP32_TX_BUFFER_SIZE, "AT+CWJAP=\"%s\",\"%s\"\r\n", ssid, pw);
153+
return esp32_execute_command(command, 5 * ESP32_COMMAND_TIMEOUT);
154+
}
155+
156+
char* esp32_sntp(char* sntp_server) {
157+
snprintf(command, ESP32_TX_BUFFER_SIZE, "AT+CIPSNTPCFG=1,-6,\"%s\"\r\n", sntp_server);
158+
esp32_execute_command(command, 20000);
159+
return esp32_execute_command("AT+CIPSNTPTIME?\r\n", ESP32_COMMAND_TIMEOUT);
160+
}
161+
162+
char* esp32_mqtt_cfg(void) {
163+
return esp32_execute_command("AT+MQTTUSERCFG=0,5,\"\",\"\",\"\",0,0,\"\"\r\n", ESP32_COMMAND_TIMEOUT);
164+
}
165+
166+
char* esp32_mqtt_username(char* mqtt_username) {
167+
snprintf(command, ESP32_TX_BUFFER_SIZE, "AT+MQTTUSERNAME=0,\"%s\"\r\n", mqtt_username);
168+
return esp32_execute_command(command, ESP32_COMMAND_TIMEOUT);
169+
}
170+
171+
char* esp32_mqtt_password(void) {
172+
return esp32_execute_command("AT+MQTTPASSWORD=0,\"\"\r\n", ESP32_COMMAND_TIMEOUT);
173+
}
174+
175+
char* esp32_mqtt_client_id(char* mqtt_client_id) {
176+
snprintf(command, ESP32_TX_BUFFER_SIZE, "AT+MQTTCLIENTID=0,\"%s\"\r\n", mqtt_client_id);
177+
return esp32_execute_command(command, ESP32_COMMAND_TIMEOUT);
178+
}
179+
180+
char* esp32_mqtt_connect(char* mqtt_host) {
181+
snprintf(command, ESP32_TX_BUFFER_SIZE, "AT+MQTTCONN=0,\"%s\",8883,1\r\n", mqtt_host);
182+
return esp32_execute_command(command, 10 * ESP32_COMMAND_TIMEOUT);
183+
}
184+
185+
char* esp32_mqtt_clean(void) {
186+
return esp32_execute_command("AT+MQTTCLEAN=0\r\n", 3 * ESP32_COMMAND_TIMEOUT);
187+
}
188+
189+
char* esp32_mqtt_conn(void) {
190+
return esp32_execute_command("AT+MQTTCONN?\r\n", 3 * ESP32_COMMAND_TIMEOUT);
191+
}
192+
193+
char* esp32_mqtt_pub(char* topic, char* data) {
194+
snprintf(command, ESP32_TX_BUFFER_SIZE, "AT+MQTTPUB=0,\"%s\",\"%s\",1,0\r\n", topic, data);
195+
return esp32_execute_command(command, 5 * ESP32_COMMAND_TIMEOUT);
196+
}
197+
198+
char* esp32_mqtt_pub_raw(char* topic, uint8_t length) {
199+
snprintf(command, ESP32_TX_BUFFER_SIZE, "AT+MQTTPUBRAW=0,\"%s\",%d,1,0\r\n", topic, length);
200+
return esp32_execute_command(command, 2 * ESP32_COMMAND_TIMEOUT);
201+
}
202+
char* esp32_mqtt_send_raw_data(char* data) {
203+
strcpy(command, data);
204+
return esp32_execute_command(command, 4 * ESP32_COMMAND_TIMEOUT);
205+
}

0 commit comments

Comments
 (0)