directory

    • preface
    • A, this routine to achieve the function
    • 2. TCP functions provided by Core
    • Wiring diagram
    • Iv. List of materials
    • 5, complete code (through the IP address to establish a connection with the server)
      • Code run result

preface

The library functions of the shineBlink Core development board (core for short) support the WIFI function, so it only takes a few lines of code to implement the network communication (TCP, UDP, MQTT) function based on the esp8266 WIFI module. For more information about TCP, UDP, and MQTT communication, go to shineblink.com.

PS:Core in only five or six lines of code can be realized Wifi/Ble/NB/Lora ThreadMesh/RFID/Eth/Usb/RS485 / RS232 communication, and more than 30 sensors/more than 10 kinds of hardware peripherals/more than 10 kinds of Mcu functions inside, And these functions can run in up to five random combinations simultaneously. Learn more about Core at shineblink.com.

A, this routine to achieve the function

The ESP8266 module establishes a TCP connection with the specified server. The ESP8266 sends a packet (5 bytes) to the server every 5 seconds, and the server sends a packet (10 bytes) to the ESP8266 every 1 second.

The server address can be either an IP address or a domain name.

2. TCP functions provided by Core

Core encapsulates 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. In addition, Core will automatically deal with abnormal situations in communication and try to restore communication (such as abnormal disconnection with router, abnormal disconnection with TCP connection with server, abnormal network, etc.). You can achieve long-term and stable online communication of WIFI without considering these complicated situations.

Wiring diagram

Iv. List of materials

The name of the Recommended purchase link (or you can make your own) Module/chip hardware information download
ESP8266 wifi module Buy links, there are many shops on Taobao for this module, you just need to choose a similar one, it is ok if the appearance is different, as long as you can connect according to the wiring diagram above. Download address
The router

Disclaimer: The vendors recommended here do not have any partnership with Core, you can go to other vendors or channels to buy, as long as the features are similar.

5, complete code (through the IP address to establish a connection with the server)

The ESP8266 module establishes a TCP connection with the specified server. The ESP8266 sends a packet (5 bytes) to the server every 5 seconds, and the server sends a packet (10 bytes) to the ESP8266 every 1 second.

server_addr = "192.168.1.101" You can also write the domain name of the server, such as "www.shineblink.com"
server_port = 8080
ap_ssid = "mywifi" -- Router account
ap_passwd = "abc123" -- Router password

-- Configure the USB to work in virtual serial mode, so that calling print() will print out the output on the serial terminal of the computer
LIB_UsbConfig("CDC")
- Enables the system to work on the 10 ms timer
LIB_10msTimerConfig("ENABLE")
-- Set esp8266 Wifi module to occupy TX0, RX0, D5 pins, TCP Client mode
Router account :mywifi Router password :abc123 server IP address :192.168.1.101 Port number :8080
-- Heartbeat packet interval 0 seconds (no heartbeat mechanism is used). See the p7 parameter description of the LIB_WifiTcpConfig function in the ApiDoc documentation if necessary
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 the 10ms interrupt callback function
function LIB_10msTimerCallback(a)
    cnt_10ms = cnt_10ms + 1
end

-- Start the big cycle
while(GC(1) = =true)
do
	Print the received data from the server
	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
Copy the code

If you are interested, the library functions starting with LIB that appear in the above code can be queried in the API documentation by Ctrl+F.

Code run result

Here, we run the software “Network Debugging Assistant” on a computer (192.168.1.101) in the LAN 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: