Advertisement
pleasedontcode

Temperature Logger rev_01

Aug 12th, 2024
165
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 Logger
  13.     - Source Code NOT compiled for: ESP32 DevKit V1
  14.     - Source Code created on: 2024-08-12 14:19:02
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* The application must utilize the DS18B20 library */
  21.     /* to interface with the temperature sensor, */
  22.     /* providing real-time temperature readings for data */
  23.     /* logging and analysis. */
  24. /****** END SYSTEM REQUIREMENTS *****/
  25.  
  26. /****** DEFINITION OF LIBRARIES *****/
  27. #include <DS18B20.h>    //https://github.com/matmunk/DS18B20
  28.  
  29. /****** FUNCTION PROTOTYPES *****/
  30. void setup(void);
  31. void loop(void);
  32.  
  33. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  34. const uint8_t temp_DS18B20_DQ_PIN_D4 = 4;
  35.  
  36. /****** DEFINITION OF LIBRARIES CLASS INSTANCES *****/
  37. // Create a Bus object with the defined pin
  38. MaximWire::Bus bus(temp_DS18B20_DQ_PIN_D4);
  39. // Create a DS18B20 object
  40. MaximWire::DS18B20 device;
  41.  
  42. void setup(void)
  43. {
  44.     // Initialize serial communication at 9600 baud rate
  45.     Serial.begin(9600);
  46.  
  47.     // Attempt to discover the DS18B20 device
  48.     if (bus.Discover().FindNextDevice(device)) {
  49.         Serial.print("Device found: ");
  50.         Serial.println(device.ToString());
  51.     } else {
  52.         Serial.println("No device found!");
  53.     }
  54. }
  55.  
  56. void loop(void)
  57. {
  58.     // Check if the device is valid
  59.     if (device.IsValid()) {
  60.         // Get the temperature from the DS18B20 sensor
  61.         float temp = device.GetTemperature<float>(bus);
  62.         if (!isnan(temp)) {
  63.             Serial.print("Temperature: ");
  64.             Serial.println(temp);
  65.         } else {
  66.             Serial.println("Failed to read temperature!");
  67.         }
  68.     } else {
  69.         // If the device is not valid, try to discover it again
  70.         if (bus.Discover().FindNextDevice(device) && device.GetModelCode() == MaximWire::DS18B20::MODEL_CODE) {
  71.             Serial.print("Device re-discovered: ");
  72.             Serial.println(device.ToString());
  73.         } else {
  74.             Serial.println("No device found!");
  75.         }
  76.     }
  77.  
  78.     // Update the device state
  79.     device.Update(bus);
  80.  
  81.     // Delay for a while before the next loop iteration
  82.     delay(1000);
  83. }
  84.  
  85. /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement