ESP8266 board with 2 relays - code for google assistant

Moderators: grovkillen, Stuntteam, TD-er

Post Reply
Message
Author
neznamnista
Normal user
Posts: 5
Joined: 24 Feb 2020, 11:57

ESP8266 board with 2 relays - code for google assistant

#1 Post by neznamnista » 24 Feb 2020, 14:40

Hi,
i have esp8266 board with 2 relays and microprocessor, everything is working fine when i am using phone app (turn on/off relays) in STA and AP mode, but how to turn on/off relays over IFTTT or Google Assistant? I found this code (below) on internet but the only thing I made it is to switch on/off led on ESP-01, but relays remained silent..Code is written for adafruit.io...

link for esp8266 board with 2relays is:
https://www.aliexpress.com/item/32843970608.html

on board there are 2 jumpers in positions RX-RX1 and TX-TX1

code is here(its code for 4 relays but never mind, i was trying to turn relays on/off

for transfer from Arduino IDE to ESP-01 am using USB programmer (attached pic)

/***************************************************
Adafruit MQTT Library ESP8266 Example
****************************************************/
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"

#define rel1 0 // GPIO 0
#define rel2 1 // Tx Pin
#define rel3 2 // GPIO 2
#define rel4 3 // Rx Pin
/************************* WiFi Access Point *********************************/

#define WLAN_SSID "YOUR-SSID"
#define WLAN_PASS "YOUR-SSID-PASSWORD"

/************************* Adafruit.io Setup *********************************/

#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "USER-NAME" //Your Username
#define AIO_KEY "API-KEY" // Your API Key
/************ Global State (you don't need to change this!) ******************/

// Create an ESP8266 WiFiClient class to connect to the MQTT server.
WiFiClient client;
// or... use WiFiFlientSecure for SSL
//WiFiClientSecure client;

// Setup the MQTT client class by passing in the WiFi client and MQTT server and login details.
Adafruit_MQTT_Client mqtt(&client, AIO_SERVER, AIO_SERVERPORT, AIO_USERNAME, AIO_KEY);

/****************************** Feeds ***************************************/

// Setup a feed called 'photocell' for publishing.
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
//Adafruit_MQTT_Publish photocell = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/photocell");

// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoff1 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay1");
Adafruit_MQTT_Subscribe onoff2 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay2");
Adafruit_MQTT_Subscribe onoff3 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay3");
Adafruit_MQTT_Subscribe onoff4 = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/relay4");
/*************************** Sketch Code ************************************/

// Bug workaround for Arduino 1.6.6, it seems to need a function declaration
// for some reason (only affects ESP8266, likely an arduino-builder bug).
void MQTT_connect();

void setup() {
//Serial.begin(115200);
delay(10);
pinMode(rel1, OUTPUT);
pinMode(rel2, OUTPUT);
pinMode(rel3, OUTPUT);
pinMode(rel4, OUTPUT);
digitalWrite(rel1,HIGH);
digitalWrite(rel2,HIGH);
digitalWrite(rel3,HIGH);
digitalWrite(rel4,HIGH);
//Serial.println(F("Adafruit MQTT demo"));
// Connect to WiFi access point.
// Serial.println(); Serial.println();
// Serial.print("Connecting to ");
// Serial.println(WLAN_SSID);

WiFi.begin(WLAN_SSID, WLAN_PASS);
while (WiFi.status() != WL_CONNECTED) {
delay(500);
// Serial.print(".");
}
// Serial.println();
// Serial.println("WiFi connected");
// Serial.println("IP address: "); Serial.println(WiFi.localIP());

// Setup MQTT subscription for onoff feed.
mqtt.subscribe(&onoff1);
mqtt.subscribe(&onoff2);
mqtt.subscribe(&onoff3);
mqtt.subscribe(&onoff4);
}

uint32_t x=0;

void loop() {
// Ensure the connection to the MQTT server is alive (this will make the first
// connection and automatically reconnect when disconnected). See the MQTT_connect
// function definition further below.
MQTT_connect();

// this is our 'wait for incoming subscription packets' busy subloop
// try to spend your time here

Adafruit_MQTT_Subscribe *subscription;
while ((subscription = mqtt.readSubscription(5000))) {
// to test the tx line data
if (subscription == &onoff1){
// Serial.print(F("Got: "));
// Serial.println((char *)onoff1.lastread);
uint16_t state1 = atoi((char *)onoff1.lastread);
// Serial.println(state1);
digitalWrite(rel1,state1);
}
if (subscription == &onoff2){
// Serial.print(F("Got: "));
// Serial.println((char *)onoff2.lastread);
uint16_t state2 = atoi((char *)onoff2.lastread);
//Serial.println(state2);
digitalWrite(rel2,state2);
}
if (subscription == &onoff3){
//Serial.print(F("Got: "));
//Serial.println((char *)onoff3.lastread);
uint16_t state3 = atoi((char *)onoff3.lastread);
// Serial.println(state3);
digitalWrite(rel3,state3);
}
if (subscription == &onoff4){
// Serial.print(F("Got: "));
// Serial.println((char *)onoff4.lastread);
uint16_t state4 = atoi((char *)onoff4.lastread);
// Serial.println(state4);
digitalWrite(rel4,state4);
}
}

// ping the server to keep the mqtt connection alive
// NOT required if you are publishing once every KEEPALIVE seconds
/*
if(! mqtt.ping()) {
mqtt.disconnect();
}
*/
}

// Function to connect and reconnect as necessary to the MQTT server.
// Should be called in the loop function and it will take care if connecting.
void MQTT_connect() {
int8_t ret;
// Stop if already connected.
if (mqtt.connected()) {
return;
}
// Serial.print("Connecting to MQTT... ");
uint8_t retries = 3;
while ((ret = mqtt.connect()) != 0) { // connect will return 0 for connected
// Serial.println(mqtt.connectErrorString(ret));
// Serial.println("Retrying MQTT connection in 5 seconds...");
mqtt.disconnect();
delay(5000); // wait 5 seconds
retries--;
if (retries == 0) {
// basically die and wait for WDT to reset me
while (1);
}
}
// Serial.println("MQTT Connected!");
}
Attachments
ch340usbprogrammer.jpg
ch340usbprogrammer.jpg (29.54 KiB) Viewed 4227 times
relay21chesp01.jpg
relay21chesp01.jpg (122.18 KiB) Viewed 4227 times

TD-er
Core team member
Posts: 2771
Joined: 01 Sep 2017, 22:13
Location: the Netherlands
Contact:

Re: ESP8266 board with 2 relays - code for google assistant

#2 Post by TD-er » 24 Feb 2020, 19:53

Just curious, How do you integrate this with ESPEasy?

neznamnista
Normal user
Posts: 5
Joined: 24 Feb 2020, 11:57

Re: ESP8266 board with 2 relays - code for google assistant

#3 Post by neznamnista » 24 Feb 2020, 20:13

it is general question, maybe some one will know how to do it

User avatar
ThomasB
Normal user
Posts: 696
Joined: 17 Jun 2018, 20:41
Location: USA

Re: ESP8266 board with 2 relays - code for google assistant

#4 Post by ThomasB » 24 Feb 2020, 21:11

It appears that your relay board expects serial commands from the ESP8266. There are many variations of the module so the exact details can vary. For example, see the last post by ntissm in this old discussion:
viewtopic.php?t=2491#p12159

Although it's just a random project I found with google, here is a link to a similar project: https://blog.lindsaystrategic.com/2018/ ... lay-board/

Keep in mind that this forum is for ESPEasy software (not random ESP8266 projects). In case you have any interest in experimenting with ESPEasy, there is a custom playground plugin that appears to support your module:
https://github.com/letscontrolit/ESPEas ... Switch.ino

- Thomas

Shardan
Normal user
Posts: 1156
Joined: 03 Sep 2016, 23:27
Location: Bielefeld / Germany

Re: ESP8266 board with 2 relays - code for google assistant

#5 Post by Shardan » 25 Feb 2020, 07:33

ThomasB wrote:
24 Feb 2020, 21:11
It appears that your relay board expects serial commands from the ESP8266. There are many variations of the module so the exact details can vary. For example, see the last post by ntissm in this old discussion:
viewtopic.php?t=2491#p12159

Although it's just a random project I found with google, here is a link to a similar project: https://blog.lindsaystrategic.com/2018/ ... lay-board/

Keep in mind that this forum is for ESPEasy software (not random ESP8266 projects). In case you have any interest in experimenting with ESPEasy, there is a custom playground plugin that appears to support your module:
https://github.com/letscontrolit/ESPEas ... Switch.ino

- Thomas
The board contains a STM8-MPU, most likely with a serial connection to the ESP8266
Regards
Shardan

neznamnista
Normal user
Posts: 5
Joined: 24 Feb 2020, 11:57

Re: ESP8266 board with 2 relays - code for google assistant

#6 Post by neznamnista » 25 Feb 2020, 11:19

Thank you Thomas for your overview
can you explain me what can I use this espeasy software for? is it free? what do i need for this
can i manage with my board with 2ch relays with this software through wifi?

Shardan
Normal user
Posts: 1156
Joined: 03 Sep 2016, 23:27
Location: Bielefeld / Germany

Re: ESP8266 board with 2 relays - code for google assistant

#7 Post by Shardan » 25 Feb 2020, 12:52

neznamnista wrote:
25 Feb 2020, 11:19
Thank you Thomas for your overview
can you explain me what can I use this espeasy software for? is it free? what do i need for this
can i manage with my board with 2ch relays with this software through wifi?
There's a long thread about relay boards with a MCU chip on it, take a look at
viewtopic.php?f=6&t=3245
Anyways this isn't the most easy end of relay driving as there are a lot of
those boards and it feels like every board uses other commands and procedures.

At the page you mentioned at AliExpress some serial commands are described,
it might help to note them for further reference.

It's not very difficult to use a nodeMCU (Board with ESP12, USB connect etc) to drive some relays
directly, that's much easier to handle.

Another point:
Your relay board uses an ESP-01, in best case it has 1MByte flash size.
This is a growing problem as ESPeasy grows... there'S a 1MB binary on Github still,
but of course it's limited equaled to 4MB on the ESP-12 and similar.

ESPeasy is free.
In a nutshell it is a multisensor/actor firmware for ESP8266.
A wide range of sensors and actors can be connected and used together with
FHEM, OpenHAB, Domotix etc.

You need an ESP Chip (ESP-12x for example) or a board with this chip on it (prefered nodeMCU).
If you use "blank" chips like the ESP-12 you need an USB-to TTL adapter for flashing firmware.
Watch out: The adapter must run with 3.3V. a 5V adapter will fry your ESP!
Do NOT use cheap "FTDI" adapters - very often the chip is faked and runs into
problems with windows drivers.
Adapters with CH340G or CP210x are the better choice and work fine.
If using a nodeMCU board, it has this adapter on board, you may connect directly to USB.

It might be possible to drive the board with ESPeasy, but some experimenting
might be necessary, see the thread mentioned above.

Many usual relay boards, even just the standard boards for a few bucks on Ali
tend to give some problems, not depending on ESPeasy or other firmware but just on
electronic basics. Honestly it is a never ending story.....
A lot of information, possible failures and workarounds can be found in the wiki:
https://letscontrolit.com/wiki/index.ph ... cs:_Relays

I prefer to design and build my relay bords myself nowadays. I can define everything myself,
it's possible to care for security if using mains (some chinese boards are really unsafe).
The picture shows a 4ch relay designed two years ago with ESP8266 and power supply
used for ceiling lights control and such (Attention: Mains voltage - know what you're doing!)
.
4ch_Relay_640.jpg
4ch_Relay_640.jpg (77.19 KiB) Viewed 4133 times
Regards
Shardan

neznamnista
Normal user
Posts: 5
Joined: 24 Feb 2020, 11:57

Re: ESP8266 board with 2 relays - code for google assistant

#8 Post by neznamnista » 25 Feb 2020, 18:08

WOW Shardan
thanks for your email
well, i am at the beginning of IOT and i chose ESP8266 and ESP-01 with MQTT and ADAFRUIT dashboard
thats was my wish to create one dashboard from which i can manage with home devices

but my IOT knowledge is very low and i am learning
until now i managed few relays to work with google assistant and i am so proud ;)

what do i need for this 4 relays board on your picture?
hardware, software?

Shardan
Normal user
Posts: 1156
Joined: 03 Sep 2016, 23:27
Location: Bielefeld / Germany

Re: ESP8266 board with 2 relays - code for google assistant

#9 Post by Shardan » 25 Feb 2020, 20:29

Hello nez,

Hardware isn't too much - relays, ESP-12 E/F/S, power supply and a lot of electronic "chicken seed", says small parts.
Of course you'll need the PCB too.
The PCB in the pic is the first version, it has a lot of SMD parts at the backside.
The newer design has all parts on frontside so it can be soldered in a reflow oven or with an infrared soldering platform.

The relay box is built with SMD parts, you'll need soldering paste, tweezers and a soldering iron with fine tip.
Honestly I recommend at least a hot air soldering gun, a lot of patience and calm hands.
Soldering SMD parts with an iron is possible... but definitely it makes one puling out his air.
Be aware that SMD soldering needs experience.....

Maybe I should mention this board is made for ceiling lights etc, the relays are connected to mains voltage directly.

The board uses mains voltage - please know what you're doing!
We prefer our forum members staying alive :)

