yet another 18B20 Temperature sensor. variable number of sensors working in parasite mode, serial 16x2 display with diagnostic output and post to a rest web service

Dependencies:   EthernetInterface HTTPClient NTPClient mbed-rtos mbed

Files at this revision

API Documentation at this revision

Comitter:
wkinkeldei
Date:
Thu Jan 03 18:50:43 2013 +0000
Parent:
0:53f05303850a
Commit message:
added switch sensor

Changed in this revision

collector_proxy.cpp Show annotated file Show diff for this revision Revisions of this file
collector_proxy.h Show annotated file Show diff for this revision Revisions of this file
local_time.cpp Show annotated file Show diff for this revision Revisions of this file
local_time.h Show annotated file Show diff for this revision Revisions of this file
main.cpp Show annotated file Show diff for this revision Revisions of this file
ntp_proxy.cpp Show annotated file Show diff for this revision Revisions of this file
ntp_proxy.h Show annotated file Show diff for this revision Revisions of this file
one_wire.h Show annotated file Show diff for this revision Revisions of this file
switch_sensor.cpp Show annotated file Show diff for this revision Revisions of this file
switch_sensor.h Show annotated file Show diff for this revision Revisions of this file
temperature_sensor.cpp Show annotated file Show diff for this revision Revisions of this file
--- a/collector_proxy.cpp	Mon Dec 31 12:08:24 2012 +0000
+++ b/collector_proxy.cpp	Thu Jan 03 18:50:43 2013 +0000
@@ -3,8 +3,7 @@
 
 #include <cstring>
 
