ad space

Display Temperature on Web with NodeMCU, LM35 and Node-Red

 In this IoT(Internet of Things) tutorial it is shown how to display temperature on the web using NodeMCU and LM35 temperature sensor. To display the temperature on the web/internet the open source IoT application builder Node-RED is used.

Here NodeMCU is connected to the PC through WiFi connection and the NodeMCU is setup as WiFi client. On the PC, MQTT server is setup. The NodeMCU reads temperature data from the LM35 temperature sensor which is then transferred from NodeMCU to PC via WiFi. The data on PC is received by the MQTT server and published via MQTT protocol and displayed on the web or internet browser with the help of NodeRED.

MQTT server NodeMCU LM35 

In order to follow this tutorial you must have installed the following applications:

1. Mosquitto MQTT broker application

MQTT is a messaging protocol used in IoT applications and Mosquitto is one of many MQTT broker application. If this is first time you are using Mosquitto MQTT application then see our earlier tutorial NodeMCU Node Red Industrial Iot platform to learn how to install it. 

2. Node-Red application

Node-Red is flow based application which can be used to create Internet of Things application. If you are new to Node-Red see the tutorial Node Red with Arduino Simple Example for installation guide and how to use Node-Red and create flows.

Interfacing NodeMCU and LM35

The following picture shows how the connection of LM35 to NodeMCU on analog pin A0 on breadboard.

NodeMCU LM35 breadboard

 The following circuit diagram shows LM35 connection with NodeMCU.

NodeMCU LM35
 The LM35 temperature sensor consist of three pins- Vcc, Analog output pin and GND pin.

The Vcc pin is connected to +5V, GND pin is connected to the NodeMCU ground and the analog output is connected to the A0 pin of the NodeMCU.

NodeMCU and LM35 Program Code

In the following code, NodeMCU is setup as WiFi client and connected to PC via WiFi. The MQTT server is also setup. The temperature is read from LM35 on analog pin A0 and then published on the MQTT server address.

#include <ESP8266WiFi.h>
#include <PubSubClient.h>

// Credentials for WiFi
const char* ssid = "your_wifi_ssid";
const char* password = "your_wifi_password";

// MQTT Server(eg 192.168.1.4)
const char* mqtt_server = "your_mqtt_server";

// WiFi Client
WiFiClient nodeClient;
PubSubClient client(nodeClient);

// LM35 Sensor pin at A0 pin
const int LM35Pin = A0;

// Function to connect NodeMCU to WiFi router
void wifiConfig(){
  delay(10);
  // We start by connecting to a WiFi network
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
  WiFi.begin(ssid, password);
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("WiFi connected, NodeMCU IP address: ");
  Serial.println(WiFi.localIP());
}

// Function to reconnect NodeMCU with MQTT broker
void reconnect(){
    while (!client.connected()) {
      Serial.print("Attempting MQTT connection...");
  
      if (client.connect("MQTTClient")){
        Serial.println("connected");  
      } 
      else{
        Serial.print("failed, State: ");
        Serial.print(client.state());
        Serial.println("try again in 5 seconds...");
        delay(5000);
      }
    }
}

void setup(){
  Serial.begin(115200);
  wifiConfig();
  client.setServer(mqtt_server, 1883);
}

void loop() {

  if (!client.connected()) {
    reconnect();
  }
  if(!client.loop())
    client.connect("MQTTClient");
    
    // Read LM35 temperature sensor
    float lm35 = analogRead(LM35Pin);
    lm35 = (lm35*5)/1023;
    lm35 = lm35*100;
    float T = (lm35*9)/5 + 32;

    //Convert float to string and store them in arrays
    static char temperature[7];
    dtostrf(T, 6, 2, temperature);

    // Publishes Temperature values
    client.publish("LM35/temperature", temperature);
    delay(5000);
} 
 

Here the ESP8266.h library is used to connect NodeMCU to the router. In the above code, you have to enter the ssid name and password of your router. The PubSubClient.h library is used to connect to MQTT server and publish and subscribe to MQTT messages. The MQTT server IP address is the IP address of the PC where the MQTT server is installed. This is usually the IP address of the PC which can be known using the cmd command ipconfig in windows OS.

 Node Red flow for NodeMCU, MQTT and LM35

 Below is the flow for connecting NodeMCU published MQTT message and displaying the data on the web.

Node-Red flow for MQTT, NodeMCU and LM35 

The first node above is the mqtt_in node which can be found under the network library and the second node is a gauge node which can be found in the dashboard library. Notice that the dashboard library is not installed in new Node-Red installation so see the tutorial Node Red IoT with Arduino DHT11 to learn how to install it.

