Git Product home page Git Product logo

micropython-mdns's Introduction

Micropython MDNS

PyPi MIT licensed

Image showing service discovery

A pure Python implementation of MDNS and the Service Discovery protocol over MDNS for MicroPython.

Intended Audience

You should not use this library if you "just" require MDNS A record lookup and Host annoucement if there is already baked in support in your MicroPython distribution. This is for example the case with the default ESP32 MicroPython distribution since v1.12. This will be in all cases more resource efficient.

If you, however, require additional functionality like Service Discovery and Annoucement, you should use this library. It supports all functionality of existing basic MDNS implementations plus these features. You will not loose any functionality.

Installation

The library is available on PyPi and can be installed via upip.

import upip
upip.install("micropython-mdns")

Alternatively you can also use the new mip package manager:

import mip
mip.install("github:cbrand/micropython-mdns")

For using this library, native C type implementations of MDNS which use the MDNS service port need to be disabled. For example, this project has been developed on the ESP32 which MicroPython implementation per default has a basic MDNS implementation available. This does only support local A record lookups and A record responding of its own host address.

The releases page on this project publishes a firmware.mp.1.13.bin which is MicroPython 1.13 with MDNS disabled as well as a firmware.mp.1.15.bin for MicroPython 1.15 in each release for easy usage. Both can also be built when having docker locally installed by running in the console the build command:

make build

Individually it is also possible to build the desired version via:

make compile-micropython-1-14
make compile-micropython-1-15
make compile-micropython-1-16
make compile-micropython-1-17
make compile-micropython-1-18
make compile-micropython-1-19
make compile-micropython-1-20

Refer to the config directory to see the configuration files when baking this into your own MicroPython ESP32 build.

Other MicroPython implementations might not require any changes inside of the firmware.

Usage

The library requires uasyncio to function. All handling is done asynchronously.

Examples on how to utilize the libraries can be found in the examples folder.

Reference

A basic API reference for the public API is inside of the REFERENCE.md.

Caveats

  • Depending on your MicroPython implementation, you must disable MDNS in the firmware.
    • For ESP32 use the Dockerfile for the specific Micropython version in the root directory.
    • For RPI Pico instructions see the instructions in this issue.
  • Currently no support for IPv6 is implemented.
  • Depending how chatty the network is, service responders and discovery might require a lot of memory. If the memory is filled by the buffer of the underlying socket, the socket is closed and reopened which looses data. It, however, seems to work fine enough in tests on an ESP32 without external memory. Depending on the project size, a module with external RAM might be advisable.

License

The library is published under the MIT license.

micropython-mdns's People

Contributors

bgamari avatar cbrand avatar hakancelikdev avatar snyk-bot avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar

micropython-mdns's Issues

mDNS on RPi Pico

I am trying to run the service_responder.py example program on a RPi Pico with Ethernet port and get this error:

OSError: [Errno 98] EADDRINUSE

Does this mean the mDNS service is already active in the MP firmware? If so, should I see the Pico advertising its host name in the local network? I tried with an mDNS browser but could not find the Pico.

MP version is this:

MicroPython v1.19.1-910-g4937174b4 on 2023-03-01; W5500-EVB-Pico with RP2040

Thanks.

How to compile / build into micropython binary / firmware file as frozen module

I'm trying to bake this project into my firmware image as frozen module but somehow fail.
Adding cloned and checked out git repo to a port's/board's manifest file like this:
freeze("$(MPY_DIR)/lib/mdns/src/mdns_client")
does successfully freeze and compile respective files in.
However - at least on the UNIX port - importing does not work:

>>> import service_discovery
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "service_discovery/__init__.py", line 3, in <module>
  File "service_discovery/discovery.py", line 6, in <module>
ImportError: can't perform relative import

Probably I'm overlooking something obvious.
Worst case is that the 'from ..X import Y`-syntax simply doesn't work when compiled in as frozen module.

If it's supposed to work, I guess a quick note on how to embed this software as frozen-module in the docs/README might be a good idea.

OSError: -202

I installed your firmware v1.15 and also the official Micropython firmware for ESP32 v1.14 (then I run import upip
upip.install("micropython-mdns") but in both cases i got the same error:

Traceback (most recent call last):
File "", line 35, in
File "uasyncio/core.py", line 1, in run_until_complete
File "uasyncio/core.py", line 1, in run_until_complete
File "uasyncio/core.py", line 1, in run_until_complete
File "/lib/mdns_client/client.py", line 182, in getaddrinfo
File "/lib/mdns_client/client.py", line 195, in mdns_getaddr
OSError: -202

CODE:

import network
import uasyncio
from mdns_client import Client
from machine import freq
freq(240000000)

ssid="MySsiD"
password="MyPassword"

wlan = network.WLAN(network.STA_IF)
wlan.active(True)

print("Intendando conectar a la red:", ssid)
wlan.connect(ssid, password)
while wlan.isconnected() == False:
    pass

ip, mask, gate, dns = wlan.ifconfig()
print("Conexion exitosa a la red", ssid)
print("Ip optenida:", ip)
print("Mascara de subred:", mask)
print("Puerta de enlace:", gate)
print("DNS:", dns)
print("\n")
gc.collect()


loop = uasyncio.get_event_loop()
client = Client(wlan.ifconfig()[0])

print(loop.run_until_complete(client.getaddrinfo("the-other-device.local", 80)))

Not functioning on Pico W

Hello!
I'm currently trying to use this library with my Pico W to advertise its web server to the LAN. Unfortunately while trying to discover the device using Zeroconf and Android DNS-SD it seems that they both have issues with parsing the packets from the Pico.

I have compiled my own version of micropython in order to disable the MDNS port from being used and I have made several attempts to rewrite the serialisation code with no success (likely because of my limited DNS knowledge lol).
I do understand this report is slightly ambiguous, therefore I have attached both the code I am using, and the debug logs from zeroconf while trying to discover the service to support any debugging of such issues

  • Micropython Version: MicroPython v1.19.1-102-g07cae9178-dirty (Version i built myself built on v1.19 on github)
  • micropython-mdns Version: 1.1.1

Pico W Code & Zeroconf Log: https://gist.github.com/twitch0001/801619733a543917a54ff9fdb7924d2a

I will attempt to provide more information as I debug this more :)

Many thanks,
twitch

Possibility to specify different service name and hostname

There are some devices which announce a service name which is different from the hostname. I have here a Tradfri gateway which shows up in avahi-browse as follows:

= wlp3s0 IPv4 gw-b072bf27b113                               _coap._udp           local
   hostname = [TRADFRI-Gateway-b072bf27b113.local]
   address = [192.168.1.60]
   port = [5684]

So the service name is gw-b072bf27b113 but the host name is TRADFRI-Gateway-b072bf27b113.local.

Can we do the same with the micropython-mdns library?

In the Responder class we can specify the host parameter which seems to be used for both service name and hostname? Is it possible to specify different names in some way?

Thanks.

help building for esp32 and micropython 1.15?

Hi, very sorry to trouble you but we're using your awesome work to give us better mdns and want to use it with latest MP. Can you provide a bit of guidance as to what you did - for example in menuconfig - to build using your mdns bits and not the defaults? Thanks for your time.

New package manager: mip?

Is there a way to install this package via the new package manager mip on the latest micropython 1.19? It no longer uses upip, thanks!

Provide binary for RPi Pico

In #9 (comment) it was identified how to embed onto the RPi Pico the Micropython library.

Use this knowledge to release the RPi libraries where MicroPython MDNS is embedded.

Service Discoverty was working fine, but no longer.

1st of all. thanks for this library.
It's really made my little IOT project act a lot more professional.

Let me see if I can explain what strangeness I'm seeing.

Specifically, I'm setting them up with ServiceDiscovery enabled, so I can easily find them in my mobile app.
I have 3 prototypes of my project on my network, and they have been working perfectly for nearly a year.
But, just about a month ago I noticed they slowly started .. not being discoverable in my app.
One day I could see all 3. A few days later only 2.. then 1. Now none.

I went through a multitude of various debugging steps.
rebooting, re-flashing, re-compiling micropython, updating my esp-idf to 5.0 and micropython from 1.20 to 1.22
I tried increasing the MAX_PACKET_SIZE value in the constants.py. Not knowing what to set it to, I just tried 2700.
Initially, I thought that fixed it, as the 1st device immediately started advertising. But, the other 2 did not.
And, after a restart, the 1st device no long advertises either.

I am running a custom built micropython 1.22.1 firmware with it's mdns disabled, on a Seeed Studio Xaio ESP32S3 with SPIRAM (8M).

One thing that is especially peculiar, is that if I leave the devices running for some time (, one of them will randomly respond to ptr requests, and announce itself. But, it will only do it once and then go silent again.

I have been using both my app that looks for a specific protocol name.
As well as avahi-browse (I run Linux on my workstation)

The devices in question are getting DHCP addresses , and are available on my network if I manually navigate to them.

Nothing has changed on my network or wifi, other than just peoples phones coming and going.
I am running a Ubiquity wifi , with a pair of access points.

I'm hoping you can help me diagnose what has happened that seems to be blocking my devices from responding.
At least direct me down some more troubleshooting ideas.

Cheers
Shane

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.