-CollectorProxy::CollectorProxy(char *url) :base_url(url) {
-}
+CollectorProxy::CollectorProxy(const char *url) :base_url(url) {}
 
 int CollectorProxy::send_measure(char *path_part, int value) {
     HTTPClient http;
--- a/collector_proxy.h	Mon Dec 31 12:08:24 2012 +0000
+++ b/collector_proxy.h	Thu Jan 03 18:50:43 2013 +0000
@@ -6,11 +6,11 @@
 
 class CollectorProxy {
 public:
-    CollectorProxy(char *url);
+    CollectorProxy(const char *url);
     int send_measure(char *path_part, int value); // result 1:OK, 0:fail
 
 private:
-    char *base_url;
+    const char *base_url;
 };
 
 #endif
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/local_time.cpp	Thu Jan 03 18:50:43 2013 +0000
@@ -0,0 +1,32 @@
+#include "local_time.h"
+
+LocalTime::LocalTime() {}
+
+struct tm *LocalTime::tm() {
+    time_t seconds = time(NULL);
+    
+    struct tm *t = localtime(&seconds);
+    if (seconds > last_sunday(t->tm_year, 3) && seconds < last_sunday(t->tm_year, 10)) {
+        seconds += 7200;
+    } else {
+        seconds += 3600;
+    }
+
+    return localtime(&seconds);
+}
+
+time_t LocalTime::last_sunday(int year, int month) {
+    struct tm t1;
+    
+    t1.tm_sec = 0;   t1.tm_min = 0;     t1.tm_hour = month == 10 ? 3 : 2;
+    t1.tm_mday = 31; t1.tm_mon = month; t1.tm_year = year;
+    
+    // convert to seconds since epoch
+    time_t seconds = mktime(&t1);
+    
+    // convert back to tm because we need to know the week day
+    struct tm *t2 = localtime(&seconds);
+    
+    // subtract weekday to get back last sunday of month requested
+    return seconds - 86400 * t2->tm_wday;
+}
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/local_time.h	Thu Jan 03 18:50:43 2013 +0000
@@ -0,0 +1,16 @@
+#ifndef LOCALTIME_H
+#define LOCALTIME_H
+
+#include "mbed.h"
+
+class LocalTime {
+public:
+    LocalTime();
+    
+    struct tm *tm();
+
+private:
+    time_t last_sunday(int year, int month);
+};
+
+#endif
--- a/main.cpp	Mon Dec 31 12:08:24 2012 +0000
+++ b/main.cpp	Thu Jan 03 18:50:43 2013 +0000
@@ -9,6 +9,8 @@
 #include "collector_proxy.h"
 #include "sensor.h"
 #include "temperature_sensor.h"
+#include "switch_sensor.h"
+#include "local_time.h"
 
 SparkFun display(p28);
 list<Sensor *> sensors;
@@ -27,7 +29,7 @@
     ntp Thread: poll NTP Server every 3 hours
 */
 void ntp_thread(void const *args) {
-    NtpProxy ntp;
+    NtpProxy ntp((const char *) args);
     
     while (true) {
         display.show_ntp_status('t');
@@ -49,7 +51,7 @@
 void display_thread(void const *args) {
     char buffer[20];
     int last_minute = -1;
-    time_t seconds;
+    LocalTime l;
     struct tm *t;
     list<Sensor *>::iterator it = sensors.begin();
     
@@ -60,8 +62,7 @@
         if (it == sensors.end()) it = sensors.begin();
         
         // display time if minute is different from displayed value
-        seconds = time(NULL) + 3600; // FIXME: time-zone calculations wanted -- toggle switch?
-        t = localtime(&seconds);
+        t = l.tm();
         if (last_minute != t->tm_min) {
             last_minute = t->tm_min;
             strftime(buffer, 20, "%H:%M", t);
@@ -101,10 +102,10 @@
     send Thread: post measures to Statistics Collector every 15 minutes
 */
 void send_thread(void const *args) {
-    CollectorProxy collector("http://kinkeldei-net.de:81/sensor");
+    CollectorProxy collector((const char *) args);
 
     while (true) {
-        // 10 minute delay with feedback in the last 9 minutes
+        // 11 minute delay with feedback in the last 10 minutes
         for (int i=10; i>=0; i--) {
             display.show_network_status(i > 9 ? ' ' : '0' + i);
             wait_minutes(1);
@@ -123,31 +124,32 @@
 
 /*
     main Thread: initialize and flash a LED
+    
+    TODO: check or modify string constants    
 */
 int main() {
     display.print_init_message();
 
-    // ConfigReader c('config.ini');
-    // c.read_config();
-    
-    sensors.push_back((Sensor *) new TemperatureSensor(p21, "erlangen/temperatur/demo", "demo"));
+    // testing only:
+    sensors.push_back((Sensor *) new TemperatureSensor(p21, "erlangen/temperatur/demo", "Temperatur"));
+    sensors.push_back((Sensor *) new SwitchSensor(     p25, "erlangen/schalter/demo",   "Schalter"));
 
-    /*
-    sensors.push_back((Sensor *) new TemperatureSensor(p21, "trainmeusel/temperatur/heizung", "heizung"));
-    sensors.push_back((Sensor *) new TemperatureSensor(p22, "trainmeusel/temperatur/keller",  "keller"));
-    sensors.push_back((Sensor *) new TemperatureSensor(p23, "trainmeusel/temperatur/flur",    "flur"));
-    sensors.push_back((Sensor *) new TemperatureSensor(p24, "trainmeusel/temperatur/aussen",  "aussen"));
+    /* live setting:
+    sensors.push_back((Sensor *) new TemperatureSensor(p21, "trainmeusel/heizung/temperatur",     "Heizung"));
+    sensors.push_back((Sensor *) new TemperatureSensor(p22, "trainmeusel/waschkueche/temperatur", "Waschkueche"));
+    sensors.push_back((Sensor *) new TemperatureSensor(p23, "trainmeusel/flur/temperatur",        "Flur"));
+    sensors.push_back((Sensor *) new TemperatureSensor(p24, "trainmeusel/aussen/temperatur",      "Aussen"));
+    sensors.push_back((Sensor *) new SwitchSensor(     p25, "trainmeusel/oel/schwimmer",          "Oel"));
     */
     
-    // must come from configreader later, then remove:
     EthernetInterface eth;
     eth.init("192.168.2.175", "255.255.255.0", "192.168.2.1");
     eth.connect();
 
-    Thread t1(ntp_thread);
+    Thread t1(ntp_thread, (void *)"time.apple.com");
     Thread t2(display_thread);
     Thread t3(measure_thread);
-    Thread t4(send_thread);
+    Thread t4(send_thread, (void *)"http://kinkeldei-net.de:81/sensor");
 
     // a periodical flash should indicate "we are alive"
     DigitalOut led(LED1);
--- a/ntp_proxy.cpp	Mon Dec 31 12:08:24 2012 +0000
+++ b/ntp_proxy.cpp	Thu Jan 03 18:50:43 2013 +0000
@@ -1,10 +1,10 @@
 #include "ntp_proxy.h"
 
-NtpProxy::NtpProxy(void) {
+NtpProxy::NtpProxy(const char * timeserver) : timeserver(timeserver) {
     ntp_client = new NTPClient();
 }
 
 int NtpProxy::set_time(void) {
-    NTPResult r = ntp_client->setTime("time.apple.com");
+    NTPResult r = ntp_client->setTime(timeserver);
     return r == NTP_OK;
 }
--- a/ntp_proxy.h	Mon Dec 31 12:08:24 2012 +0000
+++ b/ntp_proxy.h	Thu Jan 03 18:50:43 2013 +0000
@@ -8,9 +8,12 @@
     NTPClient *ntp_client;
 
 public:
-    NtpProxy(void);
+    NtpProxy(const char *timeserver);
     
     int set_time(void);
+
+private:
+    const char *timeserver;
 };
 
 #endif
--- a/one_wire.h	Mon Dec 31 12:08:24 2012 +0000
+++ b/one_wire.h	Thu Jan 03 18:50:43 2013 +0000
@@ -1,5 +1,5 @@
-#ifndef _ONE_WIRE_H
-#define _ONE_WIRE_H
+#ifndef ONE_WIRE_H
+#define ONE_WIRE_H
 
 #include "mbed.h"
 
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/switch_sensor.cpp	Thu Jan 03 18:50:43 2013 +0000
@@ -0,0 +1,14 @@
+#include "switch_sensor.h"
+
+SwitchSensor::SwitchSensor(PinName pin, char *url_part, char *name): Sensor('S', pin, url_part, name), input(pin) {
+}
+
+void SwitchSensor::measure(void) {
+    value = input;
+}
+
+char *SwitchSensor::last_measure(void) {
+    sprintf(buffer, "%s", value ? "EIN" : "AUS");
+    
+    return buffer;
+}
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/switch_sensor.h	Thu Jan 03 18:50:43 2013 +0000
@@ -0,0 +1,18 @@
+#ifndef SWITCH_SENSOR_H
+#define SWITCH_SENSOR_H
+
+#include "sensor.h"
+
+class SwitchSensor : public Sensor {
+public:
+    SwitchSensor(PinName pin, char *url_part, char *name);
+
+    virtual void measure(void);
+    virtual char *last_measure(void);
+
+protected:
+    DigitalIn input;
+    char buffer[5];
+};
+
+#endif
--- a/temperature_sensor.cpp	Mon Dec 31 12:08:24 2012 +0000
+++ b/temperature_sensor.cpp	Thu Jan 03 18:50:43 2013 +0000
@@ -1,7 +1,7 @@
 #include "temperature_sensor.h"
 #include "rtos.h"
 
-TemperatureSensor::TemperatureSensor(PinName pin, char *url_part, char *name) : Sensor('T', pin, url_part, name), one_wire(pin) {
+TemperatureSensor::TemperatureSensor(PinName pin, char *url_part, char *name): Sensor('T', pin, url_part, name), one_wire(pin) {
 }
 
 void TemperatureSensor::prepare_measure(void) {