Turn your Pi into a network monitor and I/O device
GPIOs and SNMP
The Net-SNMP agent snmpd supports the creation of custom read/write objects (OIDs). The Pass-through MIB extension command in snmpd.conf allows for script files to be called.
Pass-through script files need to follow a few rules:
- an snmpget request passes a -g parameter
- the snmpget response needs to contain three lines: OID, data type, and value
- an snmpset request passes a -s parameter
My goal was to use SNMP to turn on and off powered devices, so for this I used a PowerSwitch Tail II ($26) [5]- a power cord that is enabled/disabled with I/O pins. The PowerSwitch pins connect to pins 6 and 12 on your Pi.
The command line program gpio can be used to write to GPIO pins. Listing 3 shows the SNMP script file for reading and writing to GPIO pin 18 (board pin 12).
Listing 3
Output of powerswitch script file
01 #!/bin/bash 02 if [ "$1" = "-g" ] 03 then 04 echo .1.3.6.1.2.1.25.1.8 05 echo integer 06 gpio -g read 18 07 fi 08 09 if [ "$1" = "-s" ] 10 then 11 gpio -g write 18 $4 12 fi 13 14 exit 0
The powerswitch script file is referenced in the SNMP server configuration. Open the snmpd.conf in nando and look for the Pass-through section. Add a line with the OID you want to use, the shell to use, and a path to the script file. For example:
# "Pass-through" MIB # extension command #pass .1.3.6.1.2.1.25.1.8 /bin/sh /home/pi/powerswitch
After editing the snmpd.conf file you will need to re-start the snmpd service. If everything is done correctly, you can use the SNMP command line routines to test the reading and writing to the new OID (see Listing 4).
Listing 4
Output of snmpset
$ snmpset -c public -v 1 localhost .1.3.6.1.2.1.25.1.8 i 1 HOST-RESOURCES-MIB::hrSystem.8 = INTEGER: 1 $ snmpget -c public -v 1 localhost .1.3.6.1.2.1.25.1.8 HOST-RESOURCES-MIB::hrSystem.8 = INTEGER: 1 $ snmpset -c public -v 1 localhost .1.3.6.1.2.1.25.1.8 i 0 HOST-RESOURCES-MIB::hrSystem.8 = INTEGER: 0 $ snmpget -c public -v 1 localhost .1.3.6.1.2.1.25.1.8 HOST-RESOURCES-MIB::hrSystem.8 = INTEGER: 0
Setting SNMP Values
Node-Red currently does not have a node to set SNMP values, but you can use an exec node to run command line programs. Figure 10 shows the logic to have an "ON" and "OFF" button set a value. The button nodes are configured to pass a 1 or 0 payload. The exec node contains the snmpset command (Figure 11). My final Web dashboard (Figure 12) includes a gauge to monitor the output status and buttons to control the PowerSwitch.
« Previous 1 2 3 4 Next »
Buy this article as PDF
Pages: 4
(incl. VAT)