Control Remote Mains Switches using PHP – Tinkerforge

- Getting Started
  - Primer
  - Tutorials
  - FAQ
- Hardware
  - Bricks
  - Bricklets
  - Master Extensions
  - Power Supplies
  - Accessories
  - Timeline
- Software
  - Brick Daemon (brickd)
  - Brick Viewer (brickv)
  - API Bindings
  - Firmwares and Plugins
  - Device Identifier
  - Source Code and Bug Tracking
  - Programming Interface
- Starter Kits
  - Weather Station
  - Hardware Hacking
    - Features
    - Description
    - Technical Specifications
    - Resources
    - Required Tools
    - Firmware updating and first tests
    - How it works
    - Hardware Hacking for Beginners
    - Examples
      - Control Mains Switches Remotely
        - Hardware Setup
        - Using Android
        - Using Windows Phone
        - Using iOS
        - Using C# with GUI
        - Using C/C++
        - Using C#
        - Using Delphi/Lazarus
        - Using Java
        - Using PHP
          - Goals
          - Source Code
        - Using Python
        - Using Ruby
        - Using Visual Basic .NET
      - Read out Smoke Detectors
      - Control Garage Doors Remotely
      - Is someone at the Door? - Doorbell Notifier
  - Server Room Monitoring
  - Blinkenlights
  - Internet of Things
  - Camera Slider
- Embedded Boards
  - Raspberry Pi
- Specifications
  - Technical Data
  - TCP/IP Protocol
  - Modbus Protocol
  - Wireshark Dissector

# Control Remote Mains Switches using PHP

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

If you are totally new to PHP 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 `setMonoflop()` 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

    <?php

    require_once('Tinkerforge/IPConnection.php');
    require_once('Tinkerforge/BrickletIndustrialQuadRelay.php');

    use Tinkerforge\IPConnection;
    use Tinkerforge\BrickletIndustrialQuadRelay;

    const HOST = 'localhost';
    const PORT = 4223;
    const UID = 'ctG'; // Change to your UID
    $VALUE_A_ON  = (1 << 0) | (1 << 2); // Pin 0 and 2 high
    $VALUE_A_OFF = (1 << 0) | (1 << 3); // Pin 0 and 3 high
    $VALUE_B_ON  = (1 << 1) | (1 << 2); // Pin 1 and 2 high
    $VALUE_B_OFF = (1 << 1) | (1 << 3); // Pin 1 and 3 high

    $ipcon = new IPConnection(); // Create IP connection
    $iqr = new BrickletIndustrialQuadRelay(UID, $ipcon); // Create device object

    $ipcon->connect(HOST, PORT); // Connect to brickd
    // Don't use device before ipcon is connected

    $iqr->setMonoflop($VALUE_A_ON, 15, 1500);

    $ipcon->disconnect();

    ?>

The content of this page is licensed under Creative Commons Attribution 3.0 Unported License.