Building W5500 TCP Client with Lua Scripts(Language) — FlexLua Platform
Use Lua scripts with FlexLua to build a W5500 TCP client quickly. Core APIs handle sockets and errors, so only simple Lua code is needed.
📌 What This Article Covers
This article introduces how to build a W5500 TCP Client using only Lua scripts through the FlexLua platform.
Common challenges when developing Ethernet-based IoT devices:
- Socket management, reconnection handling, exception recovery… more code than expected
- Simple tests require full C firmware rebuild every time
- Quick PoC development becomes burden due to complex development environment
FlexLua simplifies this by wrapping W5500 control into Lua script APIs. Complex network logic is handled by the Core, and users only need to write a few lines of Lua code.
💡 What is Lua?
Lua is a lightweight scripting language that runs efficiently even on MCUs.
Key Features:
- Small code size: Optimized for embedded environments
- Simple syntax: Easy to learn for C/Python developers
- Easy C integration: Hardware functions can be easily exposed as APIs
- Instant execution: Script modification → immediate execution (no compilation needed)
Why use Lua in embedded systems?
Traditional C firmware development:
Code edit → Compile → Flash upload → Test → Edit again → Repeat...Lua script approach:
Script edit → Immediate execution → See results → DoneFlexLua is a platform that encapsulates W5500 network APIs on top of this Lua environment.
🎯 What This Example Does
Using ShineBlink Core board + W5500 module, this example creates a basic TCP client that communicates with a server:
Operation:
- W5500 (Client) → Server: Send 5 bytes every 5 seconds
- Server → W5500 (Client): Send 10 bytes every 1 second
This is a simple demo showing basic TCP Client communication implemented purely in Lua script.
🔧 Core APIs — Only 3 Functions
FlexLua Core simplifies W5500 operation into three functions:
| Function | Purpose |
|---|---|
LIB_EthTcpConfig() | TCP configuration and initialization (IP, MAC, port, etc.) |
LIB_EthTcpRecv() | Receive data from server |
LIB_EthTcpSend() | Send data to server |
Combining these three functions is all you need to build W5500-based TCP Client functionality.
🔌 Hardware Connection
Connection between ShineBlink Core and W5500:
- SPI Communication: MOSI, MISO, SCK, CS
- Power: 5V (or 3.3V compatible)
- Reset Pin: Optional
This example drives W5500 at 5V, but 3.3V operation is also supported.
📦 W5500 Module Used
Standard W5500 Ethernet module features:
- Built-in RJ45 connector
- SPI interface
- 3.3V/5V compatible
💻 Example Lua Code
This script runs directly in FlexLua and operates the complete W5500 TCP Client functionality:
LIB_UsbConfig("CDC")
LIB_10msTimerConfig("ENABLE")
mac = {0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff}
sip = {192, 168, 1, 200}
subm = {255,255,255,0}
gw = {192, 168, 1, 1}
dns = {8,8,8,8}
tip = {192, 168, 1, 103}
s_port = 1234
c_port = 54321
heart_beat = 0
LIB_EthTcpConfig("D4","D5",mac,sip,subm,gw,dns,tip,s_port,c_port,heart_beat)
cnt_10ms = 0
send_tab = {1,2,3,4,5}
function LIB_10msTimerCallback()
cnt_10ms = cnt_10ms + 1
end
while(GC(1) == true)
do
recv_flag,recv_tab = LIB_EthTcpRecv()
if recv_flag == 1 then
print(string.format("w5500 tcp client receive %d bytes", #recv_tab))
for k,v in ipairs(recv_tab) do
print(k,v)
end
end
if cnt_10ms >= 500 then
cnt_10ms = 0
LIB_EthTcpSend(send_tab)
end
endCode Breakdown:
Initialization:
- Configure USB as CDC for debugging output
- Enable 10ms timer for periodic control
Network Configuration:
- Set W5500 IP address (192.168.1.200)
- Define server IP (192.168.1.103) and port (1234)
- Configure MAC address and gateway
Main Loop:
- Check for received data with
LIB_EthTcpRecv() - Send data every 5 seconds (10ms × 500 counts)
- Print received data in real-time
📡 Actual Operation Results
Server Side Data Log
Server operation:
- Sends 10 bytes to Core every 1 second
- Receives 5 bytes from Core every 5 seconds
Client (Core + W5500) Side Reception
Core device receiving from server:
- 10 bytes received every 1 second
- Real-time data output display
⭐ Advantages of This Approach
🚀 Rapid Prototyping
- Instant testing without C code compilation/upload
- Change behavior by simply editing scripts
🎯 Simple API
- Complex socket control logic hidden
- TCP Client implementation with just 3 functions
🔧 Easy Maintenance
- Network configuration changes via script editing
- No hardware reprogramming required
📚 Gentle Learning Curve
- Accessible even for C/embedded beginners
- Intuitive Lua syntax
🎓 Application Areas
FlexLua + W5500 combination is suitable for:
- IoT Sensor Nodes: Simple data collection and transmission
- Test Equipment: Quick network functionality verification
- Educational Platforms: Embedded networking learning
- Prototype Development: Fast implementation in PoC stage
💬 Conclusion
This example demonstrates that W5500 TCP Client functionality can be implemented with very simple scripts through the FlexLua environment.
Key Takeaways:
- ✅ Complex network socket control automatically handled by Core
- ✅ Users implement TCP communication with just a few lines of Lua
- ✅ Instant testing without compilation
- ✅ Dramatically increased development speed for embedded makers
"You can build network devices with W5500 without C firmware" — this is the new possibility that FlexLua presents.

