Homer
Published © GPL3+

Arduino - Make an Internet-Based Switch

Transfer the input signal through the Internet.

BeginnerFull instructions provided1 hour7,785
Arduino - Make an Internet-Based Switch

Things used in this project

Story

Read more

Code

output.ino

Arduino
Source code for Arduino relay board. This board works as a TCP server and controls the output state
#include <Phpoc.h>
#include <PhpocExpansion.h>

#define TCP_LOCAL_PORT 1470    // enter your tcp server listening port here
#define EXPANSION_ID 1

byte expansionId = 1;
PhpocServer server(TCP_LOCAL_PORT);

ExpansionRelayOutput relays[4] = {
  ExpansionRelayOutput(expansionId, 0),
  ExpansionRelayOutput(expansionId, 1),
  ExpansionRelayOutput(expansionId, 2),
  ExpansionRelayOutput(expansionId, 3)
};

void relayOutputState(byte relay_state) {
  for (int i = 0; i < 4; i++) {
    int state = (relay_state >> i) & 0x01;
    if (state)
      relays[i].on();
    else
      relays[i].off();
  }
}

void relayPrintState(void) {
  for (int i = 0; i < 4; i++) {
    String state_str;
    String output_str = "";

    state_str = (relays[i].isOn()) ? "ON" : "OFF";
    output_str = output_str + "Relay port " + i + " is " + state_str;

    Serial.println(output_str);
  }
  Serial.println();
}

void setup() {
  Serial.begin(9600);
  while(!Serial)
    ;

  // initialize PHPoC [WiFi] Shield
  Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
  //Phpoc.begin();


  // initialize expansion board
  Expansion.begin();

  // get name and print it to serial
  Serial.println(relays[0].getName());

}

void loop() {
  PhpocClient client = server.available();
  if (client) {
    if (client.available() > 0) {
      // read a byte incoming from the client
      byte relay_state = client.read();

      // set relay output based on the received control state
      relayOutputState(relay_state);

      // get current relay state and print to serial
      relayPrintState();
      delay(500);
    }
  }

}

input.ino

Arduino
Source code for Arduino input board. This board works as a TCP client and gets the input state
#include <Phpoc.h>
#include <PhpocExpansion.h>

#define TCP_PEER_ADDR "192.168.0.101"	// enter your tcp server addr here
#define TCP_PEER_PORT 1470				// enter your tcp server port here
#define EXPANSION_ID 2

char peer_addr[] = TCP_PEER_ADDR;
int peer_port = TCP_PEER_PORT;
byte expansionId = EXPANSION_ID;

PhpocClient client;

ExpansionPhotoInput inputs[4] = {
  ExpansionPhotoInput(expansionId, 0),
  ExpansionPhotoInput(expansionId, 1),
  ExpansionPhotoInput(expansionId, 2),
  ExpansionPhotoInput(expansionId, 3)
};

int last_state[4] = { 0, 0, 0, 0 };

void setup() {
  Serial.begin(9600);
  while(!Serial)
    ;

  // initialize PHPoC [WiFi] Shield
  Phpoc.begin(PF_LOG_SPI | PF_LOG_NET);
  //Phpoc.begin();

  if(client.connect(peer_addr, peer_port))
  {
    // if connected
    Serial.println("Connected to server");
  }
  else // if not connected
    Serial.println("connection failed");
  
  // initialize expansion board
  Expansion.begin();

  // get name and print it to serial
  Serial.println(inputs[0].getName());
}

void loop() {
  bool event = false;
  byte input_state = 0;

  for (int i = 0; i < 4; i++) {
    int state;
    String state_str;

    // Get the state of each input port
    if (inputs[i].isOn())
    {
      state = 1;
      state_str = "ON";
    }
    else
    {
      state = 0;
      state_str = "OFF";
    }

    // Accumulate the input state
    input_state = input_state | (state << i);

    // Update if any input state is changed
    if(state != last_state[i])
    {
      event = true;
      last_state[i] = state;
      String input_str = "";
      input_str = input_str + "Input port " + i + " is " + state_str;
      Serial.println(input_str);
      Serial.println();
    }
  }


  if (event) // Transfer the control state to relay board
  {
    client.write(input_state);
    client.flush();
  }

  delay(500);
}

Credits

Homer

Homer

17 projects • 46 followers

Comments