In this project, we will show how control instructions are sent to an IOT device. The demo device is an LED connected to NodeMCU, an economical but power full IOT micro controller with wifi access. We will use MQTT protocolto transmit message between devices. 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
NodeMCU board x 1 pc
LED x 1 pc
220 ohm resistor x 1
Breadboard x 1 pc
Circuit Connection Graph
Before running the project, we need set up the Arduino IDE as per following procedures:
- If you don't have Arduino IDE, please go to https://www.arduino.cc/en/Main/Software download the IDE.
- Add ESP8266 support in Arduino IDE, read this http://osoyoo.com/2016/10/25/how-to-use-arduino-ide-to-program-esp8266-wifi-module/
- In Arduino IDE->Tools->Board Manager menu, select NodeMCU (0.9 or higher) and corresponding COM port (showed in your device manager)
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/samplecode/nodemcu-led.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
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.mqtt-dashboard.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:
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 LED, if the message is "0", it will turn off LED. Let us use Linux Mosquitto mqtt client software ( you can read this article http://osoyoo.com/2016/09/26/2610) to send 0 and 1, you will see result as following:
When you send linux command
mosquitto_pub -h broker.mqtt-dashboard.com -t "OsoyooCommand" -m "1"
the LED will be turned on.
When you send linux command
mosquitto_pub -h broker.mqtt-dashboard.com -t "OsoyooCommand" -m "0"
the LED will be turned off.