This is the schematic, not really a miracle ;)
.
Schematic_4ChController.jpg
Schematic_4ChController.jpg (361.62 KiB) Viewed 4102 times
.
In addition you will need a programmer, as said in my last post here.
Prefer a CH340G or CP210x type. The FTDI programmers often use faked chips and run into trouble.
Watch out you buy a programmer supporting 3.3V. Some are switchable 5V/3.3V.
Programming with 5V will damage the ESP!
This one supports 5V and 3.3V (set jumper accordingly) and works well:
https://www.aliexpress.com/item/32583391511.html
You might need some plug cables ("DuPont cables") to connect the programmer to the ESP.

You'll need the ESPeasy firmware for the board. Others might run or not, I've never tested.
ESPeasy supports MQTT so most usual home auto software like FHEM, openHAB, domotix etc should work
in a MQTT environment. FHEM has an own plugin for ESPeasy so it works nearly out of the box,
MQTT needs some additional software and preparing.
I don't know ADAFRUIT dashboard so i can't say anything about it.

Anyways, the relays don't even need much configuration, they can be set on/off just with a http command.

For beginning it might be easier to use a nodeMCU and build just the relay part.
Swap the 2N7002 to 2N7000 and use normal resistors etc, so you could build it on a testboard.
Regards
Shardan

