uPing - Ping library for MicroPython

Discussion about programs, libraries and tools that work with MicroPython. Mostly these are provided by a third party.
Target audience: All users and developers of MicroPython.
shawwwn
Posts: 16
Joined: Tue Feb 06, 2018 5:22 am

uPing - Ping library for MicroPython

Post by shawwwn » Fri Sep 21, 2018 10:08 am

MicroPython implementation of ICMP Ping.
Works exactly like linux's ping command.

https://gist.github.com/shawwwn/91cc897 ... 34c38195fb

Example:

Code: Select all

>>> import uping
>>> uping.ping('google.com')
PING google.com (64.233.185.138): 64 data bytes
84 bytes from 64.233.185.138: icmp_seq=1, ttl=40, time=71.636004 ms
84 bytes from 64.233.185.138: icmp_seq=2, ttl=40, time=70.365000 ms
84 bytes from 64.233.185.138: icmp_seq=3, ttl=40, time=71.599998 ms
84 bytes from 64.233.185.138: icmp_seq=4, ttl=40, time=82.879000 ms
4 packets transmitted, 4 packets received
(4, 4)
>>>

Usage:

Code: Select all

uping.ping(host, count=4, timeout=5000, interval=10, quiet=False, size=64)
  • @host: host's domain name or ip address to ping.
  • @count: how many packets to send
  • @timeout: stop if not receiving anything within a certain time period (milliseconds)
  • @interval: time between sending each packet
  • @quiet: suppress print messages
  • @size: ICMP packet size (bytes), must > 16
  • @return: tuple(number of packet transmitted, number of packets received)

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

Re: uPing - Ping library for MicroPython

Post by pythoncoder » Sat Sep 22, 2018 4:34 am

Very useful!

A couple of points about the code. PEP 8 suggests indenting by 4 spaces. Tabs carry hazards (notably the case where an invisible space creeps in and causes indentation errors) so are generally not recommended.

On the plus side I've been writing Python for donkey's years and it never occurred to me to write

Code: Select all

not quiet and print('something')
Neat!
Peter Hinch
Index to my micropython libraries.

shawwwn
Posts: 16
Joined: Tue Feb 06, 2018 5:22 am

Re: uPing - Ping library for MicroPython

Post by shawwwn » Sat Sep 22, 2018 7:57 am

Just changed. Thanks!

KJM
Posts: 158
Joined: Sun Nov 18, 2018 10:53 pm
Location: Sydney AU

Re: uPing - Ping library for MicroPython

Post by KJM » Sun Nov 18, 2018 11:02 pm

I can't get this to run on a pycom gpy. File "/flash/lib/uping.py", line 46, in ping AttributeError: 'function' object has no attribute 'randint' which I fudged with h.id=100 as a workaround

then File "/flash/lib/uping.py", line 51, in ping AttributeError: 'module' object has no attribute 'SOCK_RAW' which I'm not smart enough to fix.

I'm grief stricken! I could so use a pinger on this platform. Thoughts anyone on a fix?

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

Re: uPing - Ping library for MicroPython

Post by pythoncoder » Mon Nov 19, 2018 6:41 am

If you're running the Pycom fork I can only think that this fork lacks some useful features. You may be able to work round the randint issue with uos.urandom(4) with suitable conversion to an integer. FWIW the value of usocket.SOCK_RAW is 3 but if the underlying implementation is absent from their fork I'm afraid your only option is to talk nicely to Pycom.

