0

I have a scd30 sensor wired to an Arduino Uno. The scd30 works on the 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 I'm 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()));

}
1

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.

  • It doesnt seem to work. The code compiles well but I dont think it actually stores them. Do you know of any other libraries that can store in registers to modbus tcp? – user401708 Sep 11 '18 at 7:46
  • and how do you reed it? what is your Modbus TCP client? – Juraj Sep 11 '18 at 8:24
  • Its a field agent that does this. The field agent reads the registers of the arduino or should read it if the arduino is configured properly – user401708 Sep 11 '18 at 8:49
  • what software is used by field agent? some modbus clients offsets are 1-based, so a register configured as 100 in library should be 101 in this software.github.com/andresarmento/modbus-arduino/blob/master/README.md – Juraj Sep 11 '18 at 9:06
  • its a modbus slave. dont know the exact software. Tried changing it to 101 and still get the same error. ILLEGAL DATA ADDRESS – user401708 Sep 11 '18 at 9:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.