我是靠谱客的博主 激情饼干,最近开发中收集的这篇文章主要介绍ESP8266 电平采集 数据发送,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

​ 实现使用ESP8266 01 电平采集,并通过UDP将数据发送到指定服务器,ESP8266的配网是通过按键触发配网。

[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-wGuTYbVV-1588820101603)(https://s1.ax1x.com/2020/05/07/YZJo1s.png)]

​ 由于ESP8266有且仅有两个可供使用的IO,即串口使用的TX和RX,GPIO0和GPIO2虽有预留出来,但作为启动管脚不可使用,所以本设置使用以下管脚配置方案:

UART_TX <-------> 按键 上拉

UART_RX <------> 电平采集管脚

具体使用:

ESP通电工作后,长按按键即可进入AP模式,使用无线扫描热点,会发现一个名为“ESP_*********”的AP,连接,密码为“12345678”。

接入AP后,打开网络配置软件,使用TCP通讯,链接服务器 192.168.4.1,port:9999,发送以下指令即可完成配置。

AT指令功能描述
AT+SETSSID:WIFI_2G4设置WIFI名称为WIFI_2G4
AT+SETPWD:1234567890设置WIFI密码为1234567890
AT+SETIP:192.168.1.16设置UDP数据服务器IP
AT+SETPORT:1234设置UDP数据服务器PORT
AT+SETID:20设置ESP8266的ID
AT+SAVEPARM保存参数到FLASH
AT+RESET重启
AT+SSID?查询SSID
AT+PWD?查询PWD
AT+IP?查询UDP IP
AT+ID?查询ID

完成配置后保存重启,打开UDP数据接收端,即可看到ESP8266 返回的数据


udpClient.c

#include "driver/uart.h"  //串口0需要的头文件
#include "osapi.h"  //串口1需要的头文件
#include "user_interface.h" //WIFI连接需要的头文件
#include "espconn.h"//TCP连接需要的头文件
#include "mem.h" //系统操作需要的头文件
#include "../include/gpio.h"  //端口控制需要的头文件

struct espconn user_udp_espconn;
os_timer_t checkTimer_wifistate;


os_timer_t sendTimer_gpiostate;


extern struct param_save param;

void send_udp_data(void)
{
	static uint8_t interval_cnt = 0;
	uint8_t gpio_state = 0;
	uint8_t send_data[50];
	uint8_t cnt = 0;
	uint8_t i = 0;
	uint8_t temp = 0;

	gpio_state = GPIO_INPUT_GET(GPIO_ID_PIN(3));;

	send_data[cnt++] = 0xFF;
	send_data[cnt++] = 0xEE;
	send_data[cnt++] = 0x0;
	send_data[cnt++] = 0x0;
	send_data[cnt++] = 0x10;
	send_data[cnt++] = param.id >> 8;
	send_data[cnt++] = param.id;
	send_data[cnt++] = gpio_state;

	for(i = 2; i < cnt; i++)
	{
		temp += send_data[i];
	}
	send_data[cnt++] = temp;

	send_data[2] = cnt >> 8;
	send_data[3] = cnt;

	if(gpio_state == 0)
	{
		if((interval_cnt % 100) == 0)
		{
			espconn_sent(&user_udp_espconn, send_data, cnt);
			interval_cnt = 0;
		}
		interval_cnt++;
	}
	else
	{
		interval_cnt = 0;
		espconn_sent(&user_udp_espconn, send_data, cnt);
	}
}




void ICACHE_FLASH_ATTR user_udp_sent_cb(void *arg)   //发送
{
	//os_printf("rn发送成功!rn");

}

void ICACHE_FLASH_ATTR user_udp_recv_cb(void *arg,    //接收
		char *pdata, unsigned short len) {
//	os_printf("接收数据:%s", pdata);
//
//	//每次发送数据确保参数不变
//	user_udp_espconn.proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp));
//	user_udp_espconn.type = ESPCONN_UDP;
//	user_udp_espconn.proto.udp->local_port = 2000;
//	user_udp_espconn.proto.udp->remote_port = 8686;
//	const char udp_remote_ip[4] = { 255, 255, 255, 255 };
//	os_memcpy(user_udp_espconn.proto.udp->remote_ip, udp_remote_ip, 4);
//
//	espconn_sent((struct espconn *) arg, "已经收到啦!", strlen("已经收到啦!"));
}

void Check_WifiState(void) {

	uint8 getState = wifi_station_get_connect_status();

	//如果状态正确,证明已经连接
	if (getState == STATION_GOT_IP) {

		os_printf("WIFI连接成功!");
		os_timer_disarm(&checkTimer_wifistate);

		wifi_set_broadcast_if(0x01);	 //设置 ESP8266 发送 UDP广播包时,从 station 接口发送
		user_udp_espconn.proto.udp = (esp_udp *) os_zalloc(sizeof(esp_udp));//分配空间
		user_udp_espconn.type = ESPCONN_UDP;	 		  //设置类型为UDP协议
		user_udp_espconn.proto.udp->local_port = 2000;	 		  //本地端口
		user_udp_espconn.proto.udp->remote_port = param.port;	 		  //目标端口
		const char udp_remote_ip[4] = { param.ip[0], param.ip[1], param.ip[2], param.ip[3] };	 	//目标IP地址(广播)
		os_memcpy(user_udp_espconn.proto.udp->remote_ip, udp_remote_ip, 4);

		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 传输
		//espconn_sent(&user_udp_espconn, "连接服务器", strlen("连接服务器"));


		os_timer_disarm(&sendTimer_gpiostate);	  //取消定时器定时
		os_timer_setfn(&sendTimer_gpiostate, (os_timer_func_t *) send_udp_data,
		NULL);	  //设置定时器回调函数
		os_timer_arm(&sendTimer_gpiostate, 100, 1);	  //启动定时器,单位:毫秒

	}
}

void udp_client_init() //初始化
{
	wifi_set_opmode(0x01); //设置为STATION模式
	struct station_config stationConf;
	os_strcpy(stationConf.ssid, param.ssid);	  //改成你要连接的 路由器的用户名
	os_strcpy(stationConf.password, param.password); //改成你要连接的路由器的密码

	wifi_station_set_config(&stationConf);	  //设置WiFi station接口配置,并保存到 flash
	wifi_station_connect();	  //连接路由器
	os_timer_disarm(&checkTimer_wifistate);	  //取消定时器定时
	os_timer_setfn(&checkTimer_wifistate, (os_timer_func_t *) Check_WifiState,
	NULL);	  //设置定时器回调函数
	os_timer_arm(&checkTimer_wifistate, 500, 1);	  //启动定时器,单位:毫秒
}

tcpClient.c

#include "driver/uart.h"  //串口0需要的头文件
#include "osapi.h"  //串口1需要的头文件
#include "user_interface.h" //WIFI连接需要的头文件
#include "espconn.h"//TCP连接需要的头文件
#include "mem.h" //系统操作需要的头文件
#include "gpio.h"
#include "stdio.h"
struct espconn user_tcp_espconn;
extern struct param_save param;

os_timer_t ApTimer_stayAP;;
os_timer_t TcpTimer_stayAP;;

uint8_t ApTimer_stop = 0;

#define USART_REC_LEN	100
uint8_t at_rx_buf[USART_REC_LEN];

uint8_t USART_RX_BUF[USART_REC_LEN];
uint16_t USART_RX_STA=0;



void tcp_recv_callback(void *arg, uint8_t *data, uint16_t len)
{
	uint16_t i = 0;
	uint8_t ch = 0;

	uint8_t temp;

	uint8_t send_buf[100];
	uint8_t send_cnt = 0;

	for(i = 0; i < len; i++)
	{
		ch = data[i];
	    if((USART_RX_STA&0x8000)==0)
	    {
	        if(USART_RX_STA&0x4000)
	        {
	            if(ch!=0x0a)USART_RX_STA=0;
	            else
	            {
	                USART_RX_STA|=0x8000;
	            }
	        }
	        else
	        {
	            if(ch==0x0d)
	            {
	                USART_RX_STA|=0x4000;
	            }
	            else if(ch == 0x0a)
	            {
	            	USART_RX_STA|=0x8000;
	            }
	            else
	            {
	                USART_RX_BUF[USART_RX_STA&0X3FFF]=ch ;
	                USART_RX_STA++;
	                if(USART_RX_STA>(USART_REC_LEN-1))USART_RX_STA=0;
	            }
	        }
	    }

	    if(USART_RX_STA & 0x8000)
	    {
	    	send_cnt = 0;
	    	memset(send_buf, 0, sizeof(send_buf));
	        if(memcmp((char *)USART_RX_BUF, "AT+SETSSID:", 11) == 0)
	        {
	        	memset(param.ssid, 0, sizeof(param.ssid));

	        	strcpy(param.ssid, USART_RX_BUF + 11);
	        	espconn_sent((struct espconn *) arg, "OKrn", 4);

	        }
	        else if(memcmp((char *)USART_RX_BUF, "AT+SETPWD:", 10) == 0)
	        {
	        	memset(param.password, 0, sizeof(param.password));

	        	strcpy(param.password, USART_RX_BUF + 10);
	        	espconn_sent((struct espconn *) arg, "OKrn", 4);
	        }
	        else if(memcmp((char *)USART_RX_BUF, "AT+SETIP:", 9) == 0)
	        {
	        	uint16_t temp = 0;
	        	uint16_t i = 0;
	        	uint8_t j = 0;
	        	for(i = 9, j = 0; i < strlen(USART_RX_BUF); i++)
	        	{
	        		if(USART_RX_BUF[i] == '.')
	        		{
	        			param.ip[j] = temp;
	        			if(j<3)
	        				j++;
	        			temp = 0;
	        			continue;
	        		}

	        		temp = temp * 10 + (USART_RX_BUF[i] - '0');
	        	}
	        	param.ip[3] = temp;
	        	espconn_sent((struct espconn *) arg, "OKrn", 4);
	        }
	        else if(memcmp((char *)USART_RX_BUF, "AT+SETPORT:", 11) == 0)
	        {
	        	uint16_t temp = 0;
	        	uint16_t i;
	        	for(i = 11; i < strlen(USART_RX_BUF); i++)
	        	{
	        		temp = temp * 10 + (USART_RX_BUF[i] - '0');
	        	}
	        	param.port = temp;
	        	espconn_sent((struct espconn *) arg, "OKrn", 4);
	        }
	        else if(memcmp((char *)USART_RX_BUF, "AT+SETID:", 9) == 0)
	        {
	        	uint16_t temp = 0;
	        	uint16_t i;
	        	for(i = 9; i < strlen(USART_RX_BUF); i++)
	        	{
	        		temp = temp * 10 + (USART_RX_BUF[i] - '0');
	        	}
	        	param.id = temp;
	        	espconn_sent((struct espconn *) arg, "OKrn", 4);
	        }
	        else if(memcmp((char *)USART_RX_BUF, "AT+SAVEPARM", 11) == 0)
	        {
	        	system_param_save_with_protect(ESP_PARAM_START_SEC, &param, sizeof(param));
	        	espconn_sent((struct espconn *) arg, "OKrn", 4);
	        }
	        else if(memcmp((char *)USART_RX_BUF, "AT+SETINTERVAL:", 15) == 0)
	        {
	        	uint16_t temp = 0;
	        	uint16_t i;
	        	for(i = 15; i < strlen(USART_RX_BUF); i++)
	        	{
	        		temp = temp * 10 + (USART_RX_BUF[i] - '0');
	        	}
	        	param.interval = temp;
	        	espconn_sent((struct espconn *) arg, "OKrn", 4);
	        }
	        else if(memcmp((char *)USART_RX_BUF, "AT+RESET", 11) == 0)
	        {
	        	espconn_sent((struct espconn *) arg, "OKrn", 4);
	        	system_restart();
	        }

	        else if(strcmp((char *)USART_RX_BUF, "AT+SSID?") == 0)
	        {
	        	memcpy(send_buf + send_cnt, "SSID:", strlen("SSID:"));
	        	send_cnt += strlen("SSID:");
	        	memcpy(send_buf + send_cnt, param.ssid, strlen(param.ssid));
	        	send_cnt += strlen(param.ssid);
	        	memcpy(send_buf + send_cnt, "rn", strlen("rn"));
	        	send_cnt += strlen("rn");
	        	espconn_sent((struct espconn *) arg, send_buf, send_cnt);
	        }
	        else if(strcmp((char *)USART_RX_BUF, "AT+PWD?") == 0)
	        {
	        	memcpy(send_buf + send_cnt, "Password:", strlen("Password:"));
	        	send_cnt += strlen("Password:");
	        	memcpy(send_buf + send_cnt, param.password, strlen(param.password));
	        	send_cnt += strlen(param.password);
	        	memcpy(send_buf + send_cnt, "rn", strlen("rn"));
	        	send_cnt += strlen("rn");
	        	espconn_sent((struct espconn *) arg, send_buf, send_cnt);
	        }
	        else if(strcmp((char *)USART_RX_BUF, "AT+IP?") == 0)
	        {
	        	memcpy(send_buf + send_cnt, "UDP IP:", strlen("UDP IP:"));
	        	send_cnt += strlen("UDP IP:");
	        	if(param.ip[0] > 99)
	        	{
	        		temp = param.ip[0] / 100 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.ip[0] / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.ip[0] % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}else if(param.ip[0] > 9)
	        	{
	        		temp = param.ip[0] / 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.ip[0] % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else
	        	{
	        		temp = param.ip[0] + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	send_buf[send_cnt++] = '.';
	        	if(param.ip[1] > 99)
	        	{
	        		temp = param.ip[1] / 100 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.ip[1] / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.ip[1] % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}else if(param.ip[1] > 9)
	        	{
	        		temp = param.ip[1] / 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.ip[1] % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else
	        	{
	        		temp = param.ip[1] + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	send_buf[send_cnt++] = '.';

	        	if(param.ip[2] > 99)
	        	{
	        		temp = param.ip[2] / 100 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.ip[2] / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.ip[2] % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}else if(param.ip[2] > 9)
	        	{
	        		temp = param.ip[2] / 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.ip[2] % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else
	        	{
	        		temp = param.ip[2] + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	send_buf[send_cnt++] = '.';

	        	if(param.ip[3] > 99)
	        	{
	        		temp = param.ip[3] / 100 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.ip[3] / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.ip[3] % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}else if(param.ip[3] > 9)
	        	{
	        		temp = param.ip[3] / 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.ip[3] % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else
	        	{
	        		temp = param.ip[3] + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	memcpy(send_buf + send_cnt, "rn", strlen("rn"));
	        	send_cnt += strlen("rn");
	        	espconn_sent((struct espconn *) arg, send_buf, send_cnt);
	        }
	        else if(strcmp((char *)USART_RX_BUF, "AT+PORT?") == 0)
	        {
	        	memcpy(send_buf + send_cnt, "Udp Port:", strlen("Udp Port:"));
	        	send_cnt += strlen("Udp Port:");
	        	if(param.port > 9999)
	        	{
	        		temp = param.port / 10000 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.port / 1000) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.port / 100) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.port / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.port % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else if(param.port > 999)
	        	{
	        		temp = param.port / 1000 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.port / 100) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.port / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.port % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else if(param.port > 99)
	        	{
	        		temp = param.port / 100 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.port / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.port % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}else if(param.port > 9)
	        	{
	        		temp = param.port / 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.port % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else
	        	{
	        		temp = param.port + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	memcpy(send_buf + send_cnt, "rn", strlen("rn"));
	        	send_cnt += strlen("rn");
	        	espconn_sent((struct espconn *) arg, send_buf, send_cnt);
	        }
	        else if(strcmp((char *)USART_RX_BUF, "AT+ID?") == 0)
	        {
	        	memcpy(send_buf + send_cnt, "Id:", strlen("Id:"));
	        	send_cnt += strlen("Id:");
	        	if(param.id > 9999)
	        	{
	        		temp = param.id / 10000 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.id / 1000) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.id / 100) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.id / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.id % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else if(param.id > 999)
	        	{
	        		temp = param.id / 1000 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.id / 100) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.id / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.id % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else if(param.id > 99)
	        	{
	        		temp = param.id / 100 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.id / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.id % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}else if(param.id > 9)
	        	{
	        		temp = param.id / 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.id % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else
	        	{
	        		temp = param.id + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	memcpy(send_buf + send_cnt, "rn", strlen("rn"));
	        	send_cnt += strlen("rn");
	        	espconn_sent((struct espconn *) arg, send_buf, send_cnt);
	        }
	        else if(strcmp((char *)USART_RX_BUF, "AT+INTERVAL?") == 0)
	        {
	        	memcpy(send_buf + send_cnt, "Udp Send interval:", strlen("Udp Send interval:"));
	        	send_cnt += strlen("Udp Send interval:");
	        	if(param.interval > 9999)
	        	{
	        		temp = param.interval / 10000 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.interval / 1000) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.interval / 100) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.interval / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.interval % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	if(param.interval > 999)
	        	{
	        		temp = param.interval / 1000 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.interval / 100) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.interval / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.interval % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	if(param.interval > 99)
	        	{
	        		temp = param.interval / 100 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = (param.interval / 10) % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.interval % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}else if(param.interval > 9)
	        	{
	        		temp = param.interval / 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        		temp = param.interval % 10 + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	else
	        	{
	        		temp = param.interval + '0';
	        		send_buf[send_cnt++] = temp;
	        	}
	        	memcpy(send_buf + send_cnt, "rn", strlen("rn"));
	        	send_cnt += strlen("rn");
	        	espconn_sent((struct espconn *) arg, send_buf, send_cnt);
	        }
	        else
	        {
	        	memcpy(send_buf + send_cnt, "NOT SUPPPORT!rn", strlen("NOT SUPPPORT!rn"));
	        	send_cnt += strlen("NOT SUPPPORT!rn");
	        	espconn_sent((struct espconn *) arg, send_buf, send_cnt);
	        }

	        USART_RX_STA = 0;
	        memset(USART_RX_BUF, 0, sizeof(USART_RX_BUF));
	    }
	}
}


void ICACHE_FLASH_ATTR server_recv(void *arg, char *pdata, unsigned short len) {
//	if(ApTimer_stop == 0)
//	{
//		ApTimer_stop = 1;
//		os_timer_disarm(&ApTimer_stayAP);
//	}
	tcp_recv_callback(arg,pdata, len);
}
void ICACHE_FLASH_ATTR server_sent(void *arg) {
	os_printf("发送成功!");
}
void ICACHE_FLASH_ATTR server_discon(void *arg) {
	os_printf("连接已经断开!");
}

void ICACHE_FLASH_ATTR server_listen(void *arg)  //注册 TCP 连接成功建立后的回调函数
{
	struct espconn *pespconn = arg;
	espconn_regist_recvcb(pespconn, server_recv);  //接收
	espconn_regist_sentcb(pespconn, server_sent);  //发送
	espconn_regist_disconcb(pespconn, server_discon);  //断开
}
void ICACHE_FLASH_ATTR server_recon(void *arg, sint8 err) //注册 TCP 连接发生异常断开时的回调函数,可以在回调函数中进行重连
{
	os_printf("连接错误,错误代码为:%drn", err); //%d,用来输出十进制整数
}

void Inter213_InitTCP(uint32_t Local_port) {
	user_tcp_espconn.proto.tcp = (esp_tcp *) os_zalloc(sizeof(esp_tcp)); //分配空间
	user_tcp_espconn.type = ESPCONN_TCP; //设置类型为TCP协议
	user_tcp_espconn.proto.tcp->local_port = Local_port; //本地端口

	espconn_regist_connectcb(&user_tcp_espconn, server_listen); //注册 TCP 连接成功建立后的回调函数
	espconn_regist_reconcb(&user_tcp_espconn, server_recon); //注册 TCP 连接发生异常断开时的回调函数,可以在回调函数中进行重连
	espconn_accept(&user_tcp_espconn); //创建 TCP server,建立侦听
	espconn_regist_time(&user_tcp_espconn, 180, 0); //设置超时断开时间 单位:秒,最大值:7200 秒

}

void start_into_sta(void)
{
	os_timer_disarm(&ApTimer_stayAP);	  //取消定时器定时
	os_printf("udp_client_initrn");
	udp_client_init();
}


void AP_WIFI_Init() {

	struct softap_config apConfig;

	uint32_t chip_id = system_get_chip_id();
	uint8_t temp = 0;
	char wifi_ssid[12];
	wifi_ssid[0] = 'E';
	wifi_ssid[1] = 'S';
	wifi_ssid[2] = 'P';
	wifi_ssid[3] = '_';

	temp = (chip_id & 0x0000000F);
	wifi_ssid[4] = (temp > 9) ? (temp - 10 + 'A') : (temp + '0');
	temp = (chip_id & 0x000000F0) >> 4;
	wifi_ssid[5] = (temp > 9) ? (temp - 10 + 'A') : (temp + '0');
	temp = (chip_id & 0x00000F00) >> 8;
	wifi_ssid[6] = (temp > 9) ? (temp - 10 + 'A') : (temp + '0');
	temp = (chip_id & 0x0000F000) >> 12;
	wifi_ssid[7] = (temp > 9) ? (temp - 10 + 'A') : (temp + '0');
	temp = (chip_id & 0x000F0000) >> 16;
	wifi_ssid[8] = (temp > 9) ? (temp - 10 + 'A') : (temp + '0');
	temp = (chip_id & 0x00F00000) >> 20;
	wifi_ssid[9] = (temp > 9) ? (temp - 10 + 'A') : (temp + '0');
	temp = (chip_id & 0x0F000000) >> 24;
	wifi_ssid[10] = (temp > 9) ? (temp - 10 + 'A') : (temp + '0');
	temp = (chip_id & 0xF0000000) >> 28;
	wifi_ssid[11] = (temp > 9) ? (temp - 10 + 'A') : (temp + '0');
	wifi_set_opmode_current(0x02);
	apConfig.ssid_len = 12;
	os_strcpy(apConfig.ssid, wifi_ssid);
	os_strcpy(apConfig.password, "12345678");

    //设置加密模式
	apConfig.authmode = 3;
	 //信标间隔时槽100 ~ 60000 ms
	apConfig.beacon_interval = 100;
	 //通道号1 ~ 13
	apConfig.channel = 1;
	 //最大连接数
	apConfig.max_connection = 4;
	 //隐藏SSID :false
	 apConfig.ssid_hidden = 0;

	//设置 WiFi soft-AP 接口配置,并保存到 flash
	wifi_softap_set_config(&apConfig);
}
void tcp_service_init()		//初始化
{

//	os_timer_disarm(&ApTimer_stayAP);	  //取消定时器定时
//	os_timer_setfn(&ApTimer_stayAP, (os_timer_func_t *) start_into_sta,
//	NULL);	  //设置定时器回调函数
//	os_timer_arm(&ApTimer_stayAP, 11000, 1);	  //启动定时器,单位:毫秒


	AP_WIFI_Init();
	Inter213_InitTCP(9999);		//本地端口
}


ESP8266 学习链接

ESP8266 官网链接

最后

以上就是激情饼干为你收集整理的ESP8266 电平采集 数据发送的全部内容,希望文章能够帮你解决ESP8266 电平采集 数据发送所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(62)

评论列表共有 0 条评论

立即
投稿
返回
顶部