Posted By

wmacfarl on 03/01/19


Tagged


Versions (?)

Particle Photon DHT11+Light Sensor


 / Published in: C
 

Code for exposing cloud variables for DHT11 sensor readings on the Particle Photon.

  1. // This #include statement was automatically added by the Particle IDE.
  2. #include "Adafruit_DHT_Particle.h"
  3.  
  4. // Example testing sketch for various DHT humidity/temperature sensors
  5. // Written by ladyada, public domain
  6.  
  7. #define DHTPIN D2 // what pin we're connected to
  8.  
  9. // Uncomment whatever type you're using!
  10. #define DHTTYPE DHT11 // DHT 11
  11.  
  12.  
  13. #define LDRPIN A0
  14. #define POWERPIN A5
  15.  
  16. //#define DHTTYPE DHT22 // DHT 22 (AM2302)
  17. //#define DHTTYPE DHT21 // DHT 21 (AM2301)
  18.  
  19. // Connect pin 1 (on the left) of the sensor to +5V
  20. // Connect pin 2 of the sensor to whatever your DHTPIN is
  21. // Connect pin 4 (on the right) of the sensor to GROUND
  22. // Connect a 10K resistor from pin 2 (data) to pin 1 (power) of the sensor
  23.  
  24. DHT dht(DHTPIN, DHTTYPE);
  25. int loopCount;
  26. String reading;
  27.  
  28. void setup() {
  29. pinMode(LDRPIN, INPUT);
  30. pinMode(POWERPIN, OUTPUT);
  31.  
  32. Serial.begin(9600);
  33. Serial.println("DHTxx test!");
  34. Particle.publish("state", "DHTxx test start");
  35. Particle.variable("reading", reading);
  36. dht.begin();
  37. loopCount = 0;
  38. delay(2000);
  39. }
  40.  
  41. void loop() {
  42. // Wait a few seconds between measurements.
  43. // delay(2000);
  44.  
  45. // Reading temperature or humidity takes about 250 milliseconds!
  46. // Sensor readings may also be up to 2 seconds 'old' (its a
  47. // very slow sensor)
  48. float h = dht.getHumidity();
  49. // Read temperature as Celsius
  50. float t = dht.getTempCelcius();
  51. // Read temperature as Farenheit
  52. float f = dht.getTempFarenheit();
  53.  
  54. float l = analogRead(LDRPIN);
  55.  
  56. // Check if any reads failed and exit early (to try again).
  57. if (isnan(h) || isnan(t) || isnan(f)) {
  58. Serial.println("Failed to read from DHT sensor!");
  59. return;
  60. }
  61.  
  62. // Compute heat index
  63. // Must send in temp in Fahrenheit!
  64. float hi = dht.getHeatIndex();
  65. float dp = dht.getDewPoint();
  66. float k = dht.getTempKelvin();
  67.  
  68. Serial.print("Humid: ");
  69. Serial.print(h);
  70. Serial.print("% - ");
  71. Serial.print("Temp: ");
  72. Serial.print(t);
  73. Serial.print("*C ");
  74. Serial.print(f);
  75. Serial.print("*F ");
  76. Serial.print(k);
  77. Serial.print("*K - ");
  78. Serial.print("DewP: ");
  79. Serial.print(dp);
  80. Serial.print("*C - ");
  81. Serial.print("HeatI: ");
  82. Serial.print(hi);
  83. Serial.println("*C");
  84. Serial.println(Time.timeStr());
  85. //String timeStamp = Time.timeStr();
  86. reading = String::format("%4.2f,%4.2f,%4.2f,%4.2f,%4.2f", h, t, dp, hi, l);
  87. Particle.publish("readings", reading);
  88. delay(1000);
  89. }

Report this snippet  

You need to login to post a comment.