neznamnista
Normal user
Posts: 5
Joined: 24 Feb 2020, 11:57

Re: ESP8266 board with 2 relays - code for google assistant

#10 Post by neznamnista » 25 Feb 2020, 23:34

thanks S
for me this looks like driving space shuttle
i dont have that hot air gun for smd soldering, only iron
do you have any better visible scheme? i cant see all letters and numbers

as more experienced and professional in lot of ways comparing to me, what dashboard do you suggest me? of course to be free and hardware to be not expensive...main point is to have all devices on one screen

until now i have only these elements
Attachments
relay&esp01.jpg
relay&esp01.jpg (134.06 KiB) Viewed 4087 times
ch340usbprogrammer.jpg
ch340usbprogrammer.jpg (29.54 KiB) Viewed 4087 times
NodeMcu v3 CH340.jpg
NodeMcu v3 CH340.jpg (38.46 KiB) Viewed 4087 times

Shardan
Normal user
Posts: 1156
Joined: 03 Sep 2016, 23:27
Location: Bielefeld / Germany

Re: ESP8266 board with 2 relays - code for google assistant

#11 Post by Shardan » 26 Feb 2020, 11:00

Hello again,

from the dashboard side:
Basically you will need a small computer running the dashboard, no matter which one you chose.
I was using FHEM long before i discovered ESPeasy. As I had an existing installation already I stayed
with FHEM.
Fhem has one nasty disatvantage: It needs really a lot of learning, at the beginning it can be
really frustrating.
On the other side: FHEM integrates ESPeasy very easy. You may use MQTT with FHEM, but FHEM
contains an ESPeasy plugin and inside ESPEasy FHEM can be configured as a controller.
For a beginner it is the most easy way to get this up and running as MQTT is a science of its own.
It needs a small linux box, can be an old PC for example. But FHEM can be run even on a
Raspberry Pi, I had this running for quite a while here.
Another advantage: FHEM has a huge library of availlable plugins, even for some crude
LED dimmers etc. so the number of already integrated hardware actors makes things
more easy.

