191 lines
5.3 KiB
C
191 lines
5.3 KiB
C
/**
|
||
******************************************************************************
|
||
* @file : user_wifi.c
|
||
* @author : zsq
|
||
* @version : V1.0
|
||
* @date : 2020-01-08
|
||
* @brief : wifi config and control
|
||
******************************************************************************
|
||
* @attention
|
||
*
|
||
* Copyright (c) 2019 YUNHORN(Shenzhen YunHorn Technology Co., Ltd
|
||
* All rights reserved.
|
||
*
|
||
******************************************************************************
|
||
*/
|
||
//-- Includes -----------------------------------------------------------------
|
||
#include "main.h"
|
||
#include "user_wifi.h"
|
||
#include "system.h"
|
||
|
||
#include "fifo.h"
|
||
|
||
volatile uint8_t WifiConfigState = DISABLE;
|
||
//MQTT接收数组
|
||
uint8_t mqttRecvData[256];
|
||
|
||
/*******************************************************************************
|
||
**Check_WifiConfigState(void)
|
||
**功能描述:检查是否进入WIFI配置模式
|
||
**入口参数:无
|
||
**输出:无
|
||
*******************************************************************************/
|
||
void Check_WifiConfigState(void)
|
||
{
|
||
if(WifiConfigState == ENABLE)
|
||
{
|
||
//进入WIFI配置模式
|
||
//退出透传模式
|
||
printf("+++");
|
||
Delay(500);
|
||
printf("+++");
|
||
Delay(500);
|
||
//恢复出厂设置
|
||
printf("AT+RESTORE\r\n");
|
||
Delay(2000);
|
||
//设置Wi-Fi模式并保存到flash
|
||
printf("AT+CWMODE_DEF=1\r\n");
|
||
Delay(500);
|
||
//上电自动连接到AP
|
||
printf("AT+CWAUTOCONN=1\r\n");
|
||
Delay(500);
|
||
//开启SmartConfig
|
||
//printf("AT+CWSTARTSMART\r\n");
|
||
printf("AT+CWSTARTSMART=3\r\n");
|
||
Delay(500);
|
||
//设置透传模式
|
||
printf("AT+CIPMODE=1\r\n");
|
||
Delay(500);
|
||
//保存透传连接到flash
|
||
// printf("AT+SAVETRANSLINK=1,\"smartoilets.cn\",8089,\"TCP\"\r\n");
|
||
printf("AT+SAVETRANSLINK=1,\"192.168.1.233\",8899,\"TCP\"\r\n");
|
||
while(1);
|
||
}
|
||
WifiConfigState = DISABLE;
|
||
}
|
||
|
||
|
||
/*******************************************************************************
|
||
**SendDate_Wifi(void)
|
||
**功能描述:使用WIFI发送传感器数据
|
||
**入口参数:SensorDataTypeDef *sensorData 数据结构体
|
||
**输出:无
|
||
*******************************************************************************/
|
||
void SendDate_Wifi(SensorDataTypeDef *sensorData)
|
||
{
|
||
|
||
printf("POST /socketServer/statis/push HTTP/1.1\r\nHost: smartoilets.cn\r\nContent-Type: application/x-www-form-urlencoded\r\nContent-Length: 247\r\nConnection: keep-alive\r\n\r\ndata={\"code\": 1005,\"id\": \"65aa287c-093a-4999-ae43-bb240a0fdc72\",\"version\": \"yunhorn_kq_c_v1\",\"online\": true,\"data\": {\"temperature\": +%06.2f,\"humidity\": %06.2f,\"nh3\": %05.2f,\"h2s\": %05.2f,\"ch2o\": %04.2f,\"co2\": %04d,\"tvoc\": %d,\"pm25\": %04d,\"pm10\": %04d}}"
|
||
,sensorData->temperature,sensorData->humidity,sensorData->nh3,sensorData->h2s
|
||
,sensorData->ch2o,sensorData->co2,sensorData->tvoc,sensorData->pm25,sensorData->pm10);
|
||
}
|
||
|
||
/*******************************************************************************
|
||
**SendDate_Wifi_MQTT(int sock, unsigned char *buf, int buflen)
|
||
**功能描述:使用WIFI发送传感器数据(MQTT协议)
|
||
**入口参数:int sock
|
||
unsigned char *buf 发送数据
|
||
int buflen 数据长度
|
||
**返回值:发送是否成功
|
||
*******************************************************************************/
|
||
int SendDate_Wifi_MQTT(int sock, unsigned char *buf, int buflen)
|
||
{
|
||
UsartxSendDataStr(WIFI_USART,buf,buflen);
|
||
return 1;//默认发送成功(测试!!)
|
||
}
|
||
|
||
|
||
int RecvDate_Wifi_MQTT(int sock, unsigned char *buf, int buflen)
|
||
{
|
||
|
||
uint8_t read_len = 0;
|
||
uint8_t total_read_len = 0;
|
||
|
||
uint8_t rc;
|
||
|
||
|
||
// extern RingBuff_t ringBuff;
|
||
// RingBuff_Init();//初始化环形缓冲区
|
||
// while(1)
|
||
// {
|
||
// IWDG_Feed();
|
||
// USART_SendData(DEBUG_USART,'A');
|
||
// while(USART_GetFlagStatus(DEBUG_USART,USART_FLAG_TXE)==RESET);
|
||
// USART_SendData(DEBUG_USART,ringBuff.Lenght);
|
||
// while(USART_GetFlagStatus(DEBUG_USART,USART_FLAG_TXE)==RESET);
|
||
//
|
||
//
|
||
//
|
||
// if(ringBuff.Lenght > 0)//判断非空
|
||
// {
|
||
// uint16_t i;
|
||
// uint16_t length = ringBuff.Lenght;
|
||
// uint8_t *Data = (uint8_t *)malloc(sizeof(uint8_t));
|
||
// USART_SendData(DEBUG_USART,ringBuff.Lenght);
|
||
// while(USART_GetFlagStatus(DEBUG_USART,USART_FLAG_TXE)==RESET);
|
||
//// Read_RingBuff(Data);
|
||
// for(i = 0;i<length;i++)
|
||
// {
|
||
// Read_RingBuff(Data);
|
||
// USART_SendData(DEBUG_USART,*Data);
|
||
// while(USART_GetFlagStatus(DEBUG_USART,USART_FLAG_TXE)==RESET);
|
||
// }
|
||
// }
|
||
// Delay(2000);
|
||
// }
|
||
//
|
||
//
|
||
|
||
|
||
/*
|
||
打开定时器,等待超时
|
||
*/
|
||
//具体实现
|
||
|
||
// while(1)//当未超时,继续接收 测试 条件为1
|
||
// {
|
||
// for(read_len = 0; read_len < buflen; read_len++)
|
||
// {
|
||
// if(Read_RingBuff(buf+read_len)) //读取数据成功
|
||
// {
|
||
// continue;
|
||
// }
|
||
// else //读取数据失败
|
||
// {
|
||
// return read_len;//返回已读取数据长度
|
||
// }
|
||
// }
|
||
// }
|
||
return buflen; //返回已读取数据长度
|
||
}
|
||
|
||
|
||
int Wifi_Net_Connect(const char *ip, const char *port, sal_proto_t proto)
|
||
{
|
||
return 1;//ESP8266自动连接 默认连接成功 测试
|
||
}
|
||
int Wifi_Net_Close(int sock)
|
||
{
|
||
return 1;//默认关闭成功 测试
|
||
}
|
||
|
||
/*******************************************************************************
|
||
**CheckState_Wifi(void)
|
||
**功能描述:检查WIFI状态
|
||
**入口参数:无
|
||
**输出:无
|
||
*******************************************************************************/
|
||
int CheckState_Wifi(void)
|
||
{
|
||
return 1;//默认关闭成功 测试
|
||
}
|
||
/*******************************************************************************
|
||
**NetConfig_Wifi(void)
|
||
**功能描述:配置WIFI参数
|
||
**入口参数:无
|
||
**输出:无
|
||
*******************************************************************************/
|
||
int NetConfig_Wifi(void)
|
||
{
|
||
return 1;//默认关闭成功 测试
|
||
}
|