More and more of the issues in this forum are down to variations between forks/ports :(
Peter Hinch
Index to my micropython libraries.

HermannSW
Posts: 197
Joined: Wed Nov 01, 2017 7:46 am
Contact:

Re: uPing - Ping library for MicroPython

Post by HermannSW » Mon Nov 19, 2018 4:49 pm

The randint issue only occurs on my ESP8266, not on the ESP32.
This little diff fixes it:

Code: Select all

$ diff uping.py.orig uping.py
45c45,46
<     h.id = urandom.randint(0, 65535)
---
>     urandom.seed(utime.ticks_us())
>     h.id = urandom.getrandbits(16)
$ 
But with that change both ESP modules run into index error:

Code: Select all

>>> uping.ping("woogle.de")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "uping.py", line 53, in ping
IndexError: list index out of range
>>> 
Last line here is it:

Code: Select all

$ head -53 uping.py | tail -9
    urandom.seed(utime.ticks_us())
    h.id = urandom.getrandbits(16)
    h.seq = 1

    # init socket
    sock = usocket.socket(usocket.AF_INET, usocket.SOCK_RAW, 1)
    sock.setblocking(0)
    sock.settimeout(timeout/1000)
    addr = usocket.getaddrinfo(host, 1)[0][-1][0] # ip address
$ 
P.S:
I inserted "print(usocket.getaddrinfo(host, 1))", empty list explains error

Code: Select all

>>> uping.ping("google.de")
[]
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "uping.py", line 54, in ping
IndexError: list index out of range
>>> ### closed ###
$ 
P.P.S:
My fault, modules were AP only with no internet connection.
Now I made station active as well:

Code: Select all

>>> usocket.getaddrinfo("google.de",1)[0][-1][0]
'172.217.20.131'
>>> 

But now uping.py errors out on line 50 (socket init, ESP8266 module):

Code: Select all

>>> uping.ping("google.de")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "uping.py", line 50, in ping
OSError: [Errno 22] EINVAL
>>> 
What is wrong with this simple statement on 1.9.4 ESP8266 MicroPython?

Code: Select all

>>> sock = usocket.socket(usocket.AF_INET, usocket.SOCK_RAW, 1)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
OSError: [Errno 22] EINVAL
>>> 
Pico-W Access Point static file webserver:
https://github.com/Hermann-SW/pico-w

Tiny MicroPython robots (the PCB IS the robot platform)
viewtopic.php?f=5&t=11454

webrepl_client.py
https://github.com/Hermann-SW/webrepl#webrepl-shell

johncblacker
Posts: 9
Joined: Sun Nov 11, 2018 8:11 pm

Re: uPing - Ping library for MicroPython

Post by johncblacker » Tue Jan 08, 2019 8:35 pm

Running into a problem trying to run uping.ping('192.168.0.2')
I get:

>>> uping.ping('192.168.0.2')
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "uping.py", line 49, in ping
OSError: [Errno 22] Invalid argument

Tried again:

>>> import usocket
>>> sock = usocket.socket(usocket.AF_INET, usocket.SOCK_RAW, 1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
OSError: [Errno 22] Invalid argument

Doesn't like that socket create statment...

johncblacker
Posts: 9
Joined: Sun Nov 11, 2018 8:11 pm

Re: uPing - Ping library for MicroPython

Post by johncblacker » Tue Jan 08, 2019 8:53 pm

experimenting a little and found that it definitely doesn't like "usocket.SOCK_RAW"; however, I can print usocket.SOCK_RAW and get an output value of 3...

pmp-p
Posts: 13
Joined: Mon Apr 24, 2017 12:45 am

Re: uPing - Ping library for MicroPython

Post by pmp-p » Sat Jan 19, 2019 2:15 pm

@johncblacker: more likely it's the 1 it does not like, as IPPROTO_ICMP is probably not handled on various ports.

nedoskiv
Posts: 20
Joined: Fri Jan 18, 2019 10:48 am

Re: uPing - Ping library for MicroPython

Post by nedoskiv » Thu May 09, 2019 6:45 am

Works good for me, on esp32 using latest build

I can just suggest one fix to be maded, when you trying to ping hostname that cannot be resolved, it fails:

Code: Select all

>>> uping.ping("diar.bg")
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "uping.py", line 52, in ping
IndexError: list index out of range

Post Reply