wifi发送第一个数据
时间:10-02
整理:3721RD
点击:
先刨个坑
等视频上传好了再来填坑,吼吼
使用mx1081的AD模块,采集AD值,然后送wifi发送
使用UDP,简单快速明了,关键是人比较懒
虽然实现了,但是还有很多地方不是很明白
上代码,简单注释了一下main函数回复可见,哇咔咔
#include "stdio.h"
#include "ctype.h"
#include "MX1081_IO.H"
#include "stm32f2xx.h"
#include "platform.h"
#include "mxchipWNET.h"
#define ADC1_DR_Address ((u32)0x4001204C)
void ADC1_CH1_DMA_Config(void);
float AD_value_1;
vu16 ADC_ConvertedValue[2];
#define LowPowerMode
#define AP_NAME "Ljj@Home"
#define AP_PASSWORD "xxxxxxxx" //我家的密码,就不告诉你,吼吼
#define APP_INFO "mxchipWNet Demo: Wi-Fi Link"
network_InitTypeDef_st wNetConfig;
net_para_st para;
/* ========================================
User provide callback functions
======================================== */
void system_version(char *str, int len)
{
snprintf( str, len, "%s", APP_INFO);
}
void userWatchDog(void)
{
}
void WifiStatusHandler(int event)
{
switch (event) {
case MXCHIP_WIFI_UP:
printf("Station up \r\n");
break;
case MXCHIP_WIFI_DOWN:
printf("Station down \r\n");
break;
case MXCHIP_UAP_UP:
printf("uAP up \r\n");
getNetPara(¶, Soft_AP);
printf("Soft AP mode: IP address: %s \r\n", para.ip);
printf("Soft AP mode: NetMask address: %s \r\n", para.mask);
printf("Soft AP mode: MAC address: %s \r\n", para.mac);
break;
case MXCHIP_UAP_DOWN:
printf("uAP down \r\n");
break;
default:
break;
}
return;
}
void ApListCallback(UwtPara_str *pApList)
{
int i;
printf("Find %d APs: \r\n", pApList->ApNum);
for (i=0;i<pApList->ApNum;i++)
printf(" SSID: %s, Signal: %d%%\r\n", pApList->ApList.ssid, pApList->ApList.ApPower);
}
void NetCallback(net_para_st *pnet)
{
printf("Station mode: IP address: %s \r\n", pnet->ip);
printf("Station mode: NetMask address: %s \r\n", pnet->mask);
printf("Station mode: Gateway address: %s \r\n", pnet->gate);
printf("Station mode: DNS server address: %s \r\n", pnet->dns);
printf("Station mode: MAC address: %s \r\n", pnet->mac);
}
void stationModeStart(void)
{
int ret;
memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
wNetConfig.wifi_mode = Station;
strcpy((char*)wNetConfig.wifi_ssid, AP_NAME);
strcpy((char*)wNetConfig.wifi_key, AP_PASSWORD);
wNetConfig.dhcpMode = DHCP_Client;
ret = StartNetwork(&wNetConfig);
printf("connect to %s....., return %d\r\n", wNetConfig.wifi_ssid, ret);
}
void softAPModeStart(void)
{
int ret;
memset(&wNetConfig, 0x0, sizeof(network_InitTypeDef_st));
wNetConfig.wifi_mode = Soft_AP;
strcpy((char*)wNetConfig.wifi_ssid, "Soft AP test");
strcpy((char*)wNetConfig.wifi_key, "");
strcpy((char*)wNetConfig.local_ip_addr, "192.168.0.1");
strcpy((char*)wNetConfig.net_mask, "255.255.255.0");
strcpy((char*)wNetConfig.address_pool_start, "192.168.0.10");
strcpy((char*)wNetConfig.address_pool_end, "192.168.0.177");
wNetConfig.dhcpMode = DHCP_Server;
ret = StartNetwork(&wNetConfig);
printf("Setup soft AP: %s, return %d\r\n", wNetConfig.wifi_ssid, ret);
}
vs32 tick = 0;
vs32 button_irq = 0;
void RptConfigmodeRslt(network_InitTypeDef_st *nwkpara)
{
if(nwkpara == NULL){
printf("Configuration failed\r\n");
}
else{
printf("Configuration is successful, SSID:%s, Key:%s\r\n", \
nwkpara->wifi_ssid,\
nwkpara->wifi_key);
}
}
char* itostr(char *str, int i)
{
sprintf(str, "%d", i);
return str;
}
int main(void)
{int fd_udp = -1;
fd_set readfds, exceptfds;
struct timeval_t t;
struct sockaddr_t addr;
char buf[]="";
socklen_t addrLen;
mxchipInit();
UART_Init();
Button_Init();
printf("\r\n%s\r\nmxchipWNet library version: %s\r\n", APP_INFO, system_lib_version());
printf("Start scan\r\n");
mxchipStartScan();
stationModeStart(); //这里只启用station模式,连接路由器
// softAPModeStart();
ADC1_CH1_DMA_Config();
ADC_SoftwareStartConv(ADC1);
while(1){
mxchipTick();
itostr(buf,ADC_ConvertedValue[0]); //很奇怪,用庆科库函数转换有问题,自己写了个int到str的转换函数,用的sprintf来格式化
//printf("%s\r\n",buf);
if (fd_udp==-1) {
fd_udp = socket(AF_INET, SOCK_DGRM, IPPROTO_UDP); //建立UDP的socket
addr.s_port = 8090; //端口号配置
bind(fd_udp, &addr, sizeof(addr)); //套接字绑定
}
FD_ZERO(&readfds);
FD_SET(fd_udp, &readfds);
select(1, &readfds, NULL, &exceptfds, &t);
/*Read data from udp and send data back */
if (FD_ISSET(fd_udp, &readfds)) {
sendto(fd_udp, "The AD value is :", sizeof("The AD value is :"), 0, &addr, sizeof(struct sockaddr_t));
sendto(fd_udp, buf, 4, 0, &addr, sizeof(struct sockaddr_t));
sendto(fd_udp, "\r\n", sizeof("\r\n"), 0, &addr, sizeof(struct sockaddr_t));
recvfrom(fd_udp, buf, 3*1024, 0, &addr, &addrLen);
}
}
}
/*对于套接字的置位,上面FD_ISSET这里很不解,为什么必须从UDP接收到一个数据,才能够将套接字置位
UDP不能够连续单向发送吗?不一定吧。
想单步跟踪进去看看,庆科不够意思,函数封装起来,连函数库指南说得也不清不楚,求指导*/
void Button_irq_handler(void *arg)
{
printf("Start scanning by user...\r\n");
mxchipStartScan();
}
void ADC1_CH1_DMA_Config(void) //stm32的AD配置,DMA模式
{
ADC_InitTypeDef ADC_InitStructure;
ADC_CommonInitTypeDef ADC_CommonInitStructure;
DMA_InitTypeDef DMA_InitStructure;
GPIO_InitTypeDef GPIO_InitStructure;
RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_DMA2 | RCC_AHB1Periph_GPIOA, ENABLE);
RCC_APB2PeriphClockCmd(RCC_APB2Periph_ADC1, ENABLE);
// DMA_DeInit(DMA2_Stream0);
/* DMA2 Stream0 channe0 configuration **************************************/
DMA_InitStructure.DMA_Channel = DMA_Channel_0;
DMA_InitStructure.DMA_PeripheralBaseAddr = (uint32_t)ADC1_DR_Address;
DMA_InitStructure.DMA_Memory0BaseAddr = (uint32_t)&ADC_ConvertedValue;
DMA_InitStructure.DMA_DIR = DMA_DIR_PeripheralToMemory;
DMA_InitStructure.DMA_BufferSize = 2;
DMA_InitStructure.DMA_PeripheralInc = DMA_PeripheralInc_Disable;
DMA_InitStructure.DMA_MemoryInc = DMA_MemoryInc_Enable;
DMA_InitStructure.DMA_PeripheralDataSize = DMA_PeripheralDataSize_HalfWord;
DMA_InitStructure.DMA_MemoryDataSize = DMA_MemoryDataSize_HalfWord;
DMA_InitStructure.DMA_Mode = DMA_Mode_Circular;
DMA_InitStructure.DMA_Priority = DMA_Priority_High;
DMA_InitStructure.DMA_FIFOMode = DMA_FIFOMode_Disable;
DMA_InitStructure.DMA_FIFOThreshold = DMA_FIFOThreshold_HalfFull;
DMA_InitStructure.DMA_MemoryBurst = DMA_MemoryBurst_Single;
DMA_InitStructure.DMA_PeripheralBurst = DMA_PeripheralBurst_Single;
DMA_Init(DMA2_Stream0, &DMA_InitStructure);
DMA_Cmd(DMA2_Stream0, ENABLE);
/* Configure ADC1 Channe6 pin as analog input ******************************/
GPIO_InitStructure.GPIO_Pin = GPIO_Pin_6;
GPIO_InitStructure.GPIO_Mode = GPIO_Mode_AN;
GPIO_InitStructure.GPIO_PuPd = GPIO_PuPd_NOPULL ;
GPIO_Init(GPIOA, &GPIO_InitStructure);
/* ADC Common Init **********************************************************/
ADC_CommonInitStructure.ADC_Mode = ADC_Mode_Independent;
ADC_CommonInitStructure.ADC_Prescaler = ADC_Prescaler_Div2;
ADC_CommonInitStructure.ADC_DMAAccessMode = ADC_DMAAccessMode_Disabled;
ADC_CommonInitStructure.ADC_TwoSamplingDelay = ADC_TwoSamplingDelay_5Cycles;
ADC_CommonInit(&ADC_CommonInitStructure);
/* ADC1 Init ****************************************************************/
ADC_InitStructure.ADC_Resolution = ADC_Resolution_12b;
ADC_InitStructure.ADC_ScanConvMode = ENABLE;
ADC_InitStructure.ADC_ContinuousConvMode = ENABLE;
ADC_InitStructure.ADC_ExternalTrigConvEdge = ADC_ExternalTrigConvEdge_None;
ADC_InitStructure.ADC_DataAlign = ADC_DataAlign_Right;
ADC_InitStructure.ADC_NbrOfConversion = 2;
ADC_Init(ADC1, &ADC_InitStructure);
/* ADC1 regular channe6 configuration *************************************/
ADC_RegularChannelConfig(ADC1, ADC_Channel_6, 1, ADC_SampleTime_3Cycles);
// ADC_RegularChannelConfig(ADC1, ADC_Channel_7, 2, ADC_SampleTime_3Cycles);
/* Enable DMA request after last transfer (Single-ADC mode) */
ADC_DMARequestAfterLastTransferCmd(ADC1, ENABLE);
/* Enable ADC1 DMA */
ADC_DMACmd(ADC1, ENABLE);
/* Enable ADC1 */
ADC_Cmd(ADC1, ENABLE);
}
宇宙惯例,沙发自留
顺道消灭〇回复
支持,顶一下。
看下回复可见~~
对wifi还有好多未开发区域啊
封装起来也就算了 说明给的详细点也成啊 同感
wifi封装成库,理解起来感觉吃力,楼主不错的分享
好的 学习下
,看看
zzzz
回复为了看内容
楼主能测试一下UDP 1秒内能传送的最大数据量吗?可不可以考虑把OV的图像快速传送到上位机,实现视频传送
这个倒没具体测试过,不过图像应该问题不大,实时性就可能稍微差点
资源很好啊