Remotely control Piezo Buzzer with NodeMCU through MQTT IOT broker

Byamber

Remotely control Piezo Buzzer with NodeMCU through MQTT IOT broker

About NodeMCU

NodeMCU is a very popular Micro controller with wifi access. It is based on ESP8266 – an economical but powerful chip with wifi acces. It supports Arduino IDE. So if you familiar with Arduino IDE, you can program NodeMCU in no time.

Objective:

In this project, we will show how control instructions are sent to an IOT device. The demo device is a Piezo Buzzer connected to NodeMCU,. We will use MQTT protocol to exchange message between NodeMCU and another IOT device.

MQTT is a Machine to Machine IOT protocol. This protocol allows one device(sender) to publish data to a MQTT broker with specified Topic, another device(receiver) which subscribes the same topic can easily get message from sending device.

Parts and Devices

Devices
qty
NodeMCU board
1
Breadboard
1
Piezo buzzer
1
jumper wires
some

Picture 1: 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

http://osoyoo.com/wp-content/uploads/2016/12/NodeMCU_Buzzer-.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.mqtt-dashboard.com”;

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.mqttdashboard.com”, “iot.eclipse.org” etc

If you want to install your own MQTT broker in Ubuntu Linux, please read this article http://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:
Picture 2:

After the NodeMCU connected to wifi and MQTT broker, it subscribes the topic “OsoyooCommand”, if the received topic message is “1”, it will turn on Piezo buzzer, if the message is “0”, it will turn off Piezo buzzer. We will use a MQTT client to publish “0” or “1” to MQTT broker.

You can select any MQTT client tool to send message to MQTT broker. 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 use Microsoft MQTT Client for windows. You can google and download Microsoft MQTT client from internet.

After you installs Microsoft MQTT, please configure the MQTT host and topic to match Picture 2. The use MQTT publisher to send message to NodeMCU.

you will see result as following:

Picture 3

When you publish “1” , the Piezo Buzzer will beep, when you send “0”, the beep will turn off.

About the Author

amber administrator

You must be logged in to post a comment