mysensors.cpp 3.01 KB
Newer Older
1

2 3 4 5 6
#include "mysensors.h"
#include "utils.h"

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

7
#define  LDR_PIN     1   // analog
8 9 10

String read_LDR()
{
11
    return FloatToString(100/1023.0 * (1023 - analogRead(LDR_PIN)));
12 13 14 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
}

// === 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)
42
    {
43
        return String(bmp.readPressure());
44
    }
45
    else
46
    {
47
        return String("<bmp085_not_found>");
48
    }   
49 50
}

Nelso Jost's avatar
Nelso Jost committed
51 52 53 54 55 56 57 58
// === RTC_DS1307 SETUP ===========================================

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

RTC_DS1307 rtc;

String read_RTC_DS1307()
{
Nelso Jost's avatar
Nelso Jost committed
59 60
    return get_datetime_str(rtc.now());

Nelso Jost's avatar
Nelso Jost committed
61
    if (rtc.isrunning())
62
    {
Nelso Jost's avatar
Nelso Jost committed
63
        return get_datetime_str(rtc.now());
64
    }
Nelso Jost's avatar
Nelso Jost committed
65
    else
66
    {
Nelso Jost's avatar
Nelso Jost committed
67
        return String("<RTC_DS1307_not_found>");
68
    }
Nelso Jost's avatar
Nelso Jost committed
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92
}

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)
{
Nelso Jost's avatar
Nelso Jost committed
93
    /*
94 95 96
    if (!rtc.isrunning())
    {
        return String("<RTC_DS1307_not_found>");
Nelso Jost's avatar
Nelso Jost committed
97
    } */
98 99

    DateTime dt;    
Nelso Jost's avatar
Nelso Jost committed
100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123

    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);
}

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

124 125 126 127
void mysensors_setup()
{
    dht.begin();
    is_bmp085_connected = bmp.begin();
Nelso Jost's avatar
Nelso Jost committed
128 129 130 131 132 133 134 135 136

#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__)));
137
}