RichP

Easy ESP8266 Temp Sensor

Oct 9th, 2017
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #define CAYENNE_PRINT Serial
  2. #include <CayenneMQTTESP8266.h>
  3. #include <DHT.h>
  4.  
  5. #define DHTPIN D5     // what pin the DHT is connected to
  6. #define DHTTYPE DHT22   // DHT 22  (AM2302)
  7.  
  8. const int TEMPERATURE_INTERVAL = 30;
  9. unsigned long last_temperature_sent = 0;
  10.  
  11. const int HUMIDITY_INTERVAL = 30;
  12. unsigned long last_humidity_sent = 0;
  13. String display_temp;
  14. String display_humid;
  15.  
  16. DHT dht(DHTPIN, DHTTYPE);
  17.  
  18. // Your network name and password.
  19. char ssid[] = "Your WiFi SSID";
  20. char wifiPassword[] = "Your WiFi PWD";
  21.  
  22. // Cayenne authentication info. This should be obtained from the Cayenne Dashboard.
  23. char username[] = "Your Cayenne MQTT Username";
  24. char password[] = "Your Cayenne MQTT Password";
  25. char clientID[] = "Your Cayenne MQTT Client ID";
  26.  
  27.  
  28.  
  29. void getSendTemperature() {
  30.   if (millis() - last_temperature_sent >= TEMPERATURE_INTERVAL * 1000UL || last_temperature_sent == 0) {
  31.     float temperature = dht.readTemperature(true);
  32.  
  33.     if (isnan(temperature)) {
  34.       Serial.println("Failed to read from DHT sensor!");
  35.       return;
  36.     }
  37.     display_temp = temperature;
  38.     Serial.print("Temperature: ");
  39.     Serial.print(temperature);
  40.    
  41.     Serial.println(" °F");
  42.     Cayenne.virtualWrite(8, temperature);
  43.     last_temperature_sent = millis();
  44.   }
  45. }
  46.  
  47. void getSendHumid() {
  48.   if (millis() - last_humidity_sent >= HUMIDITY_INTERVAL * 1000UL || last_humidity_sent == 0) {
  49.     float humidity = dht.readHumidity();
  50.  
  51.     if (isnan(humidity)) {
  52.       Serial.println("Failed to read from DHT sensor!");
  53.       return;
  54.     }
  55.     display_humid = humidity;
  56.     Serial.print("Humidity: ");
  57.     Serial.print(humidity);
  58.     Serial.println(" %");
  59.     Cayenne.virtualWrite(9, humidity);
  60.     last_humidity_sent = millis();
  61.   }
  62. }
  63.  
  64.  
  65.  
  66.  
  67. void setup() {
  68.   // put your setup code here, to run once:
  69.   Serial.begin(230400);
  70.   dht.begin();
  71.   Cayenne.begin(username, password, clientID, ssid, wifiPassword);
  72. }
  73.  
  74. void loop() {
  75.   // put your main code here, to run repeatedly:
  76.   getSendTemperature();
  77.   getSendHumid();
  78.   Cayenne.loop();
  79. }
  80.  
  81. //Default function for processing actuator commands from the Cayenne Dashboard.
  82. //You can also use functions for specific channels, e.g CAYENNE_IN(1) for channel 1 commands.
  83. CAYENNE_IN_DEFAULT()
  84. {
  85.   CAYENNE_LOG("CAYENNE_IN_DEFAULT(%u) - %s, %s", request.channel, getValue.getId(), getValue.asString());
  86.   //Process message here. If there is an error set an error message using getValue.setError(), e.g getValue.setError("Error message");
  87. }
Advertisement
Add Comment
Please, Sign In to add comment