ESP8266 UDP通信

#include "driver/uart.h"
#include "espconn.h"

void ICACHE_FLASH_ATTR
user_rf_pre_init(void){}

struct espconn user_udp_espconn;

ETSTimer connect_timer;
ETSTimer test_timer;

//udp发送函数
void ICACHE_FLASH_ATTR user_udp_send(void *data)
{
    char yladdr[6] = {0};
    char DeviceBuffer[40] = {0};//将获取的MAC地址格式化输出到一个buffer里面
	
	os_printf("正在发送...\r\n");
    
    wifi_get_macaddr(STATION_IF, yladdr);//查询MAC地址
    os_sprintf(DeviceBuffer,"设备地址为:"MACSTR"\r\n", MAC2STR(yladdr));//格式化MAC地址
    espconn_sent(&user_udp_espconn, DeviceBuffer, os_strlen(DeviceBuffer));
}

//udp发送回调
void ICACHE_FLASH_ATTR user_udp_sent_cb(void *arg)
{
	os_timer_disarm(&test_timer);
	os_timer_setfn(&test_timer, user_udp_send, NULL);
	os_timer_arm(&test_timer, 1000, 0);//1秒钟发送一次
}

//udp接收回调
void ICACHE_FLASH_ATTR user_udp_recv_cb(void *arg,
        char *pdata,
        unsigned short len)
{
	os_printf("UDP已经接收数据:%s", pdata);
}

//创建UDP连接
void ICACHE_FLASH_ATTR wifi_conned(void *arg)
{
	uint8 status = 0;
	static uint8 count = 0;

	count++;
	os_timer_disarm(&connect_timer);
	
	status = wifi_station_get_connect_status();
	if(status == STATION_GOT_IP)
	{
		wifi_set_broadcast_if(STATIONAP_MODE); //设置UDP广播的发送接口
		user_udp_espconn.type = ESPCONN_UDP;
		user_udp_espconn.proto.udp = (esp_udp*)os_zalloc(sizeof(esp_udp));
		user_udp_espconn.proto.udp->local_port = 2525;
		user_udp_espconn.proto.udp->remote_port = 1112;

		const char udp_remote_ip[] = {255,255,255,255}; //用于存放远程IP地址
		os_memcpy(&user_udp_espconn.proto.udp->remote_ip, udp_remote_ip, sizeof(udp_remote_ip));

		espconn_regist_recvcb(&user_udp_espconn, user_udp_recv_cb); //接收回调函数
		espconn_regist_sentcb(&user_udp_espconn, user_udp_sent_cb); //发送回调函数
		espconn_create(&user_udp_espconn); //创建UDP连接
		user_udp_send(NULL);

		return;
	}
	else
	{
		if(count >= 7)
		{
			os_printf("wifi连接失败!");
        	return;
        }
	}
	
	os_timer_arm(&connect_timer, 2000, 0);
}

//加入当前可用wiff
void ICACHE_FLASH_ATTR scan_done(void *arg, STATUS status)
{
	char ssid[] = "Feixun_1003";
	char password[] = "12345678";
	struct station_config stationConf = {0};
	
	if (status == OK)
	{
		os_memcpy(&stationConf.ssid, ssid, os_strlen(ssid)); //加入当前可用wiff
		os_memcpy(&stationConf.password, password, os_strlen(password));	//密码
		wifi_station_set_config_current(&stationConf);
		wifi_station_connect();
		os_timer_setfn(&connect_timer, wifi_conned, NULL);
		os_timer_arm(&connect_timer, 2000, 0);
	}
}

void to_scan(void)
{
	wifi_station_scan(NULL, scan_done);

	return;
}

//配置ESP8266
void user_init()
{
	struct softap_config config = {0};
	char ssid[] = "ESP8266";
	char password[] = "12345678";

	uart_init(BIT_RATE_115200, BIT_RATE_115200);
	os_delay_us(60000);

	wifi_set_opmode(STATIONAP_MODE);

	wifi_softap_get_config(&config);

	config.ssid_len = os_strlen(ssid);
	os_memset(config.ssid, 0, sizeof(config.ssid));
	os_memcpy(config.ssid, ssid, config.ssid_len);
	os_memset(config.password, 0, sizeof(config.password));
	os_memcpy(config.password, password, os_strlen(password));

	wifi_softap_set_config(&config);

	system_init_done_cb(to_scan);

	return;
}

com
udp

posted @ 2017-07-19 13:06  thomas_blog  阅读(447)  评论(0编辑  收藏  举报