Welcome to Solarduino , A blog about DIY Solar PV and Arduino projects

Real Time Monitoring & Execution (Internet of Thing) via MQTT using NodeMCU board

Everybody wants to monitor and executes switching or dimming remotely anytime and anywhere. It is well known as Internet of Things (IOT). It refers to unlimited of physical devices around the world that are connected to the internet, collecting and sharing data. With the access of identical and unique channel, all devices can be interconnect and communicate to each other and become a smart electrical system. Imagine if you have a smartphone acts as a portable display and controller to read values or control the applications which are scattered all around the world while all the related devices can also be talking and control each other. Does it sounds interesting to you?

For me, Internet of Things (IOT) can be divided into 2 main categories. The first category is action or execution application which we use internet to link all the device and make action such as turn ON/OFF or dim brightness, provide alarm or signals etc. It requires physical action towards devices. Smart Home system is one of the application of this kind. The second category is monitoring and data acquisition application which we use internet to collect values from devices or from websites and store on a web server. Meteorological Weather station is one of the example of this application. In this page, we focus on the action and execution application.

There are few internet communication protocols, but the MQTT (MQ Telemetry Transport) protocol is among the most applied on IOT application as this lightweight messaging is much simple and fast. It sends messages to all devices via the broker network but only responsive devices will execute the work based on the message detail. You need a broker to provide or create the unique main channel or network for all your devices. “Publish” is the term use for devices sending message command while “Subscribe” is the term use for devices ready to receive message. “Topic” is the term used, similar to sub-channel which the message can only be received or responded to devices that subscribing the same Topic (sub-channel).

Today, let us begin with the simple project which uses smart phone app IOT ONOFF as a controller and display to monitor real time data of an ambient temperature probe and to control a light switch. The MQTT broker in this project is CloudMQTT from CloundMQTT.com. We will be using NodeMCU microprocessor board to execute all the programming.

Warning ! You may now dealing with high voltage and high power source ! We assumed that you have the basic electrical knowledge and know what you are dealing with. You may need guidance from experienced guys if you are new to electrical work. Safety and Precaution must be always have in mind. We shall not be responsible for anything happening to you.

NodeMCU micro-controller

The NodeMCU micro-controller is similar to Arduino micro-controller board. It is compatible with Arduino IDE software and is built-in with the ESP8266 module which can be connected to the internet. You can grab some at our affiliate link here !!!  

Wireless Smart Energy Monitor for Saving

This is an energy meter for the whole household monitoring and the measurement unit is fit inside electrical panel while there is a portable monitor display which is constantly communicate and acquire data via wireless with the measurement transmitter unit. You can get it at our affiliate link here !!!

Wi-Fi Smart Energy Monitoring Control Meter

This is a smart household energy meter that could monitor and controlled by phone anywhere around the world at anytime. This is for single phase household and installed in electric panel via din rail. Get it at our affiliate link here !!!

MQTT Broker – CloudMQTT

First, you need a MQTT broker. CloudMQTT is providing few plans and there is also a free plan (Cute Cat plan) available. Go in and create an account. Once your account is created, just create a instance. You can named it to anything. I named my instance as “MY_MQTT_Broker_Instance”. Once an instance is created, you will log into the main page of your plan. Below is the example of my Main Page:

Take note all these 4 values which are needed to register in the Arduino Code. All values are case sensitive, make sure copy without error. Only 1 server is required for many projects subject to connected device number limit based on each plan offers. 

On the left panel, there is a Websocket Tab. This Tab is very useful for troubleshooting and command purposes. You can see the messages of all topics going through within the network under Received Messages while you can create command message of a topic manually under Send Message as below

For this project, I have created 2 topics. The first topic is called “Temperature” to create a sub channel about the ambient temperature value. Since the temperature sensor is connected on NodeMCU micro-controller, NodeMCU will publish temperature value under topic “Temperature“. If we want to read the value in smart phone via IOT ONOFF app, the app will assign to subscribe the topic “Temperature“. We make the NodeMCU micro-controller to subscribe topic “Temperature” as well for troubleshooting purpose; in case we send message via other devices such as CloudMQTT (Websocket), the NodeMCU micro-controller can receive the message and display on Serial Monitor. 

As for second topic, I named it as “LightSwitch” to create a sub channel about the switching of a light. Since the light is connected on NodeMCU, NodeMCU will subscribe On/Off command message under topic “LightSwitch“. The NodeMCU will standby listening and receiving command message from other devices to turn on/off light. If we want to command the switching using the IOT ONOFF app, the app will assign to publish for topic “LightSwitch” as controller. On the mean time we need the app to know the status of the light switching as well, maybe command via other devices such as cloudMQTT (Websocket). Thus the app is also assign to subscribe for topic “LightSwitch” to know the current situation of light switching.

 

This is generally how it works. It sounds complicated in theory but it will be easy in the coding and attached a picture below hoping that you could understand better.

Dupont line Wires

