Industry 4.0 library
Last updated
Last updated
The Plug & Sense! Industry 4.0 device supports the most extended industrial protocols. Since it is the responsibility of the customer to choose sensors and integrate them on her Plug & Sense!, Libelium strongly recommends customers to learn to handle properly these sensors before integrating them with the Plug & Sense! node. This involves not only acquiring knowledge related to the sensor, but a deep learn of how the industrial protocols work.
A good basis on how industrial protocol works, like commands, transmission speed, format of the communication packets, etc, will greatly help and improve the integration of the sensors to the platform.
Each protocol available in the Industry 4.0 device has its own class to manage the functions related to them.
It is mandatory to include the Industry library when using this Plug & Sense!. The following line must be introduced at the beginning of the code:
Before connecting the sensors to the Plug & Sense! Industry 4.0 check the "Sensor sockets" section in this guide. Keep in mind that not every protocol is available for every socket.
Industry_4_20
classFunctions:
The 4-20 mA sensors can be connected to sockets B and F. Depending on the socket or sockets used with 4-20 mA sensors, an object will be created in the code for every sensor.
The ON()
function prepares the socket to power supply the sensor.
The OFF()
function removes the power supply from the socket.
The read()
function reads the value from the sensor. The value read from the sensor will be stored in an internal variable of the library named _current
. Additionally, the function returns the value read as a float.
You can see how to use these functions in this example:
Industry485
classFunctions:
The Industry485
class inherits methods from the Wasp232 library. For further information and prototypes of the different functions, please check the Wasp232 library in the Waspmote API.
An RS-485 sensor can be connected to socket E. In order to use the methods related to the Industry485
class, an object of that class will be created in the code. The constructor requires a parameter to determine the transmission speed.
The ON()
function prepares the socket to power supply the sensor.
The OFF()
function removes the power supply from the socket.
The setPowerSocket()
function enables the 12 V power line in the socket. The function requires one parameter, the state of the power line in the socket that will determine whether it is enabled or disabled.
State options:
SWITCH_ON
SWITCH_OFF
The receive()
function reads data received from the sensor and returns de number of bytes read. Data is stored in an internal variable of the library named _buffer
. This variable is defined as an array of uint8_t
so the length of data is stored in _length
.
The send()
function sends data through the socket. There are several prototypes of this function defined in the Wasp232 library.
You can see how to use these functions in this example:
Industry232
classFunctions:
The Industry232
class inherits methods from the Wasp232 library. For further information and prototypes of the different functions, please check the Wasp232 library in the Waspmote API.
An RS-232 sensor can be connected to socket E. In order to use the methods related to the Industry232
class, an object of that class will be created in the code. The constructor requires a parameter to determine the transmission speed.
The ON()
function prepares the socket to power supply the sensor.
The OFF()
function removes the power supply from the socket.
The setPowerSocket()
function enables the 12V power line in the socket. The function requires one parameter, the state of the power line in the socket that will determine whether it is enabled or disabled.
State options:
SWITCH_ON
SWITCH_OFF
The receive()
function reads data received from the sensor and returns de number of bytes read. Data is stored in an internal variable of the library named _buffer
. This variable is defined as an array of uint8_t
so the length of data is stored in _length
.
The send()
function sends data through the socket. There are several prototypes of this function defined in the Wasp232 library.
You can see how to use these functions in this example:
IndustrySDI12
classFunctions:
The IndustrySDI12
class inherits methods from the WaspSDI12 library. For further information and prototypes of the different functions, please check the WaspSDI12 library in the Waspmote API.
SDI-12 sensors can be connected to sockets A, B, C and D. In order to use the methods related to the IndustrySDI12
class, an object of that class will be created in the code. The constructor requires a parameter to determine the socket where the sensor is connected.
The ON()
function prepares the socket to power supply the sensor.
The OFF()
function removes the power supply from the socket.
The setPowerSocket()
function enables the 12V power line in the socket. The function requires one parameter, the state of the power line in the socket that will determine whether it is enabled or disabled.
State options:
SWITCH_ON
SWITCH_OFF
In SDI-12 sensor sockets, the 12 V power line is enabled automatically when the ON()
function is called. If the sensor does not need power supply from the Plug&Sense!, user can disable this power line with the setPowerSocket(SWITCH_OFF)
function.
The isSensor()
function checks if a sensor is connected in the socket. The function requires one paramater which is the name of the sensor. The function will check internally if the name read from the sensor is the same as the parameter.
The function uses internally two variables available for the user. The address of the sensor which is stored in the address
variable, and the serial number of the sensor that is stored in the serialNumber
variable.
The sendCommand()
function sends a command to the sensor. This function requires two parameters, the command to be sent and the length of this command.
Some of the SDI-12 protocol commands can be found in the SDI-12 section in the "Industry 4.0 Protocols" page of this guide. For a complete description of these commands, please check the official site here: SDI-12.
The readCommandAnswer()
function waits until time is out for a determined number of bytes. The function requires 2 parameters. The first one is the amount of bytes expected from the sensor. The second one is the time to wait for data response.
You can see how to use these functions in this example:
Modbus
classFunctions:
The IndustryModbus
class inherits methods from the ModbusMaster library. For further information and prototypes of the different functions, please check the ModbusMaster library in the Waspmote API.
Depending on the sensor it might be necessary different reading functions. For extended functionalities refer to the Modbus Networking Guide.
Modbus sensors can be connected to sockets E and F. In order to use the methods related to the IndustryModbus
class, an object of that class will be created in the code. The constructor requires two parameters to determine the socket where the sensor is connected and the address of the sensor.
Keep in mind that being both Modbus, in socket E the sensor must be Modbus controlled over RS-485 and in socket F sensor must be Modbus controlled over RS-232. Modbus is a software layer, the physical layer will determine to which socket the sensor can be connected to.
The ON()
function prepares the socket to power supply the sensor.
The OFF()
function removes the power supply from the socket.
The setPowerSocket()
function enables the 12V power line in the socket. The function requires one parameter, the state of the power line in the socket that will determine whether it is enabled or disabled.
State options:
SWITCH_ON
SWITCH_OFF
The setBaudrate()
function changes the data transmission speed. This function requires one parameter which is the working baud rate of the sensor. The baud rate is stored in the internal variable of the library baudrate
.
By default, most sensors use a baud rate of 9600 bps (bits per second). If this function is not used in the code, the platform will set this default baud rate.
The read()
function sends a read holding registers request to the sensor, which will respond with sensor data stored in those registers. The library internally manages all this communication process and stores data in the internal variable response
, defined as an uint16_t
array (uint16_t response[64]
).
Most of the sensor readings of sensors available on the Libelium Catalog are done by this very same function. For extended functionalities of the Modbus protocol, please refer to the Modbus Networking Guide and to the Modbus Master library in the Waspmote API.
You can see how to use these functions in this example:
Public functions
Description
Industry_4_20 (uint8_t _socket)
Class constructor
ON()
Turn on 4-20 mA socket
OFF()
Turn off 4-20 mA socket
read()
Read the 4-20 mA sensor
Public functions
Description
Industry485(unsigned long baudrate)
Class constructor
ON()
Turn on RS-485 socket
OFF()
Turn off RS-485 socket
receive()
Receives data from the socket
send(const char *s)
Sends data through socket
Public functions
Description
Industry232(unsigned long baudrate)
Class constructor
ON()
Turn on RS-232 socket
OFF()
Turn off RS-232 socket
receive()
Receives data from the socket
send(const char *s)
Sends data through socket
Public functions
Description
IndustrySDI12(uint8_t _socket)
Class constructor
ON()
Turn on SDI-12 socket
OFF()
Turn off SDI-12 socket
setPowerSocket(uint8_t _state)
Set 12V power line in socket
available()
Number of characters available
isSensor(char *sensorSearchedName)
Detects if SDI-12 sensor is present
sendCommand(char* cmd, uint8_t length)
Sends data through socket
readCommandAnswer(uint8_t length, unsigned long timeout)
Received data if available
Public function
Description
IndustryModbus(uint8_t _socket, uint8_t _sensorAddr)
Class constructor
ON()
Turn on Modbus socket
OFF()
Turn off Modbus socket
setPowerSocket(uint8_t _state)
Set 12V power line in socket
setBaudrate(unsigned long baudrate)
Set transmission speed
read(uint16_t _registerAddr, uint16_t _numOfRegisters)
Read sensor registers