/* ################################################################
################################################################
########## ESP 8266 MQTT Switch for Home Assistant V2.0 #######
################################################################
###################*** MOSQUITTO ***############################
*/
// UPDATED: SEP 5 2018
// CHANGE SwitchReset and SwitchResetConfirm to SwitchOta and SwitchOtaConfirm in the .yaml file
//########################################################################################
// You Can find this sketch on Home Assistant forum i have just modified it according to my
// Requirements as it is just a waste of time trying to re-invent the wheel
// so thank you for it random citizen :D
//** INCLUDE LIBRARIES ** make sure you have all the Libraries installed
// Get them from within the Arduino IDE Sketch > Include Libraries > Manage Libraries
//########################################################################################
// Update libraries from library Manager and Boards from Boards Manager to latest
#include <PubSubClient.h>
#include <ESP8266WiFi.h>
#include <ArduinoOTA.h>
#include <ESP8266mDNS.h>
#include <WiFiUdp.h>
//##################################################################
void callback(char* topic, byte* payload, unsigned int length);
#define MQTT_SERVER "192.168.1.6" // IP of your MQTT Server
const char* ssid = "Internet"; // SSID for you Wifi Hotspot
const char* password = "12345678qwerty"; // Password for Wifi Hotspot
//########### ESP 8266 GPIO Pins ###########################################
const int switchPin1 = 4;
const int switchPin2 = 5;
const int switchPin3 = 13;
const int switchPin4 = 12;
unsigned long secs = 0;
//#########################################################################
// ***** MQTT Topics Defined In Home Assistant's Configuration.yaml file ***
//#########################################################################
char const* switchTopic1 = "/house/switch1/"; //four switches for relays
char const* switchTopic2 = "/house/switch2/";
char const* switchTopic3 = "/house/switch3/";
char const* switchTopic4 = "/house/switch4/";
char const* switchSleep = "/house/switchSleep/"; // fifth for putting the Esp in sleep mode
char const* switchOta = "/house/switchOta/"; // sixth for updating the Esp // port 8266 //check firewall
//##########################################################################
WiFiClient wifiClient;
PubSubClient client(MQTT_SERVER, 1883, callback, wifiClient);
//#################################################################################
//#################################################################################
//############## SETUP BEGINS #####################################################
//#################################################################################
//#################################################################################
void setup()
{
digitalWrite(switchPin1, LOW);
digitalWrite(switchPin2, LOW);
digitalWrite(switchPin3, LOW);
digitalWrite(switchPin4, LOW);
// These lines will keep the Relays off during start-up
// Because i'm Using NPN transistors for
// switching the relays which are actually
// LOW-level-trigger type
// adding the transistor will invert that logic
// and LOW will be OFF because Transistor is off
pinMode(switchPin1, OUTPUT); // Relay Switch 1
pinMode(switchPin2, OUTPUT); // Relay Switch 2
pinMode(switchPin3, OUTPUT); // Relay Switch 3
pinMode(switchPin4, OUTPUT); // Relay Switch 4
//####################################################################################
Serial.begin(115200);
delay(100);
WiFi.mode(WIFI_STA); // Set Esp8266 in Station mode
WiFi.begin(ssid, password);
secs = millis() / 1000 ;
while (WiFi.waitForConnectResult() != WL_CONNECTED)
{
Serial.println("Waiting for WiFi Connection ...");
delay(2000);
/*if (millis() / 1000 - secs > 60)
{
Serial.println("Going to Sleep for 10 secs ...");
ESP.deepSleep(10 * 1000000);
} */
}
//############################################################################################
//############################################################################################
//############# CHANGE OTA UPDATE PASSWORD ###################################################
//############################################################################################
//############################################################################################
ArduinoOTA.setPassword((const char *)"123"); // Password for OTA Update "123"
ArduinoOTA.begin();
}
//##########################################################################################
//############ SETUP END ###################################################################
//##########################################################################################
bool ota_update = false ; // Don't change this manually
//#########################################################################################
//######### LOOP BEGINS ##################################################################
//#########################################################################################
void loop()
{
if (!client.connected() && WiFi.status() == 3)
{
reconnect(); // Reconnect if connection is lost
}
client.loop(); // Maintain MQTT connection
if (ota_update)
{ secs = millis() / 1000 ; }
delay(10); // MUST delay to allow ESP8266 WIFI functions to run
OTA();
}
//#########################################################################################
//################# WAIT FOR OTA UPDATE ON EVERY START UP #################
//#########################################################################################
void OTA()
{
if (ota_update)
{
Serial.println("Waiting for Ota update");
while (millis() / 1000 - secs < 20 )
{
ArduinoOTA.handle() ;
delay(10);
}
}
}
//#################################################3
//#########################################################
void callback(char* topic, byte* payload, unsigned int length) {
String topicStr = topic;
Serial.println("Callback update.");
Serial.print("Topic: ");
Serial.println(topicStr);
//################## Switch 1 ################### COPY THESE BLOCKS FOR MORE SWITCHES ####################
if (topicStr == "/house/switch1/")
{
if (payload[0] == '1')
{
digitalWrite(switchPin1, HIGH);
client.publish("/house/switchConfirm1/", "1");
delay(1000);
}
else if (payload[0] == '0')
{
digitalWrite(switchPin1, LOW);
client.publish("/house/switchConfirm1/", "0");
}
}
//################## Switch 2 #####################################################
else if (topicStr == "/house/switch2/")
{
if (payload[0] == '1')
{
digitalWrite(switchPin2, HIGH);
client.publish("/house/switchConfirm2/", "1");
delay(1000);
}
else if (payload[0] == '0')
{
digitalWrite(switchPin2, LOW);
client.publish("/house/switchConfirm2/", "0");
}
}
//################## Switch 3 #####################################################
else if (topicStr == "/house/switch3/")
{
if (payload[0] == '1')
{
digitalWrite(switchPin3, HIGH);
client.publish("/house/switchConfirm3/", "1");
delay(1000);
}
else if (payload[0] == '0')
{
digitalWrite(switchPin3, LOW);
client.publish("/house/switchConfirm3/", "0");
}
}
//################## Switch 4 #####################################################
else if (topicStr == "/house/switch4/")
{
if (payload[0] == '1')
{
digitalWrite(switchPin4, HIGH);
client.publish("/house/switchConfirm4/", "1");
delay(1000);
}
else if (payload[0] == '0')
{
digitalWrite(switchPin4, LOW);
client.publish("/house/switchConfirm4/", "0");
}
}
//################## Switch 5 Sleep Mode #######################################
else if (topicStr == "/house/switchSleep/")
{
if (payload[0] == '1')
{
client.publish("/house/switchSleepConfirm/", "1");
Serial.println("going to sleep for 60 secs");
delay(1000);
ESP.deepSleep(60 * 1000000);
}
else if (payload[0] == '0')
{
client.publish("/house/switchSleepConfirm/", "0");
}
}
//################## Switch 6 ota #######################################
else if (topicStr == "/house/switchOta/")
{
if (payload[0] == '1')
{
client.publish("/house/switchOtaConfirm/", "1");
ota_update = true ;
}
else if (payload[0] == '0')
{
client.publish("/house/switchOtaConfirm/", "0");
ota_update = false ;
}
}
}
//#########################################################################################
//######### LOOP ENDS ##################################################################
//#########################################################################################
void reconnect() {
if (WiFi.status() != WL_CONNECTED)
{
Serial.print("Connecting to ..");
Serial.println(ssid);
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.println("WiFi connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
}
if (WiFi.status() == WL_CONNECTED) {
while (!client.connected()) {
Serial.print("Attempting MQTT connection...");
// Generate client name based on MAC address and last 8 bits of microsecond counter
String clientName;
clientName += "esp8266-";
uint8_t mac[6];
WiFi.macAddress(mac);
clientName += macToStr(mac);
//#########################################################################################
//######### CHANGE MQTT USER NAME AND PASSWORD BELOW ####################################
//#########################################################################################
if (client.connect((char*) clientName.c_str(), "newuser", "12345")) {
Serial.print("\tMQTT Connected");
client.subscribe(switchSleep);
client.subscribe(switchOta); // the order in which topics are arranged here
// is how ESP will Subscribe and if you put
// relays first and also turn them on and ota or sleep too
// the switches will turn on momentarily untill it gets the message to sleep or ota update
client.subscribe(switchTopic1);
client.subscribe(switchTopic2);
client.subscribe(switchTopic3);
client.subscribe(switchTopic4);
// ##**************************######## Add MORE TOPICS FOR SWITCHES HERE #####################
}
else
{
Serial.println("\tFailed.");
delay(2000);
break;
}
}
}
}
String macToStr(const uint8_t* mac)
{
String result;
for (int i = 0; i < 6; ++i) {
result += String(mac[i], 16);
if (i < 5) {
result += ':';
}
}
return result;
}
Do-it-yourself,DIY,How-To,Electronics,Arduino,Esp,Raspberry-Pi,Programming,Hacking Stuff,Gardening,Photography,Hobby,Fun
Showing posts with label Rpi. Show all posts
Showing posts with label Rpi. Show all posts
ESP 8266 MQTT Switch for Home Assistant V2.0
D.I.Y. Make RCA/AV Cable for Raspberry pi (using earphones/headphones audio lead)
Make RCA cable for Raspberry pi
I used an old cell phone's hands-free earphones kit and a female RCA connectors cable to make an AV (audio/video) Cable for the Raspberry Pi.
First, I tried to use a "ready-made" AV cable that I had for a long time and it came with another cell phone (Nokia N95 8GB music edition) but it didn't work at first because the Raspberry pi and that cellphone have different pin-out in the Audio Jack.
So, when I plugged it in the Raspberry Pi 3 and in the TV ,I just got weird noises and no Video . After thinking about it for sometime,I tested it with a multimeter by putting it in the "continuity mode" and in that mode if you touch both of the leads of the multimeter together it shows the number "0" and if there is no connection between the two leads it shows the alphabet "l" which means out-of-limit or range. It actually measures the resistance between it's two leads.So, if there is no connection between the two points i.e. the audio jack's metal pin and the corresponding wires the resistance will be infinite or too high for the meter to measure.
So, if you connect one lead of the multimeter (black or red doesn't matter in this case) to one of the four metal contacts of the Audio lead and the other lead of the multimeter to the RCA connector's outer metal part (Ground) or the center part (which is for the Signal) you can see which part of the Audio lead is connected to which part of the RCA connector.
In my case what was happening was that the Raspberry Pi's Video signal was going to the Ground of the Rca Cable and because ground is common for all the signals (Audio Left,Audio Right ,Video) all the three RCA connectors (red,white,yellow) had Video signal in their outer metal part of the connector and the "Actual Ground" was only on the Signal part in the center of the Yellow connector because of the different pin-out.
However, I made it to work also, just to test the Video output of the Raspberry pi by using a single Male RCA connector with it's wire stripped and by wrapping it on both the outer part of the yellow connector (of that cell phones AV cable) to get the Video Signal and the center part to get the Ground and then it worked but it was too messy.
So,then i felt the need to make a proper AV cable for the pi because I also fixed a cheap LCD display and that display was part of a car's Headrest and meant to be used with the DVD player it came along with(the chinese car DVD player with built-in lcd came with two head rests with LCDs in them ) and it has two analog video inputs so it can work with the Rpi too.
How i made the AV cable can be seen in the picture below.
![]() |
Raspberry Pi Audio Jack/RCA Connections Pin-out |
![]() |
Cell phone hands-free earphones with button and a Mic and four connector lead |
![]() |
Cellphone Earphones with Button and Mic for Calls |
![]() |
Female RCA connectors Cable |
![]() |
Female RCA Connectors Separated from the "other" lead |
![]() |
Separate all the wires and test with a multimeter (on continuity mode) because sometimes the wires are insulated and they have to be "burned" to remove the insulation |
![]() |
Test all the wires before proceeding any further just to be sure because it will become difficult to troubleshoot later if there is any problem with the connections |
![]() |
Join the related wires together |
![]() |
Solder them together and Test them Again |
![]() |
Tape the wires separately and then together to make it all neat |
Then I used Male-to-Male RCA cable to connect this to the TV.
You can also use Male to Male RCA cable then cut it,strip it and join with the audio lead the same way and then it will directly connect to the TV giving the same results .
I did it this way because I also had the useless female connectors .
![]() |
Car Head-rest Rear LCD for DVD player Front View |
![]() |
Back Cover Removed View of LCD and it's control board |
![]() |
Main IC on the control board of LCD |
![]() |
Back View of LCD control Board and Connectors |
Subscribe to:
Posts (Atom)