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("\r\n發送成功!\r\n");

}

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, "OK\r\n", 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, "OK\r\n", 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, "OK\r\n", 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, "OK\r\n", 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, "OK\r\n", 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, "OK\r\n", 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, "OK\r\n", 4);
	        }
	        else if(memcmp((char *)USART_RX_BUF, "AT+RESET", 11) == 0)
	        {
	        	espconn_sent((struct espconn *) arg, "OK\r\n", 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, "\r\n", strlen("\r\n"));
	        	send_cnt += strlen("\r\n");
	        	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, "\r\n", strlen("\r\n"));
	        	send_cnt += strlen("\r\n");
	        	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, "\r\n", strlen("\r\n"));
	        	send_cnt += strlen("\r\n");
	        	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, "\r\n", strlen("\r\n"));
	        	send_cnt += strlen("\r\n");
	        	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, "\r\n", strlen("\r\n"));
	        	send_cnt += strlen("\r\n");
	        	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, "\r\n", strlen("\r\n"));
	        	send_cnt += strlen("\r\n");
	        	espconn_sent((struct espconn *) arg, send_buf, send_cnt);
	        }
	        else
	        {
	        	memcpy(send_buf + send_cnt, "NOT SUPPPORT!\r\n", strlen("NOT SUPPPORT!\r\n"));
	        	send_cnt += strlen("NOT SUPPPORT!\r\n");
	        	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("連接錯誤,錯誤代碼爲:%d\r\n", 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_init\r\n");
	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 官網鏈接

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章