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: Sensor Control
- - Source Code NOT compiled for: Arduino Nano
- - Source Code created on: 2024-10-31 20:20:58
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* led auslesen über modbus */
- /****** END SYSTEM REQUIREMENTS *****/
- /****** DEFINITION OF LIBRARIES *****/
- #include <DHT.h> // https://github.com/adafruit/DHT-sensor-library
- #include <ArduinoModbus.h> // https://github.com/arduino-libraries/ArduinoModbus
- #include <ArduinoRS485.h> // Include ArduinoRS485 for Modbus RTU
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /***** DEFINITION OF DIGITAL INPUT PINS *****/
- const uint8_t dht22_DHT22_DOUT_PIN_D2 = 2; // DHT22 data pin
- const int ledPin = LED_BUILTIN; // Define the LED pin
- /***** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- // Initialize the DHT sensor object with the pin and type
- DHT dht(dht22_DHT22_DOUT_PIN_D2, DHT22); // DHT22 is the type of sensor
- // Initialize the Modbus RTU Client
- ModbusRTUClient modbusRTUClient; // Create an instance of the Modbus RTU Client
- void setup(void)
- {
- // Start the serial communication at 9600 baud rate
- Serial.begin(9600);
- // Initialize the DHT sensor
- dht.begin();
- // Set the DHT pin as input with pull-up resistor
- pinMode(dht22_DHT22_DOUT_PIN_D2, INPUT_PULLUP);
- // Set the LED pin as output
- pinMode(ledPin, OUTPUT);
- // Start the Modbus RTU client with a baud rate of 9600
- if (!modbusRTUClient.begin(9600)) {
- Serial.println(F("Failed to start Modbus RTU Client!"));
- while (1); // Halt the program if the client fails to start
- }
- }
- void loop(void)
- {
- // Delay to allow sensor to stabilize
- delay(2000); // Wait a few seconds between measurements
- // Read temperature as Celsius
- float temperature = dht.readTemperature();
- if (isnan(temperature)) {
- Serial.println(F("Error reading temperature!"));
- } else {
- Serial.print(F("Temperature: "));
- Serial.print(temperature);
- Serial.println(F("°C"));
- }
- // Read humidity
- float humidity = dht.readHumidity();
- if (isnan(humidity)) {
- Serial.println(F("Error reading humidity!"));
- } else {
- Serial.print(F("Humidity: "));
- Serial.print(humidity);
- Serial.println(F("%"));
- }
- // Read the LED state via Modbus
- if (modbusRTUClient.coilRead(0x00)) { // Assuming coil address 0x00 is used for the LED
- digitalWrite(ledPin, HIGH); // Turn on the LED if the coil is set
- } else {
- digitalWrite(ledPin, LOW); // Turn off the LED if the coil is not set
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement