Advertisement
pleasedontcode

Temperature Monitoring rev_02

Aug 12th, 2024
161
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: Temperature Monitoring
  13.     - Source Code NOT compiled for: Arduino Nano ESP32
  14.     - Source Code created on: 2024-08-12 13:51:30
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* surveillance de temperature à l'aide du capteur */
  21.     /* DS18B20 connecté à la broche numérique D2. Lire et */
  22.     /* traiter les données de température pour les */
  23.     /* applications d'énergie solaire, garantissant des */
  24.     /* relevés précis et opportuns. */
  25. /****** END SYSTEM REQUIREMENTS *****/
  26.  
  27. /****** DEFINITION OF LIBRARIES *****/
  28. #include <DS18B20.h>    //https://github.com/matmunk/DS18B20
  29.  
  30. /****** FUNCTION PROTOTYPES *****/
  31. void setup(void);
  32. void loop(void);
  33.  
  34. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  35. const uint8_t temp_DS18B20_DQ_PIN_D2 = 2;
  36.  
  37. /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  38. // Create a Bus instance for communication on the specified pin
  39. MaximWire::Bus bus(temp_DS18B20_DQ_PIN_D2);
  40. // Create a DS18B20 instance without specifying an address (will need to discover devices)
  41. MaximWire::DS18B20 device;
  42.  
  43. /****** SETUP FUNCTION *****/
  44. void setup(void)
  45. {
  46.     // Initialize serial communication for debugging
  47.     Serial.begin(9600);
  48.    
  49.     // Set the pin mode for the temperature sensor data pin
  50.     pinMode(temp_DS18B20_DQ_PIN_D2, INPUT);
  51. }
  52.  
  53. /****** LOOP FUNCTION *****/
  54. void loop(void)
  55. {
  56.     // Check if the device is valid
  57.     if (device.IsValid()) {
  58.         // Get the temperature in Celsius
  59.         float temp = device.GetTemperature<float>(bus);
  60.         if (!isnan(temp)) {
  61.             // Print the temperature to the serial monitor
  62.             Serial.print("Temperature: ");
  63.             Serial.print(temp);
  64.             Serial.println(" °C");
  65.         } else {
  66.             // If the temperature reading is invalid, print a message
  67.             Serial.println("Temperature reading lost.");
  68.             device.Reset(); // Reset the device if lost
  69.         }
  70.     } else {
  71.         // If the device is not valid, try to discover it
  72.         if (bus.Discover().FindNextDevice(device) && device.GetModelCode() == MaximWire::DS18B20::MODEL_CODE) {
  73.             Serial.print("Found device: ");
  74.             Serial.println(device.ToString());
  75.             device.Update(bus); // Update the device state
  76.         } else {
  77.             // If no device is found, print a message
  78.             Serial.println("No DS18B20 device found.");
  79.             device.Reset(); // Reset the device if not found
  80.         }
  81.     }
  82.    
  83.     // Delay before the next loop iteration
  84.     delay(1000);
  85. }
  86.  
  87. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement