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.
- ESP32
- Wiznet Ethernet controller chips
- Tested with the M5Stack Atom POE
#include <Arduino.h>
#include <MicroNetWifi.h>
MicroNetWifi myMicroNet;
void setup(){
myMicroNet.begin("myName");
}
void loop() {
myMicroNet.update()
}#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()
}#include <Arduino.h>
#include <MicroNetEthernet.h>
MicroNetEthernet myMicroNet(MicroNetEthernet::Configuration::ATOM_POE_WITH_ATOM_LITE);
void setup(){
myMicroNet.begin("myName");
}
void loop() {
myMicroNet.update()
}#include <MicroNetEthernet.h>
MicroNetEthernet myMicroNet;#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 For Wi-Fi:
#include <MicroNetWiFi.h>
MicroNetWiFi myMicroNet;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 POEWarning
Do NOT call Ethernet.begin(). The following myMicroNet.begin(name) should be called instead!
If you are using a Preset Ethernet Configuration there is nothing to configure, the hardware will automatically be called when calling begin() (see below).
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.
- 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.
The myMicroNet.update() method performs periodic network maintenance tasks and must be called as often as possible:
myMicroNet.update();The maximum length of the device name (including the null terminator), that you should respect, is defined by:
MICRO_NET_NAME_MAX_LENGTHYou 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);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.
Returns the current IP address of the device:
IPAddress ip = myMicroNet.getIP();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 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).