Go Down

Topic: Arduino Yun Rev 2 - Modbus TCP and I2C (Read 199 times) previous topic - next topic

212690741

Sep 06, 2018, 12:47 pm Last Edit: Sep 06, 2018, 12:53 pm by 212690741
Hi,

So far I have connected a 3 in 1 sensor to an arduino yun rev 2. The sensor works on I2C communication.
My code so far allows me to connect to the arduino and read the values in the serial monitor. The next step is setting up modbus tcp on the arduino so I can connect it to a field agent that will read the data.

I only really have basic knowledge on arduinos so learning as I go along here. I've seen many libraries where people have connected sensors through digital or analogue to modbus tcp. But not many examples with I2C and modbus tcp

Any help is appreciated.
Thanks




Code: [Select]

/*
This example prints the current CO2 level, relative humidity, and temperature in C.
Click here to get the library: http://librarymanager/All#SparkFun_SCD30
*/

#include <Wire.h>
#include "SparkFun_SCD30_Arduino_Library.h"

SCD30 airSensor;

void setup()
{
Wire.begin();

Serial.begin(9600);
Serial.println("SCD30 Example");
airSensor.begin(); //This will cause readings to occur every two seconds
}

void loop()
if (airSensor.dataAvailable())
{
  Serial.print("co2(ppm):");
  Serial.print(airSensor.getCO2());

  Serial.print(" temp(C):");
  Serial.print(airSensor.getTemperature(), 1);

  Serial.print(" humidity(%):");
  Serial.print(airSensor.getHumidity(), 1);

  Serial.println();
}
else
  Serial.println("No data");
  delay(1000);
}

ShapeShifter

Full disclosure: I have no experience with modbus TCP, but I've started looking into it for an upcoming project.

You may be too hung up on the details of the examples. Any sketch like the one you need, or the examples you found, will do three main things: set up the sensors and initialize modbus, collect data from sensors, and send that data using a modbus library. Part will depend greatly on the sensors, but the modbus part will generally be substantially the same.

The examples you found probably read some inputs (analog, digital, or whatever) and put those values into variables. Then, the code reads the values from those variables and sends them to the modbus library. What you need to do is find and understand that part of the example that reads the variables and sends them to modbus. Then, add that code into your existing sketch that reads from I2C, and send your readings.

The modbus library doesn't care where the data came from (analog, digital, I2C, SPI, serial port, etc.) it just wants to be fed some data values. There are an infinite number of sensor permutations, and there is no way to write examples to cover all of the possibilities. Look at several examples that read from different types of sensors, and try to figure out what is in common between them. Much of that commonality will need to be added to your code.

Go Up