Layer: L7 Application ยท Build flags: PC_ENABLE_SNMP (optional PC_ENABLE_SNMP_V3)
What this example teaches
This exposes the device over SNMP on UDP/161: the standard MIB-II system group plus a few private objects under an enterprise subtree (1.3.6.1.4.1.49374) - a read-only free-heap gauge and a writable LED integer. It handles Get / GetNext / GetBulk / Set (so snmpwalk and snmpbulkwalk work), is callback-driven over lwIP UDP, and keeps every buffer static.
Build the MIB, then bind. You set the communities and system group, register objects (static, dynamic via a getter, or writable via a setter), then start the agent:
pc_snmp_agent_init("public");
pc_snmp_agent_set_rw_community("private");
pc_snmp_agent_set_system("...desc...", "admin@example.com", "esp32-pc", "lab bench");
pc_snmp_agent_add_dynamic(OID_FREE_HEAP, 9, SnmpTag::SNMP_GAUGE32, get_free_heap);
pc_snmp_agent_add_integer(OID_LED, 9, 0, set_led);
pc_snmp_agent_begin_udp(161);
Dynamic + writable objects. A dynamic getter fills an SnmpValue on each Get; a setter validates the incoming type (returning wrongType to the manager if it mismatches) and applies the change:
bool get_free_heap(SnmpValue *out) { out->type = SnmpTag::SNMP_GAUGE32; out->uval = ESP.getFreeHeap(); return true; }
bool set_led(const SnmpValue *in) {
if (in->type != SnmpTag::BER_INTEGER) return false;
digitalWrite(LED_BUILTIN, in->ival ? HIGH : LOW); return true;
}
Optional SNMPv3 (USM). Adding -DPC_ENABLE_SNMP_V3=1 enables an authPriv user (HMAC-SHA-256 auth + AES-128 privacy): pc_snmp_v3_init / pc_snmp_v3_set_boots / pc_snmp_v3_set_user. For outbound trap notifications, see SnmpTrap.
Build and run
pio ci --board=esp32dev --project-option="framework=arduino" \
--project-option="build_flags=-DPC_ENABLE_SNMP=1" \
--lib="." examples/L7-Application/SNMP/SNMP.ino
snmpwalk -v2c -c public <ip> system
snmpget -v2c -c public <ip> 1.3.6.1.4.1.49374.10.0 # free heap (Gauge32)
snmpset -v2c -c private <ip> 1.3.6.1.4.1.49374.20.0 i 1 # LED on
Annotated source
The complete sketch (SNMP.ino), reproduced verbatim with added explanatory comments:
#define PC_ENABLE_SNMP 1
#if PC_ENABLE_SNMP_V3
#endif
static const char *SSID = "YOUR_SSID";
static const char *PASSWORD = "YOUR_PASSWORD";
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
static const uint32_t OID_FREE_HEAP[] = {1, 3, 6, 1, 4, 1, 49374, 10, 0};
static const uint32_t OID_LED[] = {1, 3, 6, 1, 4, 1, 49374, 20, 0};
bool get_free_heap(SnmpValue *out)
{
out->type = SnmpTag::SNMP_GAUGE32;
out->uval = (uint32_t)ESP.getFreeHeap();
return true;
}
bool set_led(const SnmpValue *in)
{
if (in->type != SnmpTag::BER_INTEGER)
return false;
digitalWrite(LED_BUILTIN, in->ival ? HIGH : LOW);
return true;
}
void setup()
{
pinMode(LED_BUILTIN, OUTPUT);
Serial.print("Connecting to WiFi");
{
delay(250);
Serial.print('.');
}
Serial.printf("IP: %u.%u.%u.%u\n", (unsigned)(ip & 0xFF), (unsigned)((ip >> 8) & 0xFF),
(unsigned)((ip >> 16) & 0xFF), (unsigned)((ip >> 24) & 0xFF));
pc_snmp_agent_init("public");
pc_snmp_agent_set_rw_community("private");
pc_snmp_agent_set_system("ProtoCore SNMP agent", "admin@example.com", "esp32-pc", "lab bench");
pc_snmp_agent_add_dynamic(OID_FREE_HEAP, 9, SnmpTag::SNMP_GAUGE32, get_free_heap);
pc_snmp_agent_add_integer(OID_LED, 9, 0, set_led);
#if PC_ENABLE_SNMP_V3
pc_snmp_v3_init(nullptr, 0);
pc_snmp_v3_set_boots(1);
pc_snmp_v3_set_user("pc", "authpass12", "privpass12");
Serial.println("SNMPv3 user 'pc' enabled (authPriv: SHA-256 / AES-128)");
#endif
pc_snmp_agent_begin_udp(161);
Serial.println("SNMP agent listening on UDP/161 (try: snmpwalk -v2c -c public <ip> system)");
int32_t result = server.
begin(80);
if (result < 0)
Serial.printf("begin() failed (error %d)\n", result);
}
void loop()
{
}
Single-port HTTP server with deterministic, zero-allocation execution.
int32_t begin(const WebServerConfig *cfg=nullptr)
Initialize all connection slots and open all registered listeners.
void handle()
Drive the server - call every Arduino loop() iteration.
bool init_wifi_physical(const char *, const char *)
Connect to a WiFi access point.
uint32_t pc_net_egress_ip(void)
IPv4 (network byte order) of the current egress interface, or 0 if none.
bool wifi_ready()
True if the WiFi station link is up (associated + an IP is assigned).
Layer 1 (Physical) - link bring-up and live egress-interface reporting.
Layer 7 (Application) - public HTTP routing API.