Preface
The library function of shineblink core development board (Core for short) supports WIFI function, so only a few lines of code can realize the network communication (TCP, UDP, MQTT) function based on esp8266 WIFI module. Here we mainly introduce the function of realizing network communication through TCP. For more information about TCP, UDP, MQTT communication, please go to shineblink.com.
PS: Core can realize Wifi/Ble/NB/Lora/ThreadMesh/RFID/Eth/Usb/RS485/RS232 communication, as well as more than 30 sensors/10 hardware peripherals/10 MCU built-in functions with only five or six lines of code, and these functions can run simultaneously in up to 5 random combinations. More information about Core can be found on shineblink.com.
1. Functions implemented in this routine
A TCP connection is established between the ESP8266 module and the specified server. The ESP8266 sends a data packet (5 bytes) to the server every 5 seconds, and the server sends a data packet (10 bytes) to the ESP8266 every 1 second.
The server address can be either an IP address or a domain name.
2. Introduction to TCP functions provided by Core
Core has encapsulated TCP operations into three simple API functions: LIB_WifiTcpConfig(), LIB_WifiTcpRecv(), and LIB_WifiTcpSend(). You only need to call these three APIs to connect esp8266 to the router and send and receive data with the server. Core will automatically handle abnormal situations in communication and try to restore communication (such as abnormal disconnection with the router, abnormal disconnection of the TCP connection with the server, network abnormality, etc.). You can achieve long-term and stable online communication with WIFI without considering these complex situations.
3. Wiring Diagram
4. Materials List
name |
Recommended purchase link (or you can make your own) |
Module/Chip Hardware Data Download |
ESP8266 Wi-Fi Module |
Purchase link : There are many stores selling this module on Taobao. You only need to choose a similar one. It doesn’t matter if the appearance is different, as long as you can connect it according to the wiring diagram above. |
Download |
router |
|
|
Disclaimer: The merchants recommended here do not have any cooperative relationship with Core. You can buy from other merchants or channels as long as the functions are similar.
5. Complete code (establishing a connection with the server via IP address)
A TCP connection is established between the ESP8266 module and the specified server. The ESP8266 sends a data packet (5 bytes) to the server every 5 seconds, and the server sends a data packet (10 bytes) to the ESP8266 every 1 second.
server_addr = "192.168.1.101" --You can also directly write the server domain name here, such as "www.shineblink.com" etc.
server_port = 8080
ap_ssid = "mywifi" -- router account
ap_passwd = "abc123" --Router password
--Configure USB to work in virtual serial port mode, so that calling the print() function will print the output on the computer serial terminal
LIB_UsbConfig("CDC")
--Enable the system 10 millisecond timer to start working
LIB_10msTimerConfig("ENABLE")
--Set the esp8266 Wifi module to occupy TX0, RX0, D5 pins, TCP Client mode
--Router account: mywifi, router password: abc123, server ip: 192.168.1.101, port number: 8080
--Heartbeat packet interval 0 seconds (heartbeat mechanism is not used). If you need to use it, please refer to the detailed description of p7 parameter of LIB_WifiTcpConfig function in ApiDoc document.
LIB_WifiTcpConfig("UART0","D5",ap_ssid,ap_passwd,server_addr,server_port,0)
--Variable initialization
cnt_10ms = 0
send_tab = {1,2,3,4,5} --Data that needs to be sent to the server
--Define 10ms interrupt callback function
function LIB_10msTimerCallback()
cnt_10ms = cnt_10ms + 1
end
--Start the big cycle
while(GC(1) == true)
do
--Check whether the data sent by the server is received. If received, print out the received data
recv_flag,recv_tab = LIB_WifiTcpRecv()
if recv_flag == 1 then
print(string.format("tcp client receive %d bytes", #recv_tab))
for k,v in ipairs(recv_tab) do
print(k,v)
end
end
--Send a packet of data to the server every 5 seconds
if cnt_10ms >= 500 then --5000ms
cnt_10ms = 0
LIB_WifiTcpSend(send_tab)
end
end
If you are interested, the library functions starting with LIB in the above code can be queried in the API documentation using Ctrl+ F.
Code running results
Here we run the " Network Debugging Assistant " software on a computer in the local area network (192.168.1.101) to simulate the TCP Server for debugging.
(1) The server receives data as follows:
(2) The data received by the Client (Core development board) is as follows: