Advertisement
hms11

ModBusESP32TacoChickenSHT20

Mar 19th, 2024
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1.  
  2. #include <ModbusMaster.h>
  3.  
  4. #define MAX485_RE_DE 5
  5.  
  6. ModbusMaster node;
  7.  
  8. void preTransmission() {
  9. digitalWrite(MAX485_RE_DE, 1);
  10. }
  11.  
  12. void postTransmission() {
  13. digitalWrite(MAX485_RE_DE, 0);
  14. }
  15.  
  16. void setup() {
  17. // Initialize control pins
  18. pinMode(MAX485_RE_DE, OUTPUT);
  19. digitalWrite(MAX485_RE_DE, 0);
  20.  
  21. // Modbus communication runs at 9600 baud
  22. Serial.begin(115200);
  23. Serial1.begin(9600, SERIAL_8N1, 2, 15);
  24.  
  25. // Modbus slave ID 1
  26. node.begin(1, Serial1);
  27.  
  28. // Callbacks allow us to configure the RS485 transceiver correctly
  29. node.preTransmission(preTransmission);
  30. node.postTransmission(postTransmission);
  31. }
  32.  
  33. void loop() {
  34. // Request 2 registers starting at 0x0001
  35. uint8_t result = node.readInputRegisters(0x0001, 2);
  36. Serial.println("Data Requested");
  37.  
  38. if (result == node.ku8MBSuccess) {
  39. // Get response data from sensor
  40. Serial.print("Temperature: ");
  41. Serial.print(float(node.getResponseBuffer(0) / 10.00F));
  42. Serial.print(" Humidity: ");
  43. Serial.println(float(node.getResponseBuffer(1) / 10.00F));
  44. }
  45. delay(1000);
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement