Send LM35 Data to Thingspeak: ESP8266 | GSM | NodeMCU

In this post we are going to learn how to send LM35 sensor’s temperature data to Thingspeak using: 1) Generic ESP8266, 2) using GSM modem and 3) using NodeMCU. We will explore each method in detail and you may utilize any one of the method mentioned here.

There is a good chance that you are a beginner in IoT project and you have chosen LM35 and Arduino development environment, if so you landed on the right place. We will walk you through all the little things that you need to take care while constructing one of your first IoT project and we will also guide you where you may go wrong, in the end of this article you will be able to upload temperature data to thingspeak successfully.  

We will see:

  • Setting up Thingspeak account for receiving LM35 data.
  • Installing ESP8266 package to Arduino IDE.
  • Installing CH340G driver.
  • Sending LM35 data to thingspeak using NodeMCU.
  • Sending LM35 data to thingspeak using GSM.
  • Sending LM35 data to thingspeak using Generic ESP8266.

Setting up Thingspeak account for LM35 sensor:

Before we proceed towards construction details we need to setup our thingspeak account correctly to receive LM35 temperature data, this procedure need to be done for all three methods mentioned here.

You can sign up for thingspeak account here, if you haven’t signed up yet.

  • You need to create a new channel by clicking “New Channel” button and in the channel you should do the changes as shown:
LM35 Data to Thingspeak
LM35 Data to Thingspeak

1) Go to Channel settings tab and edit the name.

2) Enable Field 1 by checking the box and write the label as “Temperature”.

3) Scroll down and click save.

  • Now click on API keys tab:

API keys are the access keys to your channel using which you can write and read values. In this project we are using only write API key which is already generated for your channel. You need to take note of it and this write API key need to be inserted to the given program codes.

  • Now by clicking “Private View” tab you will see a blank channel. You will see some data once we send to it.
LM35 Data to Thingspeak
LM35 Data to Thingspeak

Installing ESP8266 package to Arduino IDE:

This process needs to be done if you are using NodeMCU or generic ESP8266 to connect to thingspeak. If you are going to use GSM modem to send LM35 data, this step is irrelevant for you, but for NodeMCU and ESP8266 boards this step is mandatory.

Preferences
Preferences
  • Paste the URL on the box and click “OK”.
  • Now go to tools > Board > Boards Manager.
  • Now a window will popup:
boards manager
boards manager
  • Type ESP8266 on the box as shown and you will get an installation option, select the latest version and click install.

Downloading files may take several minutes depending on your internet connection. 

Downloading Thingspeak library:

You need to download Thingspeak library for projects using NodeMCU and generic ESP8266, this library is irreverent if you are going to use GSM module in this project.

Download Thingspeak library here.

Installing CH340 driver:

This step is relevant only if you are going to use NodeMCU to send LM35 data to thingspeak. You may skip for other two mentioned methods.

Installing CH340 driver is mandatory not only for NodeMCU but also for all Arduino clones, Uno R3, nano etc. without it you cannot upload any program.

We have explained the steps how to install CH340 driver here.

Once you completed the mentioned relevant steps, we are good to go with construction details.

              Send LM35 Data to Thingspeak using NodeMCU

Circuit Diagram:

LM35 with NodeMCU
LM35 with NodeMCU
LM35 Pin Diagram
LM35 Pin Diagram

The circuit is so simple that we didn’t consider drawing one. Connections are mentioned below:

  • Vcc of LM35 connects to ‘VU’ pin of NodeMCU. VU pin provides 5V output.
  • The output terminal of LM35 connects to ‘A0’ of NodeMCU.
  • GND of LM35 connects to GND of NodeMCU.

Program code for NodeMCU:

#include "ThingSpeak.h"
#include <ESP8266WiFi.h>
const int LM35 = A0;
//----------- Enter you Wi-Fi Details---------//
char ssid[] = "XXXXXXX"; //SSID
char pass[] = "YYYYYYY"; // Password
//-------------------------------------------//

WiFiClient  client;

