-
-
Notifications
You must be signed in to change notification settings - Fork 75
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
1-Wire / I2C / PWM #58
Comments
yes, but you will need to be careful with timing. the modbus pool routine and the connected devices communication routines need to be called in a way that does not break each other, can be tricky :-) |
p.s. p.p.s. |
Integrating different sensors and PWM into your modbus enabled sketch is very easy, as long as you make sure you're able to call the poll() function often enough. How I usually do it is like this: // Initialise modbus, sensors etc.
unsigned long last_measurement = 0;
void loop()
{
watchdog.clear(); // Clear the watchdog.
if ((millis() - last_measurement) > 6000) // Start measurement every 6 seconds.
{
last_measurement = millis();
// Read your sensors here
}
else if (millis() < last_measurement)
{
last_measurement = millis();
}
slave.poll();
} What it does is read out the sensors every 6 seconds and in in between those readings constantly poll the modbus serial line. Also setting a PWM output using analogWrite() doesn't mess with the modbus timing. |
Can you show me an example of a BME280 (i2c) sensor? I do not know exactly which I have to insert the sensor. |
@DpunktS We're not here to create your projects for you. I will however give you some more guidelines as in how to read the sensor values via modbus. What you want to do is choose a proper register type to read your data from. As it is a humidity sensor which is read only a Input Register seems to be the best fitting option. To this register/function code you will connect a function in the setup. slave.cbVector[CB_READ_INPUT_REGISTERS] = your_function; Now you have two options, either you read the sensor periodically in your main sketch (like described here) and store that value somewhere. This way you can manipulate the data and do stuff like averaging. uint8_t your_function(uint8_t fc, uint16_t address, uint16_t length)
{
slave.writeRegisterToBuffer(address, BME280_sensor_value);
return STATUS_OK;
} The other option is to read the sensor when you read the modbus register and then uint8_t your_function(uint8_t fc, uint16_t address, uint16_t length)
{
slave.writeRegisterToBuffer(address, read_BME280_function());
return STATUS_OK;
} When you have multiple measurements/sensors the first version could be extended by storing the measured values in an array and read that out at various addresses. Hope this helps you finish your project. |
I have now got it so far that the sketch runs but as soon as I query the sensor with address 10 the Arduino hangs. Can someone help with that again please. // Handle the function code Read Input Registers (FC=04) and write back the values from
// the analog input pins (input registers).
uint8_t readAnalogIn(uint8_t fc, uint16_t address, uint16_t length)
{
if (address == 10)
{
slave.writeRegisterToBuffer(10, bme280.getTemperature());
return STATUS_OK;
}
else {// Check if the requested addresses exist in the array
if (address > analog_pins_size || (address + length) > analog_pins_size)
{
return STATUS_ILLEGAL_DATA_ADDRESS;
}
// Read the analog inputs
for (int i = 0; i < length; i++)
{
// Write the state of the analog pin to the response buffer.
slave.writeRegisterToBuffer(i, analogRead(analog_pins[address + i]));
}
return STATUS_OK;
}
} |
Try if you are interested enough to edit "https://github.com/yaacov/arduino-irrigation-timer" |
@DpunktS usually when a i2c sensor hangs the Arduino when being read it is due to the Wire interface not initialised properly. Try calling Wire.begin() in your setup. P.s. Also you shouldn't insert the measurement at place 10. This 10 refers to the amount of response values. Try writing the measurement to register 0 |
I have now managed to read a BME280. The sketch doesn't look nice, but it is enough for a beginner. |
@DpunktS nice ! |
@DpunktS As it seems that you're only using this to read the BME280 sensor I would simplify it to this: By adding sensor names to the enum you automatically enlarge the struct which can be read over modbus. This way you can add any measurement to modbus easily. |
hello i still use the analog and digital pins + bme280. The Arduino controls and monitors a heating circuit distributor heating / cooling. |
Is there a possibility to integrate 1-wire sensors (ds18b20), I2C sensors (BME280) and PWM in your code?
The text was updated successfully, but these errors were encountered: