Advertisement
pleasedontcode

Sensor Control rev_01

Oct 31st, 2024
86
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: Sensor Control
  13.     - Source Code NOT compiled for: Arduino Nano
  14.     - Source Code created on: 2024-10-31 20:20:58
  15.  
  16. ********* Pleasedontcode.com **********/
  17.  
  18. /****** SYSTEM REQUIREMENTS *****/
  19. /****** SYSTEM REQUIREMENT 1 *****/
  20.     /* led auslesen über modbus */
  21. /****** END SYSTEM REQUIREMENTS *****/
  22.  
  23. /****** DEFINITION OF LIBRARIES *****/
  24. #include <DHT.h>    // https://github.com/adafruit/DHT-sensor-library
  25. #include <ArduinoModbus.h>  // https://github.com/arduino-libraries/ArduinoModbus
  26. #include <ArduinoRS485.h> // Include ArduinoRS485 for Modbus RTU
  27.  
  28. /****** FUNCTION PROTOTYPES *****/
  29. void setup(void);
  30. void loop(void);
  31.  
  32. /***** DEFINITION OF DIGITAL INPUT PINS *****/
  33. const uint8_t dht22_DHT22_DOUT_PIN_D2 = 2; // DHT22 data pin
  34. const int ledPin = LED_BUILTIN; // Define the LED pin
  35.  
  36. /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
  37. // Initialize the DHT sensor object with the pin and type
  38. DHT dht(dht22_DHT22_DOUT_PIN_D2, DHT22); // DHT22 is the type of sensor
  39.  
  40. // Initialize the Modbus RTU Client
  41. ModbusRTUClient modbusRTUClient; // Create an instance of the Modbus RTU Client
  42.  
  43. void setup(void)
  44. {
  45.     // Start the serial communication at 9600 baud rate
  46.     Serial.begin(9600);
  47.    
  48.     // Initialize the DHT sensor
  49.     dht.begin();
  50.  
  51.     // Set the DHT pin as input with pull-up resistor
  52.     pinMode(dht22_DHT22_DOUT_PIN_D2, INPUT_PULLUP);
  53.  
  54.     // Set the LED pin as output
  55.     pinMode(ledPin, OUTPUT);
  56.  
  57.     // Start the Modbus RTU client with a baud rate of 9600
  58.     if (!modbusRTUClient.begin(9600)) {
  59.         Serial.println(F("Failed to start Modbus RTU Client!"));
  60.         while (1); // Halt the program if the client fails to start
  61.     }
  62. }
  63.  
  64. void loop(void)
  65. {
  66.     // Delay to allow sensor to stabilize
  67.     delay(2000); // Wait a few seconds between measurements
  68.  
  69.     // Read temperature as Celsius
  70.     float temperature = dht.readTemperature();
  71.     if (isnan(temperature)) {
  72.         Serial.println(F("Error reading temperature!"));
  73.     } else {
  74.         Serial.print(F("Temperature: "));
  75.         Serial.print(temperature);
  76.         Serial.println(F("°C"));
  77.     }
  78.  
  79.     // Read humidity
  80.     float humidity = dht.readHumidity();
  81.     if (isnan(humidity)) {
  82.         Serial.println(F("Error reading humidity!"));
  83.     } else {
  84.         Serial.print(F("Humidity: "));
  85.         Serial.print(humidity);
  86.         Serial.println(F("%"));
  87.     }
  88.  
  89.     // Read the LED state via Modbus
  90.     if (modbusRTUClient.coilRead(0x00)) { // Assuming coil address 0x00 is used for the LED
  91.         digitalWrite(ledPin, HIGH); // Turn on the LED if the coil is set
  92.     } else {
  93.         digitalWrite(ledPin, LOW); // Turn off the LED if the coil is not set
  94.     }
  95. }
  96.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement