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: **Modbus Communication**
- - Source Code NOT compiled for: ESP8266 NodeMCU V1.0
- - Source Code created on: 2024-11-28 05:25:27
- ********* Pleasedontcode.com **********/
- /****** SYSTEM REQUIREMENTS *****/
- /****** SYSTEM REQUIREMENT 1 *****/
- /* Implement a Modbus communication protocol to */
- /* enable data exchange between Arduino and Modbus- */
- /* compatible devices, ensuring reliable and */
- /* efficient control of connected components. */
- /****** END SYSTEM REQUIREMENTS *****/
- /* START CODE */
- /****** DEFINITION OF LIBRARIES *****/
- #include <ModbusMaster.h> //https://github.com/4-20ma/ModbusMaster
- //#include <SoftwareSerial.h> // SoftwareSerial is not compatible with ESP8266 as it doesn't support multiple serial ports like Arduino
- /****** FUNCTION PROTOTYPES *****/
- void setup(void);
- void loop(void);
- /****** DEFINITION OF LIBRARIES CLASS INSTANCES*****/
- ModbusMaster node;
- // Define MODBUS_SERIAL to use the default Serial for ESP8266
- #define MODBUS_SERIAL Serial
- void setup(void)
- {
- Serial.begin(115200);
- MODBUS_SERIAL.begin(115200, SERIAL_8N1, 14, 27); // IO14: RX, IO27: TX
- node.begin(1, MODBUS_SERIAL); // Initialize Modbus master with slave ID of 1
- }
- void loop(void)
- {
- uint8_t result; // Variable to hold the result of the Modbus request
- uint16_t data[10]; // Buffer to hold the data
- result = node.readHoldingRegisters(0, 1); // Read one holding register from address 0
- if (result == node.ku8MBSuccess) { // Check if the read was successful
- Serial.print("Flow Rate: ");
- Serial.println(node.getResponseBuffer(0)); // Print the value of the first holding register
- } else {
- Serial.print("Error reading Flow Rate: ");
- Serial.println(result); // Print the error code
- }
- delay(2000); // Wait for 2 seconds before the next loop iteration
- }
- /* END CODE */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement