[Visit https://www.nuvoton.com/iot_startup for more IoT solutions]
This is a reference sample code for bridge of Serial and Ethernet, built on Mbed OS and NuMaker IoT boards that support Ethernet function, such as NuMaker-IoT-M487 and NuMaker-IoT-M467 boards.
Supported Features
- Multiple UART ports
- Static IP address or obtain via DHCP
- TCP Client or Server mode
- Optional simple web server for UART and Ethernet configuration
Prerequisite
- A NuMaker-IoT-M487 or NuMaker-IoT-M467 board
- Two jumper wires
- A LAN cable
- A MicroUSB cable
- A MicroSD card
- Mbed Studio – the development tool
- Register an Mbed OS account to use Mbed tools for free https://os.mbed.com
- Download Mbed Studio and install it https://os.mbed.com/studio
- Terminal tool
- Such as Putty, Realterm, etc., that support TCP connections
Create a Project
- Run Mbed Studio
- Log in your Mbed account if it asks
- Click File
- Click Import Program
- Fill the example link to URL https://os.mbed.com/teams/Nuvoton/code/ ... -Ethernet/
- Click Add Program
- Active program is “NuMaker-mbed-Serial-to-Ethernet”
- Select target board
- Select target on the pull down menu, use M487 or M467 as keyword to find “NuMaker-IoT-M487” or “NuMaker-IoT-M467”, then select it. Or you can
- Plug the Nu-Link USB on NuMaker-IoT board. Mbed Studio will detect it automatically and then select Yes when it ask you to set the device as active.
View and Modify Code
- Click main.cpp to show the source code
- Declaration of UART pins in main.cpp. UART1 and UART5 pins are set for NuMaker-IoT-M487 board
- Refer following files for other settings
- main.cpp
- ste_config.h
- mbed_app.json
Modify Settings for NuMaker-IoT-M467
If use NuMaker-IoT-M467, modify the declaration of UART pins to match the test environment.
- Still in main.cpp, find the UART declaration for NuMaker-IoT-M467
- Modify pins of serial_1 to PA_5 and PA_4 as follows.
Code: Select all
#elif defined (TARGET_NUMAKER_IOT_M467)
BufferedSerial serial_0(PB_3, PB_2); // UART1
BufferedSerial serial_1(PA_5, PA_4); // UART5
BufferedSerial serial_2(PF_7, PF_6); // UART4
Check the Default IP address
The IP address is static by default. In main.cpp, find the IP_ADDRESS, NETWORK_MASK, and GATEWAY_ADDRESS settings. Two sets of settings have been declared.
- 192.168.1.2 / 255.255.255.0
- 169.254.108.2 / 255.255.0.0 (Default)
When the LAN on Windows PC doesn’t obtain IP from DHCP server, Windows will set its LAN IP to 169.254.xxx.xxx and netmask to 255.255.0.0.
The IP address of NuMaker IoT board set to 169.254.xxx.xxx is convenient for test without modifying Windows LAN setting. Of course, you can modify it to meet your needs.
Build Code
Click hammer to build code. After build finish, you get the firmware image file NuMaker-mbed-Serial-to-Ethernet.bin in the folder
- Windows
C:\Users\YOUR-ACCOUNT\Mbed Programs\NuMaker-mbed-Serial-to-Ethernet\BUILD\NUMAKER_IOT_M4X7\ARMC6 - Linux
~/Mbed Programs/NuMaker-mbed-Serial-to-Ethernet/BUILD/NUMAKER_IOT_M4X7/ARMC6 - X is 8 or 6 depends on your target board
Program Firmware via Nu-Link-ME
- Make sure that all ISW1 switches set to ON
- Plug in USB cable to PC
- A NuMicro MCU drive appears on PC
- Copy NuMaker-mbed-Serial-to-Ethernet.bin to NuMicro MCU drive to program firmware.
Program Firmware via Nu-Link2-ME
- Make sure that no.1 & 2 are ON and no.3 & 4 are OFF of ICESW2
- Plug in USB cable to PC
- A NuMicro MCU drive appears on PC
- Copy NuMaker-mbed-Serial-to-Ethernet.bin to NuMicro MCU drive to program firmware. Or
- Click “Connect” icon to download firmware and run.
Open the Terminal for Message Output
Mbed Studio automatically open a serial monitor when a Mbed Enabled board detected. Or you can run a terminal tool to open Nu-Link(2)-ME virtual COM port. Remember to select baud rate to 115200. printf() will output here.
Run
- Connect LAN cable for PC and NuMaker IoT board
- Press RESET button on NuMaker IoT board to restart firmware.
- The terminal shows the running messages
Run terminal tool (Putty as example) twice.
- The one connects to 169.254.108.2 port 10001, and
- the other one connects to 169.254.108.2 port 10002.
- Then click “Open” to start the TCP connection.
The NuMaker IoT board terminal will show the established connections.
Type any characters and enter key in one Putty, these characters will show on the other Putty. So data are sent and received between two Putties.
Use browser to connect to http://169.254.108.2 to display the configuration page. A MicroSD card must be inserted to NuMaker IoT board to save settings.
That's all.
![Wink ;)](./images/smilies/icon_e_wink.gif)