STM8S103 Relay Boards

../../../../../../_images/stm8relay.jpg

General

These are MODBUS multi-channel relay boards with optocouplers and digital inputs. Advertised as using STM8S103F3 microcontroller, but no model number. 1, 2, 4 and 8-channel versions are available. Only 4-channel version has been tested.

There are a number of improvements over the cheaper boards (such as R421A):

  • Barrel jack power connector provided in addition to screw terminals

  • Relays are driven using optocouplers

  • Relay outputs use pluggable board connectors

  • PCB layout has good electrical isolation between relay outputs and coil side

  • Readable digital inputs - 3.3v logic-level, active low, connected directly to microcontroller.

  • Programming connections are made available so can be programmed with custom firmware.

  • Slave address default is 1, can be changed using custom commands (no DIP switches)

Device properties include:

Address

The address of a modbus slave. Modbus docs. call this the slave ID.

Node

Represents something a slave device does. Modbus relay boards have one node for each output it controls.

Node ID

The channel a node lives on. In a modbus transaction this is the address field.

Commands

ReadCoil, WriteCoil

Read or set state of Relays

ReadDiscreteInputs

Read state of digital inputs. Bits are set to ‘1’ if input is pulled to ground.

ReadHoldingRegister

Registers must be read individually. Attempting to read multiple registers returns invalid data. 0x0004 Software month as 2 characters, e.g. “Ap” 0x0008 Software year in BCD, e.g. 0x2018 for 2018 0x0010 Software time in BCD, e.g. 0x1459 for 14:59 hrs 0x0020 Hardware version, e.g. 0x006a 0x4000 Slave address, typically broadcast (to address #0)

WriteHoldingRegister

0x4000 Set slave address, typically broadcast (to address #0)

namespace STM8Relay

Modbus/STM8Relay/Device.h

Created on: 24 June 2022

Copyright 2022 mikee47 mike@sillyhouse.net

This file is part of the IOControl Library

This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 or later.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this library. If not, see https://www.gnu.org/licenses/.

STM8S103 modbus relay board

4-channel relay boards with optocouplers and digital inputs. Advertised as using STM8S103 microcontroller, but no model number. Relays controlled using Modbus ‘coil’ commands. Digital inputs read as ‘discrete inputs’. Connected directly to 3.3v microcontroller. Grounding pin sets bit.

Modbus/STM8Relay/Request.h

Copyright 2022 mikee47 mike@sillyhouse.net

This file is part of the IOControl Library

This library is free software: you can redistribute it and/or modify it under the terms of the GNU General Public License as published by the Free Software Foundation, version 3 or later.

This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for more details.

You should have received a copy of the GNU General Public License along with this library. If not, see https://www.gnu.org/licenses/.

Variables

constexpr uint8_t RELAY_CHANNEL_MIN = 1
constexpr uint8_t RELAY_MAX_CHANNELS = 16
struct StateMask
#include <Device.h>

Tracks state of multiple relays.

Public Members

BitSet32 channelMask

Identifies valid channels.

class Device : public IO::Modbus::Device
#include <Device.h>

Public Functions

virtual IO::Request *createRequest() override

Create a request object for this device.

Return values:

Request* – Caller must destroy or submit the request

inline virtual DevNode::ID nodeIdMin() const override

Get minimum valid Node ID for this device.

Typically devices have a contiguous valid range of node IDs

inline virtual uint16_t maxNodes() const override

Determine maximum number of nodes supported by the device.

Return values:

uint16_t – 0 if device doesn’t support nodes

virtual DevNode::States getNodeStates(DevNode node) const override

Return the current set of states for all nodes controlled by this device.

Used to determine if, say, all nodes are ON, OFF or a combination.

virtual void handleEvent(IO::Request *request, Event event) override

Implementations may override this method to customise event handling.

struct Config
#include <Device.h>

R421A device configuration.

Public Members

Modbus::Device::Config modbus

Basic modbus configuration.

uint8_t channels

Number of channels (typically 4 or 8)

class Factory : public IO::RS485::Device::FactoryTemplate<Device>
#include <Device.h>

Public Functions

inline virtual const FlashString &deviceClass() const override

Return the Device class name, e.g. ‘r421a’.

class Request : public IO::Modbus::Request
#include <Request.h>

Public Functions

virtual ErrorCode parseJson(JsonObjectConst json) override

Fill this request from a JSON description.

virtual void getJson(JsonObject json) const override

Get result of a completed request in JSON format.

virtual bool setNode(DevNode node) override

If nodes are supported, implement this method.

/

/**

virtual DevNode::States getNodeStates(DevNode node) override

Query node status from response.

virtual ErrorCode callback(PDU &pdu) override

Process a received PDU.

Parameters:

pdu

Return values:

ErrorCode – If request is re-submitted, return Error::pending, otherwise request will be completed with given error.