In this article, we are going to learn “How to send temperature data to ThingSpeak Cloud?”. We can then visualize the temperature data uploaded to ThingSpeak Cloud anywhere in the world.
But “What is ThingSpeak?” ThingSpeak is an open-source IoT platform that allows you to visualize and analyze data in the cloud. One can send data to ThingSpeak. Then, one can visualize data through available interesting widgets. Lastly, one can take suitable action after reading the data.
Sending temperature data to ThingSpeak requires hardware configuration to get the temperature data from the sensor and software configuration to upload temperature data to ThingSpeak cloud.
Hardware Configuration
Hardware connection demands a temperature sensor to measure temperature and a microcontroller to interface temperature sensor.
Components Required
In this project, we are using the following hardware components:
- LM35 (Temperature Sensor)
- NodeMCU (Microcontroller)
- Breadboard
- Jumping wires (To connect temperature sensor with Microcontroller)
Most Internet of things (IoT) projects use NodeMCU as a Microcontroller. Because NodeMCU has a built-in Wi-Fi Module (ESP8266). This Wi-Fi Module helps to connect with the internet and provides a path between the device and the internet. Moreover, it is easily programmable on Arduino IDE.
To measure temperature, the LM35 sensor is the best option. It is cheap and reliable. This sensor has three pins. These pins can easily be connected to the microcontroller.
Circuit Diagram
The left pin of LM35 is a power pin and connect it to the 3.3V pin of NodeMCU. The Right pin of LM35 is a ground pin and connect it to the GND pin of NodeMCU. The middle pin is an output pin of LM35 that provides temperature value in analog. Connect this pin to the analog pin (A0) of NodeMCU.
The Circuit diagram of connection is shown below:
Software Configuration
Software Configuration requires access to ThingSpeak to send data and to program NodeMCU using Arduino IDE.
Starting with ThingSpeak
First of all, we need an account in ThingSpeak. Go to ThingSpeak website and click on Get started for free.
Then fill up all the details to sign up. Ignore if you have an account already.
Once you sign in to ThingSpeak. Create a Channel. Channel can be said as a stream of data. It identified by a numerical channel ID using which data can be inserted or retrieved using ThingSpeak APIs.
Go to Channels>My Channels> New Channel
Moving further, enter the Name of Channel and add the description of Channel. Tick the field 1 to show LM35 temperature sensor output. We are using only one field because there is only one sensor reading.
Now, click on the ‘API keys’ tab and note the Write and Read API key, here we are only using the Write key. Because we are going to write temperature data to ThingSpeak. It is a 16 digit API key code that allows an application to write data to a channel. We can use this API in Arduino IDE coding.
Programming NodeMCU
Lastly, there is a code for NodeMCU to send connected LM35 output to field1 of the ThingSpeak channel.
Converting Output Voltage to Temperature in Celsius Degree
The output pin of LM35 gives voltage. But we need to show temperature in Celsius degree as an output. There is a basic conversion to do this.
- NodeMCU analog pins can calculate up to 3.3 volts
- NodeMCU analog pin resolution is 1023 starting from 0. On 3.3 volts input, it counts to 1023.
- Lm35 max voltage output is 1.5 volts (At 150 degrees centigrade).
- NodeMCU analog pin count for 1.5 volts equals to (1.5 / 3.3)*1023 = 465.
- So, Nodemcu-LM35 Resolution = 465 / 150 = 3.1. Now if NodeMCU analog pin counts 3.1 its equal to 1 degree change in Celsius temperature of LM35.
Code
The code is mentioned below with comments for better understanding.
#include <ESP8266WiFi.h> //Library
String apiWritekey = "52KRVUKXLQUIS29T"; //Write your writeAPI here
const char* ssid = "******"; //Write your SSID
const char* password = "*******" ; //Write your Wifi Password
const char* server = "api.thingspeak.com"; //To initializing the device with Thingspeak API
WiFiClient client;
void setup()
{
Serial.begin(115200);
WiFi.disconnect(); //disconnecting previously connected Wifi
delay(10);
WiFi.begin(ssid, password); //connecting to provided Wifi
while (WiFi.status() != WL_CONNECTED)
{
delay(500);
}
Serial.println("NodeMcu connected to wifi"); //NodeMCU connected to Wifi
}
void loop()
{
float temp = analogRead(A0)/3.1; //temperature in Celsius degree
if (client.connect(server,80)) //connecting to Thingspeak Server
{
String tsData = apiWritekey;
tsData +="&field1=";
tsData += String(temp);
tsData += "\r\n\r\n";
client.print("POST /update HTTP/1.1\n");
client.print("Host: api.thingspeak.com\n");
client.print("Connection: close\n");
client.print("X-THINGSPEAKAPIKEY: "+apiWritekey+"\n");
client.print("Content-Type: application/x-www-form-urlencoded\n");
client.print("Content-Length: ");
client.print(tsData.length());
client.print("\n\n");
client.print(tsData); //temperature value sent to field1 of Thingspeak channel
Serial.print("Temperature: "); //Displaying the temperature in serial monitor
Serial.print(temp);
}
client.stop();
delay(15000);
}
Output
Now open the ThingSpeak server and observe the monitored temperature value on created channel graph.
One can also change the way of representing data by clicking on Add Widgets button. For Example, one can represent data in the form of “Numeric display” and “Gauge”.
Discover more exciting Internet of Things (IoT) blogs here.