Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /********* 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: DHT22 Transmission
- - Source Code NOT compiled for: Arduino Uno
- - Source Code created on: 2024-09-28 17:20:25
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* The application must read data from the DHT22 */
- /* sensor at regular intervals, processing */
- /* temperature and humidity values while maintaining */
- /* efficient resource usage with the DHTesp library. */
- /* and send them to thingspeek */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <DHTesp.h> //https://github.com/beegee-tokyo/DHTesp
- #include <WiFi.h> // Include the WiFi library for network connectivity
- #include <ThingSpeak.h> // Include the ThingSpeak library for sending data
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t dht_DHT22_DOUT_PIN_D2 = 2; // Define the pin for DHT22 data output
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Instantiate the DHTesp object
- DHTesp dht; // Create an instance of the DHTesp class
- // WiFi and ThingSpeak configuration
- const char* ssid = "your_SSID"; // Replace with your WiFi SSID
- const char* password = "your_PASSWORD"; // Replace with your WiFi password
- unsigned long myChannelNumber = your_CHANNEL_NUMBER; // Replace with your ThingSpeak channel number
- const char* myWriteAPIKey = "your_WRITE_API_KEY"; // Replace with your ThingSpeak Write API Key
- void setup(void)
- {
- // Initialize serial communication for debugging
- Serial.begin(115200);
- Serial.println("DHT Sensor Initialization");
- // Setup the DHT sensor
- dht.setup(dht_DHT22_DOUT_PIN_D2, DHTesp::DHT22); // Connect DHT sensor to GPIO 2
- // Connect to WiFi
- WiFi.begin(ssid, password);
- while (WiFi.status() != WL_CONNECTED) {
- delay(1000);
- Serial.println("Connecting to WiFi...");
- }
- Serial.println("Connected to WiFi");
- ThingSpeak.begin(WiFi); // Initialize ThingSpeak
- }
- void loop(void)
- {
- // Wait for the minimum sampling period
- delay(dht.getMinimumSamplingPeriod());
- // Read humidity and temperature
- float humidity = dht.getHumidity();
- float temperature = dht.getTemperature();
- // Check for errors in reading
- if (dht.getStatus() != DHTesp::ERROR_NONE) {
- Serial.print("Error reading DHT sensor: ");
- Serial.println(dht.getStatusString());
- return; // Exit the loop if there was an error
- }
- // Print the readings to the serial monitor
- Serial.print("Humidity: ");
- Serial.print(humidity, 1);
- Serial.print("%\t");
- Serial.print("Temperature: ");
- Serial.print(temperature, 1);
- Serial.print("°C\t");
- Serial.print("Temperature (F): ");
- Serial.print(dht.toFahrenheit(temperature), 1);
- Serial.print("°F\t");
- Serial.print("Heat Index: ");
- Serial.println(dht.computeHeatIndex(temperature, humidity, false), 1);
- // Send data to ThingSpeak
- ThingSpeak.setField(1, humidity); // Set field 1 to humidity
- ThingSpeak.setField(2, temperature); // Set field 2 to temperature
- ThingSpeak.setField(3, dht.toFahrenheit(temperature)); // Set field 3 to temperature in Fahrenheit
- int responseCode = ThingSpeak.writeFields(myChannelNumber, myWriteAPIKey); // Write data to ThingSpeak
- if (responseCode == 200) {
- Serial.println("Data sent to ThingSpeak successfully.");
- } else {
- Serial.print("Failed to send data to ThingSpeak. Error code: ");
- Serial.println(responseCode);
- }
- // Wait for 2 seconds before the next loop iteration
- delay(2000);
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement