【官方】MCU通过WIFI模块与机智云通信
STM32CubeMX移植机智云自动生成代码STM32CubeMX配置GPIO口https://www.stmcu.com.cn/upload/20200103/36.png
https://www.stmcu.com.cn/upload/20200103/37.png
STM32CubeMX配置USART外设
https://www.stmcu.com.cn/upload/20200103/38.pngSTM32CubeMX配置1ms定时器https://www.stmcu.com.cn/upload/20200103/39.png
STM32CubeMX配置外部按键https://www.stmcu.com.cn/upload/20200103/40.png生成代码https://www.stmcu.com.cn/upload/20200103/41.png
移植代码从机智云“stm32f103c8x”平台生成的代码中复制Gizwits、Hal、Utils文件夹到当前目录:
https://www.stmcu.com.cn/upload/20200103/42.png
在工程中添加如下的目录结构:
https://www.stmcu.com.cn/upload/20200103/43.png
修改代码首先在gizwits_product.h / hal_key.h 中将原来F103C8平台的头文件 #include <stm32f1xx.h>替换为G071RB平台的 #include <stm32g0xx.h>然后修改如下代码:修改gizwits_product.c代码:
https://www.stmcu.com.cn/upload/20200103/44.pnghttps://www.stmcu.com.cn/upload/20200103/45.png
https://www.stmcu.com.cn/upload/20200103/46.png
https://www.stmcu.com.cn/upload/20200103/47.png
添加app.c代码:/* Includes ------------------------------------------------------------------*/
#include "app.h"
#include "sht30.h"
#include "gizwits_protocol.h"
#include "gizwits_product.h"
#include "hal_key.h"
#define GPIO_KEY_NUM 2 ///< Defines the total number of key member
#define KEY1_Pin GPIO_PIN_11
#define KEY1_GPIO_Port GPIOA
#define KEY2_Pin GPIO_PIN_4
#define KEY2_GPIO_Port GPIOA
///------------------- G0 TO F7 ------------------------
static short temperature = 0, humidity = 0;
float gizwits_temperature = 0, gizwits_humidity = 0;
uint8_t tui_buf;
#define FRM_START 0x68
#define FRM_CMD_UPD_SENSOR 0x01//温湿度传感器
#define FRM_CMD_UPD_RESTWIFI 0x02//复位WIFI
#define FRM_CMD_UPD_AIRLINK 0x03//配置WIFI到路由器
#define FRM_CMD_UPD_CONFIGEND 0x04//链接到路由器
#define FRM_FIXLEN 14
#define FRM_END0 0x5c
#define FRM_END1 0x6e
#define FRM_POS_START 0
#define FRM_POS_CMD 1
#define FRM_POS_LEN 2
#define FRM_POS_DATA 3
#define FRM_POS_CRC 11
#define FRM_POS_END0 12
#define FRM_POS_END1 13
keysTypedef_t keys;
keyTypedef_t singleKey; ///< Defines a single key member array pointer
static uint8_t s_wifi_router_status = 0;
static void update_to_ui_wifi_status(uint8_t cmd)
{
if(cmd==FRM_CMD_UPD_RESTWIFI||cmd==FRM_CMD_UPD_AIRLINK||cmd==FRM_CMD_UPD_CONFIGEND)
{
s_wifi_router_status = cmd;
}
}
void ConfigWIFIfinish(void)
{
update_to_ui_wifi_status(FRM_CMD_UPD_CONFIGEND);
}
void key1ShortPress(void)
{
GIZWITS_LOG("KEY1 PRESS ,Production Mode\n");
gizwitsSetMode(WIFI_PRODUCTION_TEST);
}
/**
* key1 long press handle
* @param none
* @return none
*/
void key1LongPress(void)
{
GIZWITS_LOG("KEY1 PRESS LONG ,Soft AP\n");
gizwitsSetMode(WIFI_SOFTAP_MODE);
}
/**
* key2 short press handle
* @param none
* @return none
*/
void key2ShortPress(void)
{
GIZWITS_LOG("------>KEY2 PRESS,Air Link mode\n\r");
#if !MODULE_TYPE
gizwitsSetMode(WIFI_AIRLINK_MODE);
update_to_ui_wifi_status(FRM_CMD_UPD_AIRLINK);
#endif
}
/**
* key2 long press handle
* @param none
* @return none
*/
void key2LongPress(void)
{
//AirLink mode
GIZWITS_LOG("KEY2 PRESS LONG ,Wifi Reset mode\n");
#if !MODULE_TYPE
gizwitsSetMode(WIFI_RESET_MODE);
update_to_ui_wifi_status(FRM_CMD_UPD_RESTWIFI);
#endif
}
void keyInit(void)
{
singleKey = keyInitOne(NULL, KEY1_GPIO_Port, KEY1_Pin, key1ShortPress, key1LongPress);
singleKey = keyInitOne(NULL, KEY2_GPIO_Port, KEY2_Pin, key2ShortPress, key2LongPress);
keys.singleKey = (keyTypedef_t *)&singleKey;
keyParaInit(&keys);
}
typedef struct __frame_sensor_to_ui{
uint8_t start ;
uint8_t cmd;
uint8_t len;
uint8_t data;
uint8_t crc;
uint8_t end ;
}_frame_sensor_to_ui;
_frame_sensor_to_ui s_frame_sensor_to_ui;
//eg:68 cmd len t t h h x x y y crc 16 / crc = 68 + cmd + len + t + t + h + h + x + x + y + y 16
extern UART_HandleTypeDef huart2;
uint8_t cal_crc(uint8_t *buf,uint8_t len)
{
uint8_t t_crc = 0;
uint8_t r_crc = 0;
//check crc
for(int i=0; i<len ;i++)
{
t_crc += buf;
}
r_crc = (uint8_t)t_crc;
return r_crc;
}
#define __DEBUG_UPDATE_UI_ARRAY__
void update_to_ui(uint8_t cmd,uint8_t *data)
{
uint8_t t_buf;
int i = 0;
memset(t_buf,0,sizeof(t_buf));
t_buf=FRM_START;
t_buf=cmd;
t_buf=FRM_FIXLEN;
t_buf= data;
t_buf= data;
t_buf= data;
t_buf= data;
t_buf= s_wifi_router_status; //wifi status update
t_buf= 0;
t_buf= 0;
t_buf= 0;
t_buf=cal_crc(t_buf,FRM_FIXLEN-3);
t_buf=FRM_END0;
t_buf=FRM_END1;
#ifdef __DEBUG_UPDATE_UI_ARRAY__
GIZWITS_LOG("update_to_ui:");
for(i=0; i<FRM_FIXLEN; i++)
{
GIZWITS_LOG("x ", t_buf);
}
GIZWITS_LOG("\n\r");
#endif
HAL_UART_Transmit(&huart2, t_buf, FRM_FIXLEN, 0xFFFF);
}
///------------------- APP ------------------------
extern int SHT3X_GetTempAndHumi(short *temp, short *humi);
void APP_Init(void)
{
s_wifi_router_status = 0;
SHT3X_Init();
timerInit();
uartInit();
userInit();
gizwitsInit();
keyInit();
GIZWITS_LOG("MCU Init Success \n");
printf("MCU HAL Success , SoftVersion = %s\r\n",SOFTWARE_VERSION);
Pro_D2W_Ask_Module_Reboot();
}
void APP_Process(void)
{
if(SHT3X_GetTempAndHumi(&temperature, &humidity)==0)
{
gizwits_temperature = temperature/10.00;
gizwits_humidity = humidity / 10.00;
tui_buf = ((temperature >> 8) & 0xff);
tui_buf = (temperature & 0xff);
tui_buf = ((humidity >> 8) & 0xff);
tui_buf = (humidity & 0xff);
update_to_ui(FRM_CMD_UPD_SENSOR,tui_buf);
//printf("temp=%0.1f,humi=%0.1f\r\n",gizwits_temperature,gizwits_humidity);
}
}修改main.c的代码
int main(void)
{
/* USER CODE BEGIN 1 */
unsigned int tmp, tick = 0;
/* USER CODE END 1 */
/* MCU Configuration--------------------------------------------------------*/
/* Reset of all peripherals, Initializes the Flash interface and the Systick. */
HAL_Init();
/* USER CODE BEGIN Init */
/* USER CODE END Init */
/* Configure the system clock */
SystemClock_Config();
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_USART2_UART_Init();
MX_TIM2_Init();
MX_USART1_UART_Init();
MX_USART3_UART_Init();
MX_USART4_UART_Init();
MX_I2C1_Init();
MX_IWDG_Init();
/* USER CODE BEGIN 2 */
APP_Init();
__HAL_IWDG_START(&hiwdg);
while(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_4)==GPIO_PIN_RESET);//等待F7输出的按键信号稳定
HAL_GPIO_WritePin(ESP_RST_GPIO_Port, ESP_RST_Pin, GPIO_PIN_SET);
/* USER CODE END 2 */
/* Infinite loop */
/* USER CODE BEGIN WHILE */
while (1)
{
/* USER CODE END WHILE */
/* USER CODE BEGIN 3 */
tmp = HAL_GetTick();
if(tmp - tick > 1000)
{
tick = tmp;
APP_Process();
userHandle();
__HAL_IWDG_RELOAD_COUNTER(&hiwdg);
//printf("APP_Process\n\r");
}
gizwitsHandle((dataPoint_t *)¤tDataPoint);
}
/* USER CODE END 3 */
}
请问这个然后应该怎么连接wifi呀 Thief 发表于 2021-9-2 20:59
请问这个然后应该怎么连接wifi呀
通过配网进行绑定 冯俊波 发表于 2021-9-3 17:55
通过配网进行绑定
请问有详细点的操作步骤吗,之前没有接触过WiFi模块是不是需要像蓝牙一样进行一些配置然后才可以进行连接谢谢
页:
[1]