Using Modbus TCP on Arduino - 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()));
}

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!

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.

Related

Can't read Modbus holding register because ModbusMaster for arduino is lack of documentation

The Problem:
Hi, First of all, i am no expert at C++.
I seem can't find any of the guide(or documentation) of modbusMaster especially for this particular library. Which explains all the class method uses.
I use this library because it supports stream class. not like official library or others. Therefore, i can use softwareSerial to add more serial for rs485.
I am using Arduino pro mini, that's why i need software serial. And this Max485 module:
I am very sure everything is connected properly, thus my aim is to read coil from a Chinese brand ultrasonic level meter which the guide says:
Here's the program what i am trying to do:
#include <ModbusMaster.h>
#include <SoftwareSerial.h>
/*!
We're using a MAX485-compatible RS485 Transceiver.
Rx/Tx is hooked up to the hardware serial port at 'Serial'.
The Data Enable and Receiver Enable pins are hooked up as follows:
*/
#define MAX485_DE 9
#define MAX485_RE_NEG 8
//for software serial
#define RO_RX 7
#define DI_TX 6
// instantiate ModbusMaster object
ModbusMaster node;
// RX, TX on softser
SoftwareSerial mySerial(RO_RX, DI_TX);
void preTransmission()
{
digitalWrite(MAX485_RE_NEG, 1);
digitalWrite(MAX485_DE, 1);
}
void postTransmission()
{
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
}
void setup()
{
pinMode(MAX485_RE_NEG, OUTPUT);
pinMode(MAX485_DE, OUTPUT);
// Init in receive mode
digitalWrite(MAX485_RE_NEG, 0);
digitalWrite(MAX485_DE, 0);
// for serial monitoring on PC
Serial.begin(115200);
// Modbus communication runs at 9600 baud
mySerial.begin(9600);
// Modbus slave ID 1 and pass the software serial
node.begin(1, mySerial);
// Callbacks allow us to configure the RS485 transceiver correctly
node.preTransmission(preTransmission);
node.postTransmission(postTransmission);
Serial.println("init done...");
}
void loop()
{
// now, i'm kinda lost here..
delay(100);
Serial.println("loop end.");
}
I am confused with most of the class method without a proper explanation.
modbusObj.readWriteMultipleRegisters();
modbusObj.writeSingleRegister();
modbusObj.setTransmitBuffer();
modbusObj.readCoils();
modbusObj.writeMultipleRegisters();
modbusObj.readHoldingRegisters();
// etc..
They had no documentation. Well, Some method names are self explanatory but not about the arguments.I am trying just to read the holding register.
TL;DR:
I want to read the level meter's distance data, but the library doesn't provide a proper documentation.
Nb:
nothing wrong with the ultrasonic device, tested in PC to read holding register with some modbus tool and hooked up to rs485 to usb and working!
this question intended in stack overflow rather than stack-electrical-engineering because I am mainly asking about the code.
I've been discussing it with someone in a local facebook forum that said he's successfully utilizing the lib alongside soft-serial and pro-mini.

Using rosserial to communicate with MX-64 DYNAMIXEL motor

When I try to use rosserial to communicate with an Arduino to control the MX-64 motor, every time I use rostopic pub to send a message, it will give me this error:
"Mismatched protocol version in packet: lost sync or rosserial_python is from different ros release than the rosserial client "
If I remove the Dynamixel.move(X_SERVO_ID,rollint); function in the code or don't send any information though ROS, there will be no error. I tried all the rosserial examples, all of them are working just fine without any error.
The weird thing is even if it gives me an error, the program is still working. Every time I send a message, the DYNAMIXEL motor will start moving.
#if (ARDUINO >= 100)
#include <Arduino.h>
#else
#include <WProgram.h>
#endif
#include <ros.h>
#include <std_msgs/Int8.h>
ros::NodeHandle nh;
#include <Dynamixel_Serial.h> // Library needed to control Dynamixal servo
#include <sensor_msgs/Imu.h>
#define X_SERVO_ID 0x01 // ID which we will be set in Dynamixel too
int rollint = 0;
void motor_cb(const std_msgs::Int8& cmd_msg) {
rollint = ((1.00*(90-cmd_msg.data))/360)*4095;
Dynamixel.move(X_SERVO_ID,rollint);
}
ros::Subscriber<std_msgs::Int8> sub("MX_Motor", motor_cb);
void setup() {
delay(100); // Give time for Dynamixel to start on power-up
nh.initNode();
nh.subscribe(sub);
}
void loop(){
nh.spinOnce();
delay(1);
}
It looks like your Dynamixel servo is hooked up to the same serial port that you use to communicate with the rosserial server. In many Arduino boards like the Uno the hardware serial port (where your Dynamixel is hooked up) is connected to a USB serial converter (which connects ROS via USB).
You need to connect your Dynamixel to a different hardware (or software) serial on your Arduino and initialize it with Dynamixel.begin(Stream&) (Dynamixel_Serial.h#L162), where Stream& is that hardware / software serial.

i want to transmit data from avr to pc.whats wrong in below connection or code

connection:
USB to TTL interface modules four pins:
1)0v-volt of avr
2)vcc-5 volt avr
3)TXD-TX of avr
4)RXD-RX of avr
and the USB to TTL serial interface module connected to PC using USB to RS-232 convertor DB9 cable.
below is the code:
#include<avr/io.h>
void UART_transmit(unsigned char data);
int main(void)
{
unsigned char i,message[]="i love india\r\n";
DDRD=0x00;
PORTD=0xFF;
UCSRA=0;
UCSRB=1<<TXEN; // transmitter enable
UCSRC=1<<URSEL | 1<<UCSZ1 | 1<<UCSZ0; // 8 data bit, a stop, none parity
UBRRH=0;
UBRRL=5; // for 9600 baud at 1MHz
while(1)
{
for(i=0;message[i];i++)
{
UART_transmit(message[i]);
}
} // while(1) end
} // main() end
void UART_transmit(unsigned char data)
{
while(!(UCSRA & (1<<UDRE)));
UDR=data;
}
RX of each device must be connected to TX of the other, and vice versa, unless you are using a null modem cable, in which case your connections are correct. You might try using a multimeter and google to determine if your connecting cable is straight or null-modem.
I also recommend that you revise your code as outlined in the manufacturer's application note AVR306: Using the AVR® UART in C. This is the authoritative reference on setting up and using the UART on AVR microcontrollers.

CANbus simulation for Automotive purpose

i explain a bit...
I repair Electric powered steering systems for cars, especially Fiat/Alfa/Lancya (Delphi manufacturers) and i'm in need of making some tool to test these reparations, i mean just turning it on for example.
I have researched during some time, and i figured i need Can-bus signals to be simulated as the eps ECU is receiving ignition packets from CAN, here i go..
I need to know what way i could Read/Send CAN packets from/to BUS, i mean what tool or anything else. I have been trying with Arduino UNO + Sparkfun Shield, but i dont get any results, when everything connected, my serial console isnt sniffing any packets, i have connected all correctly i think, tried different bitRates, changed Arduino boards and shield, tried many different examples, i invested lots of hours with no profit... i was using Seat Ibiza 2010 for I+D, connected CAN-H AND CAN-L on OBD PORT, in the CAN lines from radio,etc...
Any idea of what could be wrong is welcome, as new method to make my project.. Thanks in advance!!
Info:
https://dl.dropboxusercontent.com/u/47864432/arduino/IMG_9358.JPG
https://dl.dropboxusercontent.com/u/47864432/canbus/LIBRARYS_USED.rar
There are two potential issues here:
H/W problem
CAN bus library problem
The first step is try loopback test. If all is OK, try CAN bus from any car OBD port, the speed should be 500Kb.
This one tries a couple of bus speeds -- works with sparkfun canbus shield:
#include <SPI.h>
#include <SD.h>
#include <Canbus.h>
#include <defaults.h>
#include <global.h>
#include <mcp2515.h>
#include <mcp2515_defs.h>
const int chipSelect = 9;
File dataFile;
void setup() {
// put your setup code here, to run once:
pinMode(chipSelect, OUTPUT);
Serial.begin(115200); // For debug use
Serial.println("CAN Read - Testing receival of CAN Bus message");
delay(1000);
if (Canbus.init(CANSPEED_500)) //Initialise MCP2515 CAN controller at the specified speed
Serial.println("CAN Init ok: 500k");
else if (Canbus.init(CANSPEED_250)) //Initialise MCP2515 CAN controller at the specified speed
Serial.println("CAN Init ok: 250k");
else if (Canbus.init(CANSPEED_125)) //Initialise MCP2515 CAN controller at the specified speed
Serial.println("CAN Init ok: 125k");
else
Serial.println("Can't init CAN");
delay(1000);
if (!SD.begin(chipSelect)) {
Serial.println("uSD card failed to initialize, or is not present");
return;
}
else {
Serial.println("uSD card initialized.");
delay(1500);
}
dataFile = SD.open("caninfo.txt", FILE_WRITE);
}
void loop() {
tCAN message;
if (mcp2515_check_message())
{
if (mcp2515_get_message(&message))
{
if (dataFile) {
int timeStamp = millis();
//write to uSD card
dataFile.print(timeStamp);
dataFile.print("ID: ");
dataFile.print(message.id, HEX);
dataFile.print(", ");
dataFile.print("Data: ");
dataFile.print(message.header.length, DEC);
for (int i = 0; i < message.header.length; i++)
{
dataFile.print(message.data[i], HEX);
dataFile.print(" ");
}
dataFile.println("");
Serial.println("Writing to SD");
}
else
{
Serial.println("Problem writing to SD");
}
}
}
}
If you want to communicate via CAN with Steering controller for example for an OEM like Delhpi .. this is not possible as the ECU (ELectronic control units) in the communication network are secured and the CAN protocol software decides who can participate and who can't.
As a tester tool you can read the trouble codes but you can't hack it to simulate the Ignition signal etc...

Sending AT commands to a GSM/GPRS and displaying the reply on a Serial Monitor

I am quite rusty when it comes to Serial ports. I want to send an AT command to a GSM/ GPRS shield connected to my Arduino UNO. The AT command I want to pass in particular is the command to get a networks signal strength.
I am using the SIM900 and SoftwareSerial library to send the command as the GSM library does not compile correctly for me. Meaning I have to use the SoftwareSerial library.
I have this example code from the SIM900 library working that relies on reading inputs from the serial monitor to carry out commands but I need it to be automated and the command to be passed in hardcoded. In this example code, the place of interest is the simplehwread() method.
#include "SIM900.h"
#include <SoftwareSerial.h>
int numdata;
char inSerial[40];
int i=0;
void setup()
{
//Serial connection.
Serial.begin(9600);
Serial.println("GSM Shield testing.");
//Start configuration of shield with baudrate.
//For http uses is raccomanded to use 4800 or slower.
if (gsm.begin(9600))
Serial.println("\nstatus=READY");
else Serial.println("\nstatus=IDLE");
};
void loop()
{
//Read for new byte on serial hardware,
//and write them on NewSoftSerial.
serialhwread();
//Read for new byte on NewSoftSerial.
serialswread();
};
void serialhwread()
{
i=0;
if (Serial.available() > 0) {
while (Serial.available() > 0) {
inSerial[i]=(Serial.read());
delay(10);
i++;
}
inSerial[i]='\0';
if(!strcmp(inSerial,"/END")) {
Serial.println("_");
inSerial[0]=0x1a;
inSerial[1]='\0';
gsm.SimpleWriteln(inSerial);
}
//Send a saved AT command using serial port.
if(!strcmp(inSerial,"TEST")) {
Serial.println("SIGNAL QUALITY");
gsm.SimpleWriteln("AT+CSQ");
} else {
Serial.println(inSerial);
gsm.SimpleWriteln(inSerial);
}
inSerial[0]='\0';
}
}
void serialswread()
{
gsm.SimpleRead();
}
No matter how I modify this code, the command does not get passed in and response displayed while the method here does it but not the way I want it to be done. i.e Direct input. Could anyone assist here?
i have dealt with exactly this scenario at a company with a cellular radio on board. there are many status signals that come over and if not dealt with appropriately these status flags from the cell modem will be lost
you need to look at the data sheets associated with your cell modem and its protocol so you know what flags to watch for at the various steps taken along the way from configuration, to eventual connection to cellular service.
multi-threaded coding techniques must be followed as well.
keep in mind that the comm channel is not ideal and there WILL be failures. provided your coding techniques are sound and you follow protocol requirements, then it should work.
Ron
Boise, ID

Resources