Skip to content

Instantly share code, notes, and snippets.

@jaydlawrence
Created September 18, 2017 12:12
Show Gist options
  • Save jaydlawrence/ec9d9d64ef2c7e357852b813983e7498 to your computer and use it in GitHub Desktop.
Save jaydlawrence/ec9d9d64ef2c7e357852b813983e7498 to your computer and use it in GitHub Desktop.
Particle Photon and DHT11 - Publish temperature and humidity data
// requires the DHT11 arduino library - https://playground.arduino.cc/Main/DHT11Lib
#include "dht11.h"
#include "Particle.h"
dht11 DHT11;
// pin 2 is used as the data pin to communicate with the DHT11 board
#define DHT11PIN 2
// how long between measurements
long delayTime = 1800000; // 1/2 hour delay
// variable to store the time at start of counting
unsigned long startMillis;
void setup() {
// get starting time value
startMillis = millis();
// take initial reading
takeReading();
}
void clearCounter() {
startMillis = millis();
}
// function to take reading from DHT11
void takeReading() {
// get initial reading to check status of board
int chk = DHT11.read(DHT11PIN);
// some debug outputs
Particle.publish("DEBUG", "Read sensor: ");
switch (chk)
{
case DHTLIB_OK:
Particle.publish("DEBUG", "OK");
break;
case DHTLIB_ERROR_CHECKSUM:
Particle.publish("DEBUG", "Checksum error");
break;
case DHTLIB_ERROR_TIMEOUT:
Particle.publish("DEBUG", "Time out error");
break;
default:
Particle.publish("DEBUG", "Unknown error");
break;
}
// the board requires a short delay to fetch the data
delay(1000);
// take readings and publish the results to particle's server
Particle.publish("TempHumidityReading", "Temperature: " + String(DHT11.temperature + 1) + "°C, Humidity: " + String(DHT11.humidity) + "%");
}
void loop() {
// non-blocking timer loop
// cant do a normal delay here as the Photon doesn't like it
// when you block it from talking to the server for more than 10 seconds
if (millis() - startMillis >= delayTime) {
// if delayTime as elapsed, clear counter and take readings
clearCounter();
takeReading();
}
delay(1000);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment