AM2302(DHT22) sensors support.
authorAlex Kirhenshtein <alk@netxms.org>
Wed, 5 Jun 2013 11:57:07 +0000 (11:57 +0000)
committerAlex Kirhenshtein <alk@netxms.org>
Wed, 5 Jun 2013 11:57:07 +0000 (11:57 +0000)
src/agent/subagents/rpi/Makefile.am
src/agent/subagents/rpi/htsensor.cpp
src/agent/subagents/rpi/rpi.cpp

index 0c34b56..b368ec0 100644 (file)
@@ -1,10 +1,10 @@
 SUBAGENT = rpi\r
 \r
 pkglib_LTLIBRARIES = rpi.la\r
-ecs_la_SOURCES = htsensor.cpp rpi.cpp\r
-ecs_la_CPPFLAGS=-I@top_srcdir@/include\r
-ecs_la_LDFLAGS = -module -avoid-version\r
-ecs_la_LIBADD = ../../../libnetxms/libnetxms.la -lbcm2835\r
+rpi_la_SOURCES = htsensor.cpp rpi.cpp\r
+rpi_la_CPPFLAGS=-I@top_srcdir@/include\r
+rpi_la_LDFLAGS = -module -avoid-version\r
+rpi_la_LIBADD = ../../../libnetxms/libnetxms.la -lbcm2835\r
 \r
 if !STATIC_BUILD\r
 install-exec-hook:\r
index 219e32f..587aafe 100644 (file)
 
 #define MAXTIMINGS 100
 
+/**
+ * Collector Thread data
+ */
+static bool volatile m_stopCollectorThread = false;
+static THREAD m_collector = INVALID_THREAD_HANDLE;
+
 /**
  * Sensor data 
  */
@@ -47,9 +53,11 @@ static bool ReadSensor()
        bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_OUTP);
 
        bcm2835_gpio_write(PIN, HIGH);
-       usleep(500000);  // 500 ms
+       //usleep(500000);
+       ThreadSleepMs(500);
        bcm2835_gpio_write(PIN, LOW);
-       usleep(20000);
+       ThreadSleepMs(20);
+       //usleep(20000);
 
        bcm2835_gpio_fsel(PIN, BCM2835_GPIO_FSEL_INPT);
 
@@ -59,12 +67,19 @@ static bool ReadSensor()
        counter = 0;
        while (bcm2835_gpio_lev(PIN) == 1 && counter < 1000) 
        {
-               usleep(1);
+               struct timespec ts;
+               ts.tv_sec = 0;
+               ts.tv_nsec = 1000;
+               nanosleep(&ts, NULL);
+               //usleep(1);
+               //ThreadSleepMs(1);
                counter++;
        }
 
        if (counter == 1000)
+       {
                return false;
+       }
 
        // read data
        for(int i = 0; i< MAXTIMINGS; i++) 
@@ -101,6 +116,8 @@ static bool ReadSensor()
                        
                g_sensorData[0] = h;
                g_sensorData[1] = t;
+
+               printf("t=%f, h=%f\n", t, h);
                return true;
        }
 
@@ -112,20 +129,37 @@ static bool ReadSensor()
  */
 THREAD_RESULT THREAD_CALL SensorPollingThread(void *)
 {
-       if (!bcm2835_init()) 
-       {
-               AgentWriteLog(NXLOG_ERROR, _T("RPI: call to bcm2835_init failed"));
-               return THREAD_OK;
-       }
-
        AgentWriteDebugLog(1, _T("RPI: sensor polling thread started"));
 
-       while(1
+       while(!m_stopCollectorThread
        {
                if (ReadSensor())
+               {
                        g_sensorUpdateTime = time(NULL);
-               ThreadSleepMs(500);
+               }
+               ThreadSleepMs(1500);
        }
        
        return THREAD_OK;
 }
+
+BOOL StartSensorCollector()
+{
+       if (!bcm2835_init()) 
+       {
+               AgentWriteLog(NXLOG_ERROR, _T("RPI: call to bcm2835_init failed"));
+               return FALSE;
+       }
+       m_collector = ThreadCreateEx(SensorPollingThread, 0, NULL);
+       return TRUE;
+}
+
+void StopSensorCollector()
+{
+       m_stopCollectorThread = true;
+       if (m_collector != INVALID_THREAD_HANDLE)
+       {
+               ThreadJoin(m_collector);
+               bcm2835_close();
+       }
+}
index 39e2743..caebe80 100644 (file)
 #include <nms_common.h>\r
 #include <nms_agent.h>\r
 \r
+/**\r
+ * \r
+ */\r
+BOOL StartSensorCollector();\r
+void StopSensorCollector();\r
+\r
 /**\r
  * Sensor data\r
  */\r
@@ -46,6 +52,22 @@ static LONG H_Sensors(const TCHAR *param, const TCHAR *arg, TCHAR *value)
        return ret;\r
 }\r
 \r
+/**\r
+ * Startup handler\r
+ */\r
+static BOOL SubagentInit(Config *config)\r
+{\r
+       return StartSensorCollector();\r
+}\r
+\r
+/**\r
+ * Shutdown handler\r
+ */\r
+static void SubagentShutdown()\r
+{\r
+       StopSensorCollector();\r
+}\r
+\r
 /**\r
  * Parameters\r
  */\r
@@ -62,7 +84,9 @@ static NETXMS_SUBAGENT_INFO m_info =
 {\r
        NETXMS_SUBAGENT_INFO_MAGIC,\r
        _T("RPI"), NETXMS_VERSION_STRING,\r
-       NULL, NULL, NULL,\r
+       SubagentInit,\r
+       SubagentShutdown,\r
+       NULL, // command handler\r
        sizeof(m_parameters) / sizeof(NETXMS_SUBAGENT_PARAM),\r
        m_parameters,\r
        0, NULL,                // lists\r