mDNS is working with Bonjour

Posted on
Page
of 3
/ 3
Last Next
  • What is mDNS?
    It gives a name to your ESP8266 server that the Apple Bonjour browser add in can use to access your server.
    https://www.youtube.com/watch?v=G_gCfi3M­doU


    In this example code I named the server “bob”.
    I downloaded and installed Bonjour from this site and installed it on my Windows 7 system.
    http://download.cnet.com/Bonjour-for-Win­dows/3000-18507_4-93550.html
    On the IE browser command line I enter
    “bob.local”
    Or
    http://bob.local:8080/

    //MDNS_HTTPserver1.js
    //5 OCT 2016
    
    //espruino board with ESP8266
    //PICO  with ESP8266
    
    //var Hardware=0; //Espruino board
    var Hardware =1; //PICO
    
    var SSID="ssid";
    var key= "router passcode";
    
    var Serial;
    var Wifi;
    
    // "bob" is the server name
    // in IE browser with Bonjour installed
    //  bob.local
    //  http://bob.local:8080/
    
    var mDNS="AT+MDNS=1,\"bob\",\"http\",8080\r\­n";
    
    function mysend(a){
      Wifi.at.cmd(a, 1000, function(d){console.log(d+"xxx");});
    }
    
    //var ddata="";
    function test(){
    if(Hardware===1)Serial=Serial2;
    if(Hardware===0)Serial=Serial4;
    
    if(Hardware===1){
     digitalWrite(B9,1); // enable on Pico Shim V2
     Serial.setup(115200, { rx: A3, tx : A2 }); //Pico
    }
    if(Hardware===0)Serial.setup(115200, { rx: C11, tx : C10 }); 
    //espruino board
    
    console.log("Start connection process");
    var wifi = require("ESP8266WiFi_0v25").connect(Seri­al, function(err) {
      if (err)return 1;// throw err;
      Wifi=wifi;
    console.log("Reset the ESP8266");
      wifi.reset(function(err) {
        if (err)return 1;// throw err;
    Wifi.at.cmd("AT+CWMODE_CUR=3\r\n", 1000, function(d){console.log(d+"xx1");});
    Wifi.at.cmd("AT+CIPMUX=1\r\n", 1000, function(d){console.log(d+"xx2");});
     console.log("Connecting to WiFi");
     wifi.connect(SSID,key, function(err) {
      if (err)return 1;//throw err;
      wifi.getIP(function(l,ip){
       console.log("IP= ",ip,"\n\r");
       console.log("WiFi Connected ");
       mysend(mDNS);
    
    //HTTP or Network Server goes here
    serveHTML();
    ////////////////////////////////////////­////////////////////
         });//end wifi.getIP
        });//end wifi.connectSSID
      });//end wifi.reset
    });//end wifi.connect
    }//end test
    
    
    function serveHTML(){
    var http=require("http").createServer(onPage­Request).listen(8080);
    }//end serveFile
    
    
    function onPageRequest(req, res) { 
    //console.log("Req= ",req);
    //console.log("Header",req.headers);
    if (req.method=="POST") {
      console.log("Post");
      doPost(req,res);
    }else{
      doGet(req,res);
    }//endif
    }//end on PageRequest
    
    var pdata="";
    function doPost(req,res){
    //todo
    }//end doPost
    
    
    //Simple Web Page
    var KMtext="<html><body style=\"text-align:center;margin-left:au­to;margin-right:auto;\"> \r\n<h1>mDNS Demo. bob</h1>\r\n</body> \r\n</html>  \r\n";
    
    function doGet(req,res){
      var a = url.parse(req.url, true);
      console.log("Get");
        res.writeHead(200, {'Content-Type': 'text/html'});
        res.write(KMtext);
        res.end("");
    }
    
    var x;
    console.log("Start");
    x=test();
    if(x!==0)console.log("Try again");//error seen
    
    if(x===0) console.log("Exit Seen");
    
    
    >echo(0);
    Start
    Start connection process
    Try again
    =undefined
    Reset the ESP8266
    Connecting to WiFi
    OKxx1
    OKxx2
    IP=  192.168.1.3
    WiFi Connected
    OKxxx
    Get
    >
    

    https://en.wikipedia.org/wiki/Multicast_­DNS
    Problems with older ESP8266 SDKs
    http://internetofhomethings.com/homethin­gs/?p=426

    Update: The issues I had with the ESP8266 SDK Version 1.0 have been fixed with the Version 1.0.1 release dated 24 April 2015. And now, Version 1.1.1 has been released with even more enhancements.


    1 Attachment

  • Oh wow, nice - so there's an AT+MDNS command? Is that in the standard 0.40 firmware?

    That could be rolled into the existing AT command drivers for Espruino really easily.

    1. AT+MDNS – Set MDNS function
      ESP8266 softAP mode can not support MDNS. Do not contain special characters (for example, "."
      character), or use a protocol name (for example, "http"), when defining "host_name" and
      "server_name" for MDNS.
      AT+WPS – Set WPS function
      Example
      AT+CWMODE=1
      AT+WPS=1
      Command AT+WPS=
      Response OK or ERROR
      Parameter

      1 : start WPS function
      0 : stop WPS function
      AT+MDNS – Set MDNS function
      Example AT+MDNS=1, "espressif", "iot", 8080
      Command AT+MDNS=, , ,
      Response OK or ERROR
      Parameter

      1 : enable MDNS function
      0 : disable MDNS function
      MDNS host name
      MDNS server name
      MDNS server port

    http://bbs.espressif.com/viewtopic.php?t­=1714
    https://cdn.sparkfun.com/assets/learn_tu­torials/4/0/3/4A-ESP8266AT_Instruction_SetEN_v0.30.pdf
    https://espressif.com/en/support/downloa­d/sdks-demos
    https://espressif.com/en/support/downloa­d/overview?keys=&field_type_tid%5B%5D=14­
    http://bbs.espressif.com/

  • Using AT+GMR my ESP8266 reports the following:

    AT version:0.60.0.0(Jan 29 2016 15:10:17)
    SDK version:1.5.2(7eee54f4)

  • Ahh, nice! Looks like an addition in 0.60.

    Shame, as the Espruino WiFi boards currently ship with 0.40, and while it's possible to update firmware, it's a bit of a pain.

  • Latest AT document 2.0
    https://espressif.com/sites/default/file­s/documentation/4a-esp8266_at_instructio­n_set_en.pdf

    37. AT+MDNS – Set MDNS function
    ESP8266 softAP mode cannot support MDNS. Please do not use special characters (for example, "."
    character), or use a protocol name (for example, "http"), when defining "host_name" and
    "server_name" for MDNS.
    AT+WPS – Set WPS function
    Example
    AT+CWMODE=1
    AT+WPS=1
    Command AT+WPS=<enable>
    Response OK
    Parameter
    <enable>
    1 : start WPS function
    0 : stop WPS function
    AT+MDNS – Set MDNS function
    Example AT+MDNS=1, "espressif", "iot", 8080
    Command AT+MDNS=<enable>, <hostname>, <server_name>, <server_port>
    Response OK
    Parameter
    <enable>
    1 : enable MDNS function
    0 : disable MDNS function
    <hostname> MDNS host name
    <server_name> MDNS server name
    <server_port> MDNS server port
    
  • I know this thread is quite old, but in a previous comment Gordon mentioned updating the AT firmware was a bit of a pain... as such the Espruino Wifi v2.04 still ships with AT 0.40. Are there plans to upgrade it to a more recent version?

  • The next batch of WiFi boards I get made will probably have a newer version on but that's some way off given the sales volume and stock I have.

    In the past I've had a lot of success updating the ESP8266 without any need for soldering. I'll have a go now and see what happens.

  • Ok, try this out:

    https://www.espruino.com/WiFi#updating-e­sp8266-firmware

    Let me know if you have any issues - tried this on my Linux dev PC just now and it worked first time. Only trick was finding a firmware that worked :)

  • Did work on first try (Win10) 👏
    Did you look at Espressif's releases?

  • Did work on first try (Win10)

    Fantastic! Anything I should change in the docs for Win10 users?

    Did you look at Espressif's releases?

    I did, but the first one I tried didn't work so I went with an AI-Thinker specific one I found online that works. I feel happier with the full images (that you write to address 0) because then they blow away any saved config data that might potentially brick the device.

    If you get one that works I'm more than happy to update the instructions though!

  • Fantastic! Anything I should change in the docs for Win10 users?

    Just copy-pasted the relevant commands, and it did work. IIRC installation of esptool wasn't terribly difficult either.
    Saw a couple of "probably won't work on windows" remarks, especially in the BLE parts, but seem to work just fine in with the built-in Bluetooth. Web bluetooth works with chrome.

    I did, but the first one I tried didn't work

    Didn't try it, but that was my guess :)

  • Rather than create a new post, I'll add on to this old one...

    So is mDNS implemented on the Espruino Wifi? In short, what functionality is currently available to discover services via mDNS or mDNS-SD, and to be discovered? And what code do I need to write? Thanks!

  • Sun 2019.10.20

    'So is mDNS implemented on the Espruino Wifi?'

    See post #8 "next batch of WiFi boards . . . "

    My take on this would be not yet, that is boards currently ship with 0v40, given the comment is only three months ago.

    But, the link in #9 suggests the 8266 may be upgraded to 1v54 using esptool following the instructions there.


    'what functionality is currently available'

    I searched the 'Reference' page and found a note beneath:

    http://www.espruino.com/Reference#l_Wifi­_getHostname

    If one clicks on the Right-Facing arrow to the right of the heading there, that hyperlinks to the source which is at:

    https://github.com/espruino/Espruino/blo­b/master/libs/network/jswrap_wifi.c#L483­

    The notes within the source, mirror the note beneath the above heading. I observe the conflicting comment and conditional #if also.

  • Thanks, @Robin. Funny, I did a search on the espruino website for "mDNS" and got zero results. Yet that string exists in wifi.getHostname and wifi.setHostname. But I digress...

    I inserted that into the code and didn't notice anything happen when I ran mDNS discovery, but I'm at the learning stage. Anyway, I'm guessing that the code will respond only when in access point mode - as a response to network devices looking for a DHCP server. I'm not using it in AP mode - I'm just trying to use zeroconf to discover other devices and to be discovered. So far adding .setHostname() didn't help. I imagine there are other steps I need to accomplish, but I don't know what they are.

  • Our posts may have crossed, did you follow the link in #9 above?


    'I did a search on the espruino website for "mDNS"'

    It is usually best to load up a fresh Google page and use the 'site:' keyword qualifier.

    ex: mDNS wifi   site:espruino.com

  • oh wait, that's not Espruino fw (2v04), it's ESP8266 firmware, I totally blew past that. Thanks I'll give that try.

  • I got the ESP8266 fw update installed, validated it was 1.5.4, but still no difference. So here's what I'm doing:

    I'm calling wifi.setHostname() in the wifi 'connected' callback, and I get a success on that.

    Then I call 'dns-sd -B _http._tcp' from the Mac Terminal to browse for the device, not using Windows.

    I fear that I need to do more, but I don't know what it would be.

  • Ok, so I think now you've got the 1.5.4 firmware MDNS should work, but it needs turning on - it's not in the Espruino drivers because the ESP8266 they ship with doesn't have it.

    The actual command handbook is here: https://www.espressif.com/sites/default/­files/documentation/4a-esp8266_at_instru­ction_set_en.pdf

    wifi.setHostname calls AT+CWHOSTNAME, which is to do with WiFi rather than DNS, so I don't think is what you want.

    I believe this will work:

    wifi.enableMDNS = function(hostname, callback) {
      turnOn(MODE.CLIENT, function(err) {
        if (err) return callback(err);
        at.cmd("AT+MDNS=1,"+JSON.stringify(hostn­ame)+',"http",80\r\n',500,function(d) {
          callback(d=="OK"?null:d);
        });
      });
    };
    /// then wifi is connected do
    wifi.enableMDNS("foobar",function() {
     ... done
    });
    

    And you should be able to find your Espruino WiFi as foobar.local

  • Cool, thanks @Gordon. So I'm not sure where 'turnOn' and 'at' objects reside. It sucks that you have to hold my hand so much. How do I use this function - I can't just paste it into my code, because 'turnOn' and 'at' are undefined.

  • Oh, sorry - that's my mistake copy/pasting. Try:

    wifi.enableMDNS = function(hostname, callback) {
      this.at.cmd("AT+MDNS=1,"+JSON.stringify(­hostname)+',"http",80\r\n',500,function(­d) {
        callback(d=="OK"?null:d);
      });
    };
    
  • it works!
    The device can now be discovered. Any pointers on how to discover other devices? I think I need to send a UDP packet on 224.0.0.251:5353, but (a) I don't know what data to send to that destination, or (b) if that's the right thing to do.

  • I tweaked Gordon's example code snippet for flexibility:

    wifi.enableMDNS = function(hostname, serviceType, port, callback) {
    	let mdns = "AT+MDNS=1," + JSON.stringify(hostname) + "," + JSON.stringify(serviceType) + "," + JSON.stringify(port) + "\r\n";
    	this.at.cmd(mdns, 500, function(d) {
    		callback(d);
    	});
    };
    

    Now you can specify the service type and port, like this:

    wifi.enableMDNS("esp-livingroom", "iot", 80, function(result) {
        console.log("result is " + result);
    });
    
    
  • Do you have any examples of changing the service type? I left it out because I couldn't really see what it'd be used for :)

    I'm afraid I don't know how you'd use MDNS to find other devices though - I'd have hoped the ESP8266 (or your router) would be able to do that for you automatically. Usually I can just request mydevice or mydevice.local and it's fine with that.

  • From what I can tell, it's used mainly for filtering out services you don't care about discovering. For example, Apple uses a service type called something like "_daap" (digital audio something player) from iTunes to find compatible players. So it won't find your printers or web servers or home automation devices, because it's filtering by that service type. So I can create my own service type and then discover only the devices I created, ignoring everything else on the network.

    I don't think it's a very big deal when you have only a few discoverable devices on your network, but if you had hundreds, filtering by service type is kinda cool.

  • Post a reply
    • Bold
    • Italics
    • Link
    • Image
    • List
    • Quote
    • code
    • Preview
About

mDNS is working with Bonjour

Posted by Avatar for ClearMemory041063 @ClearMemory041063

Actions