About NodeMCU
NodeMCU is a very popular Micro controller with wifi access. It is based on ESP8266 – a cheap but powerful chip and fully support Arduino IDE. If you familiar with Arduino IDE, you can program NodeMCU in no time.
Objective
In this project, we will connect a push button switch with NodeMCU. When push button is pressed, a “pressed” message will be sent to remote MQTT client. When push button is released, a “not pressed” message will be sent to remote client. Button status will be check every 2 seconds by NodeMCU.
Parts and Devices
NodeMCU board x 1 pc
Button x 1 pc
1K resistor x 1
Breadboard x 1 pc
jumper wires
Circuit Connection Graph
Programming Prerequisite:
Before running the project, we need set up the Arduino IDE as per following procedures:
Library Installation:
In Arduino IDE, we need install MQTT client library to communicate with MQTT broker, please download the library from following link:
Unzip above file, move the unzipped folder to Arduino IDE library folder (you can also import these two zip files to Arduino library from IDE).
Download sample code from following link
https://osoyoo.com/wp-content/uploads/2016/12/nodemcuiot_button_code.txt , copy the code into Arduino IDE. Before running the code, please do following changes to fit your wifi and MQTT setting:
1)Line 19 and 20:
const char* ssid = “your_hotspot_ssid”;
const char* password = “your_hotspot_password”;
You need change these 2 lines to match your wifi SSID and password
2)Line 21
const char* mqtt_server = “broker.emqx.io”;
You can use your own MQTT broker URL or IP address to set above mqtt_server value. You can also use some famous free MQTT server to test the project such as “broker.emqx.io”, “iot.eclipse.org” etc
If you want to install your own MQTT broker in Ubuntu Linux, please read this article https://osoyoo.com/2016/09/07/how-to-install-mosquitto-mqtt-server-on-linux
3)if your MQTT server require username and password authentication, you need change line 86
Running the code
After you running the code, please open the serial terminal window in upright corner of Arduino IDE.
If wifi hotspot name and password setting is ok and MQTT broker is connected, you will see following result:
After the NodeMCU connected to wifi and MQTT broker, it will collect push button status and publish to a MQTT broker with topic “OsoyooCommand”.
You can select any MQTT client tool to subscribe the topic ” OsoyooCommand” from the same MQTT broker of NodeMCU. simply use google to search MQTT client in internet, or search MQTT in Apple store or Google Play, you can find many free MQTT client software. In my case, I uses MQTTBox for Windows, you can learn MQTTBox configuration in following article: https://osoyoo.com/2016/12/02/nodemcu-potentiometer-mqtt/
After click Save button, Click Add Subscriber button and create a new subscriber screen. When you press or release push button switch, the subscriber screen will show the button status as per following picture.
DownLoad Url osoyoo.com
You must be logged in to post a comment.
If i’d like to send MQTT messages to multiple topics based on which input pin, what would I need to do.
An other way around would be; how do I publish different messages to the same topic?
You can change the topic in the code.txt, but you need to match the topic in the code.txt as your mqtt APP.
You can definite the message in the code and you can publish different message to the same topic.
how can i modify the code to send a variable to the MQTT? i want to send the number of times the button was pressed rather than the state of the button. i also only want to update mqtt when the # changes rather than every 2 seconds. suggestions?
Hi,dipstick,
This link may be helpful:
https://www.arduino.cc/en/Tutorial/StateChangeDetection
If you have another questions, you can contact me by [email protected].
Best Regards,
David Guo
i sent you an email
Try to upload below code to your board, it could count the number of times
const int buttonPin = 2;
const int ledPin = 13;
int buttonPushCounter = 0;
int buttonState = 0;
int lastButtonState = 0;
void setup() {
pinMode(buttonPin, INPUT);
pinMode(ledPin, OUTPUT);
Serial.begin(9600);
}
void loop() {
buttonState = digitalRead(buttonPin);
if (buttonState != lastButtonState) {
if (buttonState == HIGH) {
buttonPushCounter++;
Serial.println(“on”);
Serial.print(“number of button pushes: “);
Serial.println(buttonPushCounter);
} else {。
Serial.println(“off”);
}
delay(50);
}
lastButtonState = buttonState;
if (buttonPushCounter % 4 == 0) {
digitalWrite(ledPin, HIGH);
} else {
digitalWrite(ledPin, LOW);
}
}
You did a phenomenal job, and saved me some time (had the exact same need to send a MQTT message when pressing a button on a NodeMCU!) so I specially registered to osoyoo to Thank You 🙂