To make the above flow first it is good practice to create a tab and group name for the dashboard node(gauge node here). From the dashboard palette create a new tab first with name it "LM35" and then from within this new tab, create a new group and name it "Temperature".


Once you have created the tab and group name, double click and open the gauge node configuration dialog box and choose the tab/group field as LM35/Temperature. Also type Temperature in the label field and use °C in the Units field. Change the range from 0 to 100 in the min and max fields. Leave other fields as it is. This is shown below.

Next open the  mqtt_in configuration dialog box and type localhost:1883 in the Server field and type in LM35/Temperature in the the Topic field. Leave other fields as it is.


The click on the Deploy button and then Start button.

Open web browser and type in the url http://localhost:1880:ui to open the dashboard and you should see the following web page showing temperature data.

 So what has happened here in short is that the NodeMCU is connected via WiFi to the PC and the NodeMCU is setup as WiFi client. The MQTT server is setup on the PC and run. The NodeMCU reads temperature data from the LM35 temperature sensor which is transferred to the MQTT broker which passes it to the Node-Red application. Node-Red reads that MQTT published data and displays the temperature data on the web browser. So this tutorial is a simple Node-Red ESP8266 MQTT Publish Example. For subscribe example see Node-Red ESP8266 MQTT Subscribe Example.

The node red flow code is below.


[
    {
        "id": "5368dda7df7be6f0",
        "type": "tab",
        "label": "Flow 2",
        "disabled": false,
        "info": "",
        "env": []
    },
    {
        "id": "c5f9aed4d27cb0e5",
        "type": "mqtt in",
        "z": "5368dda7df7be6f0",
        "name": "",
        "topic": "LM35/temperature",
        "qos": "2",
        "datatype": "auto-detect",
        "broker": "56d8bb395649bc55",
        "nl": false,
        "rap": true,
        "rh": 0,
        "inputs": 0,
        "x": 350,
        "y": 220,
        "wires": [
            [
                "0a8c59e16484db6f"
            ]
        ]
    },
    {
        "id": "0a8c59e16484db6f",
        "type": "ui_gauge",
        "z": "5368dda7df7be6f0",
        "name": "",
        "group": "ec7bd33dd0e0f242",
        "order": 0,
        "width": 0,
        "height": 0,
        "gtype": "gage",
        "title": "Temperature",
        "label": "°C",
        "format": "{{value}}",
        "min": 0,
        "max": "100",
        "colors": [
            "#00b500",
            "#e6e600",
            "#ca3838"
        ],
        "seg1": "",
        "seg2": "",
        "className": "",
        "x": 570,
        "y": 220,
        "wires": []
    },
    {
        "id": "148c247d79a7fcf0",
        "type": "ui_spacer",
        "z": "5368dda7df7be6f0",
        "name": "spacer",
        "group": "6af2adfd5ce27e94",
        "order": 2,
        "width": 1,
        "height": 1
    },
    {
        "id": "56d8bb395649bc55",
        "type": "mqtt-broker",
        "name": "",
        "broker": "localhost",
        "port": "1883",
        "clientid": "",
        "autoConnect": true,
        "usetls": false,
        "protocolVersion": "4",
        "keepalive": "60",
        "cleansession": true,
        "birthTopic": "",
        "birthQos": "0",
        "birthPayload": "",
        "birthMsg": {},
        "closeTopic": "",
        "closeQos": "0",
        "closePayload": "",
        "closeMsg": {},
        "willTopic": "",
        "willQos": "0",
        "willPayload": "",
        "willMsg": {},
        "userProps": "",
        "sessionExpiry": ""
    },
    {
        "id": "ec7bd33dd0e0f242",
        "type": "ui_group",
        "name": "Temperature",
        "tab": "bf7a6bc59e9ea6aa",
        "order": 1,
        "disp": false,
        "width": "6",
        "collapse": false,
        "className": ""
    },
    {
        "id": "6af2adfd5ce27e94",
        "type": "ui_group",
        "name": "PWM",
        "tab": "1a1b68ef501148ad",
        "order": 1,
        "disp": false,
        "width": "5",
        "collapse": false,
        "className": ""
    },
    {
        "id": "bf7a6bc59e9ea6aa",
        "type": "ui_tab",
        "name": "LM35",
        "icon": "dashboard",
        "order": 2,
        "disabled": false,
        "hidden": false
    },
    {
        "id": "1a1b68ef501148ad",
        "type": "ui_tab",
        "name": "Motor",
        "icon": "dashboard",
        "order": 1,
        "disabled": false,
        "hidden": false
    }
]

Post a Comment

Previous Post Next Post