Temperature & Humidity

To sense basic temperature and humidity, we can use the SHT31-D sensor. It's chosen for its accuracy and compatibility with 3.3V circuit systems.

What type of sensor can you use to replace the button? For starters, basic sensors that report a simple ON/OFF state will be great. Examples include the PIR movement sensor, or the piezo vibration sensor.


Libraries Used

(learn how to import them in the Build IDE):


Code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
// This #include statement was automatically added by the Particle IDE.
#include <adafruit-sht31.h>

// how long between each log update? feel free to change this value,
// but this must not be anything less than 1000 milliseconds!
#define INTERVAL_BETWEEN_LOGS 5000

// initialise the timer
Timer dataTimer(INTERVAL_BETWEEN_LOGS, doDataUpdate);

// initialise the temp/humidity sensor
Adafruit_SHT31 sht31 = Adafruit_SHT31();

// code in this setup function runs just once, when Photon is powered up or reset
void setup() {
    Serial.begin(9600);         // Open a connection via the Serial port / USB cable – useful for debugging

    Serial.println("SHT31 test");
    if (! sht31.begin(0x44)) {   // Set to 0x45 for alternate i2c addr
        Serial.println("Couldn't find SHT31");
    }

    delay(5000);                // Common practice to allow board to 'settle' after connecting online
    dataTimer.start();
}

// code in this loop function runs forever, until you cut power!
// for the A/D blackbox, there is nothing to do in here because data updates are handled by our timer
void loop() {

}

// doDataUpdate runs every interval set in INTERVAL_BETWEEN_LOGS
void doDataUpdate() {
    // IMPORTANT: to prevent server overload, the Particle cloud can only accept
    // update rates of once per second, with the option to 'burst' 4 updates in a
    // second (but then you'll get blocked for the next 4 seconds). 'Ration' your
    // INTERVAL_BETWEEN_LOGS and the number of readings you are publishing; in our
    // default example, we are frugally using just 1 publish, by concatenating
    // all the data we want into a single publish statement

    // first we save what we want to read into temporary variables first.
    // feel free to add/remove these lines as you see fit in your application:

    float t = sht31.readTemperature();
    float h = sht31.readHumidity();

    if (! isnan(t)) {  // check if 'is not a number'
        //Temperature in C
        Serial.print("Temp C = "); Serial.println(t);
    } else {
        Serial.println("Failed to read temperature");
    }

    if (! isnan(h)) {  // check if 'is not a number'
        Serial.print("Hum. % = "); Serial.println(h);
    } else {
        Serial.println("Failed to read humidity");
    }

    // now we form the concatenated string to put them all together. This String
    // must NOT exceed 255 characters!
    String output = "T:" + String(t,2) + ",H:" + String(h,2);

    // prints this out the Serial port (coolterm) for us humans to verify and
    // debug; comment the next line if you don't want to see it in Coolterm
    Serial.println(output);

    // finally, send it out (and have your IFTTT recipe ready to use it):
    Particle.publish("sensorData", output);

}