/********* Pleasedontcode.com **********
Pleasedontcode thanks you for automatic code generation! Enjoy your code!
- Terms and Conditions:
You have a non-exclusive, revocable, worldwide, royalty-free license
for personal and commercial use. Attribution is optional; modifications
are allowed, but you're responsible for code maintenance. We're not
liable for any loss or damage. For full terms,
please visit pleasedontcode.com/termsandconditions.
- Project: DHT11 Telemetry
- Source Code NOT compiled for: Arduino Uno
- Source Code created on: 2024-08-02 09:13:59
********* Pleasedontcode.com **********/
/****** SYSTEM REQUIREMENTS *****/
/****** SYSTEM REQUIREMENT 1 *****/
/* Connect arduino to iot hub and send the telemetry */
/****** END SYSTEM REQUIREMENTS *****/
/****** DEFINITION OF LIBRARIES *****/
#include //https://github.com/adafruit/DHT-sensor-library
/****** FUNCTION PROTOTYPES *****/
void setup(void);
void loop(void);
void sendTelemetry(float humidity, float temperature); // Function prototype for sending telemetry
/***** DEFINITION OF DIGITAL INPUT PINS *****/
const uint8_t Temp_DHT11_DOUT_PIN_D2 = 2; // Pin for DHT11 sensor
const uint8_t Temp_DHT11_DOUT_PIN_D3 = 3; // Another pin (not used in this example)
/****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
// Initialize the DHT sensor object with the pin and type
#define DHTTYPE DHT11 // Define the type of DHT sensor
DHT dht(Temp_DHT11_DOUT_PIN_D2, DHTTYPE); // Create an instance of the DHT class
void setup(void)
{
// Initialize serial communication for debugging
Serial.begin(9600);
Serial.println(F("DHTxx test!"));
// Initialize the DHT sensor
dht.begin(); // Start the DHT sensor
// Set pin modes for digital pins
pinMode(Temp_DHT11_DOUT_PIN_D2, INPUT_PULLUP);
pinMode(Temp_DHT11_DOUT_PIN_D3, INPUT_PULLUP);
}
void loop(void)
{
// Delay between readings
delay(2000);
// Read humidity and temperature from the DHT sensor
float h = dht.readHumidity(); // Read humidity
float t = dht.readTemperature(); // Read temperature in Celsius
float f = dht.readTemperature(true); // Read temperature in Fahrenheit
// Check if any reads failed and exit early (to try again).
if (isnan(h) || isnan(t) || isnan(f)) {
Serial.println(F("Failed to read from DHT sensor!"));
return; // Exit the loop if reading failed
}
// Compute heat index
float hif = dht.computeHeatIndex(f, h); // Heat index in Fahrenheit
float hic = dht.computeHeatIndex(t, h, false); // Heat index in Celsius
// Print the results to the Serial Monitor
Serial.print(F("Humidity: "));
Serial.print(h);
Serial.print(F("% Temperature: "));
Serial.print(t);
Serial.print(F("°C "));
Serial.print(f);
Serial.print(F("°F Heat index: "));
Serial.print(hic);
Serial.print(F("°C "));
Serial.print(hif);
Serial.println(F("°F"));
// Send telemetry data to IoT hub
sendTelemetry(h, t); // Call the function to send telemetry data
}
// Function to send telemetry data to IoT hub
void sendTelemetry(float humidity, float temperature) {
// Placeholder for sending data to IoT hub
// Replace this with actual code to connect to your IoT platform and send the data
Serial.print(F("Sending telemetry... Humidity: "));
Serial.print(humidity);
Serial.print(F("%, Temperature: "));
Serial.print(temperature);
Serial.println(F("°C"));
}
/* END CODE */