当前位置:网站首页>Esp8266 access to cloud platform ----- DNS domain name connection server
Esp8266 access to cloud platform ----- DNS domain name connection server
2022-07-27 01:29:00 【Paradise_ Violet】
Catalog
Suppose I want to visit Baidu search server
In the callback function of software timing
In the network connection initialization function
DNS_ End of domain name resolution _ Callback function
TCP Callback function for successful connection establishment
Preface
Introduce 8266 How to get through the domain name ip Address , So as to connect to the corresponding server on the Internet
【 domain name 】Domain Name, A string of characters separated by dots , It's a certain station on the Internet / Name of a group of computers .
【DNS】 ==Domain Name System【 The domain name system 】 It enables us to directly use domain names to access the corresponding computers or servers on the Internet
Suppose I want to visit Baidu search server
The first one is The method is to use ip Address to access , Such as 123.125.115.110 Successfully visit Baidu search server
The second kind It is accessed through the domain name of Baidu search www.baidu.com, First, the domain name system will convert this domain name into the corresponding ip Address followed by ip Address to visit Baidu search server
adopt 【 Ruisheng net 】 domain name Parsing 【 Ruisheng net 】 Of IP Address ,8266 Use the parsed IP Address to 【 Ruisheng net 】
#define DN_Server "www.rationmcu.com" // 【 Ruisheng net 】 domain name
stay user_init in
take 8266 Set to sta Mode and software timing
void ICACHE_FLASH_ATTR user_init(void)
{
uart_init(115200,115200); // Initialize the serial port baud rate
os_delay_us(10000); // Wait for the serial port to stabilize
os_printf("\r\n=================================================\r\n");
os_printf("\t Project:\t%s\r\n", ProjectName);
os_printf("\t SDK version:\t%s", system_get_sdk_version());
os_printf("\r\n=================================================\r\n");
// OLED Display initialization
//------------------------------------------------------------
OLED_Init(); // OLED initialization
OLED_ShowString(0,0,"ESP8266-Client"); // ESP8266-Client
OLED_ShowString(0,2,"IP:"); // ESP8266_IP
OLED_ShowString(0,4,"rationmcu.com"); // www.rationmcu.com
OLED_ShowString(0,6,"IP:"); // The server _IP
//------------------------------------------------------------
LED_Init_JX(); // LED initialization
ESP8266_STA_Init_JX(); // ESP8266_STA initialization
OS_Timer_IP_Init_JX(1000,1); // 1 Second repetition timing ( obtain IP Address )
}In the callback function of software timing
Judge if you succeed in obtaining ip Address , Then turn off the timer and initialize the network connection
// Callback function of software timing
//=========================================================================================================
void ICACHE_FLASH_ATTR OS_Timer_IP_cb(void)
{
u8 C_LED_Flash = 0; // LED Scintillation meter
struct ip_info ST_ESP8266_IP; // ESP8266 Of IP Information
u8 ESP8266_IP[4]; // ESP8266 Of IP Address
// Successful access to WIFI【STA In mode , If open DHCP( Default ), be ESO8266 Of IP Address by WIFI Router automatic allocation 】
//-------------------------------------------------------------------------------------
if( wifi_station_get_connect_status() == STATION_GOT_IP ) // Determine whether to obtain IP
{
wifi_get_ip_info(STATION_IF,&ST_ESP8266_IP); // obtain STA Of IP Information
ESP8266_IP[0] = ST_ESP8266_IP.ip.addr; // IP Eight digits higher than the address == addr Lower eight
ESP8266_IP[1] = ST_ESP8266_IP.ip.addr>>8; // IP Next highest eight digits of address == addr Lower eight
ESP8266_IP[2] = ST_ESP8266_IP.ip.addr>>16; // IP Address lower eight digits == addr Second highest eight
ESP8266_IP[3] = ST_ESP8266_IP.ip.addr>>24; // IP Lower eight digits of address == addr Top eight digit
// Show ESP8266 Of IP Address
//------------------------------------------------------------------------------------------------
os_printf("\nESP8266_IP = %d.%d.%d.%d\n",ESP8266_IP[0],ESP8266_IP[1],ESP8266_IP[2],ESP8266_IP[3]);
OLED_ShowIP(24,2,ESP8266_IP); // OLED Show ESP8266 Of IP Address
//------------------------------------------------------------------------------------------------
// Access WIFI After success ,LED Flash 3 Time
//----------------------------------------------------
for(; C_LED_Flash<=5; C_LED_Flash++)
{
GPIO_OUTPUT_SET(GPIO_ID_PIN(4),(C_LED_Flash%2));
delay_ms(100);
}
os_timer_disarm(&OS_Timer_IP); // off timer
ESP8266_NetCon_Init_JX(); // Initialize the network connection (TCP signal communication )
}
}In the network connection initialization function
Assign a value to the local port and the remote server port , Here we only know the domain name of the remote server , I don't know about the remote server ip Address , So far end server ip The address cannot be assigned temporarily
Use this API To carry out DNS initialization , Parameter 2 is represented by macro definition . Parameter 3 ip Address structure pointer, so we need to define a ip Address structure . Parameter 4 is the callback function after domain name resolution
When we finish executing this statement ,8266 Domain name resolution
// Get the corresponding IP Address //【 Parameters 1: Network connection structure pointer / Parameters 2: Domain name string pointer / Parameters 3:IP Address structure pointer / Parameters 4: Callback function 】 //------------------------------------------------------------------------------------------------- espconn_gethostbyname(&ST_NetCon, DN_Server, &IP_Server, DNS_Over_Cb_JX);after Successfully registered connection callback function 、 Exception disconnect callback function , But you can't take 8266 Connect to tcp_server, Because the server is 【 Ruisheng net 】 Of ip We don't know the address yet , When 8266 After the domain name resolution , He will enter Callback function for the end of domain name resolution
void ICACHE_FLASH_ATTR ESP8266_NetCon_Init_JX()
{
// Structure assignment
//--------------------------------------------------------------------------
ST_NetCon.type = ESPCONN_TCP ; // Set to TCP agreement
ST_NetCon.proto.tcp = (esp_tcp *)os_zalloc(sizeof(esp_tcp)); // Open up memory
// ESP8266 As TCP_Client, Want to connect TCP_Server, You must know TCP_Server Of IP Address
//--------------------------------------------------------------------------------
ST_NetCon.proto.tcp->local_port = espconn_port() ; // Local port 【 Get available ports 】
ST_NetCon.proto.tcp->remote_port = 80; // Target port 【HTTP Port number 80】
//u8 remote_ip[4] = {121,198,34,91}; // The goal is IP【www.rationmcu.com】
//os_memcpy(ST_NetCon.proto.udp->remote_ip,remote_ip,4);
// Get the corresponding IP Address
//【 Parameters 1: Network connection structure pointer / Parameters 2: Domain name string pointer / Parameters 3:IP Address structure pointer / Parameters 4: Callback function 】
//-------------------------------------------------------------------------------------------------
espconn_gethostbyname(&ST_NetCon, DN_Server, &IP_Server, DNS_Over_Cb_JX);
// Successfully registered connection callback function 、 Exception disconnect callback function
//--------------------------------------------------------------------------------------------------
espconn_regist_connectcb(&ST_NetCon, ESP8266_TCP_Connect_Cb_JX); // register TCP Callback function for successful connection establishment
espconn_regist_reconcb(&ST_NetCon, ESP8266_TCP_Break_Cb_JX); // register TCP Callback function with abnormally disconnected connection
// Connect TCP server
//----------------------------------------------------------
//espconn_connect(&ST_NetCon); // Connect TCP-server
}DNS_ End of domain name resolution _ Callback function
If ip The address structure pointer is equal to 0 The domain name resolution fails , If ip The address structure pointer is not equal to 0 And parsed ip The address is not 0 Words , The domain name resolution is successful , The serial port prints the string of successful domain name resolution and obtains the server ( Ruisheng net ) Of ip Address , take ip Copied to the tcp Connect , Serial port printing and oled Show Ruisheng's IP Address , Then call API take 8266 Connect to Ruisheng
// DNS_ End of domain name resolution _ Callback function 【 Parameters 1: Domain name string pointer / Parameters 2:IP Address structure pointer / Parameters 3: Network connection structure pointer 】
//=========================================================================================================
void ICACHE_FLASH_ATTR DNS_Over_Cb_JX(const char * name, ip_addr_t *ipaddr, void *arg)
{
struct espconn * T_arg = (struct espconn *)arg; // Cache network connection structure pointer
//………………………………………………………………………………
if(ipaddr == NULL) // Domain name resolution failed
{
os_printf("\r\n---- DomainName Analyse Failed ----\r\n");
return;
}
//……………………………………………………………………………………………………………
else if (ipaddr != NULL && ipaddr->addr != 0) // Domain name resolution succeeded
{
os_printf("\r\n---- DomainName Analyse Succeed ----\r\n");
IP_Server.addr = ipaddr->addr; // Get server IP Address
// The server that will be resolved to IP The address is set to TCP Remote end of connection IP Address
//------------------------------------------------------------------------------
os_memcpy(T_arg->proto.tcp->remote_ip, &IP_Server.addr, 4); // Setup server IP Address
// Show 【 Ruisheng net 】 Of IP Address
//------------------------------------------------------------------------------
os_printf("\r\nIP_Server = %d.%d.%d.%d\r\n", // Serial printing
*((u8*)&IP_Server.addr), *((u8*)&IP_Server.addr+1),
*((u8*)&IP_Server.addr+2), *((u8*)&IP_Server.addr+3));
OLED_ShowIP(24,6,T_arg->proto.tcp->remote_ip); // OLED Show
//------------------------------------------------------------------------------
// Successfully registered connection callback function 、 Exception disconnect callback function
//----------------------------------------------------------------------------------------------
//espconn_regist_connectcb(T_arg, ESP8266_TCP_Connect_Cb_JX); // register TCP Callback function for successful connection establishment
//espconn_regist_reconcb(T_arg, ESP8266_TCP_Break_Cb_JX); // register TCP Callback function with abnormally disconnected connection
// Connect TCP server
//----------------------------------------------------------
espconn_connect(T_arg); // Connect TCP-server
}
}TCP Callback function for successful connection establishment
When tcp After the connection is established successfully , Serial printing ESP8266_TCP_Connect_OK The string of successful connection
void ICACHE_FLASH_ATTR ESP8266_TCP_Connect_Cb_JX(void *arg)
{
espconn_regist_sentcb((struct espconn *)arg, ESP8266_WIFI_Send_Cb_JX); // Register the callback function for sending network data successfully
espconn_regist_recvcb((struct espconn *)arg, ESP8266_WIFI_Recv_Cb_JX); // Register the callback function for successful network data reception
espconn_regist_disconcb((struct espconn *)arg,ESP8266_TCP_Disconnect_Cb_JX); // Registration successfully disconnected TCP Connected callback function
os_printf("\n--------------- ESP8266_TCP_Connect_OK ---------------\n");
} 
Successful connection
explain 8266 Successfully resolved to Ruisheng's ip Address 8.129.233.140, And successfully connected to Ruisheng network
Command prompt
We can see here that he passes the command prompt , To get Ruisheng ip Address , With us 8266 Resolved ip The address is the same , explain 8266 The domain name was successfully resolved to ip Address