In order to connect wiring between Arduino board and module fast and easy, you need the dupont line cables. These cables are suitable during testing and troubleshoot where a lot of wire modifications. You can get it at our affiliate link here !!! 

Ferrule Lugs and Crimper Set

Ferrule lugs are used at terminals for tight and secure connection. Besides, it also prevent stranded cables accidentally touching adjacent power line that may cause short circuit. Grab 1 at our affiliate link here !!!

IOT OnOff Smartphone App

In this project, I will be using IOT OnOff smartphone app which is free and available at their official website https://www.iot-onoff.com/. It supports both Android and IOS phone. It is a free smartphone application that has a lot of flexibility to customize own dashboard display. Free version app has at least 10 widgets to suits quite a number of application such as graph, display value, slider (for brightness), meter or gauge, switch, and message screen. The setup level is suitable for beginner as it is easy and simple.

The app is not suitable for more advance user. Besides its simplicity, it lacks of few other important features such as no memory recording (Once you shutdown the app and restart, the old values are missing thus only able to display real time values) and few topics cannot be displayed together in a widget. As for Solar PV application user, it is good to have an app that could also records and display previous measured values such as voltages, current, battery level and generation power so that we can trace back all the values few days before. In addition, sometimes we also need a widget which can combine generation and consumption in the same graph so that there can be easily compared.  

Overall, it is consider a good app especially for beginner that just started to explore the IOT application as it is a free app and user friendly. It is also suitable for user that needs only real time monitoring, switching (ON/OFF) or power adjustment (PWM) applications. 

UNI-T Multimeter is a good quality with decent price. We have been using UNI-T multimeter for years has not been any issue. We were using UT33C model. You can get yours at our affiliate link here !!!

Setup IOT ONOFF App

Once you have downloaded and installed the IOT ONOFF app on your smart phone, be sure that you have jot down the 4 values (MQTT Server, MQTT User, MQTT Password and MQTT Port), you need to key in all the values in the app. Below is the walk-through diagram:

Once all values are keyed in, go back to image number 4 (above) and check the connection status. It should able to connect and status will be “Connected“. The second step will be adding widgets. To recall the project, we will need a widget to show temperature value and a widget as a switch to turn on/off light switch.

The final view of dashboard will look like above. However, your widgets will not show any values as your NodeMCU board not yet connected online. Once the template is ready, the final step will be the NodeMCU wiring and code uploading.

You need the connector that can secure cables and isolate from accidental touch. Get the fast connector at our affiliate link here !!!

NodeMCU Wiring

You may now dealing with high power or high voltage power source, any exposed conductors need to be isolated and fused from power source side. I recommend to use ferrule bootlace for terminal tight connection (for power cable) and to prevent stranded conductor accidentally touching adjacent cable line which might cause short circuit and fire risk. Furthermore, no conductive materials are placed around the workplace and use non-metallic enclosure or project box. Make sure wiring for all dupont cables are tight (for signal cable)

NTC Temperature Sensor

NTC Thermistor is a temperature sensor based on resistance value changes. The higher the temperature, the lower its resistance. You can grab it at our affiliate link here !!!

2 Relay Module

This is a relay module that can support 2 isolated wiring load control with each switching comes with NC and NO. Can support up to 10A for AC /DC loads. Controlable by NodeMCU or Arduino Board. Kindly get it at our affiliate link here !!! 

Software Code

The final step would be adding source code onto NodeMCU board. I assume you have installed the Arduino IDE Software. If you have not installed the software, the link here can bring you to the official download site. Once you have downloaded the software, you may download the code file (.ino) for this application below (right click save link). 

Before uploading the code to NodeMCU, if this is the first time you are dealing with NodeMCU, you need to add ESP8266 Support file and add NodeMCU board library into IDE software. To add ESP8266 Support file, open Arduino IDE Software, go to files>preferences>Additional Boards Manager URLs, paste this link http://arduino.esp8266.com/stable/package_esp8266com_index.json to the black space and click OK.

After finished this step, go to Tools > Boards > Boards Manager and download the ESP8266 library by ESP8266 Community

Once done, you can start download the .ino code file below and upload to the NodeMCU board. Before Upload the file, kindly unplug the dupont wire from pin D0 to RST pin or else code cannot be uploaded.

I will not display the code here because it is long. You can download the .ino file to see for your own. Almost all code lines are with explanation.

Before we end, we would like to give gratitude to you for taking the time to read the post. We would need readers like you to support us in order to keep growing. You can support us in the following ways :

Donate & Fund Raising

If you like my work, please send me a donation to encourage me to do more. Thanks

Aliexpress Affiliate

We are the member of Aliexpress affiliate marketing. Do support us by clicking the affiliate product links if you do wish to purchase them.

Like and Share

If you like our post, we need your support to like and share our posts or videos so that it can reach more and more people like you !!

Result – In Serial Monitor

Result – On Smart Phone

 

For Arduino Code Files, Remember to Right Click > Save Link As … You may alter the internal code as you wish. Happy coding !! 

IOT MQTT Ambient Temperature & Light Switch.ino