HI All,
i made connection 4ch relay boards to nodemcu...also nodemcu is connected to AIO Server adafruit...Sometimes when i press ON on dashboard page my relay is not reacting because of bad internet connection or similar....so i need to press on several times to turn on relay...and everything is ok when i am near relay board and know which relay is turned on...but outside of home its hard to know if my internet was good at certain moment when i press on on dashboard on the server....does anyone of you know how to read the state of the relays and be sure that state of the relay is on or off?
thanks
Other post/duplicate DELETED
Please do NOT cross post / duplicate as it wastes peoples time and efforts to have more than one post for a single topic.
Continued cross posting could result in a time out from the forum.
Could you also take a few moments to [url=https://forum.arduino.cc/index.php?topic=710766.0]Learn How To Use The Forum[/url].
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.
sorry just wanted more people to be able to read and i described problem in different words...sorry it wont happend
Depends on what you are switching with your relay board ... If it is low voltage you may use an optocoupler to detect the state reverse from the output.
i dont understand
What is the operating voltage of the relay-controlled device?
Usually you connect devices with a relay to switch one or more devices.
On the sketch above there is one device connected to a separate power supply. The question is: What voltage do you switch in your application? Is it mains (230 VAC or 110VAC) or 12VDC or 24VDC or anything else.
Depending on the voltage there might be different technical solutions to sense the real state of the relay(s), which means that you sense the side of the relay where the screwed connectors are. So in the sketch above, where the device is wired to.
This gives you a clear information whether the relay has switched or not.
If you can live with the state at the side where the relay is controlled from (the side where you connect the NodeMCU to) there might be other possibilities, depending on the make of the relay board.
This is the schematic of an Elegoo 4 relay board:
If you are satisfied when the relay has got the 5V to switch (just to give some examples),
- you may use GPIOs connected to four LDR and mount each LDR on top of the LEDs (D3, D4, D5, D6) or
- use four optocouplers (which is the same technology named OPTOISO1 (U1 to U4) in the diagram and connect them to GND and the transistors where it says RELAY1 to RELAY4.
This way you can check whether the voltage is correct on the "low voltage" side but you cannot detect if the relay is broken.
However, if you just want a feedback from your NodeMCU that it has received your command, you do not need this! Just add a software feedback to your control unit that it has received and performed the command.
well i am using 5V DC for 4ch board as it is on the picture you posted....that power supply is connected to Nodemcu 12E and then to 4ch relay board....yes, i need a feedback from nodemcu that it has received the command and do not know how to do that....thanks for your feedback anyway
Ok, I think this way we have found out, that you do not require a feedback that the relay has really switched, but a response that your command has been received well ... That is a big difference
Would you mind to post the sketch that works in your NodeMCU ? That will help to investigate what can be done.
thanks @ec2021
here is the code that is working properly....short info, nodemcu is connected to 4ch relays board, nodemcu is used to send data to aio server adafruit on which is placed dashboard so i can manage relays online globally....problem is when i press button to turn on some device at home from distance, button on dashboard on adafruit turns green and shows that relay is on, but at home relay is not on because there were any problem....so how to be informed that relay is really turned on when i press button on dashboard to turn on the relay
Adafruit MQTT Library ESP8266 Example
Must use ESP8266 Arduino from:
https://github.com/esp8266/Arduino
Works great with Adafruit's Huzzah ESP board & Feather
----> https://www.adafruit.com/product/2471
----> https://www.adafruit.com/products/2821
Adafruit invests time and resources providing this open source code,
please support Adafruit and open-source hardware by purchasing
products from Adafruit!
Written by Tony DiCola for Adafruit Industries.
MIT license, all text above must be included in any redistribution
Code is edited by Sachin Soni for it's project called
Ultimate Home Automation
For Project video, visit his YouTube channel named "CreativeDesk"
****************************************************/
//for esp32 use <wifi.h> ,for esp8266 use <esp8266wifi.h>
// this code is for esp32 for using esp8266 you just need to change <wifi.h> into <esp8266wifi.h> and also change the relay pin
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
>
/************************* Pin Definition *********************************/
//Relays for switching appliances
#define l1 D0
#define l2 D1
#define l3 D2
#define l4 D3
#define buzzer 26
//buzzer to know the status of MQTT connections and can be used for any other purpose according to your project need.
/************************* WiFi Access Point *********************************/
#define WLAN_SSID "x123456"
#define WLAN_PASS "212121"
/************************* Adafruit.io Setup *********************************/
#define AIO_SERVER "io.adafruit.com"
#define AIO_SERVERPORT 1883 // use 8883 for SSL
#define AIO_USERNAME "username"
#define AIO_KEY "password"
/************ 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 ***************************************/
// Notice MQTT paths for AIO follow the form: <username>/feeds/<feedname>
//Adafruit_MQTT_Publish Light = Adafruit_MQTT_Publish(&mqtt, AIO_USERNAME "/feeds/light");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/spavaca");
/*************************** 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(l1,OUTPUT);
digitalWrite (l1, HIGH);
pinMode(l2,OUTPUT);
digitalWrite (l2, HIGH);
pinMode(l3,OUTPUT);
digitalWrite (l3, HIGH);
pinMode(l4,OUTPUT);
digitalWrite (l4, 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(&onoffbutton);
}
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))) {
if (subscription == &onoffbutton) {
Serial.print(F("Got: "));
Serial.println((char *)onoffbutton.lastread);
String RAW = (char *)onoffbutton.lastread;
int i=0;
while(RAW.charAt(i) != '_')
i++;
String pin = RAW.substring(0,i);
//Serial.println(pin);
int pin_number = pin.toInt();
Serial.println(!pin_number);
int j=i+1;
while(RAW.charAt(j) != '_')
j++;
// Serial.println(i);
//Serial.println(j);
String state = RAW.substring(i+1,j);
//Serial.println(state);
int state_number = state.toInt();
Serial.println(state_number);
if(pin_number == 1)
{
digitalWrite(l1,!state_number);
}
else if(pin_number == 2)
{
digitalWrite(l2,!state_number);
}
else if(pin_number == 3)
{
digitalWrite(l3,!state_number);
}
else if(pin_number == 4)
{
digitalWrite(l4,!state_number);
}
}
}
// 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;
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
digitalWrite(buzzer, HIGH);
delay(200);
digitalWrite(buzzer, LOW);
delay(200);
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!");
digitalWrite(buzzer, HIGH);
delay(2000);
digitalWrite(buzzer, LOW);
}
On a first glance I think this sketch is just retrieving and reacting on information from the MQTT Server but does not give any feedback. So you have only a "one-way" road from control device to the actuator. You never know if and when your command was read by the ESP.
What you need is a response from the ESP to the MQTT Server when a command was identified and action was taken which than has to be read by your controlling device ...
How about googling for a method to send info from ESP to the MQTT Server and to implement that as a feedback ...?
dear @ec2021
i tried to find out the solution for this, but nothing....
Would you mind to tell us how you control the ESP from outside? Is it your smartphone and a browser?
And: This is what shows an example how to get a feedback from a controller like node_mcu to the Adafruit mqtt server for display. You would not create a Gauge but an Indicator block:
Of course, for controlling ESP and giving orders ON/OFF to relays through ESP i am using mobile app IoTMQTT...
Пожалуйста, проверьте ссылку на Adafruit самостоятельно, там все есть, так что вы сможете сделать это самостоятельно..
Успехов!
i dont know how to make NodeMCU 12E to send me state of the relay...when i press 2 buttons on Adafruit and turn on 2 devices, commands move to nodemcu and Nodemcu turns 2 relays of 2 devices...but now when i turn off electricity and turn on back both relays are off and devices on dashboard on Adafruit are turned on...so how to make relays to read state of the buttons on Adafruit and out relays in that positions?
That is desribed in the link from post #13 ... However, I will not be able to solve this for you ...
i will try of course
I tried and follow guide @ec2021 sent me but nothing happened....and i didnt solve this problem
is it possible that nobody knows how to make nodemcu to send relay state to email or to read relay state according to mqtt adafruit server?