I need some simple guidance in setting up a simple pushbutton connected to an esp 8266-01. I want this device to control a sonoff basic switch that I reflashed in accordance with the instructions in your home automation course. I have modified two of these switches and I have them working using node red/mqtt on my server using the RPi. I have also been successful in controlling both switches via my phone independent of my wifi. However, I just can’t seem to figure out how to set up the node red flow to use a remote switch.. I have studied your tutorial on using the ESP code but I want to avoid using the IFTTT completely because I want to remain independent of the internet.
Can you offer me any advice. Ii have just put this project aside until I can get some fresh ideas.
Thanks
Just to double-check where you are at the moment:
- You have a SONOFF already being controlled with MQTT on Node-RED
- You can control that SONOFF with your Node-RED Dashboard buttons
You want to have another ESP in your home with a pushbutton that controls the SONOFF on/off, right?
Rui,
Absolutely correct. I think if I get a start, that I can eventually adapt a remote with perhaps two or three pushbuttons. I want to make this device to run with a lipo battery. Power conservation is not really important as I can easily keep it charged using an extra port on my phone multi charger. I know that I can and have, done this with my phone, but I am just not one who carries a phone tied to me at all times. I currently have twelve sonoff basic switches in the house and many of them are automatically turned on and off by the ewelink app. But my goal here is to cut the dependence on the internet completely.
I have currently been successful in flashing two of the sonoffs with your code. I simply would like to be able to control with a small handheld remote.
Thank you
Basically, here’s what you need to do:
- Grab one of the ESP8266 MQTT examples that publishes sensor reading
- Setup an ESP with just a pushbutton
- Publish an MQTT on or off message in the SONOFF MQTT topic according to the button press
Unfortunately I don’t have any tutorials on that exact subject, but I think you can modify my examples to publish messages (like we publish the sensor readings).
Instead of sending a message through Node-RED, another ESP can publish a message in the SONOFF topic to control it.
I hope this helps!
https://pastebin.com/QHR0W4eU
Rui,
I really need some help with this sketch. I have the remote device from your tutorials regarding the DASH button set up but I am lost in configuring the MQTT code. I realize that I need to to set a variable that defines the switch state and then checks the variable to determine the message sent. I see what your sketch is doing but I don’t want to use IFTTT. How can I adapt that into the MQTT message? Are there MQTT examples somewhere that explain that? I am trying to adapt the “Examples” code for the BUTTON as found on the IDE.
On my dashboard, I have two methods applied to operate the sonoff switches using either the switch or the button. I want use this remote at various places in the home to control devices that are otherwise run by BigTimer. Sometimes these devices need to be operated during periods when the timer is not in operation.
Any assistance greatly appreciated.
Hello, here’s how you can send any MQTT message:
String mqttMessage = "This is my message";
client.publish("your_mqtt_topic", mqttMessage.c_str());
Using that method, you can easily publish a message with any content that you need. Is that what you’re looking for?
Rui,
I have had some success but primarily trial and error. Here are my dashboard and sketch
https://pastebin.com/svSHf9y5
https://pastebin.com/jTHiX63H
The results are not exactly what I expected but then again I don’t really know what I am doing. When I run the sketch the serial monitor prints out the condition of the switch every half-second. Either “on” or “off” .
On the dashboard I added two MQTT output nodes with debug nodes just to see what was happening. The upper part of the flow works as I had hoped. The first sonoff is controlled with the dashboard switch 1 and works fine. Sonoff 2 works fine as well with the switch and with the two buttons added in such a manner that the Sonoff2 status icon reflects the action of both the switch and the buttons. The debug display works just as I would expect showing the on /off and sonoff2 is working properly.
The way it runs right now is that the debug will display the “on” msg for every .5sec that I hold the button down on the ESP8266-12E. The msg stops when I let the button up. I don’t know why the “off” msg doesn’t display something.
As for the two extra MQTT output nodes, they are just there as that is where I got lost and decided to get some help. It’s obvious I don’t know enough to continue.
Any advice greatly and sincerely appreciated
Kent
Unfortunately I receive too many emails per day and I don’t have time to write custom code, I can only try to help if you encounter an issue with one of my projects. Your code is currently sending “off” message every 500 ms.
You need to only send a message when you press the pushbutton, I recommend taking a look at the Arduino example “File” > “Examples” > “Digital” > “StateChangeDetection”, you need to add an extra variable (boolean) to your code and condition to only send an MQTT message when you press the pushbutton.
Take a look at that default example that I’ve mentioned and here:
// compare the buttonState to its previous state if (buttonState != lastButtonState) { // if the state has changed, increment the counter if (buttonState == HIGH) { // if the current state is HIGH then the button went from off to on: buttonPushCounter++; Serial.println("on"); Serial.print("number of button pushes: "); Serial.println(buttonPushCounter); } else { // if the current state is LOW then the button went from on to off: Serial.println("off"); } // Delay a little bit to avoid bouncing delay(50); }
In the if (buttonState == HIGH) { and else { you can send the MQTT publish messages.
Can you modify your example with that suggestion? Make sure you open the example “File” > “Examples” > “Digital” > “StateChangeDetection”.
Rui,
I fully understand the situation and I have managed to get a sketch that works after much trial and error. This sketch toggles the LED on and off and at the same time sends the appropriate message to the MQTT server and then on to the sonoff device that was flashed with your procedure. There is one drawback however. If I wait more than thirty to forty seconds after the device prints the connected messages, It quits working. If I reset the device, it does the same thing. If I continue to push the button , it could go on forever as long as I don’t let it idle for any time.The sketch does the same with either the NodeMCU-12E or a Wemos D1 miniPro.
If you have time, I would appreciate your insight as to what this might be.
Thank you
https://pastebin.com/dl/qYGX43mE
You should add the delay of 50ms (or higher) after the else statement to help you avoid false button presses:
(....)
else
{
digitalWrite(LED, LOW); // set the LED off
client.publish("home/office/sonoff1", "off");
}
// Delay a little bit to avoid bouncing
delay(50);
}
}
Your code seems to be correct to me, so to be honest I don’t know why that’s happening…
Rui,
I finally got this thing working with the help of several other forums. I have included the final code and a couple of images
https://pastebin.com/Jcuer08A
https://imgur.com/NpXJ5jg
https://imgur.com/0kMoTbA