unsigned long myChannelNumber = 000000; // Channel ID here
const int FieldNumber = 1;
const char * myWriteAPIKey = "ZZZZZZZZZZZZZZ"; // Your Write API Key here

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
}
void loop()
{
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      Serial.print(".");
      delay(5000);
    }
    Serial.println("\nConnected.");
  }
  int ADC;
  float temp;
  ADC = analogRead(LM35);  /* Read Temperature */
  temp = (ADC * 3); /* Convert adc value to equivalent voltage */
  temp = (temp / 10); /* LM35 gives output of 10mv/°C */
  Serial.print("Temperature = ");
  Serial.print(temp);
  Serial.println(" *C");
  delay(1000);
  ThingSpeak.writeField(myChannelNumber, FieldNumber, temp, myWriteAPIKey);
  delay(1000);
}

In the code you need to change the following:

You need to insert your Wi-Fi credentials in the place of XXXX and YYYY.

//----------- Enter you Wi-Fi Details---------//
char ssid[] = "XXXXXXX"; // SSID
char pass[] = "YYYYYYY"; // Password
//-------------------------------------------//

You need to change “00000” with your channel ID and replace “ZZZZZZZZZ” with your write API key.

unsigned long myChannelNumber = 000000; // Channel ID here
const int FieldNumber = 1;
const char * myWriteAPIKey = "ZZZZZZZZZZZZZZ"; // Your Write API Key here

How to upload this program code to NodeMCU properly?

  • Go to Tools > Board. Select NodeMCU 1.0 (ESP-12E Module).
  • Select baud rate “115200”.
  • Select the right COM port.
  • Now press and hold flash button on the NodeMCU and press reset button once and leave the flash button. Only by doing this you can upload program to NodeMCU.
  • Now press upload, uploading code to NodeMCU will take a minute. On successful program upload you will see this:
Program uploaded to ESP8266
Program uploaded to ESP8266
  • If you failed to upload the code, you will see something similar to this:
Upload Error ESP8266
Upload Error ESP8266

If you fail to upload the code, you did not press the “flash” and “reset” button in the correct sequence. 

LM35 NodeMCU Output (at thingspeak):

LM35 Thingspeak Output
LM35 Thingspeak Output

Send LM35 Data to Thingspeak Using GSM modem

Here we are going to explore how to send LM35 temperature data to thingspeak using GSM module. Using NodeMCU or generic ESP8266 we need a Wi-Fi router beside them for providing internet, but using GSM modem we can access GPRS internet anywhere without the need for Wi-Fi, only requirement is that GSM module must be within the cellular network range.

Circuit diagram:

Send LM35 Data to Thingspeak Using GSM
Send LM35 Data to Thingspeak Using GSM

Circuit Description:

The circuit consists of Arduino and GSM modem (SIM 900 or SIM 800 or you can use any other model of GSM modem). The Arduino can be powered via USB or DC jack; however GSM modem must be power only through DC jack by applying 9V at 1 ampere.

You need to insert a valid SIM card that can access GPRS (2G) with pre-activated mobile data. Rest of the circuit is self-explanatory.

Note: Don’t insert Jio SIM card or any SIM card whose network is not 2G backward compatible.

Prototype:

Send LM35 Data to Thingspeak Using GSM
Send LM35 Data to Thingspeak Using GSM

Program code:

