In this lesson, we will connect infrared receiver to NodeMCU, When infrared controller key is pressed, it will send an IR signal,then the receiver will catch these signals and send the IR code to the NodeMCU, the NodeMCU will publish these Messages to MQTT broker. If a MQTT client subscribe to the corresponding topic, it will get the messages from broker.
Overhere,we use D2 as the digital input pin.
Connect the NodeMCU to computer via USB cable,open this sketch by using Arduino IDE(Version1.6.4+):
Edit the code to fit your own WiFi and MQTT settings as following operations:
const char* ssid = “your_hotspot_ssid”;
const char* password = “your_hotspot_password”;
Find above code line,put your own ssid and password on there.
2)MQTT Server Address Setting
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.
3)MQTT Client Settings
If your MQTT broker require clientID,username and password authentication,you need to change
if (client.connect(clientId,userName,passWord)) //put your clientId/userName/passWord here
If not,just keep them as default.
After do that,choose the coresponding board type and port type as below,then upload the sketch to the NodeMCU.
About how to config the MQTT Client,check this link.
Onece the upload done,if wifi hotspot ssid and password setting is ok and MQTT broker is connected, open the Serial Monitor,you will see following result:
The NodeMCU serial port will show the IP address and the connection satatus,then print the recevie value：
Once upload down,open the Serial Monitor and choose the right baudrate(115200 here),you will see the output as below:
Press different button on the infrared remote controller,you will see different receive value as follow,so you can use this way to know the infrared code for each button:
At the same time,you cam also open the MQTT client and click “Subscribe” button on the “OsoyooData” topic,you will see the message that published by NodeMCU:
DownLoad Url osoyoo.com
You must be logged in to post a comment.
I really liked your this Tutorial. Can you please Complete this tutorial like now we can send ir codes (saved in database of mqtt server) to TV / Air Conditioners to switch it off/on.. please make this tutorial. i will be very happy..
I don’t know about you guys. But I had to add below library to get it working
#include // Add this library