Advertisement
pleasedontcode

DHT11 Telemetry rev_01

Aug 2nd, 2024
319
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /********* Pleasedontcode.com **********
  2.  
  3.     Pleasedontcode thanks you for automatic code generation! Enjoy your code!
  4.  
  5.     - Terms and Conditions:
  6.     You have a non-exclusive, revocable, worldwide, royalty-free license
  7.     for personal and commercial use. Attribution is optional; modifications
  8.     are allowed, but you're responsible for code maintenance. We're not
  9.     liable for any loss or damage. For full terms,
  10.     please visit pleasedontcode.com/termsandconditions.
  11.  
  12.     - Project: DHT11 Telemetry
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-08-02 09:13:59
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* Connect arduino to iot hub and send the telemetry */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <DHT.h>    //https://github.com/adafruit/DHT-sensor-library
  25.  
  26. /****** FUNCTION PROTOTYPES *****/
  27. void setup(void);
  28. void loop(void);
  29. void sendTelemetry(float humidity, float temperature); // Function prototype for sending telemetry
  30.  
  31. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  32. const uint8_t Temp_DHT11_DOUT_PIN_D2        = 2; // Pin for DHT11 sensor
  33. const uint8_t Temp_DHT11_DOUT_PIN_D3        = 3; // Another pin (not used in this example)
  34.  
  35. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  36. // Initialize the DHT sensor object with the pin and type
  37. #define DHTTYPE DHT11 // Define the type of DHT sensor
  38. DHT dht(Temp_DHT11_DOUT_PIN_D2, DHTTYPE); // Create an instance of the DHT class
  39.  
  40. void setup(void)
  41. {
  42.     // Initialize serial communication for debugging
  43.     Serial.begin(9600);
  44.     Serial.println(F("DHTxx test!"));
  45.  
  46.     // Initialize the DHT sensor
  47.     dht.begin(); // Start the DHT sensor
  48.  
  49.     // Set pin modes for digital pins
  50.     pinMode(Temp_DHT11_DOUT_PIN_D2, INPUT_PULLUP);
  51.     pinMode(Temp_DHT11_DOUT_PIN_D3, INPUT_PULLUP);
  52. }
  53.  
  54. void loop(void)
  55. {
  56.     // Delay between readings
  57.     delay(2000);
  58.  
  59.     // Read humidity and temperature from the DHT sensor
  60.     float h = dht.readHumidity(); // Read humidity
  61.     float t = dht.readTemperature(); // Read temperature in Celsius
  62.     float f = dht.readTemperature(true); // Read temperature in Fahrenheit
  63.  
  64.     // Check if any reads failed and exit early (to try again).
  65.     if (isnan(h) || isnan(t) || isnan(f)) {
  66.         Serial.println(F("Failed to read from DHT sensor!"));
  67.         return; // Exit the loop if reading failed
  68.     }
  69.  
  70.     // Compute heat index
  71.     float hif = dht.computeHeatIndex(f, h); // Heat index in Fahrenheit
  72.     float hic = dht.computeHeatIndex(t, h, false); // Heat index in Celsius
  73.  
  74.     // Print the results to the Serial Monitor
  75.     Serial.print(F("Humidity: "));
  76.     Serial.print(h);
  77.     Serial.print(F("%  Temperature: "));
  78.     Serial.print(t);
  79.     Serial.print(F("°C "));
  80.     Serial.print(f);
  81.     Serial.print(F("°F  Heat index: "));
  82.     Serial.print(hic);
  83.     Serial.print(F("°C "));
  84.     Serial.print(hif);
  85.     Serial.println(F("°F"));
  86.  
  87.     // Send telemetry data to IoT hub
  88.     sendTelemetry(h, t); // Call the function to send telemetry data
  89. }
  90.  
  91. // Function to send telemetry data to IoT hub
  92. void sendTelemetry(float humidity, float temperature) {
  93.     // Placeholder for sending data to IoT hub
  94.     // Replace this with actual code to connect to your IoT platform and send the data
  95.     Serial.print(F("Sending telemetry... Humidity: "));
  96.     Serial.print(humidity);
  97.     Serial.print(F("%, Temperature: "));
  98.     Serial.print(temperature);
  99.     Serial.println(F("°C"));
  100. }
  101.  
  102. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement