Skip to content

thomasfredericks/MicroNet

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

47 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

MicroNet

MicroNet is a network connection manager library for Arduino and PlatformIO.

It provides:

  • A unified interface for Wi-Fi or Ethernet (switch from one to the other by changing only a few lines of code).
  • DHCP client support.
  • mDNS (Bonjour) support.
  • A captive Wi-Fi configuration portal.

Tested Hardware

Microcontrollers

  • ESP32

Ethernet Boards

Tiny Examples

Wi-Fi

#include <Arduino.h>
#include <MicroNetWifi.h>
MicroNetWifi myMicroNet;
void setup(){
    myMicroNet.begin("myName");
}
void loop() {
   myMicroNet.update()
}

Generic Ethernet

#include <Arduino.h>
#include <MicroNetEthernet.h>
MicroNetEthernet myMicroNet;
void setup(){
    SPI.begin(22, 23, 33, 19); // ATOM LITE POE
    Ethernet.init(19);  // ATOM LITE POE
    // DO NOT CALL Ethernet.begin(), call the following instead:
    myMicroNet.begin("myName");
}
void loop() {
   myMicroNet.update()
}

Preset Ethernet Configuration

#include <Arduino.h>
#include <MicroNetEthernet.h>
MicroNetEthernet myMicroNet(MicroNetEthernet::Configuration::ATOM_POE_WITH_ATOM_LITE);
void setup(){
    myMicroNet.begin("myName");
}
void loop() {
   myMicroNet.update()
}

Integration

Global Declarations

Generic Ethernet

#include <MicroNetEthernet.h>
MicroNetEthernet myMicroNet;

Preset Ethernet Configurations

#include <MicroNetEthernet.h>
MicroNetEthernet myMicroNet(MicroNetEthernet::Configuration::ATOM_POE_WITH_ATOM_LITE);

Available presets:

MicroNetEthernet::Configuration::ATOM_POE_WITH_ATOM_LITE // Atom POE with Atom Lite
MicroNetEthernet::Configuration::ATOM_POE_WITH_ATOMS3 // Atom POE with Atom S3 

Wi-Fi

For Wi-Fi:

#include <MicroNetWiFi.h>
MicroNetWiFi myMicroNet;

Setup code

Generic Ethernet

If you are using Generic Ethernet, configure the hardware before starting MicroNet (do NOT call Ethernet.begin()).

Example configuration for an Atom Lite POE:

SPI.begin(22, 23, 33, 19); // ATOM LITE POE
Ethernet.init(19);        // ATOM LITE POE

Warning

Do NOT call Ethernet.begin(). The following myMicroNet.begin(name) should be called instead!

Preset Ethernet Configuration

If you are using a Preset Ethernet Configuration there is nothing to configure, the hardware will automatically be called when calling begin() (see below).

Begin

Start MicroNet with myMicroNet.begin() - the method will only return once it gets an IP from the DHCP server:

myMicroNet.begin(name);
  • name — C string (const char*) used as the device and mDNS name.
What happens when begin is called
  • For Wi‑Fi:
    • MicroNet attempts to connect to the configured access point.
    • If it fails, a captive configuration portal is opened using the provided name.
    • Connect to this portal to enter Wi‑Fi credentials.
  • For Wi‑Fi and Ethernet:
    • Once connected to the network, MicroNet requests an IP address via DHCP.
    • After an IP is obtained, the device registers its mDNS name.

Loop integration

The myMicroNet.update() method performs periodic network maintenance tasks and must be called as often as possible:

    myMicroNet.update();

Additional configuration

Maximum name length

The maximum length of the device name (including the null terminator), that you should respect, is defined by:

MICRO_NET_NAME_MAX_LENGTH

Append part of the MAC address to a "prefix"

You can append part of the MAC address to an existing C string to generate a unique name:

myMicroNet.appendMacToCString(prefix, destMaxSize, numBytes)
  • prefix — Destination C string (char*) containing the name prefix.
  • destMaxSize — Maximum size of the destination buffer (size_t).
  • numBytes — Number of MAC address bytes (uint8_t) to append (each byte adds two hexadecimal characters).

Example producing a name like atom-932AE4:

  char myName[MICRO_NET_NAME_MAX_LENGTH] = "atom-"; // name prefix
  myMicroNet.appendMacToCString(myName, MICRO_NET_NAME_MAX_LENGTH, 3);
  // START MicroNet with the custom name
  myMicroNet.begin(myName);

Getting the MAC address

Copies the device MAC address into the provided buffer:

myMicroNet.copyMac(mac);
  • mac — A 6‑byte array (uint8_t[6]) that will receive the MAC address.

Get the IP address

Returns the current IP address of the device:

IPAddress ip = myMicroNet.getIP();

Additional mDNS (Bonjour) code

Resolving an mDNS hostname

The resolveName() method will resolve an mDNS host name to its IP. The method only returns if the host is found! Do not append .local to the hostName.

IPAddress ip = myMicroNet.resolveName(hostName);
  • hostName — C string (const char*) containing the mDNS hostname.

Announce TCP or UDP service

Announce TCP service:

myMicroNet.announceTCPService(serviceName, servicePort);
  • serviceName — C string (const char*) with the service name.
  • servicePort — TCP port number (uint16_t).

Announce UDP service:

myMicroNet.announceUDPService(serviceName, servicePort);
  • serviceName — C string (const char*) with the service name.
  • servicePort — TCP port number (uint16_t).

About

A network connection manager library for Arduino and PlaformIO

Resources

Stars

Watchers

Forks

Packages

 
 
 

Contributors

Languages