There are a bunch of other controllers around, like OpenHAB, domotix etc. but honestly
I just know the names :).

So now lets get to the ESPeasy side.

As you already have a nodeMCU I'd suggest to start with that board as it avoids a lot of
possible difficulties with flashing etc.
From the picture I see it uses a CH340 chip to connect to USB. The CH340 needs a driver
within windows. If windows does not install the driver itself, I can supply a driver file.
Flashing the firmware is easy
Download the firmware from github: https://github.com/letscontrolit/ESPEasy/releases
In the zip file you'll find a lot of files. Many different firmware versions for use, testing, different ESP variations...
Use the ESP_Easy_mega-2020xxxxxx_normal_ESP8266_4M1M.bin for the nodeMCU.
A flash tool is included with the zip file, there is an own thread for the flashtool here in the forum.

After flashing it takes a minute until the chip's memory is initialized, then you're ready to go.
You should connect the nodeMCU to your WiFi now, a small tutorial is found at https://letscontrolit.com/wiki/index.ph ... =EasySetup

For using sensors etc, open the nodeMCU's webpage and select "devices".
You'll find a list of "tasks" that can be configured to lots of sensors etc.
Relays just use a GPIO pin as output, no configuration necessary, it's just driven by an http command.
A list of availlable commands: https://www.letscontrolit.com/wiki/index.php?title=GPIO

