Advertisement
pleasedontcode

DHT22 Transmission rev_01

Sep 28th, 2024
58
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: DHT22 Transmission
  13.     - Source Code NOT compiled for: Arduino Uno
  14.     - Source Code created on: 2024-09-28 17:20:25
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The application must read data from the DHT22 */
  21.     /* sensor at regular intervals, processing */
  22.     /* temperature and humidity values while maintaining */
  23.     /* efficient resource usage with the DHTesp library. */
  24.     /* and send them to thingspeek */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <DHTesp.h> //https://github.com/beegee-tokyo/DHTesp
  29. #include <WiFi.h>     // Include the WiFi library for network connectivity
  30. #include <ThingSpeak.h> // Include the ThingSpeak library for sending data
  31.  
  32. /****** FUNCTION PROTOTYPES *****/
  33. void setup(void);
  34. void loop(void);
  35.  
  36. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  37. const uint8_t dht_DHT22_DOUT_PIN_D2 = 2; // Define the pin for DHT22 data output
  38.  
  39. /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  40. // Instantiate the DHTesp object
  41. DHTesp dht; // Create an instance of the DHTesp class
  42.  
  43. // WiFi and ThingSpeak configuration
  44. const char* ssid = "your_SSID"; // Replace with your WiFi SSID
  45. const char* password = "your_PASSWORD"; // Replace with your WiFi password
  46. unsigned long myChannelNumber = your_CHANNEL_NUMBER; // Replace with your ThingSpeak channel number
  47. const char* myWriteAPIKey = "your_WRITE_API_KEY"; // Replace with your ThingSpeak Write API Key
  48.  
  49. void setup(void)
  50. {
  51.     // Initialize serial communication for debugging
  52.     Serial.begin(115200);
  53.     Serial.println("DHT Sensor Initialization");
  54.  
  55.     // Setup the DHT sensor
  56.     dht.setup(dht_DHT22_DOUT_PIN_D2, DHTesp::DHT22); // Connect DHT sensor to GPIO 2
  57.  
  58.     // Connect to WiFi
  59.     WiFi.begin(ssid, password);
  60.     while (WiFi.status() != WL_CONNECTED) {
  61.         delay(1000);
  62.         Serial.println("Connecting to WiFi...");
  63.     }
  64.     Serial.println("Connected to WiFi");
  65.     ThingSpeak.begin(WiFi); // Initialize ThingSpeak
  66. }
  67.  
  68. void loop(void)
  69. {
  70.     // Wait for the minimum sampling period
  71.     delay(dht.getMinimumSamplingPeriod());
  72.  
  73.     // Read humidity and temperature
  74.     float humidity = dht.getHumidity();
  75.     float temperature = dht.getTemperature();
  76.  
  77.     // Check for errors in reading
  78.     if (dht.getStatus() != DHTesp::ERROR_NONE) {
  79.         Serial.print("Error reading DHT sensor: ");
  80.         Serial.println(dht.getStatusString());
  81.         return; // Exit the loop if there was an error
  82.     }
  83.  
  84.     // Print the readings to the serial monitor
  85.     Serial.print("Humidity: ");
  86.     Serial.print(humidity, 1);
  87.     Serial.print("%\t");
  88.     Serial.print("Temperature: ");
  89.     Serial.print(temperature, 1);
  90.     Serial.print("°C\t");
  91.     Serial.print("Temperature (F): ");
  92.     Serial.print(dht.toFahrenheit(temperature), 1);
  93.     Serial.print("°F\t");
  94.     Serial.print("Heat Index: ");
  95.     Serial.println(dht.computeHeatIndex(temperature, humidity, false), 1);
  96.    
  97.     // Send data to ThingSpeak
  98.     ThingSpeak.setField(1, humidity); // Set field 1 to humidity
  99.     ThingSpeak.setField(2, temperature); // Set field 2 to temperature
  100.     ThingSpeak.setField(3, dht.toFahrenheit(temperature)); // Set field 3 to temperature in Fahrenheit
  101.     int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); // Write data to ThingSpeak
  102.  
  103.     if (responseCode == 200) {
  104.         Serial.println("Data sent to ThingSpeak successfully.");
  105.     } else {
  106.         Serial.print("Failed to send data to ThingSpeak. Error code: ");
  107.         Serial.println(responseCode);
  108.     }
  109.    
  110.     // Wait for 2 seconds before the next loop iteration
  111.     delay(2000);
  112. }
  113.  
  114. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement