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 NodeMCU to get light strength from Photoresistor and publish the data to MQTT broker. Another MQTT client software will subscribe the NodeMCU message topic and get real time light data.
Parts and Devices

Devices
qty
NodeMCU board
1
Breadboard
1
Photoresistor
1
resistors(1k)
1
jumper wires
some

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

nodemcu-Photoresistor.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 publish the light strength data to MQTT broker under topic “OsoyooData”.  We will use  a MQTT client to subscribe topic “OsoyooData” from same MQTT broker and display remote light strength value in real time.

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 downloaded MQTTBox for Windows and did following configuration:

Open MQTTBox software,do following configuration:

Special Notes:

Host :broker.mqttdashboard.com:1883 , this is MQTT broker which should  match the mqtt_server variable showed in NodeMCU code as following”  , “:1883” is the default port of MQTT

Will – Topic : OsoyooData which should match the NodeMCU client.publish() function.

Protocol: mqtt/tcp

You can read more information in MQTTBox Client Setting Help

After click Save button, you will see a pop-up window as following:

Click Subscribe button , you will see real time light strength value in MQTTbox,

Open serial window in Arduino IDE, you will see NodeMCU light sensor data, the value should match the MQTTBox light strength values.