Skip to content

Commit

Permalink
Add multiple devcie polling example
Browse files Browse the repository at this point in the history
  • Loading branch information
shortbloke committed Jul 17, 2022
1 parent 8325ec7 commit 67da60a
Show file tree
Hide file tree
Showing 6 changed files with 176 additions and 10 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# CHANGELOG for SNMP Manager For ESP8266/ESP32/Arduino

## 1.1.9

- Added a new example file for ESP MCU to show polling of multiple devices and storing results in a device record array. #20

## 1.1.8

- Fixed #19 timeticks should be of type unsigned integer. This change impacts `SMNPManager::addTimestampHandler`.
Expand Down
6 changes: 3 additions & 3 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
MIT License

Copyright (c) 2020 Martin Rowan (https://github.com/shortbloke/Arduino_SNMP_Manager) - Create SNMP Manager without Agent
Copyright (c) 2019 Niich (https://github.com/Niich/Arduino_SNMP) - Initial SNMP Manager derived from SNMP Agent
Copyright (c) 2017 Aidan Cyr (https://github.com/fusionps/Arduino_SNMP) - SNMP Agent
Copyright (c) 2020-2022 Martin Rowan (https://github.com/shortbloke/Arduino_SNMP_Manager) - Create SNMP Manager without Agent
Parts Copyright (c) 2019 Niich (https://github.com/Niich/Arduino_SNMP) - Initial SNMP Manager derived from SNMP Agent
Parts Copyright (c) 2017 Aidan Cyr (https://github.com/fusionps/Arduino_SNMP) - SNMP Agent

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
11 changes: 6 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# SNMP Manager For ESP8266/ESP32/Arduino (and more)

An SNMP Manager for Arduino and similar MCU. Providing simple SNMP Get-Request support for specified OIDs.
An SNMP Manager for ESP, Arduino and similar MCU. Providing simple SNMP Get-Request support for specified OIDs.

The library supports:

Expand Down Expand Up @@ -158,15 +158,16 @@ The examples folder contains an SNMP GetRequest example for each of the data typ

### Examples folder contents

- [ESP32_ESP8266_SNMP_Manager.ino](examples/ESP32_ESP8266_SNMP_Manager/ESP32_ESP8266_SNMP_Manager.ino) - ESP32 or ESP2866 boards
- [ESP32_ESP8266_SNMP_Manager.ino](examples/ESP32_ESP8266_SNMP_Manager/ESP32_ESP8266_SNMP_Manager.ino) - ESP32/ESP2866 boards
- [ESP_Multiple_SNMP_Device_Polling.ino](examples/ESP_Multiple_SNMP_Device_Polling/ESP_Multiple_SNMP_Device_Polling.ino) - ESP32/ESP8266 boards querying multiple devices and storing results in a device record array
- [Arduino_Ethernet_SNMP_Manager.ino](examples/Arduino_Ethernet_SNMP_Manager/Arduino_Ethernet_SNMP_Manager.ino) - Arduino Mega with Ethernet Shield

## Tested Devices

The following devices have been confirmed to work with this library:
The following devices have been confirmed to work with this library (these are affiliate links that help support my work):

- Wemos D1 Mini (v3.1) - ESP8266 - [eBay UK](https://www.ebay.co.uk/itm/WeMos-D1-Mini-LATEST-V3-1-UK-Stock-Arduino-NodeMCU-MicroPython-WiFi-ESP8266/112325195239)
- ESP32S Dev Module - [Amazon UK](https://amzn.to/2TAqWZJ)
- WeMos D1 Mini - ESP8266 - [Amazon UK](https://amzn.to/3z6rQBt) [Amazon US](https://amzn.to/3AY4aBE)
- ESP32S Dev Module - [Amazon UK](https://amzn.to/2TAqWZJ) [Amazon US](https://amzn.to/3PgUZAx)

## Projects using this library

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#if defined(ESP8266)
#include <ESP8266WiFi.h> // ESP8266 Core WiFi Library
#else
#include <WiFi.h> // ESP32 Core WiFi Library
#endif

#include <WiFiUdp.h>
#include <Arduino_SNMP_Manager.h>

//************************************
//* Your WiFi info *
//************************************
const char *ssid = "SSID";
const char *password = "PASSWORD";
//************************************

//************************************
//* SNMP Device Info *
//************************************
const char *community = "public"; // SNMP Community string
const int snmpVersion = 1; // Enum, SNMP Version 1 = 0, SNMP Version 2 = 1
// OIDs
const char *oidSysName = ".1.3.6.1.2.1.1.5.0"; // OctetString SysName
const char *oidUptime = ".1.3.6.1.2.1.1.3.0"; // TimeTicks uptime (hundredths of seconds)
//************************************

//************************************
//* Settings *
//************************************
int devicePollInterval = 100; // delay in milliseconds
int lastDeviceWaitPeriod = 5000; // delay in milliseconds
#define LOWEROCTETLIMIT 1 // Set the lowest IP address to 1, .0 typically isn't used and isn't well supported.
#define UPPEROCTETLIMIT 6 // Set the upper limit of the range of IPs to query
//************************************

//************************************
//* Initialise *
//************************************
// Structures
struct device
{
IPAddress address;
char name[50];
char *sysName = name; // StringHandler needs pointer to char*
unsigned int uptime;
}; // Structure for the device records

// Global Variables
struct device deviceRecords[UPPEROCTETLIMIT + 1]; // Array of device records. _1 as we're not using the 0 index in the array.
int lastOctet = LOWEROCTETLIMIT; // Initialise last octet to lowest IP
bool allDevicesPolled = false; // Flag to to indicate all devices have been sent SNMP requests. Note responses may not yet have arrived.
// Initisialise variables used for timer counters to zero.
unsigned long devicePollStart = 0;
unsigned long intervalBetweenDevicePolls = 0;
unsigned long intervalBetweenLastDeviceReadyPolls = 0;
unsigned long deviceReadyStart = 0;

// SNMP Objects
WiFiUDP udp; // UDP object used to send and receive packets
SNMPManager snmp = SNMPManager(community); // Starts an SMMPManager to listen to replies to get-requests
SNMPGet snmpRequest = SNMPGet(community, snmpVersion); // Starts an SMMPGet instance to send requests
ValueCallback *callbackSysName; // Callback pointer for each OID
ValueCallback *callbackUptime; // Callback pointer for each OID
//************************************

//************************************
//* Function declarations *
//************************************
void sendSNMPRequest(IPAddress, struct device *deviceRecord);
int getNextOctet(int current);
void printVariableValues();
//************************************

void setup()
{
Serial.begin(115200);
WiFi.begin(ssid, password);
Serial.println("");
// Wait for connection
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.printf("\nConnected to SSID: %s - IP Address: ", ssid);
Serial.println(WiFi.localIP());

snmp.setUDP(&udp); // give snmp a pointer to the UDP object
snmp.begin(); // start the SNMP Manager
}

void loop()
{
snmp.loop(); // Needs to be called frequently to process incoming SNMP responses.
intervalBetweenDevicePolls = millis() - devicePollStart; // Timer for triggering per device Polls
intervalBetweenLastDeviceReadyPolls = millis() - deviceReadyStart; // Timer to trigger end of polling all devices (allowing some time for final packets to be processed)
if (allDevicesPolled)
{
if (intervalBetweenLastDeviceReadyPolls > lastDeviceWaitPeriod) // Waiting time after all devices polled complete?
{
deviceReadyStart += lastDeviceWaitPeriod; // This prevents drift in the delays
printVariableValues(); // Print the values to the serial console
allDevicesPolled = false; // Reset the flag
}
}
else
{
if (intervalBetweenDevicePolls >= devicePollInterval) // Time to poll the next device?
{
devicePollStart += devicePollInterval; // This prevents drift in the delays
IPAddress deviceIP(192, 168, 200, lastOctet); // Set IP address to be queried. Note: This simple example will only work with the last Octet of the address changing as this is used as a simple index for the device records.
sendSNMPRequest(deviceIP, &deviceRecords[lastOctet]); // Function call to send SNMP requests to specified device. Values will be storied in the deviceRecords arrary when they are returned (async)
lastOctet = getNextOctet(lastOctet); // Update to the next IP address to be queried
}
}
}

void sendSNMPRequest(IPAddress target, struct device *deviceRecord)
{
Serial.print("sendSNMPRequest - target: ");
Serial.println(target);
deviceRecord->address = target;
// Get callbacks from creating a handler for each of the OID
callbackSysName = snmp.addStringHandler(target, oidSysName, &deviceRecord->sysName);
callbackUptime = snmp.addTimestampHandler(target, oidUptime, &deviceRecord->uptime);

// Build a SNMP get-request add each OID to the request
snmpRequest.addOIDPointer(callbackSysName);
snmpRequest.addOIDPointer(callbackUptime);

snmpRequest.setIP(WiFi.localIP()); // IP of the listening MCU
snmpRequest.setUDP(&udp);
snmpRequest.setRequestID(rand() % 5555);
snmpRequest.sendTo(target);
snmpRequest.clearOIDList();
}

int getNextOctet(int current)
{
if (current == UPPEROCTETLIMIT)
{
allDevicesPolled = true;
return LOWEROCTETLIMIT;
}
return current + 1;
}

void printVariableValues()
{
int i;
for (i = LOWEROCTETLIMIT; i <= UPPEROCTETLIMIT; i++)
{
Serial.print("Address: ");
Serial.print(deviceRecords[i].address);
Serial.print(" - Name: ");
Serial.print(deviceRecords[i].name);
Serial.print(" - Uptime: ");
Serial.print(deviceRecords[i].uptime);
Serial.println();
}
}
2 changes: 1 addition & 1 deletion library.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "SNMP Manager",
"version": "1.1.8",
"version": "1.1.9",
"description": "An SNMP Manager library to make SNMP requests to other SNMP enabled devices. Supporting SNMP v1 and v2, SNMP requests can be sent (GetRequest) and their responses received (GetResponse) for various SNMP data types.",
"repository": {
"type": "git",
Expand Down
2 changes: 1 addition & 1 deletion library.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name=SNMP Manager
version=1.1.7
version=1.1.9
author=Martin Rowan <martin@martinrowan.co.uk>
maintainer=Martin Rowan <martin@martinrowan.co.uk>
sentence=An SNMP Manager library to make SNMP requests to other SNMP enabled devices.
Expand Down

0 comments on commit 67da60a

Please sign in to comment.