Dear community,
The strangest thing happens to me; I was asked to deliver a project showing the location of some assets while on operation in the field. Due to the fact that there are harsh environment conditions I have selected Arduino OPTA and F&F GPS Sensor MB-GPS-1 . Also I have 5 projects delivered with Arduino OPTA and the end user is using the dashboard and they like the IoT app. After a few fail attempts to read the sensor with Modbus examples I decided to make sure the sensor is working so I bough an Serial to USB convertor CH340 and used Modbus Poll to call sensor data and it worked well with the following specs:
- Slave ID : 1
- Baud Rate : 9600
- 8 data bits
- no pariry
- 1 stop bit
And it is 100% working well.
However, when trying to get this data with OPTA and display it on a map on arduino dashboard I always fail.
First question is around the physical wire connection. The working Modbus Poll case wiring was (100% working):
OPTA would be wired how?
And normally I would connect sensor A to OPTA A and Senor B to OPTA B but the fact that opta has A(-) and B(+) while CH340 adaptor has A (D+) and B(D-) makes me think how it is correct,
and Chap GPT suggest:
Nevertheless, second question is about the code. I have no idea why this would not work. No matter what I do I always get the same error related to timeout. See below code for a simple code:
#include "thingProperties.h"
#include <ArduinoRS485.h>
#include <ArduinoModbus.h>
void setup() {
// Initialize serial and wait for port to open:
Serial.begin(9600);
// This delay gives the chance to wait for a Serial Monitor without blocking if none is found
delay(1500);
// Defined in thingProperties.h
initProperties();
// Connect to Arduino IoT Cloud
ArduinoCloud.begin(ArduinoIoTPreferredConnection);
/*
The following function allows you to obtain more information
related to the state of network and IoT Cloud connection and errors
the higher number the more granular information you’ll get.
The default is 0 (only errors).
Maximum is 4
*/
setDebugMessageLevel(2);
ArduinoCloud.printDebugInfo();
// Configure RS485 delays
RS485.setDelays(3600, 3600);
//ModbusRTUClient.begin(9600, SERIAL_8N1);
// Start Modbus RTU client with specified baud rate and frame format (8N1)
if (!ModbusRTUClient.begin(9600, SERIAL_8N1)) {
while (1); // If Modbus setup fails, halt
}
// Set timeout for Modbus communications
ModbusRTUClient.setTimeout(2000);
}
void loop() {
ArduinoCloud.update();
// Your code here
if (ModbusRTUClient.requestFrom(1, HOLDING_REGISTERS, 0, 1)) {
int value = ModbusRTUClient.read();
Serial.print("Value: ");
Serial.println(value);
}
else {
Serial.println(ModbusRTUClient.lastError());
}
}
Plese note that I am trying to read register 0x00 with 03 command as per below snip from F&F manual:
Sorry for long post and thanks in advance for all your help!