Hi,
I have setup a “simpleMosbus” network that works fine.
This controls an electric gate opener and a door bell.
slave id2 is controlling the gate motor.
Slave id3 picks up doorbell press and open close buttons
I now want to add ioT using the android Blynk app. www.blynk.cc
the app works on its own, the simpleModbus works on its own,
as soon as i enable Blynk ( BlynkSimpleEthernet.h ) , comms over simpleModbus fails.
Master code
//Master Code
#include <SimpleModbusMaster.h>
//#define BLYNK_PRINT Serial // Comment this out to disable prints and save space
#include <SPI.h>
#include <Ethernet.h>
#include <BlynkSimpleEthernet.h> // IF this is left in (allong with Blynk.run(); in loop), communication on simplemodbus fails////////////////////////////////////////////////
#include <SimpleTimer.h>
// You should get Auth Token in the Blynk App.
// Go to the Project Settings (nut icon).
char auth[] = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
SimpleTimer timer;
//////////////////// Port information ///////////////////
#define baud 9600
#define timeout 1000
#define polling 150 // the scan rate //was 200 dropped to speed up update
#define retry_count 10
// used to toggle the receive/transmit pin on the driver
#define TxEnablePin 2
// This is the easiest way to create new packets
// Add as many as you want. TOTAL_NO_OF_PACKETS
// is automatically updated.
enum
{
PACKET1,
PACKET2,
PACKET3,
PACKET4,
// PACKET5,
// PACKET6,
TOTAL_NO_OF_PACKETS // leave this last entry
};
// Create an array of Packets to be configured
Packet packets[TOTAL_NO_OF_PACKETS];
// The total amount of available memory on the master to store data
//////////////// registers of your master ///////////////////
enum
{
// just add or remove registers and your good to go...
// The first register starts at address 0
//DATA IN/OUT OF SLAVE ID2
GATE_CONTROL, //0 in from slave3 internal phone buttons
DOOR_BELL, //1 in from slave3 bell pressed?
GATE_CONTROL_MASTER, //2 send this to slave 2 to open/close/stop gate
SPAIR3, //3
SPAIR4, //4
GATE_STATUS, //5 in frome slave2
SPAIR6, //6
SPAIR7, //7
SPAIR8, //8
SPAIR9, //9
REGS_SIZE // leave this one
// total number of registers for function 3 and 16 share the same register array
// i.e. the same address space
};
// Masters register array
unsigned int regs[REGS_SIZE];
void setup()
{
//move data detween slaves
//slave id 1 = door phone
//slave id 2 = gate controller
//slave id 3 = internal phone buttons
// modbus_construct(&packets[PACKET1], 1, READ_HOLDING_REGISTERS, 5, 3, 0); // data read from the slave from first and next x many. put in last
// modbus_construct(&packets[PACKET2], 1, PRESET_MULTIPLE_REGISTERS, 1, 1, 1); // data to be written to the slave
modbus_construct(&packets[PACKET1], 2, READ_HOLDING_REGISTERS, 5, 1, 5); // 2= slavegate controler read reg5 for ONE reg , put it in my reg5 (first digit)
modbus_construct(&packets[PACKET2], 3, READ_HOLDING_REGISTERS, 5, 2, 0); // 3= slavegate controler read reg5 for two reg , put it in my reg0 (first digit)
modbus_construct(&packets[PACKET3], 2, PRESET_MULTIPLE_REGISTERS, 0, 1, 0); //put from masters reg0 for 1 reg put it in slave reg0
modbus_construct(&packets[PACKET4], 3, PRESET_MULTIPLE_REGISTERS, 0, 1, 5); //put from masters reg0 for 1 reg put it in slave reg5
// Initialize the Modbus Finite State Machine
modbus_configure(&Serial, baud, SERIAL_8N2, timeout, polling, retry_count, TxEnablePin, packets, TOTAL_NO_OF_PACKETS, regs);
Serial.begin(9600); // See the connection status in Serial Monitor
Blynk.begin(auth);
// Setup a function to be called every second
timer.setInterval(1000L, sendUptime);
}
void loop()
{
modbus_update();
Blynk.run(); // Initiates Blynk
timer.run(); // Initiates SimpleTimer
// if ( Regs[GATE_CONTROL] == 1 && BUTTONb == LOW ){ }
}
void sendUptime()
{
// You can send any value at any time.
// Please don't send more that 10 values per second.
Blynk.virtualWrite(V5, millis() / 1000);
}
CPP file
#include "SimpleModbusMaster.h"
#include "HardwareSerial.h"
// SimpleModbusMasterV2rev2
// state machine states
#define IDLE 1
#define WAITING_FOR_REPLY 2
#define WAITING_FOR_TURNAROUND 3
#define BUFFER_SIZE 64
unsigned char state;
unsigned char retry_count;
unsigned char TxEnablePin;
// frame[] is used to receive and transmit packages.
// The maximum number of bytes in a modbus packet is 256 bytes
// This is limited to the serial buffer of 64 bytes
unsigned char frame[BUFFER_SIZE];
unsigned char buffer;
long timeout; // timeout interval
long polling; // turnaround delay interval
unsigned int T1_5; // inter character time out in microseconds
unsigned int frameDelay; // frame time out in microseconds
long delayStart; // init variable for turnaround and timeout delay
unsigned int total_no_of_packets;
Packet* packetArray; // packet starting address
Packet* packet; // current packet
unsigned int* register_array; // pointer to masters register array
HardwareSerial* ModbusPort;
// function definitions
void idle();
void constructPacket();
unsigned char construct_F15();
unsigned char construct_F16();
void waiting_for_reply();
void processReply();
void waiting_for_turnaround();
void process_F1_F2();
void process_F3_F4();
void process_F5_F6_F15_F16();
void processError();
H File
#ifndef SIMPLE_MODBUS_MASTER_H
#define SIMPLE_MODBUS_MASTER_H
// SimpleModbusMasterV2r2
/*
SimpleModbusMaster allows you to communicate
to any slave using the Modbus RTU protocol.
To communicate with a slave you need to create a packet that will contain
all the information required to communicate to the slave.
Information counters are implemented for further diagnostic.
These are variables already implemented in a packet.
You can set and clear these variables as needed.
The following modbus information counters are implemented:
requests - contains the total requests to a slave
successful_requests - contains the total successful requests
failed_requests - general frame errors, checksum failures and buffer failures
retries - contains the number of retries
exception_errors - contains the specific modbus exception response count
These are normally illegal function, illegal address, illegal data value
or a miscellaneous error response.
And finally there is a variable called "connection" that
at any given moment contains the current connection
status of the packet. If true then the connection is
active. If false then communication will be stopped
on this packet until the programmer sets the connection
variable to true explicitly. The reason for this is
because of the time out involved in modbus communication.
Each faulty slave that's not communicating will slow down
communication on the line with the time out value. E.g.
Using a time out of 1500ms, if you have 10 slaves and 9 of them
stops communicating the latency burden placed on communication
will be 1500ms * 9 = 13,5 seconds!
Communication will automatically be stopped after the retry count expires
on each specific packet.
All the error checking, updating and communication multitasking
takes place in the background.
In general to communicate with to a slave using modbus
RTU you will request information using the specific
slave id, the function request, the starting address
and lastly the data to request.
Function 1, 2, 3, 4, 5, 6, 15 & 16 are supported. In addition to
this broadcasting (id = 0) is supported for function 5, 6, 15 & 16.
Constants are provided for:
Function 1 - READ_COIL_STATUS
Function 2 - READ_INPUT_STATUS
Function 3 - READ_HOLDING_REGISTERS
Function 4 - READ_INPUT_REGISTERS
Function 5 - FORCE_SINGLE_COIL
Function 6 - PRESET_SINGLE_REGISTER
Function 15 - FORCE_MULTIPLE_COILS
Function 16 - PRESET_MULTIPLE_REGISTERS
Note:
The Arduino serial ring buffer is 64 bytes or 32 registers.
Most of the time you will connect the Arduino using a MAX485 or similar.
In a function 3 or 4 request the master will attempt to read from a
slave and since 5 bytes is already used for ID, FUNCTION, NO OF BYTES
and two BYTES CRC the master can only request 58 bytes or 29 registers.
In a function 16 request the master will attempt to write to a
slave and since 9 bytes is already used for ID, FUNCTION, ADDRESS,
NO OF REGISTERS, NO OF BYTES and two BYTES CRC the master can only write
54 bytes or 27 registers.
Note:
Using a USB to Serial converter the maximum bytes you can send is
limited to its internal buffer which differs between manufactures.
Since it is assumed that you will mostly use the Arduino to connect without
using a USB to Serial converter the internal buffer is set the same as the
Arduino Serial ring buffer which is 64 bytes.
*/
#include "Arduino.h"
#define COIL_OFF 0x0000 // Function 5 OFF request is 0x0000
#define COIL_ON 0xFF00 // Function 5 ON request is 0xFF00
#define READ_COIL_STATUS 1 // Reads the ON/OFF status of discrete outputs (0X references, coils) in the slave.
#define READ_INPUT_STATUS 2 // Reads the ON/OFF status of discrete inputs (1X references) in the slave.
#define READ_HOLDING_REGISTERS 3 // Reads the binary contents of holding registers (4X references) in the slave.
#define READ_INPUT_REGISTERS 4 // Reads the binary contents of input registers (3X references) in the slave. Not writable.
#define FORCE_SINGLE_COIL 5 // Forces a single coil (0X reference) to either ON (0xFF00) or OFF (0x0000).
#define PRESET_SINGLE_REGISTER 6 // Presets a value into a single holding register (4X reference).
#define FORCE_MULTIPLE_COILS 15 // Forces each coil (0X reference) in a sequence of coils to either ON or OFF.
#define PRESET_MULTIPLE_REGISTERS 16 // Presets values into a sequence of holding registers (4X references).
typedef struct
{
// specific packet info
unsigned char id;
unsigned char function;
unsigned int address;
// For functions 1 & 2 data is the number of points
// For function 5 data is either ON (oxFF00) or OFF (0x0000)
// For function 6 data is exactly that, one register's data
// For functions 3, 4 & 16 data is the number of registers
// For function 15 data is the number of coils
unsigned int data;
unsigned int local_start_address;
// modbus information counters
unsigned int requests;
unsigned int successful_requests;
unsigned int failed_requests;
unsigned int exception_errors;
unsigned int retries;
// connection status of packet
unsigned char connection;
}Packet;
// function definitions
void modbus_update();
void modbus_construct(Packet *_packet,
unsigned char id,
unsigned char function,
unsigned int address,
unsigned int data,
unsigned _local_start_address);
void modbus_configure(HardwareSerial* SerialPort,
long baud,
unsigned char byteFormat,
long _timeout,
long _polling,
unsigned char _retry_count,
unsigned char _TxEnablePin,
Packet* _packets,
unsigned int _total_no_of_packets,
unsigned int* _register_array);
#endif