Introduction: HomeAssistant - Custom WiFi Enabled GPIO

Do you use HomeAssistatn?

Would you like to create your own WiFi enabled devices to work with it?

If you answered yes to both of these questions then you want to check out this instructable. In this you will find out how to use an ESP8622 WiFi enabled device to join a local WiFi network and be able to exchange control of the on board GPIO of the ESP8622 with HomeAssitant running on a Raspberry Pi 3.

Step 1: First Gather Components and Install Software

First thing is first you will need to set up your ES8622 WiFi module.

Things you will need:

ESP8622 WiFi Module

https://www.amazon.com/gp/product/B01EA3UJJ4/ref=a...

USB to Serial FTDI connector

https://www.amazon.com/gp/product/B00QT7LQ88/ref=a...

Good news is that this is Arduino compatible. You will need to download the Arduino development environment from this link here: https://www.arduino.cc/en/Main/Software and install the arduino software.

Step 2: Now Setup Arduino Software for ESP8622

Now you will need to launch your arduino software after you have installed it.

Then go to Tools -> Board -> Boards Manager...

You will need to download the board definition for the ESP8622 by typing in ESP in the search field fo the boards manager and then pressing enter.

Select the one labeled ESP8266 ( I know they got the numbers transposed lol). Once selected choose in the lower right "Install" and this will install the board definition.

After that finishes installing you can go back to Tools -> Boards and then select the Generic ESP8266 board.

Now you will need to install one final thing and that is the library so you can program it. To do this you will need to go to Sketch -> Include Library -> Manage Libraries.

Once there you will perform yet another search for the library by typing in ESP and then selecting the one associated with ESP8266WiFi.h

Once that is installed you are now ready to get programming.

Step 3: The Code for Getting on WiFi Network First

Now you will need to build the code .

The code will be following. The only thing you need to change, for getting on the network to start with, will be the SSID and password variables. Don't worry about the MQTT variable in this step it will be covered later.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Update these with values suitable for your network. const char* ssid = ""; const char* password = ""; const char* mqtt_server = "";

WiFiClient espClient; PubSubClient client(espClient); int SwitchedPin = 0; String switch1; String strTopic; String strPayload;

void setup_wifi() { Serial.begin(115200); delay(100); // We start by connecting to a WiFi network Serial.println(); Serial.println(); Serial.print("Connecting to "); Serial.println(ssid); WiFi.begin(ssid, password); while (WiFi.status() != WL_CONNECTED) { delay(500); Serial.print("."); } Serial.println(""); Serial.println("WiFi connected"); Serial.println("IP address: "); Serial.println(WiFi.localIP()); }

void callback(char* topic, byte* payload, unsigned int length) { payload[length] = '\0'; strTopic = String((char*)topic); if(strTopic == "ha/switch1") { switch1 = String((char*)payload); if(switch1 == "ON") { Serial.println("ON"); digitalWrite(SwitchedPin, HIGH); } else { Serial.println("OFF"); digitalWrite(SwitchedPin, LOW); } } } void reconnect() { // Loop until we're reconnected while (!client.connected()) { Serial.print("Attempting MQTT connection..."); // Attempt to connect if (client.connect("arduinoClient")) { Serial.println("connected"); // Once connected, publish an announcement... client.subscribe("ha/#"); } else { Serial.print("failed, rc="); Serial.print(client.state()); Serial.println(" try again in 5 seconds"); // Wait 5 seconds before retrying delay(5000); } } } void setup() { setup_wifi(); client.setServer(mqtt_server, 1883); client.setCallback(callback);

pinMode(SwitchedPin, OUTPUT); digitalWrite(SwitchedPin, LOW); } void loop() { if (!client.connected()) { reconnect(); } client.loop(); }

Step 4: Wiring Up the ESP8266

Now you will need to wire up the device.

I have provided the diagram above for you to see how to connect it.

Once you have connected the RS232 to USB converter you will need to ground down the GPIO #0 pin so that when you reset the device you can ground the RST pin on the device to hold it into reset then remove the ground and it will boot into bootloader mode.

Once here you will need to choose the board for programming.

To do this you will go to Tools -> Board: -> Generic ESP8266 Module

Once this is chosen you will need to look and see what com port your USB to Serial converter is on and choose it via: Tools -> Port

If you have trouble finding what COM port the USB to FTDI device is on just look in your device manager on your windows machine or perform an ls usb on your linux device.

Once you have that all set up you will just need to press upload in your arduino software.

Step 5: Final Thoughts and Video Walkthough

I hope you enjoyed getting your ESP8266 device up and running with this tutorial. I have included some videos from my youtube channel to help visually show you how to get this all set up and running.

I also included how to set up the MQTT in order to send IoT command between the ESP8266 and home assistant software.

Enjoy and thanks for watching.