A nice and simple test: A LED connected to GPIO12 (D6) via a 330Ohm resistor should give
light on / light off with

Code: Select all

http://<espeasyip>/control?cmd=GPIO,12,1
http://<espeasyip>/control?cmd=GPIO,12,0
So now... relays.

I'd suggest to just build your own simple relay module.
.
Relay circuit.jpg
Relay circuit.jpg (156.82 KiB) Viewed 4062 times
.
That's all. A 5V-relay, a transistor, some parts... done. It can be built on a grid card or experimental board.
Don't forget to connect GND from relay to power supply and nodeMCU
Connect the input from the relay to one of GPIO 12, 13 or 14. (D6, D7, D5 on the nodeMCU board).
Now power the nodeMCU and the power supply for the relay and give it a try with the commands from above.

For testing a power supply for the relays and USB power for the nodeMCU works.

Oh btw: Why making the module yourself, there are so many cheap modules on the market...
That simple: Because this circuit is known working!
It is a never ending story with relayboards. Search forum for relay and you'll find a lot of
"My relay does not switch on" or "My relay switches on but never off" and such.
Of course every sold module is described "compatible with anything" in the market...
Well more or less compatible, more often less....
The simple circuit with 2N7000 FET works

Power supply:
Attention: Double trap!
The nodeMCU has a pin "5V". This is output only on most boards, it is mostly not possible to supply the nodeMCU with 5V via this pin.
Even worse the 5V output can't take much current.
Best way out: Use a 5V power supply and add a voltage regulator down to 3.3V Such regulators can be bought
(see https://www.aliexpress.com/item/32875174690.html ) as a small board or you can solder it yourself.
The schematic shows two alternatives, the circuit is all same, just different regulator types:
.
Regulator_Circuits.jpg
Regulator_Circuits.jpg (276.76 KiB) Viewed 4062 times
.
Both work fine.

So use a 5V supply, connect the relay to the 5V/GND, connect the regulator input and GND too.
The 3.3V/GND output can be connected to the 3V3 and GND pins of the nodeMCU.

Oh, a word about power supplies
Do yourself a favour: Dump all and every cheap power supplies.
There's an incredible amount of shit on the market, sorry to say.
I've seen a "5V 3A" supply - inside a regulator that can take a maximum of 0.7A!
And on top forget about electrical security and other points..
I'm using the power supplies made by MeanWell mostly. Yes, I know, expensive. But they run!
So many problems are caused by a poor power supply....

I'm sorry that the schematic isn't readable well..... sadly the forum still does not allow to attach PDF's.
If possible send me your mail address via PM here, then I'll mail schematics in PDF format.
Regards
Shardan

Post Reply

Who is online

Users browsing this forum: No registered users and 16 guests