Control Remote Mains Switches using C

For this project we are assuming, that you have a C development environment set up and that you have a rudimentary understanding of the C language.

If you are totally new to C itself you should start here. If you are new to the Tinkerforge API, you should start here.

We are also assuming that you have the remote control connected to an Industrial Quad Relay Bricklet as described here.

Goals

The goal of this little project is to give an idea how the relays of the Industrial Quad Relay Bricklet have to be switched to turn the mains switches on/off.

The following code uses industrial_quad_relay_set_monoflop to trigger a button press on the remote control. A monoflop will set a new state (relay open or close) and hold it for a given time (1.5s in the example code). After this time the previous state is restored. This approach simulates a button click that takes 1.5s (1500ms).

According to the hardware setup section the inputs of the remote control should be connected as follows:

Signal Relay
A 0
B 1
ON 2
OFF 3

To trigger the switch "A ON" of the remote control the relays 0 and 2 of the Industrial Quad Relay Bricklet have to be closed. This is represented by the selection mask (1 << 0) | (1 << 2).

So the monoflop function should be called with this selection mask and a time of 1500ms to simulate a button press of "A ON". See the following source code for a minimal example.

Source Code

Download

#include <stdio.h>

#include "ip_connection.h"
#include "bricklet_industrial_quad_relay.h"

#define HOST "localhost"
#define PORT 4223
#define UID "XYZ" // Change to your UID

#define VALUE_A_ON  ((1 << 0) | (1 << 2)) // Pin 0 and 2 high
#define VALUE_A_OFF ((1 << 0) | (1 << 3)) // Pin 0 and 3 high
#define VALUE_B_ON  ((1 << 1) | (1 << 2)) // Pin 1 and 2 high
#define VALUE_B_OFF ((1 << 1) | (1 << 3)) // Pin 1 and 3 high

int main(void) {
    // Create IP connection
    IPConnection ipcon;
    ipcon_create(&ipcon);

    // Create device object
    IndustrialQuadRelay iqr;
    industrial_quad_relay_create(&iqr, UID, &ipcon);

    // Connect to brickd
    if(ipcon_connect(&ipcon, HOST, PORT) < 0) {
        fprintf(stderr, "Could not connect\n");
        return 1;
    }
    // Don't use device before ipcon is connected

    // Set pins to high for 1.5 seconds
    industrial_quad_relay_set_monoflop(&iqr, VALUE_A_ON, 15, 1500);

    ipcon_destroy(&ipcon); // Calls ipcon_disconnect internally
    return 0;
}