Maybe someone will ask , Can we parse in advance 8266 The domain name to be connected ? Then write the resolved address directly to 8266 In the process of , In this case 8266 You don't need domain name resolution
answer : In theory, it is possible to do so , But this is not recommended
①: A server is a computer , It may break , It may be scrapped , It is possible to move the position ,IP The address may change
②: If the server's IP The address has changed , that , Burned into the chip IP The address is wrong , Need to re burn , It is very troublesome in large quantities
③: The domain name will not be changed easily , Just know the domain name , You can get the server IP Address
Therefore, it is recommended that you use domain names to access servers on the Internet
边栏推荐
猜你喜欢

c语言实现三子棋游戏

Li Kou brushes 300 record posts

ESP8266连接乐鑫云平台IOT_Demo

ESP8266 STA_Mode

Finding the greatest common divisor

Jenkins -- Basic -- 5.3 -- system configuration -- global security configuration

ESP8266 STA_UDP_Client

Jenkins--基础--5.3--系统配置--全局安全配置

Warning: IPv4 forwarding is disabled Networking will not work.

【Oracle】获取最近工作日及前N个工作日
随机推荐
MQTT协议------上
ESP8266 STA_TCP_Client
【Oracle】获取最近工作日及前N个工作日
ks 怎么抓salt值?api,did?
Basic syntax of Verilog
Solve the problem that rsyslog service occupies too much memory
5. Legal bracket string
ESP8266连接乐鑫云平台IOT_Demo
ESP8266 STA_Server
Come and help you understand the mobile Internet in a minute
集中式版本控制工具代码合并问题
Next generation Internet: Video Networking
C language to realize the three chess game
Adding, deleting, checking and modifying dynamic sequence table with C language
#快捷键介绍
Cannot find a valid baseurl for repo: HDP-3.1-repo-1
Li Hongyi machine learning (2021 Edition)_ P5-6: small gradient processing
软件测试面试题之软件基础
快来帮您一分钟了解移动互联网
Longest common substring