Authorized Online Retailers:
In this lesson, we will show how to use NodeMCU to get real-time light strength data from the light sensor , then publish these datas to a MQTT broker via MQTT protocol,so we can subscribe to these messages by MQTT client.
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, all the connections are normal, open the Serial Monitor,you will see following result:the NodeMCU serial port keep output real-time light strength data every 0.5 second,and these messages will be published to the MQTT broker via MQTT protocol,then they will be subscribed by the MQTT client.
At the same time,open the MQTT client, choose the subscribe topic "OsoyooData" and click "Subcribe" button, the MQTTBox will subscribe to this topic, so you will see the real-time light strength data as below: