Receiving time information from NTP server using WIZ750SR
In an environment where a network is available, time information can be obtained smoothly using the provided NTP library.
Contents
In this Contents, assuming a device that cannot connect directly to the network, we will summarize the contents of receiving time information from an NTP server using an S2E (Serial to ethernet) device. The S2E device is a Serial to ethernet device, and as you might expect from the name, it is a device that converts data between serial and Ethernet. Test configuration is simple.
When request data is transmitted through the serial interface, the packet is converted to Ethernet and transmitted to the configured Remote Host (NTP server in this case). When a response is received from the server, it is received through Ethernet and converted back to serial data to receive information.
WIZ750SR is an S2E product equipped with W7500P MCU and is designed to include a pin header and RJ-45 connector.
WIZ750SR Settings
WIZnet supports a GUI-based Configuration Tool (hereinafter referred to as Config Tool) that can set up the WIZ750SR product.
First, we used the Config tool to set the operation mode and connection information as shown below.
- Remote host / port
- Remote host: pool.ntp.org
- Remote port: 123
- Operation mode: UDP
- Local port: 123
When you enter the settings and press the Setting button at the top, the device settings are changed and saved and the device is automatically reset.
If a pop-up appears with the message Device configuration complete, the setup is successful.
Check debug message
Operation status can be monitored through the debug port.
When using EVB, an FTDI chip is included, so you can supply power and check debug messages by connecting via a Micor 5pin USB cable.
Python Code
Python code was used to convert data transmission and reception using the WIZ750SR's serial interface.
The NTP-related code was referenced from the link below, and the part that was described as socket communication in the original code was changed to serial communication.
import struct
import time
import serial
PORT = "COM27"
comport = serial.Serial(PORT, 115200, timeout=1)
def RequestTimefromNtpWithS2E():
print('[RequestTimefromNtpWithS2E]')
REF_TIME_1970 = 2208988800 # Reference time
reqdata = b'\x1b' + 47 * b'\0'
comport.write(reqdata)
ser_recvdata = comport.readline()
#print('ser_recvdata:', ser_recvdata)
if ser_recvdata:
ts = struct.unpack('!12I', ser_recvdata)[10]
ts -= REF_TIME_1970
print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(ts)))
return time.ctime(ts), ts
if __name__ == "__main__":
print(RequestTimefromNtpWithS2E())
Install package
pyserial
Install the package to transmit data via serial .
pip install pyserial
or,
python -m pip install pyserial
Execution
python requestTimefromNtp.py
This is an example code that receives time information from an NTP server, obtains a Timestamp value, and %Y-%m-%d %H:%M:%S
outputs it in format.
If the current time is displayed, the time information has been retrieved correctly.