Skip to content

Instantly share code, notes, and snippets.

@udibagas
Last active August 18, 2017 12:42
Show Gist options
  • Save udibagas/0f18a898eb2d4973d7a340fb786cef92 to your computer and use it in GitHub Desktop.
Save udibagas/0f18a898eb2d4973d7a340fb786cef92 to your computer and use it in GitHub Desktop.
/**
* Program untuk membaca arus, gas, suhu, kelembaban, jarak
*/
#include <Streaming.h>
#include <Ethernet.h>
#include <SPI.h>
#include <MemoryFree.h>
#include <Agentuino.h>
#include <Flash.h>
#include "EmonLib.h"
#include <dht11.h>
static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
static byte ip[] = { 192, 168, 99, 13 };
static byte subnet[] = { 255, 255, 255, 0 };
// static byte gateway[] = { 192 , 168, 2, 1 };
const char sysDescr[] PROGMEM = "1.3.6.1.2.1.1.1.0";
// sesuai file MIB UNITRONS
const char bacaGas[] PROGMEM = "1.3.6.1.4.1.43";
const char bacaSuhu[] PROGMEM = "1.3.6.1.4.1.44";
const char bacaLembab[] PROGMEM = "1.3.6.1.4.1.45";
const char bacaArus[] PROGMEM = "1.3.6.1.4.1.46";
const char bacaJarak[] PROGMEM = "1.3.6.1.4.1.80";
// RFC1213 local values
static char locDescr[] = "Unitron, Integrated Data Center Monitoring";
static int suhu = 0;
static int lembab = 0;
static int arus = 0;
static int gas = 0;
static int jarak = 0;
long duration;
int analogGas;
char oid[SNMP_MAX_OID_LEN];
SNMP_API_STAT_CODES api_status;
SNMP_ERR_CODES status;
EnergyMonitor emon1;
dht11 DHT11;
// Digital PIN
#define DHT11_PIN 3 // Temperature & Humidity
#define TRIGGER_PIN 4 // ultrasonic
#define ECHO_PIN 5 // ultrasonic
// Analog PIN
#define GAS_PIN A1
#define EMON_PIN A3
void pduReceived()
{
SNMP_PDU pdu;
api_status = Agentuino.requestPdu(&pdu);
if ( pdu.type == SNMP_PDU_GET && pdu.error == SNMP_ERR_NO_ERROR && api_status == SNMP_API_STAT_SUCCESS ) {
pdu.OID.toString(oid);
if ( strcmp_P(oid, sysDescr ) == 0 ) {
status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locDescr);
pdu.type = SNMP_PDU_RESPONSE;
pdu.error = status;
}
// BUAT BACA SUHU (OK)
else if ( strcmp_P(oid, bacaSuhu ) == 0 ) {
int chk = DHT11.read(DHT11_PIN);
suhu = (chk == 0) ? DHT11.temperature : 9999;
status = pdu.VALUE.encode(SNMP_SYNTAX_INT, suhu);
pdu.type = SNMP_PDU_RESPONSE;
pdu.error = status;
}
// BUAT BACA KELEMBABAN (OK)
else if ( strcmp_P(oid, bacaLembab ) == 0 ) {
int chk = DHT11.read(DHT11_PIN);
lembab = (chk == 0) ? DHT11.humidity : 9999;
status = pdu.VALUE.encode(SNMP_SYNTAX_INT, lembab);
pdu.type = SNMP_PDU_RESPONSE;
pdu.error = status;
}
// BUAT BACA ARUS (OK)
else if ( strcmp_P(oid, bacaArus ) == 0 ) {
double Irms = emon1.calcIrms(1480); // Calculate Irms only
arus = Irms*230.0;
status = pdu.VALUE.encode(SNMP_SYNTAX_INT, arus);
pdu.type = SNMP_PDU_RESPONSE;
pdu.error = status;
}
// BUAT BACA GAS (OK)
else if ( strcmp_P(oid, bacaGas ) == 0 ) {
analogGas = analogRead(GAS_PIN);
gas = map(analogGas, 0, 1023, 0, 255);
status = pdu.VALUE.encode(SNMP_SYNTAX_INT, gas);
pdu.type = SNMP_PDU_RESPONSE;
pdu.error = status;
}
// BUAT BACA JARAK
else if ( strcmp_P(oid, bacaJarak ) == 0 ) {
digitalWrite(TRIGGER_PIN, LOW);
delayMicroseconds(2);
digitalWrite(TRIGGER_PIN, HIGH);
delayMicroseconds(10);
digitalWrite(TRIGGER_PIN, LOW);
duration = pulseIn(ECHO_PIN, HIGH);
jarak = duration*0.034/2;
status = pdu.VALUE.encode(SNMP_SYNTAX_INT, jarak);
pdu.type = SNMP_PDU_RESPONSE;
pdu.error = status;
}
else {
pdu.type = SNMP_PDU_RESPONSE;
pdu.error = SNMP_ERR_NO_SUCH_NAME;
}
Agentuino.responsePdu(&pdu);
}
Agentuino.freePdu(&pdu);
}
void setup()
{
Serial.begin(9600);
Ethernet.begin(mac, ip);
api_status = Agentuino.begin();
emon1.current(EMON_PIN, 30);
pinMode(TRIGGER_PIN, OUTPUT);
pinMode(ECHO_PIN, INPUT);
if (api_status == SNMP_API_STAT_SUCCESS) {
Agentuino.onPduReceive(pduReceived);
delay(10);
Serial << F("SNMP Agent Initalized...") << endl;
return;
}
delay(10);
Serial << F("SNMP Agent Initalization Problem...") << status << endl;
}
void loop()
{
Agentuino.listen();
}
@udibagas
Copy link
Author

udibagas commented Aug 18, 2017

/etc/snmp/snmp.conf

mibs +UNITRONS

/usr/share/snmp/mibs/unitron.txt


UNITRONS DEFINITIONS ::= BEGIN

--- http://www.iana.org/assignments/enterprise-numbers/enterprise-numbers
--- Private Enterprise Number (PEN).

IMPORTS
   MODULE-IDENTITY, OBJECT-TYPE, 
   NOTIFICATION-TYPE, enterprises,
   Integer32                                    FROM  SNMPv2-SMI
   DisplayString                                FROM  SNMPv2-TC;

obyekUni	OBJECT IDENTIFIER ::= { enterprises }
bacaGas		OBJECT IDENTIFIER ::= { obyekUni 43 }
bacaSuhu	OBJECT IDENTIFIER ::= { obyekUni 44 }
bacaLembab	OBJECT IDENTIFIER ::= { obyekUni 45 }
bacaArus        OBJECT IDENTIFIER ::= { obyekUni 46 }
bacaJarak	OBJECT IDENTIFIER ::= { obyekUni 80 }


END

Cara ngetest:

snmpget -v 1 -c public [ip-address] bacaSuhu
snmpget -v 1 -c public [ip-address] bacaLembab
snmpget -v 1 -c public [ip-address] bacaGas
snmpget -v 1 -c public [ip-address] bacaArus
snmpget -v 1 -c public [ip-address] bacaJarak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment