Advertisement
Guest User

Agentuino lm335 temperature working

a guest
Oct 26th, 2013
444
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 12.52 KB | None | 0 0
  1. /**
  2. * Compiles with arduino 1.5.4r2
  3. *
  4. * Using  LM335Z temperature sensor
  5. *
  6. * If you use any shield you will need to use at least a DC 9V 300ma power supply
  7. * if not you will get incorrect readings from the 5v rail due to not enough power
  8. * supplied by usb. I'm using seeed Ethernet shield 1.1v. Oh and the Agentuino snmp
  9. * library for compiling this I'm using r48 and did not use the suggest Ethernet libraries
  10. * they are depreciated and don't work just use the 1.5.4r2 Ethernet libraries
  11. * this is far from my code its patched together using examples I found. I take
  12. * no credit out side of patching it together and getting it to work!
  13. */
  14. #include <Streaming.h>         // Include the Streaming library
  15. #include <Ethernet.h>          // Include the Ethernet library
  16. #include <SPI.h>
  17. #include <EthernetDHCP.h>
  18. #include <MemoryFree.h>
  19. #include <Agentuino.h>
  20. //
  21. #define DEBUG
  22. #define watchdogpin 2
  23. //
  24. int temperaturePin = 2;
  25. // SNMP stuff
  26. byte count=0; // count number of processed SNMP requests
  27.  
  28. // global variables holding sensor values
  29. static int32_t v_AnalogPin0=0;
  30.  
  31. // parameters for the Ethernet library
  32. static byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
  33. static byte ip[] = { 192, 168, 2, 64 };
  34. static byte gateway[] = { 192, 168, 2, 1 };
  35. static byte subnet[] = { 255, 255, 255, 0 };
  36.  
  37.  
  38. // must-have RFC1213-MIB OIDs (taken from agentuino sample sketch)
  39. // .iso.org.dod.internet.mgmt.mib-2.system.sysDescr (.1.3.6.1.2.1.1.1)
  40. static char sysDescr[] PROGMEM      = "1.3.6.1.2.1.1.1.0";  // read-only  (DisplayString)
  41. // .iso.org.dod.internet.mgmt.mib-2.system.sysObjectID (.1.3.6.1.2.1.1.2)
  42. static char sysObjectID[] PROGMEM   = "1.3.6.1.2.1.1.2.0";  // read-only  (ObjectIdentifier)
  43. // .iso.org.dod.internet.mgmt.mib-2.system.sysUpTime (.1.3.6.1.2.1.1.3)
  44. static char sysUpTime[] PROGMEM     = "1.3.6.1.2.1.1.3.0";  // read-only  (TimeTicks)
  45. // .iso.org.dod.internet.mgmt.mib-2.system.sysContact (.1.3.6.1.2.1.1.4)
  46. static char sysContact[] PROGMEM    = "1.3.6.1.2.1.1.4.0";  // read-write (DisplayString)
  47. // .iso.org.dod.internet.mgmt.mib-2.system.sysName (.1.3.6.1.2.1.1.5)
  48. static char sysName[] PROGMEM       = "1.3.6.1.2.1.1.5.0";  // read-write (DisplayString)
  49. // .iso.org.dod.internet.mgmt.mib-2.system.sysLocation (.1.3.6.1.2.1.1.6)
  50. static char sysLocation[] PROGMEM   = "1.3.6.1.2.1.1.6.0";  // read-write (DisplayString)
  51. // .iso.org.dod.internet.mgmt.mib-2.system.sysServices (.1.3.6.1.2.1.1.7)
  52. static char sysServices[] PROGMEM   = "1.3.6.1.2.1.1.7.0";  // read-only  (Integer)
  53.  
  54. // custom OIDs
  55. static char AnalogPin0[] PROGMEM   = "1.3.6.1.3.2009.0.3.0";  // Analog pin 0 Level (0-1023)
  56.  
  57. // RFC1213 local values (taken from agentuino sample sketch)
  58. static char locDescr[]              = "Gridshield SNMP Sensor";  // read-only (static)
  59. static char locObjectID[]           = "1.3.6.1.3.2009.0";                       // read-only (static)
  60. static uint32_t locUpTime           = 0;                                        // read-only (static)
  61. static char locContact[20]          = "www.gridshield.net";                            // should be stored/read from EEPROM - read/write (not done for simplicity)
  62. static char locName[20]             = "Datacenter";                              // should be stored/read from EEPROM - read/write (not done for simplicity)
  63. static char locLocation[20]         = "San Jose, CR";                        // should be stored/read from EEPROM - read/write (not done for simplicity)
  64. static int32_t locServices          = 7;                                        // read-only (static)
  65.  
  66. uint32_t prevMillis = millis(); // used for system uptime calculation
  67. char oid[SNMP_MAX_OID_LEN];     // will hold the OID of an SNMP request
  68. SNMP_API_STAT_CODES api_status; // agentuino SNMP status
  69. SNMP_ERR_CODES status;
  70.  
  71. // SNMP callback function, taken from agentuino sample sketch and extended to include my custom OIDs
  72. void pduReceived()  // is being called when an SNMP packet has been received
  73. {
  74.   SNMP_PDU pdu;
  75.   //
  76.   #ifdef DEBUG
  77.    Serial << "UDP Packet Received Start.." << " RAM:" << freeMemory() << endl;
  78.   #endif
  79.   //
  80.   api_status = Agentuino.requestPdu(&pdu);
  81.  
  82.   // check if valid packet
  83.   if ( (pdu.type == SNMP_PDU_GET || pdu.type == SNMP_PDU_GET_NEXT || pdu.type == SNMP_PDU_SET)
  84.         && pdu.error == SNMP_ERR_NO_ERROR && api_status == SNMP_API_STAT_SUCCESS )
  85.     {
  86.     //
  87.     pdu.OID.toString(oid);
  88.     //
  89.     Serial << "OID: " << oid << endl;
  90.     //
  91.    
  92.     if ( strcmp_P(oid, sysDescr ) == 0 )
  93.     {
  94.       // handle sysDescr (set/get) requests
  95.       if ( pdu.type == SNMP_PDU_SET )
  96.       {
  97.         // response packet from set-request - object is read-only
  98.         pdu.VALUE.encode(SNMP_SYNTAX_NULL);
  99.         pdu.type = SNMP_PDU_RESPONSE;
  100.         pdu.error = SNMP_ERR_READ_ONLY;
  101.       }
  102.       else
  103.       {
  104.         // response packet from get-request - locDescr
  105.         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locDescr);
  106.         pdu.type = SNMP_PDU_RESPONSE;
  107.         pdu.error = status;
  108.       }
  109.       //
  110.       #ifdef DEBUG
  111.        Serial << "sysDescr..." << locDescr << " " << pdu.VALUE.size << endl;
  112.       #endif
  113.     }
  114.    
  115.     else if ( strcmp_P(oid, sysObjectID ) == 0 )
  116.     {
  117.       // handle sysName (set/get) requests
  118.       if ( pdu.type == SNMP_PDU_SET )
  119.       {
  120.         // response packet from set-request - object is read-only
  121.         pdu.type = SNMP_PDU_RESPONSE;
  122.         pdu.error = SNMP_ERR_READ_ONLY;
  123.       }
  124.       else
  125.       {
  126.         // response packet from get-request - locUpTime
  127.         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locObjectID);
  128.         pdu.type = SNMP_PDU_RESPONSE;
  129.         pdu.error = status;
  130.       }
  131.       //
  132.       #ifdef DEBUG
  133.        Serial << "ObjectID" << locObjectID << " " << pdu.VALUE.size << endl;
  134.       #endif
  135.     }
  136.    
  137.     else if ( strcmp_P(oid, sysUpTime ) == 0 )
  138.     {
  139.       // handle sysName (set/get) requests
  140.       if ( pdu.type == SNMP_PDU_SET )
  141.       {
  142.         // response packet from set-request - object is read-only
  143.         pdu.type = SNMP_PDU_RESPONSE;
  144.         pdu.error = SNMP_ERR_READ_ONLY;
  145.       }
  146.       else
  147.       {
  148.         // response packet from get-request - locUpTime
  149.         status = pdu.VALUE.encode(SNMP_SYNTAX_TIME_TICKS, locUpTime);
  150.         pdu.type = SNMP_PDU_RESPONSE;
  151.         pdu.error = status;
  152.       }
  153.       //
  154.       #ifdef DEBUG
  155.        Serial << "sysUpTime..." << locUpTime << " " << pdu.VALUE.size << endl;
  156.       #endif
  157.     }
  158.    
  159.     else if ( strcmp_P(oid, sysName ) == 0 )
  160.     {
  161.       // handle sysName (set/get) requests
  162.       if ( pdu.type == SNMP_PDU_SET )
  163.       {
  164.         // response packet from set-request - object is read/write
  165.         status = pdu.VALUE.decode(locName, strlen(locName));
  166.         pdu.type = SNMP_PDU_RESPONSE;
  167.         pdu.error = status;
  168.       }
  169.       else
  170.       {
  171.         // response packet from get-request - locName
  172.         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locName);
  173.         pdu.type = SNMP_PDU_RESPONSE;
  174.         pdu.error = status;
  175.       }
  176.       //
  177.       #ifdef DEBUG
  178.        Serial << "sysName..." << locName << " " << pdu.VALUE.size << endl;
  179.       #endif
  180.     }
  181.    
  182.     else if ( strcmp_P(oid, sysContact ) == 0 )
  183.     {
  184.       // handle sysContact (set/get) requests
  185.       if ( pdu.type == SNMP_PDU_SET )
  186.       {
  187.         // response packet from set-request - object is read/write
  188.         status = pdu.VALUE.decode(locContact, strlen(locContact));
  189.         pdu.type = SNMP_PDU_RESPONSE;
  190.         pdu.error = status;
  191.       }
  192.       else
  193.       {
  194.         // response packet from get-request - locContact
  195.         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locContact);
  196.         pdu.type = SNMP_PDU_RESPONSE;
  197.         pdu.error = status;
  198.       }
  199.       //
  200.       #ifdef DEBUG
  201.        Serial << "sysContact..." << locContact << " " << pdu.VALUE.size << endl;
  202.       #endif
  203.     }
  204.    
  205.     else if ( strcmp_P(oid, sysLocation ) == 0 )
  206.     {
  207.       // handle sysLocation (set/get) requests
  208.       if ( pdu.type == SNMP_PDU_SET )
  209.       {
  210.         // response packet from set-request - object is read/write
  211.         status = pdu.VALUE.decode(locLocation, strlen(locLocation));
  212.         pdu.type = SNMP_PDU_RESPONSE;
  213.         pdu.error = status;
  214.       }
  215.       else
  216.       {
  217.         // response packet from get-request - locLocation
  218.         status = pdu.VALUE.encode(SNMP_SYNTAX_OCTETS, locLocation);
  219.         pdu.type = SNMP_PDU_RESPONSE;
  220.         pdu.error = status;
  221.       }
  222.       //
  223.       #ifdef DEBUG
  224.        Serial << "sysLocation..." << locLocation << " " << pdu.VALUE.size << endl;
  225.       #endif
  226.     }
  227.    
  228.     else if ( strcmp_P(oid, sysServices) == 0 )
  229.     {
  230.       // handle sysServices (set/get) requests
  231.       if ( pdu.type == SNMP_PDU_SET )
  232.       {
  233.         // response packet from set-request - object is read-only
  234.         pdu.type = SNMP_PDU_RESPONSE;
  235.         pdu.error = SNMP_ERR_READ_ONLY;
  236.       }
  237.       else
  238.       {
  239.         // response packet from get-request - locServices
  240.         status = pdu.VALUE.encode(SNMP_SYNTAX_INT, locServices);
  241.         pdu.type = SNMP_PDU_RESPONSE;
  242.         pdu.error = status;
  243.       }
  244.       //
  245.       #ifdef DEBUG
  246.        Serial << "locServices..." << locServices << " " << pdu.VALUE.size << endl;
  247.       #endif
  248.     }
  249.    
  250.     else if ( strcmp_P(oid, AnalogPin0 ) == 0 ) // Analog Pin 0 Level request
  251.     {
  252.       // handle sysName (set/get) requests
  253.       if ( pdu.type == SNMP_PDU_SET )
  254.       {
  255.         // response packet from set-request - object is read-only
  256.         pdu.type = SNMP_PDU_RESPONSE;
  257.         pdu.error = SNMP_ERR_READ_ONLY;
  258.       }
  259.       else
  260.       {
  261.         // response packet from get-request - AnalogPin0
  262.         status = pdu.VALUE.encode(SNMP_SYNTAX_INT32, v_AnalogPin0);
  263.         pdu.type = SNMP_PDU_RESPONSE;
  264.         pdu.error = status;
  265.       }
  266.       Serial << "v_AnalogPin0..." << v_AnalogPin0 << " " << pdu.VALUE.size << endl;
  267.     }
  268.  
  269.  
  270. // add more custom OID blocks here
  271.    
  272.     else
  273.     {
  274.       // oid does not exist
  275.       //
  276.       // response packet - object not found
  277. //pdu.VALUE.encode(SNMP_SYNTAX_NULL);
  278.       pdu.type = SNMP_PDU_RESPONSE;
  279.       pdu.error = SNMP_ERR_NO_SUCH_NAME;
  280.       Serial << "Unknown OID" << endl;
  281.     }
  282.    
  283.     //
  284.     Serial << "Sending response..." << endl;
  285.    
  286.     Agentuino.responsePdu(&pdu);
  287.     count++;
  288.   }
  289.  
  290.   else
  291.   // packet not valid, send GENERAL_ERROR response. Required, otherwise the invalid packet
  292.   // will get stuck in the buffer and processed over and over again
  293.  
  294.   {
  295.     Serial << "Unknown Packet!!" << endl;
  296.     Serial << "PDU Type: " << pdu.type << " PDU Error: " << pdu.error << " API status: "<< api_status << endl;
  297.     pdu.type = SNMP_PDU_RESPONSE;
  298.     pdu.error = SNMP_ERR_GEN_ERROR;
  299.     Agentuino.responsePdu(&pdu);
  300.     Serial << "Sent 'GENERAL_ERROR' response" << endl;
  301.   }
  302.  
  303.   //
  304.   Serial << "freeing PDU.." << " RAM:" << freeMemory() << endl;
  305.   Agentuino.freePdu(&pdu);
  306.   //
  307.   Serial << "UDP Packet Received End.." << " RAM:" << freeMemory() << endl;
  308. }
  309.  
  310. void setup()
  311. {
  312.  
  313.   // reset hardware watchdog timer  
  314.   pinMode(watchdogpin, OUTPUT);
  315.   digitalWrite(watchdogpin, LOW);
  316.   delay(50);
  317.   digitalWrite(watchdogpin, HIGH);
  318.  
  319.   Serial.begin(9600);
  320.   Serial << endl << "##################RESET###################" << endl << endl;
  321.  
  322.  
  323.  Ethernet.begin(mac, ip);
  324.   // give the Ethernet shield a second to initialize:
  325.   delay(1000);
  326.  
  327.   // start Agentuino SNMP library
  328.   api_status = Agentuino.begin();
  329.   if ( api_status == SNMP_API_STAT_SUCCESS )
  330.   {
  331.     Agentuino.onPduReceive(pduReceived); // register callback function that is being called when an SNMP packet has been received
  332.     delay(10);
  333.     Serial << "SNMP Agent Initalized..." << endl;
  334.   }
  335.   else
  336.   {
  337.     delay(10);
  338.     Serial << "SNMP Agent Initalization Problem..." << status << endl;
  339.   }
  340. }
  341.  
  342. void loop()
  343. {
  344.    
  345.    float temperature = getTemperature(temperaturePin) * 0.004882812 * 100; // convert to kelvin
  346.   Serial.println(temperature);
  347.   v_AnalogPin0 = ((temperature - 2.5) * 9 / 5) - 459.67; // change to Fahrenheit
  348.  
  349.   digitalWrite(watchdogpin, LOW);
  350.   delay(50);
  351.   digitalWrite(watchdogpin, HIGH);
  352.  
  353.   // listen for / handle incoming SNMP requests
  354.   Agentuino.listen();
  355.  
  356.   // sysUpTime - The time (in hundredths of a second) since
  357.   // the network management portion of the system was last
  358.   // re-initialized.
  359.   if ( millis() - prevMillis > 1000 ) {
  360.     // increment previous milliseconds
  361.     prevMillis += 1000;
  362.     //
  363.     // increment up-time counter
  364.     locUpTime += 100;
  365.   }
  366. }
  367.  
  368. float getTemperature(int pin){
  369.   return (analogRead(pin));  
  370. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement