Skip to content

Instantly share code, notes, and snippets.

@udibagas
Created September 25, 2017 02:29
Show Gist options
  • Save udibagas/997445c31a44ae8e0261f4b69d9365ff to your computer and use it in GitHub Desktop.
Save udibagas/997445c31a44ae8e0261f4b69d9365ff to your computer and use it in GitHub Desktop.
/**
* Program untuk jarak
*/
#include <Streaming.h>
#include <Ethernet.h>
#include <SPI.h>
#include <MemoryFree.h>
#include <Agentuino.h>
#include <Flash.h>
static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x98 };
static byte ip[] = { 192, 168, 99, 152 };
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 bacaJarak[] PROGMEM = "1.3.6.1.4.1.80";
// RFC1213 local values
static char locDescr[] = "Unitron, Integrated Data Center Monitoring";
static int jarak = 0;
long duration;
char oid[SNMP_MAX_OID_LEN];
SNMP_API_STAT_CODES api_status;
SNMP_ERR_CODES status;
#define TRIGGER_PIN 2 // ultrasonic
#define ECHO_PIN 3 // ultrasonic
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 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();
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();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment