Components and supplies
1
Arduino Ethernet Shield 2
1
Arduino UNO
1
Solderless Breadboard Half Size
4
Temperature Sensor
6
Jumper wires (generic)
Tools and machines
1
Soldering iron (generic)
Project description
Code
v3
csharp
1#include <Streaming.h> 2#include <Ethernet.h> 3#include <SPI.h> 4#include <MemoryFree.h> 5#include <Agentuino.h> 6 7 int raw2 = 0; 8 int raw3 = 0; 9 int raw4 = 0; 10 int raw5 = 0; 11 float temp2 = 0; 12 float temp3 = 0; 13 float temp4 = 0; 14 float temp5 = 0; 15 16const char reg= "C"; //C=Temperature in Centigrade, F=Temperature in Farenheight 17static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED }; //Mac Address for Arduino Ethernet Shield 18static byte ip[] = { 192, 168, 111, 33 }; //IP Address for Arduino Ethernet Shield 19static byte gateway[] = { 192, 168, 111, 1 }; 20static byte subnet[] = { 255, 255, 255, 0 }; 21 22const int a2 = A2; 23const int a3 = A3; 24const int a4 = A4; 25const int a5 = A5; 26 27 28int term2=0; 29int term3=0; 30int term4=0; 31int term5=0; 32 33 34 35 36const char sysDescr[] PROGMEM = "1.3.6.1.2.1.1.1.0"; // System Description 37const char sysContact[] PROGMEM = "1.3.6.1.2.1.1.4.0"; // System Contact 38const char sysName[] PROGMEM = "1.3.6.1.2.1.1.5.0"; // System Name 39const char sysLocation[] PROGMEM = "1.3.6.1.2.1.1.6.0"; // System Location 40const char sysServices[] PROGMEM = "1.3.6.1.2.1.1.7.0"; // System Services 41 42 43 44//My Custom OID's 45 46 47const char temperature2[] PROGMEM = "1.3.6.1.3.2016.5.0.2"; //Temperature in Celsius 48const char temperature3[] PROGMEM = "1.3.6.1.3.2016.5.0.3"; //Temperature in Celsius 49const char temperature4[] PROGMEM = "1.3.6.1.3.2016.5.0.4"; //Temperature in Celsius 50const char temperature5[] PROGMEM = "1.3.6.1.3.2016.5.0.5"; //Temperature in Celsius 51 52 53// RFC1213 local values 54 55static char locDescr[] = "SNMP Temperature monitoring"; // read-only (static) 56static char locContact[50] = "radio-portal.ru"; 57static char locName[20] = "ServerRoom"; 58static char locLocation[20] = "Planet Earth"; 59static int32_t locServices = 2; // read-only (static) 60 61 62uint32_t prevMillis = millis(); 63char oid[SNMP_MAX_OID_LEN]; 64SNMP_API_STAT_CODES api_status; 65SNMP_ERR_CODES status; 66 67 68void pduReceived() 69 70{ 71 72 SNMP_PDU pdu; 73 api_status = Agentuino.requestPdu(&pdu); 74 75 if ((pdu.type == SNMP_PDU_GET || pdu.type == SNMP_PDU_GET_NEXT || pdu.type == SNMP_PDU_SET) 76 && pdu.error == SNMP_ERR_NO_ERROR && api_status == SNMP_API_STAT_SUCCESS ) { 77 78 pdu.OID.toString(oid); 79 80 81 if ( strcmp_P(oid, sysDescr ) == 0 ) { 82 if ( pdu.type == SNMP_PDU_SET ) { 83 pdu.type = SNMP_PDU_RESPONSE; 84 pdu.error = SNMP_ERR_READ_ONLY; 85 } else { 86 status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locDescr); 87 pdu.type = SNMP_PDU_RESPONSE; 88 pdu.error = status; 89 } 90 } else if ( strcmp_P(oid, sysName ) == 0 ) { 91 if ( pdu.type == SNMP_PDU_SET ) { 92 status = pdu.VALUE.decode(locName, strlen(locName)); 93 pdu.type = SNMP_PDU_RESPONSE; 94 pdu.error = status; 95 } else { 96 status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locName); 97 pdu.type = SNMP_PDU_RESPONSE; 98 pdu.error = status; 99 } 100 } else if ( strcmp_P(oid, sysContact ) == 0 ) { 101 if ( pdu.type == SNMP_PDU_SET ) { 102 status = pdu.VALUE.decode(locContact, strlen(locContact)); 103 pdu.type = SNMP_PDU_RESPONSE; 104 pdu.error = status; 105 } else { 106 status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locContact); 107 pdu.type = SNMP_PDU_RESPONSE; 108 pdu.error = status; 109 } 110 } else if ( strcmp_P(oid, sysLocation ) == 0 ) { 111 if ( pdu.type == SNMP_PDU_SET ) { 112 status = pdu.VALUE.decode(locLocation, strlen(locLocation)); 113 pdu.type = SNMP_PDU_RESPONSE; 114 pdu.error = status; 115 } else { 116 status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locLocation); 117 pdu.type = SNMP_PDU_RESPONSE; 118 pdu.error = status; 119 } 120 } else if ( strcmp_P(oid, sysServices) == 0 ) { 121 if ( pdu.type == SNMP_PDU_SET ) { 122 pdu.type = SNMP_PDU_RESPONSE; 123 pdu.error = SNMP_ERR_READ_ONLY; 124 } else { 125 status = pdu.VALUE.encode(SNMP_SYNTAX_INT, locServices); 126 pdu.type = SNMP_PDU_RESPONSE; 127 pdu.error = status; 128 } 129 130 } 131 132 else if ( strcmp_P(oid, temperature2 ) == 0 ) 133 { 134 if ( pdu.type == SNMP_PDU_SET ) 135 { 136 pdu.type = SNMP_PDU_RESPONSE; 137 pdu.error = SNMP_ERR_READ_ONLY; 138 } 139 else 140 { 141 status = pdu.VALUE.encode(SNMP_SYNTAX_INT, term2); 142 pdu.type = SNMP_PDU_RESPONSE; 143 pdu.error = status; 144 } 145 } 146 147 148 149 else if ( strcmp_P(oid, temperature3 ) == 0 ) 150 { 151 if ( pdu.type == SNMP_PDU_SET ) 152 { 153 pdu.type = SNMP_PDU_RESPONSE; 154 pdu.error = SNMP_ERR_READ_ONLY; 155 } 156 else 157 { 158 status = pdu.VALUE.encode(SNMP_SYNTAX_INT, term3); 159 pdu.type = SNMP_PDU_RESPONSE; 160 pdu.error = status; 161 } 162 } 163 164 165 else if ( strcmp_P(oid, temperature4 ) == 0 ) 166 { 167 if ( pdu.type == SNMP_PDU_SET ) 168 { 169 pdu.type = SNMP_PDU_RESPONSE; 170 pdu.error = SNMP_ERR_READ_ONLY; 171 } 172 else 173 { 174 status = pdu.VALUE.encode(SNMP_SYNTAX_INT, term4); 175 pdu.type = SNMP_PDU_RESPONSE; 176 pdu.error = status; 177 } 178 } 179 180 181 else if ( strcmp_P(oid, temperature5 ) == 0 ) 182 { 183 if ( pdu.type == SNMP_PDU_SET ) 184 { 185 pdu.type = SNMP_PDU_RESPONSE; 186 pdu.error = SNMP_ERR_READ_ONLY; 187 } 188 else 189 { 190 status = pdu.VALUE.encode(SNMP_SYNTAX_INT, term5); 191 pdu.type = SNMP_PDU_RESPONSE; 192 pdu.error = status; 193 } 194 } 195 196 197 else { 198 pdu.type = SNMP_PDU_RESPONSE; 199 pdu.error = SNMP_ERR_NO_SUCH_NAME; 200 } 201 Agentuino.responsePdu(&pdu); 202 } 203 Agentuino.freePdu(&pdu); 204} 205 206 207void setup() 208{ 209 Ethernet.begin(mac); //Initialize Ethernet Shield 210 api_status = Agentuino.begin(); //Begin Snmp agent on Ethernet shield 211 pinMode( A2, INPUT ); 212 pinMode( A3, INPUT ); 213 pinMode( A4, INPUT ); 214 pinMode( A5, INPUT ); 215 if ( api_status == SNMP_API_STAT_SUCCESS ) { 216 Agentuino.onPduReceive(pduReceived); 217 delay(10); 218 return; 219 } 220 delay(10); 221} 222 223 224void loop() 225{ 226 227Agentuino.listen(); 228 229float kPinTemp2 = analogRead(a2); 230float kPinTemp3 = analogRead(a3); 231float kPinTemp4 = analogRead(a4); 232float kPinTemp5 = analogRead(a5); 233 234float terme2=( kPinTemp2/1023.0 )*5.0*1000/10; 235float terme3=( kPinTemp3/1023.0 )*5.0*1000/10; 236float terme4=( kPinTemp4/1023.0 )*5.0*1000/10; 237float terme5=( kPinTemp5/1023.0 )*5.0*1000/10; 238 239if ( reg == "F" ) { 240float terme2=((terme2 ) * 9.0 / 5.0) + 32.0; 241float terme3=((terme3 ) * 9.0 / 5.0) + 32.0; 242float terme4=((terme4 ) * 9.0 / 5.0) + 32.0; 243float terme5=((terme5 ) * 9.0 / 5.0) + 32.0; 244} 245 246 term2=(int)terme2; 247 term3=(int)terme3; 248 term4=(int)terme4; 249 term5=(int)terme5; 250; 251}
Downloadable files
v2
v2

v2
v2

Comments
Only logged in users can leave comments