BREAKING NEWS

04 November 2015

Android Arduino communication via RS485 and Modbus

In this post I will talk about one of the projects I'm working on at this time. It is to use amulti touch panel Android that communicates via RS485 and Modbus with one (or more) Arduino board. In particular, the scope of the project covers the home automation (on / off / status lights, temperature control, etc.) although many of the techniques and concepts that we will see can be successfully applied in other fields.


The nice thing about the touch screen  I've used is the wide availability of ports, extensive connectivity in general: Ethernet, USB, RS232, RS485 and I²C also. Although it has ports very fast, I specifically chosen to utilzzare rs485 'cause the device Arduino (One for example) to support for example the Ethernet interface are in need of additional electronics that drives up the price further (although from the point of view of Feasibility there are several projects in the network). 
Rs485 is a standard well known in industrial and home automation. And 'half-duplex, uses two wires, very tolerant of noise, you can achieve good speed and connect to remote devices even at 1,200 meters.

In addition, using the protocol Modbus serial, also this developed mainly in industrial automation, is open, simple and robust to develop and maintain. I chose to use a variant called the Modbus RTU , although some still exist in others.
The sections of the article are the following:
  • Electronic parts
  • Circuit Diagram
  • Arduino Sketch
  • Android App

Electronic parts

The key ingredients that I used in this project are as follows:
  • panel multi touch Android already rooted
  • One (or more) board Arduino ( not necessarily Mega)
  • A chip MAX485 Maxim (or possibly a cheaper one of TI, the ' sn75176a )
  • Led
  • A temperature sensor (such as for instance TMP36 )
  • Switches
  • A potentiometer
  • A 120 ohm resistor (although projects such as this where the distance between master and slave is limited)
  • Wires and some welding experience :)

Circuit Diagram

Android Arduino communication via Modbus RS485
The right figure is a schematic diagram of the circuit inpiegato in this project (some figures were made ​​with Fritzing ). Although it was not shown, you can be connected to the main bus more 'Arduino board.

Arduino sketch

The sketch Arduino uses essentially the library SimpleSlaveModbus  ( how to install a new library Arduino ). Although it is relatively simple to use, I suggest you also to read the documentation, so as to understand more in detail its funzionamente but above all to understand what are the limitations.
After including the library (Slave) Modbus in the head module, I declared an integer array (called holdingRegs) which, as the name recalls, it stores logs Modbus. The function 16combito will have to change as the function 3 to read the logs. 
The first two registers are reserved for analog values ​​that are read via the function analogRead pin A0 and A1. Registers INDIG0 to INDIG4, are reserved for the digital inputs, OUTD0 to OUTD4 for digital outputs and AOUT0 for the PWM. 
In the setup function of the sketch, call the function modbus_configure to configure the Modbus providing parameters such as baud rate, the 'ID of the slave (address), the enable pin to the transmission and the number of registers of the Modbus. Subsequently configure, through the function pinMode, the digital pins of the board.
In the loop function of the sketch, I placed a call to the modbus_update , specifying as its first parameter a reference to the array that contains the Modbus registers. This function will be who will take charge of managing the commands received from the Modbus serial, changing the value of the registers in case of writings or responding with the value of the registers in the case of reading. 
The remaining part of the sketch reads the values ​​of the analog pins A0 and A1, reads the values ​​of the digital inputs (digital output pins were set as INPUT_PULLUP) and finally writes the values ​​of interuttori.
To get an idea about the state of the communication channel, I used a red LED that indicates when a Modbus message is received and processed. All this can be done by changing the library sources Modbus, adding a pair of digitalWrites (set-up ad HIGH LOW beginning and the end) of a pin (7 in our case)
In the post called Project Home automation with Android , I talk about how to use a library to communicate with the native Modbus slave Arduino.
If you have access to a network RJ45, power even think of using the Modbus TCP forcommunication between Android and Arduino .
What do you think?
Update: For those of you who use an updated version of the library SimpleModbusSlave, the Arduino code presented above will be slightly modified. Using for example the version V7, you must change the method call modbus_configure . In fact, compared to previous versions, now accepts the following parameters:
  • A pointer to the serial interface (hardware)
  • The baud rate of the Modbus connection
  • The Modbus-byte format. The values ​​given are as follows
    • SERIAL_8N2: 1 start bit, 8 data bits, 2 stop bits
    • SERIAL_8E1: 1 start bit, 8 data bits, 1 bit Even parity, 1 stop bit
    • SERIAL_8O1: 1 start bit, 8 data bits, 1 Odd parity bits, 1 stop bit
  • The id of the node
  • The enable pin transceiver MAX485 (or similar)
  • The size of the array that contains the Modbus registers
  • A pointer to the array that contains the Modbus registers
In essence, the new method call is the following:

As the array reference that contains the Modbus registers was passed to the library with the configuration call Modbus, no longer you need to specify this reference when calling the method modbus_update .

Share this:

Post a Comment

 
Back To Top
Distributed By Blogger Templates | Designed By OddThemes