Skip to content
Giampiero Baggiani edited this page Oct 3, 2022 · 26 revisions

This library provides simple functions to monitor and control Iono's I/Os and RS-485 interface.

Import the library in your sketch:

#include <Iono.h>

The library includes the following constants, corresponding to Iono's pins (inputs/outputs), to be used as the pin parameter of the methods described below:

DO1     DO2     DO3     DO4     DO5     DO6

DI1     AV1     AI1

DI2     AV2     AI2

DI3     AV3     AI3

DI4     AV4     AI4

DI5     DI6     AO1

For each pin of Iono there is the corresponding IONO_PIN_<XX> constant mapping to the corresponding Arduino's/RP2040's pin. For instance IONO_PIN_DO1 maps to A4 on Iono Uno and to 3 on Iono MKR. Additionally, IONO_PIN_DI5_BYP and IONO_PIN_DI6_BYP are defined, mapping to the Arduino/RP2040 pins connected respectively to DI5 and DI6 bypassed (jumper in BYP position).

A reference to the serial port connected to the RS-485 interface is available as IONO_RS485.
Usage example:

IONO_RS485.begin(19200, SERIAL_8N1);

Iono MKR and Iono RP need the TX-enable pin to be controlled when sending or receiving data, use the serialTxEn() method, e.g.:

Iono.serialTxEn(true);
IONO_RS485.write(data);
IONO_RS485.write(more_data);
IONO_RS485.flush();
Iono.serialTxEn(false);
IONO_RS485.readBytes(buffer, length);

Following are the available methods:

setup

Iono.setup()

To be called once in your setup() function to initialize Iono.

read

Iono.read(uint8_t pin)

This method returns the value read from the specified pin. If the specified pin is digital (i.e. DO1...DO6, DI1...DI6) the returned value can be HIGH or LOW. If the specified pin is an analog voltage (i.e. AV1...AV4) the returned value is a float ranging from 0.0 to 10.0 (30.0 for Iono MKR). If the specified pin is an analog current (i.e. AI1...AI4) the returned value is a float ranging from 0.0 to 20.0 (25.0 for Iono MKR). If the specified pin is AO1, the returned value is a float ranging from 0.0 to 10.0.

readAnalogAvg

Iono.readAnalogAvg(uint8_t pin, int n)

This method returns the average of n subsequent readings on the specified analog input.

setBypass

Iono.setBypass(uint8_t pin, iono_pin_mode_t mode)

This method shall be called when using DI5 and/or DI6 as TTL-level I/O lines (i.e. the corresponding internal jumper is set in the bypass - BYP - position). Set mode to INPUT or OUTPUT to specify the line direction.

write

Iono.write(uint8_t pin, float value)

This method writes the passed value to the specified output pin. If the specified pin is a digital output (i.e. DO1...DO6) the accepted values are HIGH or LOW. If the specified pin is AO1 the accepted values ranges from 0.0 to 10.0

flip

Iono.flip(uint8_t pin)

This method switches the state of the specified digital output pin (i.e. DO1...DO6).

subscribeDigital

Iono.subscribeDigital(uint8_t pin, unsigned long stableTime, Callback *callback);

This method can be used to attach a callback method to the change of state of a digital input.

The callback parameter must point to a void function accepting two parameters: a uint8_t and a float; for instance:

void myCallback(uint8_t pin, float value)

This function will be called every time the specified pin changes state and maintains such state at least for a time equal to the stableTime parameter, in milliseconds. The parameters passed to the callback function will correspond to the monitored pin and the value that triggered the call.

subscribeAnalog

Iono.subscribeAnalog(uint8_t pin, unsigned long stableTime, float minVariation, Callback *callback)

This method can be used to attach a callback function to the change of state of an analog input.

The callback parameter must point to a void function accepting two parameters: a uint8_t and a float; for instance:

void myCallback(uint8_t pin, float value)

This function will be called every time the specified pin changes value of an amount equal or bigger than the minVariation parameter and maintains such difference at least for a time equal to the stableTime parameter, in milliseconds. The parameters passed to the callback function will correspond to the monitored pin and the value that triggered the call.

linkDiDo

Iono.linkDiDo(uint8_t dix, uint8_t dox, uint8_t mode, unsigned long stableTime)

This method can be used to link the state of a digital input (DI1...DI6) to a relay output (DO1...DO6).

The mode parameter can be set to:
LINK_FOLLOW: to have the relay closed when the input is high and open when low
LINK_INVERT: to have the relay open when the input is high and closed when low
LINK_FLIP_H: to have the relay flipped at any input transition from low to high
LINK_FLIP_L: to have the relay flipped at any input transition from high to low
LINK_FLIP_T: to have the relay flipped at any input transition (low to high or high to low)

The stableTime parameter specifies the minimum time (in millisecond) the input must maintain its state before the command is performed on the relay (debounce filter).

N.B. This function can be used in combination with subscribeDigital(), but the stableTime parameters must be set to the same value.

process

Iono.process()

This method must be called periodically (inside the loop() function) if subscribeDigital(), subscribeAnalog() or linkDiDo() are used.

This method checks the inputs and calls the callback functions and performs the relay actions if required.

serialTxEn

Iono.serialTxEn(bool enabled)

This method shall be used on Iono MKR and Iono RP to control the TX-enable line of the RS-485 interface. If called on Iono Uno it has no effect.

Call Iono.serialTxEn(true) before writing to the IONO_RS485 serial. When incoming data is expected, call Iono.serialTxEn(false) before. Good practice is to call Iono.serialTxEn(false) as soon as data has been written and flushed to the serial port.