Forum Rule: Always post complete source code & details to reproduce any issue!
Results 1 to 5 of 5

Thread: Non Blocking Ethernet/DHCP library (?)

  1. #1
    Senior Member
    Join Date
    Jan 2013
    Posts
    966

    Non Blocking Ethernet/DHCP library (?)

    The functionality I am looking for is:

    1. in setup(); I want to initialize/start an Ethernet connection and initialize/start a DHCP lease, but I don't want to wait for it's completion.
    2. in loop(); I want to check on the state of the connection while the system is already happily performing its main function. If the Ethernet connection is not successful I simply go on with life ;-)


    My LED lighting systems can be remotely controlled either per UDP/OSC or using an Apple Aluminum IR Remote Control (The Apple Remote control can be paired to a device which makes it a very nice choice!)
    If the ethernet connection fails or times out for some reason I don't want for my sketch to be blocked. I want for the lamp to turn on immediately and perhaps provide feedback whether an Ethernet connection was successful by using a different lighting patterned. Flash red a few times if it was successful, flash green if the connection failed. If the connection fails it can still be remotely controlled via IR, so an Ethernet connection is a nice to have rather than essential to the operation of a lamp.

    I googled non-blocking Ethernet library and found a not too old version on on Github. I've just barely started comparing the two and I am wondering if the desired behavior described above is not already possible with the existing library ?

    Another very old version of the Ethernet library was already able to provide this but I already an good amount of time upgrading the Bonjour library from that same source that I would like avoid having to do the same for the DHCP part of the Ethernet library ;-)

    Any hints and help iare appreciated!
    Last edited by Headroom; 03-07-2015 at 01:42 PM.

  2. #2
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    So after some investigation it was clear very quickly that the DHCP version of Ethernet.begin was blocking when using the version of the Ethernet Library that ships with Teensyduino.
    However the NBEthernet library from Davy Landman was quickly converted to use the SPI transactions and now is non blocking, meaning my sketch can already do other things without having to wait for the Ethernet connection.

  3. #3
    Junior Member
    Join Date
    Nov 2015
    Posts
    1
    I'm in a similar situation to you, and want an Ethernet library which is as non-blocking as possible. If it's too much bother then just ignore this, but I'm still confused about the state of these libraries. Could you please give me a quick update on how you're getting on with this? How board specific is this solution? (Sorry I'm still very new to Arduino development)

    "Please post there as a reminder for me and I'll post a zipped file containing the updated library."

    And also this, (again if it's not too much bother).

    Many thanks

  4. #4
    Senior Member PaulStoffregen's Avatar
    Join Date
    Nov 2012
    Posts
    18,655
    Quote Originally Posted by Headroom View Post
    I googled non-blocking Ethernet library and found a not too old version on on Github.
    That one is on a long list of stuff I'm considering merging into Teensyduino's copy of the Ethernet library.

    Truth is, I haven't really looked at it in detail, and I almost certainly won't until at least the upcoming Prop Shield is released, and maybe not even until after the high-end Teensy is selling. But it's on my list of Ethernet stuff to consider, so I don't forget.

    If anyone plays with it, I'd be curious to hear how it works?

  5. #5
    Senior Member
    Join Date
    Jan 2013
    Posts
    966
    The library is attached. It works with Tensy 3.0 3.1 and LC. It may well work with other Boards but i have not tested it.

    Ive deployed it in one of my LED Lighting Systems in the field with a Teensy 3.0 and WIZ820io and the library works great for DHCP.

    Also, as a side not the reason I wanted this non blocking behavior is that I did not want of the lamp to remain dark while waiting for the network connection and the registration of the OSC service per Bonjour.

    @paul,
    I am not sure you are aware of this but since Arduino 1.6.6 Multicast DNS is part of the Ethernet library.

    I'll see if I can get an example together as it's not necessarily self-explanatory how this works but here is some code that may help:

    These are the necessary includes:
    Code:
    #include <SPI.h>
    #include <macAddress.h>
    #include <Ethernet.h>
    #include <EthernetUdp.h>
    These are some variables and objects needing to be defined before setup:
    Code:
    macAddress myMac; //object retrieves mac address of Teensy 3.x or Teensy LC
    char mac_string[20];		 //string to hold MAC address for Bonjour host name
    //boolean runEthernet = 0;
    boolean _ethernetInitialized = 0;
    elapsedMillis sinceCheckedEthernet;
    elapsedMillis delayEthernetStart;
    EthernetUDP Udp;
    Here's what needs to go into setup:

    Code:
    Ethernet.begin(myMac); // start of non-blocking Ethernet. Ethernet.maintain() in loop() will poll status.
    This goes in loop:
    Code:
    	if(delayEthernetStart >= 3000){ 	  // initial power-up start delay to allow router to boot
    		if (sinceCheckedEthernet >= 500){ // time passed in millisecond
    			checkEthernet();
    			sinceCheckedEthernet = 0;			  // reset the counter to 0 so the counting starts over...
    		}
    	}
    and this is the function checkEthernet() forget about the EthernetBonjour as it's not needed for NBEthernet:

    Code:
    void checkEthernet(){
    	if(Ethernet.initialized()){
    		if( Ethernet.maintain()==0){
    			if (_ethernetInitialized == 0){
    				Serial.print("My IP is ");
    				Serial.println(Ethernet.localIP());
    				//start Bonjour service and register link local name
    				if (EthernetBonjour.begin(mac_string)){
    					Serial.print("Setting Bonjour Host Name : ");
    					Serial.print(mac_string);
    					Serial.println(".local");
    				}
    				else{
    					Serial.println("Bonjour Host Name could not be set");
    				}
    
    				//register UDP OSC service on the network
    				if(
    						EthernetBonjour.addServiceRecord("Lord Of The Rings._osc",
    								serverPort,
    								MDNSServiceUDP)){
    					Serial.println("Bonjour Service Record added successfully");
    				}
    				else{
    					Serial.println("Bonjour Service could not be added");
    				}
    				//start UDP server
    				Udp.begin(serverPort);
    				//Start EthernetBonjour service. This has to be run at least once otherwise nothing will work
    				EthernetBonjour.run();
    				_ethernetInitialized = 1;
    			}
    			else {
    				EthernetBonjour.run();
    			}
    		}
    	}
    }
    See where you can get with that. I'll see if I can get a better example together in the next week or two.

    NBEthernet.zip
    Last edited by Headroom; 11-20-2015 at 09:44 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •