How to make relay to read the toogle button
Moderators: adafruit_support_bill, adafruit
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
How to make relay to read the toogle button
I have nodemcu connected to relay board 2ch..how to make nodemcu to read the status of toggle button so relay can follow that status....many times when i turn on button on dashboard on adafruit, relay follows, but when i lost wifi connection relay goes to off when toggle button is on....please help
- brubell
- Learn User Page
- Posts: 2010
- Joined: Fri Jul 17, 2015 10:33 pm
Re: How to make relay to read the toogle button
Do you have code? Could you please copy and paste your code here, within ?
Code: Select all
code blocks
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
Re: How to make relay to read the toogle button
Code: Select all
#include <ESP8266WiFi.h>
#include "Adafruit_MQTT.h"
#include "Adafruit_MQTT_Client.h"
>
/************************* Pin Definition *********************************/
#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/test");
// Setup a feed called 'onoff' for subscribing to changes.
Adafruit_MQTT_Subscribe onoffbutton = Adafruit_MQTT_Subscribe(&mqtt, AIO_USERNAME "/feeds/faza1");
/*************************** 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);
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
Re: How to make relay to read the toogle button
[img]
[/img]
[/img]
- Attachments
-
- fash.jpg (49.64 KiB) Viewed 226 times
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
Re: How to make relay to read the toogle button
@brubell can you help? i posted code and pic
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
Re: How to make relay to read the toogle button
how to get notification from nodemcu that relay is really turned on when i press toggle block on dashboard on adafruit?
- brubell
- Learn User Page
- Posts: 2010
- Joined: Fri Jul 17, 2015 10:33 pm
Re: How to make relay to read the toogle button
I'm having trouble figuring out what your primary issue is, is this the issue you're having?but when i lost wifi connection relay goes to off when toggle button is on....please help
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
Re: How to make relay to read the toogle button
1. Code is working great
2. Problems are:
- When i am outside and press toggle block ON i dont have any notification that relay is really turned on
- And also when i turn on nodemcu my relays are OFF by default (in that moment on dashboard toggle block is ON) how to make nodemcu to read status of the toggle block ON and to turn ON relay.
thanks
2. Problems are:
- When i am outside and press toggle block ON i dont have any notification that relay is really turned on
- And also when i turn on nodemcu my relays are OFF by default (in that moment on dashboard toggle block is ON) how to make nodemcu to read status of the toggle block ON and to turn ON relay.
thanks
- brubell
- Learn User Page
- Posts: 2010
- Joined: Fri Jul 17, 2015 10:33 pm
Re: How to make relay to read the toogle button
Aside from checking that the value was written to the feed, there's not much you can do to check if the device is actually online. You may want to consider making a "status" feed and publishing to it every 5 minutes, as a way to check if the device is online.- When i am outside and press toggle block ON i dont have any notification that relay is really turned on
- And also when i turn on nodemcu my relays are OFF by default (in that moment on dashboard toggle block is ON) how to make nodemcu to read status of the toggle block ON and to turn ON relay.
You may want to use the */get topic in this situation: https://io.adafruit.com/api/docs/mqtt.h ... -get-topic
The Adafruit IO Arduino library has this functionality built in (see an example here - https://github.com/adafruit/Adafruit_IO ... d_read.ino). You'll need to implement it yourself if you're using the Adafruit MQTT library (since this library is not tied to the Adafruit IO Service, it's just a MQTT client)
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
Re: How to make relay to read the toogle button
thanks for answer
but how to add this to my code...i dont know how and where
but how to add this to my code...i dont know how and where
- brubell
- Learn User Page
- Posts: 2010
- Joined: Fri Jul 17, 2015 10:33 pm
Re: How to make relay to read the toogle button
I suggest using the Adafruit IO Arduino library, instead, and modifying this example to fit your current code: https://github.com/adafruit/Adafruit_IO ... d_read.ino
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
Re: How to make relay to read the toogle button
I tried but still nothing
- brubell
- Learn User Page
- Posts: 2010
- Joined: Fri Jul 17, 2015 10:33 pm
Re: How to make relay to read the toogle button
Could you post this code?
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
Re: How to make relay to read the toogle button
I posted my code in #3
- axiomnote
- Posts: 27
- Joined: Sat Jan 11, 2020 4:10 pm
Re: How to make relay to read the toogle button
so nobody wants to help me with the code or nobody knows how to do it?
Forum rules
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.
If you're posting code, please make sure your code does not include your Adafruit IO Active Key or WiFi network credentials.