mirror of
https://gitee.com/beecue/fastbee.git
synced 2025-12-18 08:55:53 +08:00
compile ok after transfering mqtt protocol
This commit is contained in:
@@ -1,8 +1,9 @@
|
||||
#include "esp8266.h"
|
||||
#include "usart.h"
|
||||
|
||||
extern UART_HandleTypeDef huart3;
|
||||
|
||||
STRUCT_USART_Fram_t ESP8266_Fram_Record_Struct;
|
||||
|
||||
struct STRUCT_USART_Fram ESP8266_Fram_Record_Struct= { 0 }; //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>һ<EFBFBD><D2BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>֡<EFBFBD>ṹ<EFBFBD><E1B9B9>
|
||||
void ESP8266_Init(uint32_t bound)
|
||||
{
|
||||
GPIO_InitTypeDef GPIO_InitStructure;
|
||||
@@ -17,7 +18,7 @@ void ESP8266_Init(uint32_t bound)
|
||||
// GPIO_InitStructure.Pin = ESP8266_CH_PD_Pin;
|
||||
// HAL_GPIO_Init(ESP8266_CH_PD_Pin_Port, &GPIO_InitStructure);
|
||||
//
|
||||
ESP8266_Rst();
|
||||
// ESP8266_Rst();
|
||||
}
|
||||
|
||||
//<2F><>ESP8266ģ<36>鷢<EFBFBD><E9B7A2>ATָ<54><D6B8>
|
||||
@@ -27,8 +28,11 @@ void ESP8266_Init(uint32_t bound)
|
||||
//<2F><><EFBFBD><EFBFBD>1<EFBFBD><31><EFBFBD>ͳɹ<CDB3><C9B9><EFBFBD> 0ʧ<30><CAA7>
|
||||
bool ESP8266_Send_AT_Cmd(char *cmd,char *ack1,char *ack2,uint32_t time)
|
||||
{
|
||||
ESP8266_Fram_Record_Struct.InfBit .FramLength = 0; //<2F><><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ݰ<EFBFBD>
|
||||
ESP8266_USART("%s\r\n", cmd);
|
||||
ESP8266_Fram_Record_Struct.InfBit.FramLength = 0; //<2F><><EFBFBD>½<EFBFBD><C2BD><EFBFBD><EFBFBD>µ<EFBFBD><C2B5><EFBFBD><EFBFBD>ݰ<EFBFBD>
|
||||
ESP8266_Fram_Record_Struct.InfBit.FramFinishFlag = 0;
|
||||
memset(ESP8266_Fram_Record_Struct.Data_RX_BUF, 0x00, sizeof(ESP8266_Fram_Record_Struct.Data_RX_BUF));
|
||||
|
||||
hal_AT_printf("%s\r\n", cmd);
|
||||
if(ack1==0&&ack2==0) //<2F><><EFBFBD><EFBFBD>Ҫ<EFBFBD><D2AA><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
return true;
|
||||
@@ -51,17 +55,17 @@ bool ESP8266_Send_AT_Cmd(char *cmd,char *ack1,char *ack2,uint32_t time)
|
||||
|
||||
}
|
||||
|
||||
/*-------------------------------------------------*/
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>WiFi<46><69>λ */
|
||||
/*<2A><> <20><><EFBFBD><EFBFBD>timeout<75><74><EFBFBD><EFBFBD>ʱʱ<CAB1>䣨100ms<6D>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>0<EFBFBD><30><EFBFBD><EFBFBD>ȷ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*-------------------------------------------------*/
|
||||
|
||||
//<2F><>λ<EFBFBD><CEBB><EFBFBD><EFBFBD>
|
||||
void ESP8266_Rst(void)
|
||||
char ESP8266_Rst(void)
|
||||
{
|
||||
// ESP8266_RST_Pin_SetL;
|
||||
// delay_ms(500);
|
||||
// ESP8266_RST_Pin_SetH;
|
||||
;
|
||||
return ESP8266_Send_AT_Cmd ( "AT+RST\r\n", "OK", 0, 5000 );
|
||||
}
|
||||
|
||||
|
||||
//<2F><><EFBFBD>ͻָ<CDBB><D6B8><EFBFBD><EFBFBD><EFBFBD>Ĭ<EFBFBD><C4AC><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ָ<EFBFBD>ģ<EEBDAB><C4A3><EFBFBD>ָ<EFBFBD><D6B8>ɳ<EFBFBD><C9B3><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
void ESP8266_AT_Test(void)
|
||||
{
|
||||
@@ -85,16 +89,15 @@ void ESP8266_ATE0(void)
|
||||
delay_ms(1000);
|
||||
while(count < 10)
|
||||
{
|
||||
if(ESP8266_Send_AT_Cmd("ATE0","OK",NULL,500))
|
||||
if(ESP8266_Send_AT_Cmd("ATE0","OK",NULL,1000))
|
||||
{
|
||||
printf("OK\r\n");
|
||||
printf("ATE0 OK\r\n");
|
||||
return;
|
||||
}
|
||||
++ count;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//ѡ<><D1A1>ESP8266<36>Ĺ<EFBFBD><C4B9><EFBFBD>ģʽ
|
||||
// enumMode ģʽ<C4A3><CABD><EFBFBD><EFBFBD>
|
||||
//<2F>ɹ<EFBFBD><C9B9><EFBFBD><EFBFBD><EFBFBD>true<75><65>ʧ<EFBFBD>ܷ<EFBFBD><DCB7><EFBFBD>false
|
||||
@@ -116,7 +119,6 @@ bool ESP8266_Net_Mode_Choose(ENUM_Net_ModeTypeDef enumMode)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
//ESP8266<36><36><EFBFBD><EFBFBD><EFBFBD>ⲿ<EFBFBD><E2B2BF>WIFI
|
||||
//pSSID WiFi<46>ʺ<EFBFBD>
|
||||
//pPassWord WiFi<46><69><EFBFBD><EFBFBD>
|
||||
@@ -126,7 +128,7 @@ bool ESP8266_JoinAP( char * pSSID, char * pPassWord)
|
||||
char cCmd [120];
|
||||
|
||||
sprintf ( cCmd, "AT+CWJAP=\"%s\",\"%s\"", pSSID, pPassWord );
|
||||
return ESP8266_Send_AT_Cmd( cCmd, "OK", NULL, 5000 );
|
||||
return ESP8266_Send_AT_Cmd( cCmd, "OK", "WIFI CONNECTED", 5000 );
|
||||
}
|
||||
|
||||
//ESP8266 <><CDB8>ʹ<EFBFBD><CAB9>
|
||||
@@ -142,6 +144,18 @@ bool ESP8266_Enable_MultipleId (FunctionalState enumEnUnvarnishTx )
|
||||
|
||||
}
|
||||
|
||||
//ESP8266 ʹ<><CAB9><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
|
||||
//enumEnUnvarnishTx <20>Ƿ<EFBFBD><C7B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӣ<EFBFBD>bool<6F><6C><EFBFBD><EFBFBD>
|
||||
//<2F><><EFBFBD>óɹ<C3B3><C9B9><EFBFBD><EFBFBD><EFBFBD>true<75><65><EFBFBD><EFBFBD>֮false
|
||||
bool ESP8266_Enable_AutoConnect (int enable )
|
||||
{
|
||||
char cStr [20];
|
||||
|
||||
sprintf ( cStr, "AT+CWAUTOCONN=%d", enable );
|
||||
|
||||
return ESP8266_Send_AT_Cmd ( cStr, "OK", 0, 500 );
|
||||
|
||||
}
|
||||
|
||||
//ESP8266 <20><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
//enumE <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
@@ -187,7 +201,6 @@ bool ESP8266_UnvarnishSend ( void )
|
||||
|
||||
return
|
||||
ESP8266_Send_AT_Cmd( "AT+CIPSEND", "OK", ">", 500 );
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -202,15 +215,12 @@ bool ESP8266_SendString(FunctionalState enumEnUnvarnishTx, char * pStr, uint32_t
|
||||
char cStr [20];
|
||||
bool bRet = false;
|
||||
|
||||
|
||||
if ( enumEnUnvarnishTx )
|
||||
{
|
||||
ESP8266_USART ( "%s", pStr );
|
||||
hal_AT_printf ( "%s", pStr );
|
||||
|
||||
bRet = true;
|
||||
|
||||
}
|
||||
|
||||
else
|
||||
{
|
||||
if ( ucId < 5 )
|
||||
@@ -222,10 +232,8 @@ bool ESP8266_SendString(FunctionalState enumEnUnvarnishTx, char * pStr, uint32_t
|
||||
ESP8266_Send_AT_Cmd ( cStr, "> ", 0, 1000 );
|
||||
|
||||
bRet = ESP8266_Send_AT_Cmd ( pStr, "SEND OK", 0, 1000 );
|
||||
}
|
||||
|
||||
}
|
||||
return bRet;
|
||||
|
||||
}
|
||||
|
||||
|
||||
@@ -233,7 +241,7 @@ bool ESP8266_SendString(FunctionalState enumEnUnvarnishTx, char * pStr, uint32_t
|
||||
void ESP8266_ExitUnvarnishSend ( void )
|
||||
{
|
||||
delay_ms(1000);
|
||||
ESP8266_USART( "+++" );
|
||||
hal_AT_printf( "+++" );
|
||||
delay_ms( 500 );
|
||||
}
|
||||
|
||||
@@ -249,18 +257,87 @@ uint8_t ESP8266_Get_LinkStatus ( void )
|
||||
{
|
||||
if ( strstr ( (char *)ESP8266_Fram_Record_Struct.Data_RX_BUF, "STATUS:2\r\n" ) )
|
||||
return 2;
|
||||
|
||||
else if ( strstr ( (char *)ESP8266_Fram_Record_Struct.Data_RX_BUF, "STATUS:3\r\n" ) )
|
||||
return 3;
|
||||
|
||||
else if ( strstr ( (char *)ESP8266_Fram_Record_Struct.Data_RX_BUF, "STATUS:4\r\n" ) )
|
||||
return 4;
|
||||
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
|
||||
|
||||
extern char mqtt_tcp_connect(int timeout);
|
||||
|
||||
/*-------------------------------------------------*/
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>WiFi<46><69><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><> <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>0<EFBFBD><30><EFBFBD><EFBFBD>ȷ <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*-------------------------------------------------*/
|
||||
char WiFi_Connect_IoTServer(void)
|
||||
{
|
||||
// printf("<><D7BC><EFBFBD><EFBFBD>λģ<CEBB><C4A3>\r\n"); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
||||
// ESP8266_Rst();
|
||||
|
||||
// ȡ<><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
ESP8266_ATE0();
|
||||
|
||||
printf("ready to set STA mode\r\n");
|
||||
// <20><><EFBFBD><EFBFBD>STAģʽ
|
||||
if (ESP8266_Net_Mode_Choose(STA))
|
||||
{
|
||||
printf("Set STA mode\r\n");
|
||||
}
|
||||
|
||||
// <><D7BC><EFBFBD>رն<D8B1>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
|
||||
if (ESP8266_Enable_MultipleId(DISABLE))
|
||||
{
|
||||
printf("close multipled ID success\r\n");
|
||||
}else
|
||||
{
|
||||
printf("close multipled ID error\r\n");
|
||||
}
|
||||
// ȡ<><C8A1><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD><EFBFBD>
|
||||
if(ESP8266_Enable_AutoConnect(0)) //ȡ<><C8A1><EFBFBD>Զ<EFBFBD><D4B6><EFBFBD><EFBFBD>ӣ<EFBFBD>100ms<6D><73>ʱ<EFBFBD><CAB1>λ<EFBFBD><CEBB><EFBFBD>ܼ<EFBFBD>5s<35><73>ʱʱ<CAB1><CAB1>
|
||||
{
|
||||
printf("ȡ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD>ӳɹ<EFBFBD>\r\n"); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
||||
}else
|
||||
{
|
||||
printf("ȡ<EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʧ<EFBFBD>ܣ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>\r\n"); //<2F><><EFBFBD>ط<EFBFBD>0ֵ<30><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>if<69><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
||||
return 3;
|
||||
}
|
||||
|
||||
|
||||
// <20><><EFBFBD><EFBFBD>·<EFBFBD><C2B7><EFBFBD><EFBFBD>
|
||||
if(ESP8266_JoinAP(User_SSID, User_PWD))
|
||||
{
|
||||
printf("connect to router success\r\n");
|
||||
}else
|
||||
{
|
||||
printf("connect to router failed\r\n");
|
||||
return 9;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD>
|
||||
if(mqtt_tcp_connect(500)) //<2F><><EFBFBD>ӷ<EFBFBD><D3B7><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>100ms<6D><73>ʱ<EFBFBD><CAB1>λ<EFBFBD><CEBB><EFBFBD>ܼ<EFBFBD>10s<30><73>ʱʱ<CAB1><CAB1>
|
||||
{
|
||||
printf("connect to server success\r\n"); //<2F><><EFBFBD>ط<EFBFBD>0ֵ<30><D6B5><EFBFBD><EFBFBD><EFBFBD><EFBFBD>if<69><66><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
||||
//<2F><><EFBFBD><EFBFBD>10
|
||||
}else
|
||||
{
|
||||
printf("connect to server failed\r\n"); //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ʾ<EFBFBD><CABE><EFBFBD><EFBFBD>
|
||||
return 10;
|
||||
}
|
||||
|
||||
// <20><><EFBFBD><EFBFBD><EFBFBD><CDB8>ģʽ
|
||||
if (ESP8266_UnvarnishSend() )
|
||||
{
|
||||
printf("go into unvarnishSend mode\r\n");
|
||||
}
|
||||
return 0; //<2F><>ȷ<EFBFBD><C8B7><EFBFBD><EFBFBD>0
|
||||
}
|
||||
|
||||
|
||||
static char *itoa( int value, char *string, int radix )
|
||||
{
|
||||
int i, d;
|
||||
@@ -311,7 +388,7 @@ static char *itoa( int value, char *string, int radix )
|
||||
} /* NCL_Itoa */
|
||||
|
||||
|
||||
void USART_printf ( char * Data, ... )
|
||||
void hal_AT_printf ( char * Data, ... )
|
||||
{
|
||||
const char *s;
|
||||
int d;
|
||||
@@ -382,7 +459,6 @@ void USART_printf ( char * Data, ... )
|
||||
Data++;
|
||||
while( __HAL_UART_GET_FLAG(&huart3, UART_FLAG_TXE) == false);
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
#ifndef __ESP8266_H
|
||||
#define __ESP8266_H
|
||||
#ifndef __ESP8266_H__
|
||||
#define __ESP8266_H__
|
||||
|
||||
#include "main.h"
|
||||
|
||||
@@ -7,9 +7,30 @@
|
||||
#include <string.h>
|
||||
#include <stdbool.h>
|
||||
|
||||
#if defined ( __CC_ARM )
|
||||
#pragma anon_unions
|
||||
#endif
|
||||
#define ESP8266_RST_Pin GPIO_PIN_4 //<2F><>λ<EFBFBD>ܽ<EFBFBD>
|
||||
#define ESP8266_RST_Pin_Port GPIOA //<2F><>λ
|
||||
|
||||
#define ESP8266_CH_PD_Pin GPIO_PIN_5 //ʹ<>ܹܽ<DCB9>
|
||||
#define ESP8266_CH_PD_Pin_Port GPIOA //ʹ<>ܶ˿<DCB6>
|
||||
|
||||
|
||||
#define ESP8266_RST_Pin_SetH HAL_GPIO_WritePin(ESP8266_RST_Pin_Port, ESP8266_RST_Pin, GPIO_PIN_SET)
|
||||
#define ESP8266_RST_Pin_SetL HAL_GPIO_WritePin(ESP8266_RST_Pin_Port, ESP8266_RST_Pin, GPIO_PIN_RESET)
|
||||
|
||||
|
||||
#define ESP8266_CH_PD_Pin_SetH HAL_GPIO_WritePin(ESP8266_CH_PD_Pin_Port,ESP8266_CH_PD_Pin, GPIO_PIN_SET)
|
||||
#define ESP8266_CH_PD_Pin_SetL HAL_GPIO_WritePin(ESP8266_CH_PD_Pin_Port,ESP8266_CH_PD_Pin, GPIO_PIN_RESET)
|
||||
|
||||
|
||||
#define ESP8266_USART(fmt, ...) USART_printf (fmt, ##__VA_ARGS__)
|
||||
#define PC_USART(fmt, ...) printf(fmt, ##__VA_ARGS__) //<2F><><EFBFBD>Ǵ<EFBFBD><C7B4>ڴ<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>ִ<EFBFBD><D6B4>printf<74><66><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ִ<EFBFBD><D6B4>fput<75><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD>printf<74><66>
|
||||
|
||||
|
||||
#define RX_BUF_MAX_LEN 1024 //<2F><><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
|
||||
|
||||
#define User_SSID "brown1" //wifi<66><69>
|
||||
#define User_PWD "123456789a" //wifi<66><69><EFBFBD><EFBFBD>
|
||||
|
||||
|
||||
//ESP8266ģʽѡ<CABD><D1A1>
|
||||
typedef enum
|
||||
@@ -55,32 +76,10 @@ typedef enum{
|
||||
TYPE_RECV_NONE,
|
||||
}type_recv_e;
|
||||
|
||||
#define ESP8266_RST_Pin GPIO_PIN_4 //<2F><>λ<EFBFBD>ܽ<EFBFBD>
|
||||
#define ESP8266_RST_Pin_Port GPIOA //<2F><>λ
|
||||
|
||||
#define ESP8266_CH_PD_Pin GPIO_PIN_5 //ʹ<>ܹܽ<DCB9>
|
||||
#define ESP8266_CH_PD_Pin_Port GPIOA //ʹ<>ܶ˿<DCB6>
|
||||
|
||||
|
||||
#define ESP8266_RST_Pin_SetH HAL_GPIO_WritePin(ESP8266_RST_Pin_Port, ESP8266_RST_Pin, GPIO_PIN_SET)
|
||||
#define ESP8266_RST_Pin_SetL HAL_GPIO_WritePin(ESP8266_RST_Pin_Port, ESP8266_RST_Pin, GPIO_PIN_RESET)
|
||||
|
||||
|
||||
#define ESP8266_CH_PD_Pin_SetH HAL_GPIO_WritePin(ESP8266_CH_PD_Pin_Port,ESP8266_CH_PD_Pin, GPIO_PIN_SET)
|
||||
#define ESP8266_CH_PD_Pin_SetL HAL_GPIO_WritePin(ESP8266_CH_PD_Pin_Port,ESP8266_CH_PD_Pin, GPIO_PIN_RESET)
|
||||
|
||||
|
||||
#define ESP8266_USART(fmt, ...) USART_printf (fmt, ##__VA_ARGS__)
|
||||
#define PC_USART(fmt, ...) printf(fmt, ##__VA_ARGS__) //<2F><><EFBFBD>Ǵ<EFBFBD><C7B4>ڴ<EFBFBD>ӡ<EFBFBD><D3A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>1<EFBFBD><31>ִ<EFBFBD><D6B4>printf<74><66><EFBFBD><EFBFBD><EFBFBD>Զ<EFBFBD>ִ<EFBFBD><D6B4>fput<75><74><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ض<EFBFBD><D8B6><EFBFBD><EFBFBD><EFBFBD>printf<74><66>
|
||||
|
||||
|
||||
|
||||
#define RX_BUF_MAX_LEN 1024 //<2F><><EFBFBD><EFBFBD><EFBFBD>ֽ<EFBFBD><D6BD><EFBFBD>
|
||||
|
||||
typedef int (*wifi_data_arrvied)(type_recv_e type, uint8_t *data, int len); // <20><><EFBFBD><EFBFBD>ָ<EFBFBD>룬<EFBFBD><EBA3AC><EFBFBD><EFBFBD>WIFI<46><49><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
|
||||
|
||||
extern struct STRUCT_USART_Fram //<2F><><EFBFBD><EFBFBD>֡<EFBFBD>ṹ<EFBFBD><E1B9B9>
|
||||
#pragma anon_unions
|
||||
typedef struct STRUCT_USART_Fram //<2F><><EFBFBD><EFBFBD>֡<EFBFBD>ṹ<EFBFBD><E1B9B9>
|
||||
{
|
||||
uint8_t Data_RX_BUF[RX_BUF_MAX_LEN];
|
||||
union
|
||||
@@ -92,18 +91,17 @@ extern struct STRUCT_USART_Fram //
|
||||
__IO uint16_t FramFinishFlag :1; // 15
|
||||
}InfBit;
|
||||
};
|
||||
wifi_data_arrvied wifi_data_recv_cb;
|
||||
wifi_data_arrvied wifi_data_recv_cb;
|
||||
|
||||
}ESP8266_Fram_Record_Struct;
|
||||
|
||||
|
||||
}STRUCT_USART_Fram_t;
|
||||
extern STRUCT_USART_Fram_t ESP8266_Fram_Record_Struct;
|
||||
|
||||
//<2F><>ʼ<EFBFBD><CABC><EFBFBD><EFBFBD>TCP<43><50><EFBFBD>ܺ<EFBFBD><DCBA><EFBFBD>
|
||||
void ESP8266_Init(uint32_t bound);
|
||||
void ESP8266_AT_Test(void);
|
||||
void ESP8266_ATE0(void);
|
||||
bool ESP8266_Send_AT_Cmd(char *cmd,char *ack1,char *ack2,uint32_t time);
|
||||
void ESP8266_Rst(void);
|
||||
char ESP8266_Rst(void);
|
||||
bool ESP8266_Net_Mode_Choose(ENUM_Net_ModeTypeDef enumMode);
|
||||
bool ESP8266_JoinAP( char * pSSID, char * pPassWord );
|
||||
bool ESP8266_Enable_MultipleId ( FunctionalState enumEnUnvarnishTx );
|
||||
@@ -112,15 +110,8 @@ bool ESP8266_SendString(FunctionalState enumEnUnvarnishTx, char * pStr, uint32_t
|
||||
bool ESP8266_UnvarnishSend ( void );
|
||||
void ESP8266_ExitUnvarnishSend ( void );
|
||||
uint8_t ESP8266_Get_LinkStatus ( void );
|
||||
void USART_printf( char * Data, ... );
|
||||
|
||||
//MQTT<54><54><EFBFBD>ܺ<EFBFBD><DCBA><EFBFBD>
|
||||
bool ESP8266_MQTTUSERCFG( char * pClient_Id, char * pUserName,char * PassWord);
|
||||
bool ESP8266_MQTTCONN( char * Ip, int Num);
|
||||
bool ESP8266_MQTTSUB(char * Topic);
|
||||
bool ESP8266_MQTTPUB( char * Topic,char *temp);
|
||||
bool ESP8266_MQTTCLEAN(void);
|
||||
bool MQTT_SendString(char * pTopic,char *temp2);
|
||||
void hal_AT_printf( char * Data, ... );
|
||||
char WiFi_Connect_IoTServer(void);
|
||||
|
||||
#endif
|
||||
|
||||
|
||||
@@ -1,13 +1,5 @@
|
||||
#include "mqtt.h"
|
||||
|
||||
#define MQTT_NAME "n87ieud/my_board2"
|
||||
#define MQTT_PW "k4e9trh5qjwjhc2q"
|
||||
#define MQTT_CLIENT_ID "my_board2"
|
||||
|
||||
#define MQTT_TOPIC_UPDATE "$baidu/iot/shadow/my_board2/update"
|
||||
#define MQTT_TOPIC_DELTA "$baidu/iot/shadow/my_board2/delta"
|
||||
#define MQTT_TOPIC_SNAPSHOT "$baidu/iot/shadow/my_board2/update/snapshot"
|
||||
|
||||
#include "usart.h"
|
||||
|
||||
char str[] = "{\"deviceNum\": \"E8DB84933089\",\"relayStatus\": 0,\"lightStatus\": 0,\"isOnline\": 1,\"rssi\": -54,\"deviceTemperature\": 38}";
|
||||
|
||||
@@ -16,7 +8,7 @@ static int mqtt_send(uint8_t head, uint32_t vlen, uint8_t *vhead,
|
||||
{
|
||||
int32_t tmp_encode, tmp_x;
|
||||
uint32_t i = 0;
|
||||
uint8_t msg[RX_BUFF_LEN];
|
||||
uint8_t msg[RX_BUF_MAX_LEN];
|
||||
msg[i++] = head;
|
||||
|
||||
tmp_x = vlen + pay_len;
|
||||
@@ -37,10 +29,13 @@ static int mqtt_send(uint8_t head, uint32_t vlen, uint8_t *vhead,
|
||||
memcpy(&msg[i], payload, pay_len);
|
||||
i = i + pay_len;
|
||||
}
|
||||
esp8266_send(msg, i);
|
||||
ESP8266_Fram_Record_Struct.InfBit.FramFinishFlag = 0;
|
||||
ESP8266_Fram_Record_Struct.InfBit.FramLength = 0;
|
||||
HAL_UART_Transmit(&huart3, msg, i, 0xFFFF);
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
static int mqtt_str_fill(uint8_t *outbuff, char *instr)
|
||||
{
|
||||
uint8_t *p_tmp;
|
||||
@@ -53,17 +48,20 @@ static int mqtt_str_fill(uint8_t *outbuff, char *instr)
|
||||
|
||||
static int mqtt_recv(uint8_t *ackbuff, uint16_t *ack_len, int timeout)
|
||||
{
|
||||
|
||||
do {
|
||||
if (esp8266_recv(ackbuff, &ack_len) == 0) {
|
||||
if(1 == ESP8266_Fram_Record_Struct.InfBit.FramFinishFlag)
|
||||
{
|
||||
memcpy(ackbuff, ESP8266_Fram_Record_Struct.Data_RX_BUF, ESP8266_Fram_Record_Struct.InfBit.FramLength);
|
||||
*ack_len = ESP8266_Fram_Record_Struct.InfBit.FramLength;
|
||||
ESP8266_Fram_Record_Struct.InfBit.FramFinishFlag = 0;
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (timeout > 0) {
|
||||
HAL_Delay(100);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
||||
} while (timeout--);
|
||||
return -1;
|
||||
}
|
||||
@@ -71,7 +69,7 @@ static int mqtt_recv(uint8_t *ackbuff, uint16_t *ack_len, int timeout)
|
||||
static int mqtt_ack_check(uint8_t head, int timeout, uint16_t msg_id)
|
||||
{
|
||||
uint8_t recv_head;
|
||||
uint8_t recv_buff[RX_BUFF_LEN];
|
||||
uint8_t recv_buff[RX_BUF_MAX_LEN];
|
||||
uint16_t recv_len;
|
||||
|
||||
if (mqtt_recv(recv_buff, &recv_len, timeout) != 0) {
|
||||
@@ -97,6 +95,20 @@ static int mqtt_ack_check(uint8_t head, int timeout, uint16_t msg_id)
|
||||
return -1;
|
||||
}
|
||||
|
||||
/*-------------------------------------------------*/
|
||||
/*<2A><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><> <20><><EFBFBD><EFBFBD>timeout<75><74> <20><>ʱʱ<CAB1>䣨100ms<6D>ı<EFBFBD><C4B1><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*<2A><><EFBFBD><EFBFBD>ֵ<EFBFBD><D6B5>True<75><65><EFBFBD><EFBFBD>ȷ False<73><65><EFBFBD><EFBFBD><EFBFBD><EFBFBD> */
|
||||
/*-------------------------------------------------*/
|
||||
char mqtt_tcp_connect(int timeout)
|
||||
{
|
||||
char cCmd [120];
|
||||
|
||||
sprintf (cCmd, "AT+CIPSTART=\"TCP\",\"%s\",%d\r\n", User_MQTTServer_IP, User_MQTTServer_PORT);
|
||||
|
||||
return ESP8266_Send_AT_Cmd ( cCmd, "OK", "WIFI GOT IP", timeout);
|
||||
}
|
||||
|
||||
static int mqtt_connect(char *name, char *pwd, char *client_id)
|
||||
{
|
||||
uint32_t i, j;
|
||||
@@ -251,8 +263,9 @@ static int mqtt_publish_from_cloud(uint8_t *ackbuff, uint16_t ack_len)
|
||||
payload_len = remain_len - vhead_len - 4;
|
||||
|
||||
memcpy(payload, &ackbuff[i], payload_len);
|
||||
if (strcmp((char*) topic, MQTT_TOPIC_DELTA) == 0) {
|
||||
sensor_data_set(payload);
|
||||
if (strcmp((char*) topic, User_MQTTServer_Topic_SUB) == 0) {
|
||||
// sensor_data_set(payload);
|
||||
;
|
||||
}
|
||||
mqtt_publish_ack(msg_id);
|
||||
return 0;
|
||||
@@ -268,7 +281,7 @@ int mqtt_beat(void)
|
||||
uint32_t i = 0, j = 0;
|
||||
mqtt_send(head, i, vhead, j, payload);
|
||||
if (mqtt_ack_check(MQTT_PINGRESP, 100, 0) != 0) {
|
||||
if (mqtt_connect(MQTT_NAME, MQTT_PW, MQTT_CLIENT_ID) != 0) {
|
||||
if (mqtt_connect(User_Username, User_Password, User_Client_ID) != 0) {
|
||||
return -1;
|
||||
}else{
|
||||
return 0;
|
||||
@@ -280,7 +293,7 @@ int mqtt_beat(void)
|
||||
int mqtt_handle(void)
|
||||
{
|
||||
uint8_t recv_head;
|
||||
uint8_t recv_buff[RX_BUFF_LEN];
|
||||
uint8_t recv_buff[RX_BUF_MAX_LEN];
|
||||
uint16_t recv_len;
|
||||
|
||||
if (mqtt_recv(recv_buff, &recv_len, 0) != 0) {
|
||||
@@ -299,7 +312,7 @@ int mqtt_handle(void)
|
||||
|
||||
int mqtt_publish_update(uint8_t *msg, uint16_t msg_len)
|
||||
{
|
||||
if (mqtt_publish(MQTT_TOPIC_UPDATE, msg, msg_len, msg_id++) != 0) {
|
||||
if (mqtt_publish(User_MQTTServer_Topic_PUB, msg, msg_len, msg_id++) != 0) {
|
||||
return -1;
|
||||
}
|
||||
return 0;
|
||||
@@ -307,17 +320,20 @@ int mqtt_publish_update(uint8_t *msg, uint16_t msg_len)
|
||||
|
||||
int mqtt_init(void)
|
||||
{
|
||||
if (mqtt_connect(MQTT_NAME, MQTT_PW, MQTT_CLIENT_ID) != 0) {
|
||||
mqtt_disconnect();
|
||||
esp8266_init();
|
||||
HAL_Delay(100);
|
||||
if (mqtt_connect(MQTT_NAME, MQTT_PW, MQTT_CLIENT_ID) != 0) {
|
||||
return -1;
|
||||
}
|
||||
if (mqtt_connect(User_Username, User_Password, User_Client_ID) != 0) {
|
||||
printf("MQTT handshake error\r\n");
|
||||
return -1;
|
||||
}else
|
||||
{
|
||||
printf("MQTT handshake success\r\n");
|
||||
}
|
||||
|
||||
if (mqtt_subscriber(MQTT_TOPIC_DELTA, msg_id++) != 0) {
|
||||
if (mqtt_subscriber(User_MQTTServer_Topic_SUB, msg_id++) != 0) {
|
||||
printf("subscribe error\r\n");
|
||||
return -1;
|
||||
}else
|
||||
{
|
||||
printf("subscribe success\r\n");
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
@@ -1,26 +1,18 @@
|
||||
#ifndef __MQTT_H
|
||||
#define __MQTT_H
|
||||
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
#ifndef __MQTT_H__
|
||||
#define __MQTT_H __
|
||||
|
||||
#include "main.h"
|
||||
#include "esp8266.h"
|
||||
|
||||
#define User_Client_ID "50fccfefb50a48b98a93ce27bb04592f" //MQTTclientID <20><><EFBFBD>ڱ<EFBFBD>־client<6E><74><EFBFBD><EFBFBD> <20>256<35>ֽ<EFBFBD>
|
||||
#define User_Username "admin" //<2F><><EFBFBD>ڵ<EFBFBD>¼ MQTT <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> username, <20> 64 <20>ֽ<EFBFBD>
|
||||
#define User_Password "admin123" //<2F><><EFBFBD>ڵ<EFBFBD>¼ MQTT <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> password, <20> 64 <20>ֽ<EFBFBD>
|
||||
#define User_MQTTServer_IP "106.12.9.213" //MQTT<54><54><EFBFBD>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>IP
|
||||
#define User_MQTTServer_PORT 1883 //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿ں<CBBF>
|
||||
#define User_MQTTServer_Topic_PUB "status" //<2F><><EFBFBD><EFBFBD>MQTT<54><54><EFBFBD><EFBFBD>
|
||||
#define User_MQTTServer_Topic_SUB "status/set/E8DB84933088" //<2F><><EFBFBD><EFBFBD>MQTT<54><54><EFBFBD><EFBFBD>
|
||||
|
||||
#define User_ESP8266_SSID "brown" //wifi<66><69>
|
||||
#define User_ESP8266_PWD "123456789a" //wifi<66><69><EFBFBD><EFBFBD>
|
||||
|
||||
#define User_ESP8266_client_id "50fccfefb50a48b98a93ce27bb04591f" //MQTTclientID <20><><EFBFBD>ڱ<EFBFBD>־client<6E><74><EFBFBD><EFBFBD> <20>256<35>ֽ<EFBFBD>
|
||||
#define User_ESP8266_username "admin" //<2F><><EFBFBD>ڵ<EFBFBD>¼ MQTT <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> username, <20> 64 <20>ֽ<EFBFBD>
|
||||
#define User_ESP8266_password "admin123" //<2F><><EFBFBD>ڵ<EFBFBD>¼ MQTT <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD> <20><> password, <20> 64 <20>ֽ<EFBFBD>
|
||||
#define User_ESP8266_MQTTServer_IP "106.12.9.213" //MQTT<54><54><EFBFBD>ط<EFBFBD><D8B7><EFBFBD><EFBFBD><EFBFBD>IP
|
||||
#define User_ESP8266_MQTTServer_PORT 1883 //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿ں<CBBF>
|
||||
#define User_ESP8266_MQTTServer_Topic_PUB "status" //<2F><><EFBFBD><EFBFBD>MQTT<54><54><EFBFBD><EFBFBD>
|
||||
#define User_ESP8266_MQTTServer_Topic_SUB "status/set/E8DB84933089" //<2F><><EFBFBD><EFBFBD>MQTT<54><54><EFBFBD><EFBFBD>
|
||||
|
||||
enum{
|
||||
typedef enum {
|
||||
MQTT_NULL = 0,
|
||||
MQTT_CONNECT = 1 << 4,
|
||||
MQTT_CONNACK = 2 << 4,
|
||||
@@ -38,7 +30,7 @@ enum{
|
||||
MQTT_DISCONNECT = 14 << 4,
|
||||
}MQTT_HEAD_T;
|
||||
|
||||
enum{
|
||||
typedef enum{
|
||||
CONNECT_FLAG = 0,
|
||||
CONNECT_FLAG_NAME = 1 << 7,
|
||||
CONNECT_FLAG_PWD = 1 << 6,
|
||||
@@ -48,7 +40,7 @@ enum{
|
||||
CONNECT_FLAG_CLS = 1 << 1,
|
||||
}CONNECT_T;
|
||||
|
||||
enum{
|
||||
typedef enum{
|
||||
PUBLISH_FLAG_RETN = 1,
|
||||
PUBLISH_FLAG_QOS0 = 0,
|
||||
PUBLISH_FLAG_QOS1 = 1 << 1,
|
||||
@@ -61,10 +53,5 @@ int mqtt_handle(void);
|
||||
int mqtt_beat(void);
|
||||
int mqtt_publish_update(uint8_t *msg, uint16_t msg_len);
|
||||
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif /* __MQTT_H */
|
||||
|
||||
|
||||
@@ -1,42 +0,0 @@
|
||||
#include "tcp.h"
|
||||
|
||||
volatile uint8_t TcpClosedFlag = 0;
|
||||
|
||||
void ESP8266_STA_TCPClient_Test(void)
|
||||
{
|
||||
uint8_t res;
|
||||
|
||||
char str[100]={0};
|
||||
ESP8266_AT_Test();
|
||||
printf("<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ESP8266\r\n");
|
||||
ESP8266_Net_Mode_Choose(STA);
|
||||
while(!ESP8266_JoinAP(User_ESP8266_SSID, User_ESP8266_PWD));
|
||||
ESP8266_Enable_MultipleId ( DISABLE );
|
||||
while(!ESP8266_Link_Server(enumTCP, User_ESP8266_TCPServer_IP, User_ESP8266_TCPServer_PORT, Single_ID_0));
|
||||
while(!ESP8266_UnvarnishSend());
|
||||
printf("\r\n<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>");
|
||||
while ( 1 )
|
||||
{
|
||||
sprintf (str,"<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>а<EFBFBD><EFBFBD>ſɿƼ<EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˾" );//<2F><>ʽ<EFBFBD><CABD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ<EFBFBD><D6B7><EFBFBD><EFBFBD><EFBFBD>TCP<43><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
||||
ESP8266_SendString ( ENABLE, str, 0, Single_ID_0 );
|
||||
delay_ms(1000);
|
||||
if(TcpClosedFlag) //<2F>ж<EFBFBD><D0B6>Ƿ<EFBFBD>ʧȥ<CAA7><C8A5><EFBFBD><EFBFBD>
|
||||
{
|
||||
ESP8266_ExitUnvarnishSend(); //<2F>˳<EFBFBD><EFBFBD><CDB8>ģʽ
|
||||
do
|
||||
{
|
||||
res = ESP8266_Get_LinkStatus(); //<2F><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD>״̬
|
||||
}
|
||||
while(!res);
|
||||
|
||||
if(res == 4) //ȷ<><C8B7>ʧȥ<CAA7><C8A5><EFBFBD>ӣ<EFBFBD><D3A3><EFBFBD><EFBFBD><EFBFBD>
|
||||
{
|
||||
|
||||
|
||||
while (!ESP8266_JoinAP(User_ESP8266_SSID, User_ESP8266_PWD ) );
|
||||
while (!ESP8266_Link_Server(enumTCP, User_ESP8266_TCPServer_IP, User_ESP8266_TCPServer_PORT, Single_ID_0 ) );
|
||||
}
|
||||
while(!ESP8266_UnvarnishSend());
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,22 +0,0 @@
|
||||
#ifndef __TCP_H
|
||||
#define __TCP_H
|
||||
|
||||
#include "main.h"
|
||||
#include "esp8266.h"
|
||||
|
||||
/*
|
||||
*<2A><><EFBFBD>²<EFBFBD><C2B2><EFBFBD><EFBFBD><EFBFBD>Ҫ<EFBFBD>û<EFBFBD><C3BB><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IJ<DEB8><C4B2>ܲ<EFBFBD><DCB2><EFBFBD><EFBFBD>ù<EFBFBD>
|
||||
*/
|
||||
|
||||
#define User_ESP8266_SSID "miot_default" //wifi<66><69>
|
||||
#define User_ESP8266_PWD "123456789x" //wifi<66><69><EFBFBD><EFBFBD>
|
||||
|
||||
#define User_ESP8266_TCPServer_IP "192.168.31.16" //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>IP
|
||||
#define User_ESP8266_TCPServer_PORT "8888" //<2F><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>˿ں<CBBF>
|
||||
|
||||
|
||||
extern volatile uint8_t TcpClosedFlag; //<2F><><EFBFBD><EFBFBD>״̬<D7B4><CCAC>־
|
||||
|
||||
void ESP8266_STA_TCPClient_Test(void);
|
||||
|
||||
#endif
|
||||
Reference in New Issue
Block a user