Prototype Evaluation

Hi again!

So my project of controlling the lights by using a relay as a 4-way switch and sensing the current (up to 5A) to know the state is coming along quite nicely!

I've got my first working prototype soldered on a board.
I want to share my progress and receive some feedback as this is my first attempt on something like this.

This is my schematic:

This is how the final project should look like:

This is what it looks like now: (relay is different, can't source the actual one yet)



This is the code: (for the relay I'm currently using)

#include <ESP8266WiFi.h>
#include <MQTTClient.h>
#include <ESP8266WebServer.h>
#include <ESP8266mDNS.h>
#include <ESP8266HTTPUpdateServer.h>

//Device Name  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
char* DevName = "Arduino_Switch_test";

// Wi-Fi - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const char* ssid = "@@@";
const char* password = "@@@"; 

//MQTT - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
String subscribeTopic = String("openhab/out/") + DevName + "/command"; 
String outTopic = String("openhab/in/") + DevName + "/state";

const char* server = "@@@";
const char* mqttDeviceID = DevName;
const char* mqttDeviceUsername = "@@@"; 
const char* mqttDevicePassword = "@@@"; 

// Pins and other Variables - - - - - - - - - - - - - - - - - - - - - - - - - -
const byte inputPin = 14;
const byte outputPin = 5; //LED_BUILTIN;
byte state;
byte LastState;
bool first_run = true;

// Web Updater Settings - - - - - - - - - - - - - - - - - - - - - - - - - - - -
const char* host = DevName;
const char* update_path = "/firmware";
const char* update_username = "@@@";
const char* update_password = "@@@";
ESP8266WebServer httpServer(80);
ESP8266HTTPUpdateServer httpUpdater;

// Something - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
WiFiClient net;
MQTTClient client;

//Connect to WiFI and MQTT - - - - - - - - - - - - - - - - - - - - - - - - - -
void connect();

//Setup  - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
void setup() {
     Serial.begin(74880);
   pinMode(inputPin, INPUT);
   pinMode(outputPin, OUTPUT);
   digitalWrite(outputPin, LOW);
   pinMode(LED_BUILTIN, OUTPUT);
   digitalWrite(LED_BUILTIN, LOW);
   
   WiFi.mode(WIFI_STA);
  
   WiFi.begin(ssid, password);
   client.begin(server, net);
   client.onMessage(messageReceived);

   connect();

   //something about the webserver
   MDNS.begin(host);
   httpUpdater.setup(&httpServer, update_path, update_username, update_password);
   httpServer.begin();
   MDNS.addService("http", "tcp", 80);
}

//Connect to WiFI and MQTT - - - - - - - - - - - - - - - - - - - - - - - - - -
void connect() 
{
  while (WiFi.status() != WL_CONNECTED) 
  {
    delay(1000);
  }

  while (!client.connect(mqttDeviceID,mqttDeviceUsername,mqttDevicePassword)) 
  {
    delay(1000);
  }

  client.subscribe(subscribeTopic);
}

//Loop - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
int i=0;
void loop() {
  // MQTT Loop
  client.loop();
  // Make sure device is connected
  if(!client.connected()) 
  {
    connect();
  }
  PubState();
  httpServer.handleClient();
  delay(10);
} 

//Publish new state to MQTT  - - - - - - - - - - - - - - - - - - - - - - - - -
void PubState(){
  String msg;
  state = digitalRead(inputPin);
  if (state == 0){
    msg="ON";
  }
  else{
    msg="OFF";
  }
  if (first_run == true){
    client.publish(outTopic,String(msg));
    LastState = state;
    first_run = false;
  }
  if(state != LastState){
    client.publish(outTopic,String(msg));
    LastState = state;
  }
}

// Change the state of a relay - - - - - - - - - - - - - - - - - - - - - - - -
void messageReceived(String &topic, String &payload) {
  if (digitalRead(outputPin) == LOW){
    digitalWrite(outputPin, HIGH);
  }
  else{
    digitalWrite(outputPin, LOW);
  }
  delay(20);
}

I'm still troubleshooting some minor issues but I sure would like to hear if I made any mistakes so far.

Are you going to use this in either North or South America, or in the rest of the world. The primary power is wired different. If NA, SA, how are you ensuring the hot line is being switched and not the return wire?

Paul

This will be done in Europe.
And the photo is just a prototype.
The final version will have a dpdt relay wired for phase reversal switching the lines between the two wall switches.