EasyManua.ls Logo

Espressif Systems ESP8266EX - Page 22

Espressif Systems ESP8266EX
41 pages
Print Icon
To Next Page IconTo Next Page
To Next Page IconTo Next Page
To Previous Page IconTo Previous Page
To Previous Page IconTo Previous Page
Loading...
!
3. Sample Codes
printf("ESP8266󰲧UDP󰲧task󰲧>󰲧socket󰲧OK!\n");
3. Bind a local port.
do{
󰲧󰲧󰲧󰲧󰲧ret󰲧=󰲧bind(sock_fd,󰲧(struct󰲧sockaddr󰲧*)&server_addr,󰲧sizeof(server_addr));
󰲧󰲧󰲧󰲧󰲧if󰲧(ret󰲧!=󰲧0)󰲧{
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧printf("ESP8266󰲧UDP󰲧task󰲧>󰲧captdns_task󰲧failed󰲧to󰲧bind󰲧sock!\n");
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧vTaskDelay(1000/portTICK_RATE_MS);
󰲧󰲧󰲧󰲧󰲧󰲧}
󰲧󰲧󰲧󰲧}while(ret󰲧!=󰲧0);
󰲧󰲧󰲧
󰲧󰲧󰲧󰲧printf("ESP8266󰲧UDP󰲧task󰲧>󰲧bind󰲧OK!\n");
4. Receive and transmit the UDP data.
while(1){
󰲧󰲧󰲧󰲧󰲧memset(udp_msg,󰲧0,󰲧UDP_DATA_LEN);
󰲧󰲧󰲧󰲧󰲧memset(&from,󰲧0,󰲧sizeof(from));
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧
󰲧󰲧󰲧󰲧󰲧setsockopt(sock_fd,󰲧SOL_SOCKET,󰲧SO_RCVTIMEO,󰲧(char󰲧*)&nNetTimeout,󰲧sizeof(int));
󰲧󰲧󰲧󰲧󰲧fromlen󰲧=󰲧sizeof(struct󰲧sockaddr_in);
󰲧󰲧󰲧󰲧󰲧ret󰲧=󰲧recvfrom(sock_fd,󰲧(uint8󰲧*)udp_msg,󰲧UDP_DATA_LEN,󰲧0,(struct󰲧sockaddr󰲧*)&from,
(socklen_t󰲧*)&fromlen);
󰲧󰲧󰲧󰲧󰲧if󰲧(ret󰲧>󰲧0)󰲧{
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧printf("ESP8266󰲧UDP󰲧task󰲧>󰲧recv󰲧%d󰲧Bytes󰲧from󰲧%s,󰲧Port󰲧%d\n”,ret,󰲧
inet_ntoa(from.sin_addr),󰲧ntohs(from.sin_port));
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧sendto(sock_fd,(uint8*)udp_msg,󰲧ret,󰲧0,󰲧(struct󰲧sockaddr󰲧*)&from,󰲧fromlen);
󰲧󰲧󰲧󰲧󰲧󰲧󰲧󰲧}
}
if(udp_msg){
󰲧󰲧󰲧󰲧󰲧free(udp_msg);
󰲧󰲧󰲧󰲧󰲧udp_msg󰲧=󰲧NULL;
}
close(sock_fd);
5. Compile the application program, generate firmware and download it into the ESP8266
module.
6. Power off the module, and change it to operation mode; then power on the module and
run the program.
Result:
ip:192.168.1.103,mask:255.255.255.0,gw:192.168.1.1
ESP8266󰲧UDP󰲧task󰲧>󰲧socket󰲧ok!
Espressif
! /!18 37
2017.05

Other manuals for Espressif Systems ESP8266EX

Related product manuals