//-----Electronics-project-hub>com------//
#include <SoftwareSerial.h>
SoftwareSerial gsm(10, 11); // RX, TX
const int LM35 = A0;
float temp;	
void setup()
{
  Serial.begin(9600);
  gsm.begin(9600);
  modem_init();
  data_init();
  internet_init();
}
void loop()
{
  int ADC;
  ADC = analogRead(LM35);  
  temp = (ADC * 4.88); 
  temp = (temp / 10); 
  Serial.print("Temperature = ");
  Serial.print(temp);
  Serial.println(" *C");
  Send_data();
  delay(1000);
}
void modem_init()
{
  Serial.println("Please wait.....");
  gsm.println("AT");
  delay(1000);
  gsm.println("AT+CMGF=1");
  delay(1000);
  gsm.println("AT+CNMI=2,2,0,0,0");
  delay(1000);
}
void data_init()
{
  Serial.println("Please wait.....");
  gsm.println("AT");
  delay(1000); delay(1000);
  gsm.println("AT+CPIN?");
  delay(1000); delay(1000);
  gsm.print("AT+SAPBR=3,1");
  gsm.write(',');
  gsm.write('"');
  gsm.print("contype");
  gsm.write('"');
  gsm.write(',');
  gsm.write('"');
  gsm.print("GPRS");
  gsm.write('"');
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(1000); ;
  gsm.print("AT+SAPBR=3,1");
  gsm.write(',');
  gsm.write('"');
  gsm.print("APN");
  gsm.write('"');
  gsm.write(',');
  gsm.write('"');
  //------------APN------------//
  gsm.print("bsnlnet"); //APN Here
  //--------------------------//
  gsm.write('"');
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(1000);
  gsm.print("AT+SAPBR=3,1");
  gsm.write(',');
  gsm.write('"');
  gsm.print("USER");
  gsm.write('"');
  gsm.write(',');
  gsm.write('"');
  gsm.print("  ");
  gsm.write('"');
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(1000);
  gsm.print("AT+SAPBR=3,1");
  gsm.write(',');
  gsm.write('"');
  gsm.print("PWD");
  gsm.write('"');
  gsm.write(',');
  gsm.write('"');
  gsm.print("  ");
  gsm.write('"');
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(2000);
  gsm.print("AT+SAPBR=1,1");
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(3000);
}
void internet_init()
{
  Serial.println("Please wait.....");
  delay(1000);
  gsm.println("AT+HTTPINIT");
  delay(1000); delay(1000);
  gsm.print("AT+HTTPPARA=");
  gsm.print('"');
  gsm.print("CID");
  gsm.print('"');
  gsm.print(',');
  gsm.println('1');
  delay(1000);
}
void Send_data()
{
  gsm.print("AT+HTTPPARA=");
  gsm.print('"');
  gsm.print("URL");
  gsm.print('"');
  gsm.print(',');
  gsm.print('"');
  gsm.print("http:");
  gsm.print('/');
  gsm.print('/');
  //-----------------------Your API Key Here----------------------//
  //Replace xxxxxxxxxxx with your write API key.
  gsm.print("api.thingspeak.com/update?api_key=XXXXXXXXXX&field1=");
  //---------------------------------------------------------------//
  gsm.print(temp);//>>>>>>  variable 1 (temperature)
  gsm.write(0x0d);
  gsm.write(0x0a);
  delay(1000);
  Serial.println("Data sent to thingspeak......");
  gsm.println("AT+HTTPACTION=0");
  delay(1000);
}
//-----Electronics-project-hub>com------//

NOTE 1: Insert your write API key here.

//-----------------------Your API Key Here----------------------//
  //Replace xxxxxxxxxxx with your write API key.
  gsm.print("api.thingspeak.com/update?api_key=XXXXXXXXXX&field1=");
  //---------------------------------------------------------------//

Note 2: You need to insert the APN for your SIM card’s cellular network.

//------------APN------------//
  gsm.print("bsnlnet"); //APN Here
  //--------------------------//

LM35 GSM thingspeak output:

LM35 data to Thingspeak
LM35 data to Thingspeak
LM35 Thingspeak Output
LM35 Thingspeak Output

What is APN? What is APN for my network? Why data is not getting updated on thingspeak? These are some of the frequent question raised by beginner in IoT projects. These questions are answered clearly in this post. Scroll down to the FAQ.

Send LM35 data to thingspeak using generic ESP8266

In this section we are going to see how to send LM35 temperature data to thingspeak using Arduino and generic ESP8266. The generic ESP8266 is the name of the board illustrated below:

ESP8266 Pin diagram
ESP8266 Pin diagram

Pin diagram is also labelled at the back side:

Pin Diagram of ESP8266
Pin Diagram of ESP8266

ESP8266 Description:

ESP8266 is an IoT board it has its own CPU and memory and it works on 3.3V (not 5V). We have to upload an appropriate code to this board to make it functional. It also has two GPIO pins which can be used for any digital input and output purposes.

Since it has just two GPIO pins we don’t use them for big or medium scale IoT projects, instead we use Arduino or any other microcontroller to collect data from sensors and send data to ESP8266 via UART (Serial data). The received data will be sent to an IoT server, in this case we are sending to thingspeak server. 

