user401708
user401708

Reputation: 23

Using Modbus TCP on Arduino

I have a scd30 sensor wired to an arduino uno. The scd30 works on theh I2c protocol. I am able to read data live on the serial monitor on the arduino IDE. I have an ethernet shield on my arduino. I would like the arduino to communicate with a field agent which will upload the data to the internet.

I have tried numerous modbus tcp libraries and dont seem to be getting anywhere. I can connect my arduino to the field agent but whenever it sends data I get a 0x02 exception code - Illegal data address. This is the Library im using https://github.com/andresarmento/modbus-arduino/tree/master/libraries/ModbusIP/examples

I believe the right way to go about it is through holding registers but im not sure how to do this when using i2c. The connection is fine, the problem is the format. any help is appreciated thanks.

/*
  Reading CO2, humidity and temperature from the SCD30
  This example prints the current CO2 level, relative humidity, and temperature in C.
*/
#include <SPI.h>
#include <Ethernet.h>
#include <Modbus.h>
#include <ModbusIP.h>
#include <Wire.h>
#include <Streaming.h>
#include "SparkFun_SCD30_Arduino_Library.h" 

SCD30 airSensor;
//Modbus Registers Offsets (0-9999)
const int SENSOR_ISTS = 100; 
//ModbusIP object
ModbusIP mb;
long ts;    

void setup()
{
  Wire.begin();
  Serial.begin(9600);
  Serial.println("SCD30 Example");

 // The media access control (ethernet hardware) address for the shield
    byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };  
    // The IP address for the shield
    byte ip[] = { 000 , 00,0, 00  }; 
    byte gateway[] = { 0, 0, 0, 0 };  
    byte subnet[] = { 255, 255, 255, 0 }; 
    //Config Modbus IP 
    mb.config(mac, ip,gateway,subnet);
    // Add SWITCH_ISTS register - Use addIsts() for digital inputs 
    mb.addHreg(SENSOR_ISTS);
  airSensor.begin(); //This will cause readings to occur every two seconds

}

void loop()
{

mb.task();
   mb.Hreg(SENSOR_ISTS, digitalRead(airSensor.getTemperature()));

}

Upvotes: 0

Views: 3456

Answers (2)

Juraj
Juraj

Reputation: 3736

The ModbusIP library expects from you that you supply the value of the register. The AirSensor library gives you that value.

Set the register value to Hreg:

mb.Hreg(SENSOR_ISTS, airSensor.getTemperature());

I tested your sketch without the sensor library and it is working. Client was my java test client I use to test access to Modbus TCP registers of my photovoltaic system.

Make sure that the client calls "0x03 - Read Holding Registers" and test address 100 and 101 because some modbus clients offsets are 1-based.

Upvotes: 1

shubham kumar
shubham kumar

Reputation: 53

I have read your problem. In my view you first have to create a local server, somewhere like thingspace (https://thingspace.verizon.com/) or other online local servers, from there you can easily handle the data coming from the sensors.

You are using a code from library so it must be correct in any way. So, from my view you should check the data transactions.

wish my ans helps you thanks!

Upvotes: 2

Related Questions