Script running on python2.7 and python3.5 but not on micropython

General discussions and questions abound development of code with MicroPython that is not hardware specific.
Target audience: MicroPython Users.
Post Reply
ing00hal
Posts: 2
Joined: Mon Aug 13, 2018 12:25 pm

Script running on python2.7 and python3.5 but not on micropython

Post by ing00hal » Mon Aug 13, 2018 12:49 pm

When running following script on an Adafruit HUZZAH32 with MicroPython v1.9.4-447-g86e0b2553 on 2018-08-13; ESP32 module with ESP32 I got an error :

>>>
paste mode; Ctrl-C to cancel, Ctrl-D to finish
=== from socket import AF_INET, SOCK_DGRAM
=== import sys
=== import socket
=== import struct, time
===
=== def getNTPTime(host = "pool.ntp.org"):
=== port = 123
=== buf = 1024
=== address = (host, port)
=== msg = '\x1b' + 47 * '\0'
=== msg = msg.encode('utf-8')
=== # reference time (in seconds since 1900-01-01 00:00:00)
=== TIME1970 = 2208988800 # 1970-01-01 00:00:00
===
=== # connect to server
=== client = socket.socket(AF_INET, SOCK_DGRAM)
=== client.sendto(msg, address)
=== msg, address = client.recvfrom(buf)
===
=== t = struct.unpack("!12I", msg)[10]
=== t -= TIME1970
=== return time.ctime(t).replace(" "," ")
===
=== if __name__ == "__main__":
=== print(getNTPTime())
===
===
Traceback (most recent call last):
File "<stdin>", line 25, in <module>
File "<stdin>", line 17, in getNTPTime
ValueError: invalid arguments
>>>

User avatar
pythoncoder
Posts: 5956
Joined: Fri Jul 18, 2014 8:01 am
Location: UK
Contact:

Re: Script running on python2.7 and python3.5 but not on micropython

Post by pythoncoder » Thu Aug 16, 2018 2:17 pm

I suggest you look at this solution.
Peter Hinch
Index to my micropython libraries.

ing00hal
Posts: 2
Joined: Mon Aug 13, 2018 12:25 pm

Re: Script running on python2.7 and python3.5 but not on micropython

Post by ing00hal » Thu Aug 16, 2018 5:50 pm

Thank you very much. Works great!

Ingmar Hallberg

Post Reply