mysensors.cpp 3.12 KB
Newer Older
Nelso Jost's avatar
Nelso Jost committed
1
2
3
4
5
6
7
8
9
/*------------------------------------------------------------------------------
 * AUTO-GENERATED CODE FROM CTA-EMM-WEB
 * EDIT ONLY IF YOU KNOW WHAT YOU ARE DOING
 *----------------------------------------------------------------------------*/
#include "mysensors.h"
#include "utils.h"

// === LDR SETUP =======================================

10
#define  LDR_PIN     1   // analog
Nelso Jost's avatar
Nelso Jost committed
11
12
13

String read_LDR()
{
14
    return FloatToString(100/1023.0 * analogRead(LDR_PIN));
Nelso Jost's avatar
Nelso Jost committed
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
}

// === DHT22 SETUP =======================================

// https://github.com/adafruit/DHT-sensor-library

#define DHT22_PIN        4   // digital
#define DHTTYPE DHT22
DHT dht(DHT22_PIN, DHTTYPE);

String read_DHT22_TEMP()
{
    return FloatToString(dht.readTemperature());
}

String read_DHT22_AH()
{
    return FloatToString(dht.readHumidity());
}

// === BMP085 SETUP ===============================================

// https://github.com/adafruit/Adafruit-BMP085-Library

Adafruit_BMP085 bmp;
bool is_bmp085_connected=false;

String read_BMP085_PRESSURE()
{
    if (is_bmp085_connected)
        return String(bmp.readPressure());
    else
        return String("<bmp085_not_found>");
}
// ----------------------------------------------------------------

Nelso Jost's avatar
Nelso Jost committed
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// === RTC_DS1307 SETUP ===========================================

// https://github.com/adafruit/RTClib

RTC_DS1307 rtc;

String read_RTC_DS1307()
{
    if (rtc.isrunning())
        return get_datetime_str(rtc.now());
    else
        return String("<RTC_DS1307_not_found>");
}

String get_datetime_str(DateTime dt)
{
    String result="";

    result += String(dt.year(), DEC);
    result += String("-");
    result += String(dt.month(), DEC);
    result += String("-");
    result += String(dt.day(), DEC);
    result += String(" ");
    result += String(dt.hour(), DEC);
    result += String(":");
    result += String(dt.minute(), DEC);
    result += String(":");
    result += String(dt.second(), DEC);

    return result;
}

// Expects something like "2015,6,28,13,13,10"
String set_time_from_csv(String s)
{
    DateTime dt;

    int i_month = s.indexOf(CSV_SEP);
    int i_day = s.indexOf(CSV_SEP, i_month + 1);
    int i_hour = s.indexOf(CSV_SEP, i_day + 1);
    int i_minute = s.indexOf(CSV_SEP, i_hour + 1);
    int i_second = s.indexOf(CSV_SEP, i_minute + 1);

    String year = s.substring(0, i_month);
    String month = s.substring(i_month + 1, i_day);
    String day = s.substring(i_day + 1, i_hour);
    String hour = s.substring(i_hour + 1, i_minute);
    String minute = s.substring(i_minute + 1, i_second);
    String second = s.substring(i_second + 1);

    dt = DateTime(year.toInt(), month.toInt(), day.toInt(), \
                  hour.toInt(), minute.toInt(), second.toInt());

    rtc.adjust(dt);

    return String("done: ") + get_datetime_str(dt);
}

// ----------------------------------------------------------------

Nelso Jost's avatar
Nelso Jost committed
112
113
114
115
void mysensors_setup()
{
    dht.begin();
    is_bmp085_connected = bmp.begin();
Nelso Jost's avatar
Nelso Jost committed
116
117
118
119
120
121
122
123
124

#ifdef AVR
    Wire.begin();
#else
    Wire1.begin(); // Shield I2C pins connect to alt I2C bus on Arduino Due
#endif
    rtc.begin();
    // adjust rtc from compile date time
    // rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
Nelso Jost's avatar
Nelso Jost committed
125
}