2025-04-14 18:35:28 +08:00
/******************************************************************************
* @ file drv_qfc41d . c
* @ brief drv_qfc41d driver
* @ version V1 .0
* @ author Gary
* @ copyright
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * */
# include "drv_qfc41d.h"
uint8_t rev_buff [ QFC41D_MAX_RECV_SIZE ] = { 0 } ;
uint8_t send_buff [ QFC41D_MAX_SEND_SIZE ] = { 0 } ;
2025-04-15 19:17:47 +08:00
char wifiName [ 40 ] = " bluesun-ess-XJ-5G " , wifiPassWord [ 40 ] = " bluesun007 " , bleName [ 40 ] = " bsm1 " ;
2025-04-14 18:35:28 +08:00
char cmd [ 100 ] = { 0 } ; //<2F> <> <EFBFBD> <EFBFBD> ջ<EFBFBD> ռ <EFBFBD> ʹ <EFBFBD> <CAB9>
//<2F> <> <EFBFBD> <EFBFBD>
WifiBleItem qfc41dItem = {
. dev = QFC41D_UART_PORT ,
. buf_pos = 0 ,
. buf_size = QFC41D_MAX_RECV_SIZE ,
. buf = rev_buff ,
. sendCall = NULL ,
} ;
static void drv_qfc41d_push_data ( WifiBleItem * item , uint8_t * buf , uint16_t len )
{
uint16_t i = 0 ;
if ( ( item ! = NULL ) & & ( item - > buf_pos + len < item - > buf_size ) )
{
item - > buf [ item - > buf_pos + + ] = buf [ i ] ;
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> з<EFBFBD> <D0B7> <EFBFBD> <EFBFBD> <EFBFBD> ʾ һ <CABE> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> AT ָ<> <EFBFBD> <EEB7B5>
if ( buf [ i ] = = ' \n ' )
{
item - > buf [ item - > buf_pos ] = ' \0 ' ; // <20> <> <EFBFBD> ӽ<EFBFBD> <D3BD> <EFBFBD> <EFBFBD> <EFBFBD>
item - > buf_pos = 0 ; // <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <D7BC> <EFBFBD> <EFBFBD> һ <EFBFBD> <D2BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
}
}
}
static void drv_qf41d_uart_rx_it_call ( kit_ret_e res , void * data )
{
OS_TRACE_ISR_ENTER ( ) ;
drv_qfc41d_push_data ( & qfc41dItem , data , 1 ) ;
OS_TRACE_ISR_EXIT ( ) ;
}
// <20> <> <EFBFBD> ͵<EFBFBD> <CDB5> <EFBFBD> <EFBFBD> ַ<EFBFBD>
void drv_wireless_send_char ( char c )
{
while ( ! ( USART2 - > SR & USART_SR_TXE ) ) ; // <20> ȴ<EFBFBD> <C8B4> <EFBFBD> <EFBFBD> ͻ<EFBFBD> <CDBB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
USART2 - > DR = c ;
}
// <20> <> <EFBFBD> <EFBFBD> <EFBFBD> ַ<EFBFBD> <D6B7> <EFBFBD>
void drv_wireless_send_string ( const char * str )
{
while ( * str )
{
drv_wireless_send_char ( * str + + ) ;
}
drv_wireless_send_char ( ' \r ' ) ; // <20> <> <EFBFBD> <EFBFBD> CR
drv_wireless_send_char ( ' \n ' ) ; // <20> <> <EFBFBD> <EFBFBD> LF
}
// <20> <> <EFBFBD> <EFBFBD> AT ָ<> <EFBFBD> ȴ<EFBFBD> <C8B4> <EFBFBD> <EFBFBD> <EFBFBD> ֵ
int drv_at_send_cmd ( const char * cmd , const char * response , uint32_t timeout )
{
memset ( qfc41dItem . buf , 0 , QFC41D_MAX_RECV_SIZE ) ;
qfc41dItem . buf_pos = 0 ; // <20> <> <EFBFBD> ս <EFBFBD> <D5BD> ջ<EFBFBD> <D5BB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
drv_wireless_send_string ( cmd ) ;
uint32_t start_time = kit_time_get_tick ( ) ; // <20> <> ȡ<EFBFBD> <C8A1> ǰʱ<C7B0> <CAB1>
while ( kit_time_get_tick ( ) - start_time < timeout )
{
if ( strstr ( ( char * ) qfc41dItem . buf , response ) )
{
return 1 ; // <20> ɹ<EFBFBD> ƥ<EFBFBD> ䷵<EFBFBD> <E4B7B5> ֵ
}
}
return 0 ; // <20> <> ʱδ<CAB1> յ<EFBFBD> <D5B5> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
}
static uint8_t drv_wifi_init ( )
{
// 1. <20> <> <EFBFBD> <EFBFBD> ģ<EFBFBD> 鲨<EFBFBD> <E9B2A8> <EFBFBD> <EFBFBD>
if ( ! drv_at_send_cmd ( QFC41D_SET_BATE , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " WiFi Module not responding! \r \n " ) ;
return 1 ;
}
// 2. <20> <> <EFBFBD> <EFBFBD> AT ָ<> <D6B8>
if ( ! drv_at_send_cmd ( " AT " , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " WiFi Module not responding! \r \n " ) ;
return 1 ;
}
// 3. <20> <> <EFBFBD> <EFBFBD> Wi-Fi Ϊ DHCPʹ <50> <CAB9>
if ( ! drv_at_send_cmd ( QFC41D_SET_DHCP_ENABLE , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to set WiFi mode! \r \n " ) ;
return 1 ;
}
// 4. <20> <> <EFBFBD> <EFBFBD> Wi-Fi
snprintf ( cmd , sizeof ( cmd ) , " AT+QSTAAPINFODEF=%s,%s " , wifiName , wifiPassWord ) ;
if ( ! drv_at_send_cmd ( cmd , " WIFI CONNECTED " , WIFI_TIMEOUT * 2 ) )
{
KIT_PRINTF ( " Failed to connect to WiFi! \r \n " ) ;
//return 1; //<2F> <> <EFBFBD> <EFBFBD> <F3B2BBB7> <EFBFBD> -<2D> <> ֹӰ<D6B9> <D3B0> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ִ<EFBFBD> <D6B4>
}
// 5. <20> <> ȡ IP <20> <> ַ
if ( ! drv_at_send_cmd ( " AT+QGETIP=station " , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to get IP address! \r \n " ) ;
//return 1; //<2F> <> <EFBFBD> <EFBFBD> <F3B2BBB7> <EFBFBD> -<2D> <> ֹӰ<D6B9> <D3B0> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ִ<EFBFBD> <D6B4>
}
KIT_PRINTF ( " WiFi Initialization Successful! \r \n " ) ;
return 0 ;
}
static uint8_t drv_ble_init ( )
{
// 1. <20> <> ʼ <EFBFBD> <CABC> ,<2C> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> Ϊ<EFBFBD> <CEAA> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
if ( ! drv_at_send_cmd ( " AT+QBLEINIT=2 " , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to power off BLE! \r \n " ) ;
return 1 ;
}
// 2. <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
snprintf ( cmd , sizeof ( cmd ) , " AT+QBLENAME=%s " , bleName ) ;
if ( ! drv_at_send_cmd ( cmd , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to set BLE name! \r \n " ) ;
return 1 ;
}
// 3. <20> <> <EFBFBD> ù㲥<C3B9> <E3B2A5> <EFBFBD> <EFBFBD>
if ( ! drv_at_send_cmd ( " AT+QBLEADVPARAM=2048,2048 " , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to enable BLE! \r \n " ) ;
return 1 ;
}
// 4. <20> <> <EFBFBD> ù㲥<C3B9> <E3B2A5> <EFBFBD> <EFBFBD>
if ( ! drv_at_send_cmd ( " AT+QBLEADVDATA=06094643343144 " , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to enable BLE! \r \n " ) ;
return 1 ;
}
// 5. <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
if ( ! drv_at_send_cmd ( " AT+QBLEGATTSSRV=FFF0 " , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to set BLE role! \r \n " ) ;
return 1 ;
}
// 6. <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ֵ
if ( ! drv_at_send_cmd ( " AT+QBLEGATTSSRV=FFF1 " , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to set BLE role! \r \n " ) ;
return 1 ;
}
// 7. <20> <> ʼ <EFBFBD> 㲥
if ( ! drv_at_send_cmd ( " AT+QBLEADVSTART " , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to start BLE advertising! \r \n " ) ;
return 1 ;
}
KIT_PRINTF ( " BLE Initialization Successful! \r \n " ) ;
return 0 ;
}
uint8_t drv_qfc41d_init ( void )
{
//<2F> <> ʼ <EFBFBD> <CABC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
drv_uart_init ( ( UartDev ) qfc41dItem . dev , 115200 , 0 | UART_CFG_STOP_BIT_1 , kGpioType_WIRELESSUart_Tx , kGpioType_WIRELESSUart_Rx ) ;
//<2F> <> <EFBFBD> ý<EFBFBD> <C3BD> ջص<D5BB> <D8B5> <EFBFBD> <EFBFBD> <EFBFBD>
drv_uart_set_interrupt ( QFC41D_UART_PORT , kUartInterrupt_Rx , APP_CFG_INT_PRIO_UART2_RX , drv_qf41d_uart_rx_it_call ) ; //#define SIM_UART_PORT kUartDev_2
//<2F> <> ʼ <EFBFBD> <CABC> wifi
if ( drv_wifi_init ( ) ! = 0 )
{
return 1 ;
}
/* <20> <> <EFBFBD> <EFBFBD> Ԥ<EFBFBD> <D4A4>
//<2F> <> ʼ <EFBFBD> <CABC> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> BLE<4C> <45>
if ( drv_ble_init ( ) ! = 0 )
{
return 1 ;
}
*/
return 0 ;
}
2025-04-15 19:17:47 +08:00
# define MQTT_BROKER "mqtt.47.120.14.45" // <20> <> <EFBFBD> <EFBFBD> MQTT <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ַ
# define MQTT_PORT 3011 // MQTT <20> ˿<EFBFBD>
2025-04-14 18:35:28 +08:00
# define MQTT_CLIENT_ID "fc41d_client" // <20> ͻ<EFBFBD> <CDBB> <EFBFBD> ID
# define MQTT_USER "user" // MQTT <20> û<EFBFBD> <C3BB> <EFBFBD>
# define MQTT_PASSWORD "password" // MQTT <20> <> <EFBFBD> <EFBFBD>
2025-04-15 19:17:47 +08:00
# define MQTT_TOPIC "aa" // <20> <> <EFBFBD> <EFBFBD> /<2F> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
2025-04-14 18:35:28 +08:00
# define MQTT_MESSAGE "Hello from FC41D!" // <20> <> <EFBFBD> ͵<EFBFBD> <CDB5> <EFBFBD> Ϣ
// <20> <> <EFBFBD> <EFBFBD> MQTT <20> <> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD>
static uint8_t drv_mqtt_connect ( )
{
//1<> <31> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> MQTT Э<> <D0AD> <EFBFBD> İ汾<C4B0> <E6B1BE> Ϊ V4
snprintf ( cmd , sizeof ( cmd ) , " AT+QMTCFG= \" version \" ,1,4 " ) ;
if ( ! drv_at_send_cmd ( cmd , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to set MQTT server! \r \n " ) ;
return 1 ;
}
//2<> <32> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> MQTT <20> ͻ<EFBFBD> <CDBB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ֮<EFBFBD> <D6AE> <EFBFBD> ĻỰ<C4BB> <E1BBB0> <EFBFBD> <EFBFBD>
snprintf ( cmd , sizeof ( cmd ) , " AT+QMTOPEN=1, \" %s \" ,%d " , MQTT_BROKER , MQTT_PORT ) ;
if ( ! drv_at_send_cmd ( cmd , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to set MQTT server! \r \n " ) ;
return 1 ;
}
//2<> <32> MQTT <20> ͻ<EFBFBD> <CDBB> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> ֮<EFBFBD> 佨<EFBFBD> <E4BDA8> MQTT <20> <> <EFBFBD> <EFBFBD> AT+QMTCONN=<clientID>,<client_identity>,<username>,<password>
snprintf ( cmd , sizeof ( cmd ) , " AT+QMTCONN=1,%s,%s,%s " , MQTT_CLIENT_ID , MQTT_USER , MQTT_PASSWORD ) ;
if ( ! drv_at_send_cmd ( cmd , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to set MQTT client credentials! \r \n " ) ;
return 1 ;
}
KIT_PRINTF ( " MQTT Connection Successful! \r \n " ) ;
return 0 ;
}
// <20> <> <EFBFBD> <EFBFBD> MQTT <20> <> <EFBFBD> <EFBFBD>
static uint8_t drv_mqtt_subscribe ( )
{
snprintf ( cmd , sizeof ( cmd ) , " AT+MQTTSUB=1,1, \" %s \" ,0 " , MQTT_TOPIC ) ;
if ( ! drv_at_send_cmd ( cmd , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to subscribe to topic! \r \n " ) ;
return 1 ;
}
KIT_PRINTF ( " Subscribed to MQTT topic successfully! \r \n " ) ;
return 0 ;
}
// <20> <> <EFBFBD> <EFBFBD> MQTT <20> <> Ϣ
/*
* AT + QMTPUB = < clientID > , < msgID > , < QoS > , < retain > , < topic > , < payload_len gth > , < payload >
* length <EFBFBD> <EFBFBD> Χ 0 ~ 1500
* str <EFBFBD> <EFBFBD> mqtt <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> <EFBFBD> json <EFBFBD> <EFBFBD>
*/
uint8_t drv_mqtt_publish ( char * str , uint16_t length )
{
snprintf ( cmd , sizeof ( cmd ) , " AT+QMTPUB=1,1,0,0,%s,%d,%s " , MQTT_TOPIC , length , str ) ;
if ( ! drv_at_send_cmd ( cmd , " OK " , WIFI_TIMEOUT ) )
{
KIT_PRINTF ( " Failed to publish message! \r \n " ) ;
return 1 ;
}
KIT_PRINTF ( " MQTT Message Published Successfully! \r \n " ) ;
return 0 ;
}
// MQTT <20> <> ʼ <EFBFBD> <CABC>
uint8_t drv_mqtt_init ( )
{
if ( drv_mqtt_connect ( ) ! = 0 )
{
return 1 ;
}
if ( drv_mqtt_subscribe ( ) ! = 0 )
{
return 1 ;
}
return 0 ;
}