Here we are uploading a program code to ESP8266 and one more program code to Arduino board. Since ESP8266 don’t have built-in hardware programmer like Arduino or NodeMCU, we need to setup a circuit by which we can upload appropriate code to ESP8266.

How to Upload Code to Generic ESP8266:

Circuit diagram for uploading code to ESP8266
Circuit diagram for uploading code to ESP8266
Uploading code to ESP8266
Uploading code to ESP8266

You need to setup the above circuit to upload any program to ESP8266. Make sure that you removed ATmega328P IC from Arduino board otherwise program won’t get uploaded and don’t connect Vcc of ESP8266 to 5V line.

Once you setup this circuit, power the Arduino via USB, now you need to press the two buttons in a sequential manner, only then the program code get uploaded.

  • Press and hold the flash button.
  • Now press reset button once, while holding flash button.
  • Now you can leave the flash button, but don’t leave flash button while leaving reset button.

Now ESP8266 is ready to upload program code.

Program code for Generic ESP8266:

#include "ThingSpeak.h"
#include <ESP8266WiFi.h>

char ssid[] = "xxxxxxxx"; // Wi-Fi SSID
char pass[] = "yyyyyyyy"; // Wi-Fi password
WiFiClient  client;

unsigned long Channel_ID = 123456; // Place your channel ID
const int FieldNumber = 1;
const char * myWriteAPIKey = "XXXXXXXXXX"; // Place your write API key.

String value = "";

void setup()
{
  Serial.begin(115200);
  WiFi.mode(WIFI_STA);
  ThingSpeak.begin(client);
}

void loop()
{
  if (Serial.available() > 0)
  {
    while (Serial.available() > 0)
    {
      int inChar = Serial.read();

      value += (char)inChar;

    }
    Serial.println(value);
  }
  if (WiFi.status() != WL_CONNECTED)
  {
    Serial.print("Attempting to connect to SSID: ");
    Serial.println(ssid);
    while (WiFi.status() != WL_CONNECTED)
    {
      WiFi.begin(ssid, pass);
      Serial.print(".");
      delay(5000);
    }
    Serial.println("\nConnected.");
  }
  ThingSpeak.writeField(Channel_ID, FieldNumber, value, myWriteAPIKey);
  value = "";
}

Note: Don’t forget to insert your channel ID, write API key, WI-FI SSID and password in the above code.

  • Go to Tools > Board > select Generic ESP8266.
  • Select baud rate as 115200.
  • Hit the upload button.

If you successfully upload the code you will see a success message:

Program uploaded to ESP8266
Program uploaded to ESP8266

If you failed to upload the code, you will get some error message. If you get error message, you didn’t press the reset and flash buttons in the mentioned sequence or you did not wire the circuit correctly.

Full circuit diagram:

LM35 data to Thingspeak
LM35 data to Thingspeak

Once you uploaded the code to ESP8266, you need to wire the circuit as shown above and insert the IC back to Arduino board and upload the below given code to Arduino.

Program code for Arduino:

#include <SoftwareSerial.h>
SoftwareSerial mySerial(10, 11); // RX, TX
const int LM35 = A0;  
void setup()
{
  Serial.begin(9600);
  mySerial.begin(115200);
}

void loop()
{
  int ADC;
  float temp;
  ADC = analogRead(LM35);  
  temp = (ADC * 4.88); 
  temp = (temp / 10); 
  Serial.print("Temperature = ");
  Serial.print(temp);
  Serial.println(" *C");
  mySerial.println(temp);
  delay(1000);
}

Prototype:

LM35 data to Thingspeak
LM35 data to Thingspeak

Output:

LM35 Thingspeak Output
LM35 Thingspeak Output

Now you know how to send LM35 temperature data to thingspeak using GSM, NodeMCU and generic ESP8266.

If you have any questions regarding these projects comment your questions below, you will get a guaranteed reply from us.

Avatar photo

My nick name is blogthor, I am a professional electronics engineer specialized in Embedded System. I am a experienced programmer and electronics hardware developer. I am the founder of this website, I am also a hobbyist, DIYer and a constant learner. I love to solve your technical queries via comment section.