How to: 1-Wire Relay

To make it easier to work with certain 1-Wire devices we implemented module called twr_onewire_relay which allows you to control relay modules connected via 1-Wire bus, for example the Relay module developed by Denkovi.

Requirements

For this tutorial you will need:

SDK

There is very simple workflow for controlling the relay:

  • you have to instantiate a variable representing the relay twr_onewire_relay_t relay;

  • relay connection has to be initiated in application_init() function by calling twr_onewire_relay_init(&relay, TWR_GPIO_P4, 0x00);
    • the second parameter represents ports A and B on the Sensor module. Use _P4 for A and _P5 for B.

    • the third parameter represents number of connected device represented as uint64_t value

  • now you only have to use the twr_onewire_relay_set_state() function to set desired state of every relay

Example

In this example we allow the relays to be controlled with button integrated to Core module. On every press of the button one more relay will be activated. When all relays are active, the next push of button will switch off all of them and the cycle repeats.

 1#include <application.h>
 2
 3twr_onewire_relay_t relay;
 4twr_button_t button;
 5
 6twr_onewire_relay_channel_t relays[] = {
 7        TWR_ONEWIRE_RELAY_CHANNEL_Q1,
 8        TWR_ONEWIRE_RELAY_CHANNEL_Q2,
 9        TWR_ONEWIRE_RELAY_CHANNEL_Q3,
10        TWR_ONEWIRE_RELAY_CHANNEL_Q4,
11        TWR_ONEWIRE_RELAY_CHANNEL_Q5,
12        TWR_ONEWIRE_RELAY_CHANNEL_Q6,
13        TWR_ONEWIRE_RELAY_CHANNEL_Q7,
14        TWR_ONEWIRE_RELAY_CHANNEL_Q8
15};
16
17int activated = 0;
18
19void button_event_handler(twr_button_t *self, twr_button_event_t event, void *event_param)
20{
21    (void) self;
22    (void) event_param;
23
24    if (event == TWR_BUTTON_EVENT_PRESS)
25    {
26        if (activated == 8) {
27            for (int i = 0; i < 8; ++i) {
28                twr_onewire_relay_set_state(&relay, relays[i], false);
29            }
30
31            activated = 0;
32        } else {
33            twr_onewire_relay_set_state(&relay, relays[activated], true);
34            activated++;
35        }
36    }
37}
38
39void application_init(void)
40{
41    twr_onewire_relay_init(&relay, TWR_GPIO_P4, 0x00);
42
43    twr_button_init(&button, TWR_GPIO_BUTTON, TWR_GPIO_PULL_DOWN, 0);
44    twr_button_set_event_handler(&button, button_event_handler, NULL);
45}

Tip

You can get a complete example in our GitHub repository