2511 views|4 replies

8

Posts

0

Resources
The OP
 

Zero-based development of WIFI devices [Copy link]

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.insert image description here

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

insert image description here

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.

insert image description here

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:

insert image description here

(2) The data received by the Client (Core development board) is as follows:

insert image description here

This post is from MCU

Latest reply

Haha, I’m afraid I can’t do it without any basic knowledge on microcontrollers.   Details Published on 2020-12-6 10:26
 

1942

Posts

2

Resources
2
 

The board is pretty good, but the basics are not the same as the WIFI basics.

This post is from MCU

Comments

Well, zero WIFI basics, and also zero MCU development basics.  Details Published on 2020-12-5 22:56
 
 

8

Posts

0

Resources
3
 
w494143467 posted on 2020-12-5 13:35 The board is pretty good, but the zero foundation is not the WIFI zero foundation.

Well, zero WIFI basics, and also zero MCU development basics.

This post is from MCU

Comments

Haha, I’m afraid I can’t do it without any basic knowledge on microcontrollers.  Details Published on 2020-12-6 10:26
 
 
 

1942

Posts

2

Resources
4
 
wadewade posted on 2020-12-5 22:56 Well, zero WIFI foundation, and also zero MCU development foundation.

Haha, I’m afraid I can’t do it without any basic knowledge on microcontrollers.

This post is from MCU

Comments

That's also possible. Many routines have nothing to do with the microcontroller.  Details Published on 2020-12-13 12:49
 
 
 

8

Posts

0

Resources
5
 
w494143467 Published on 2020-12-6 10:26 Haha, I'm afraid I can't do it with zero foundation in microcontrollers.

That's also possible. Many routines have nothing to do with the microcontroller.

This post is from MCU
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

Related articles more>>

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list