MEVIHUB

How to Send Data(LM35) to ThingSpeak using ESP32

Overview

In this project, we will record and monitor temperature data online using a ThingSpeak IoT server. And we can see the recorded data and graph over time on your website. It is made using an ESP32 WiFi module and an LM35 temperature sensor. The ESP32WiFi chip reads the current temperature from the LM35 using an ADC and sends it to a ThingSpeak server for live monitoring from anywhere in the world.

Components Required

Hardware

Connections

Preparing ESP32 in Arduino IDE

Step 1: Firstly you have to download and install Arduino IDE software which you can download from https://www.arduino.cc/en/Main/Software for free. If you have already installed it on your PC then make sure that it is the latest version of IDE as the older version doesn’t include the ESP32 board.

Step 2: After installing, open IDE and go to Files -> Preferences and open preference window and see the “Additional Boards Manager URL’s” as:

Step 3

This box can be empty or contain some other URL if you have used it before for ESP32. Simply paste the URL below into this box if the box already contains another URL, and then paste it by separating another URL with a comma (,).

https://raw.githubusercontent.com/espressif/arduino-esp32/gh-pages/package_esp32_dev_index.json

Step 4

After pasting the given URL, my window looks like this because I already used ESP32, now press ok and the window will be gone.

Step 5

Now go to Tools-> Board-> Board Manager and search for ESP32 and press install, it will take some time to install make sure that you have an internet connection while installing after installing your window looks like this:

After this close, the window of board manager and your IDE is ready to work with ESP32.

Software

ThingSpeak Setup for ESP32

ThingSpeak is a very good platform for IoT based projects. By using the channels and web pages provided by ThingSpeak, we can monitor any online data from anywhere and we can also control our online system. ThingSpeak “collects” data from sensors and “analyzes and visualizes” the data and “actions” by triggering a reaction. Here we explain how to set up a ThingSpeak account for this project.

  1. Firstly, the user needs to create an account on ThingSpeak.com, then log in and click “Get Started”.
Click on get started for free
Click on create one
Now go to the ‘Channels’ menu and click on the ‘New Channel’ option on the same page.

You will now see a form to create the channel, fill in the name and description as per your choice. Then fill in “TempF in field 1” and TempF in field 2. Check the “Make public” checkbox at the bottom of the form and finally save the channel. Now your new channel is ready.

Now click on the ‘API keys’ tab and note the Write and Read API key, here we are only using the Write key. You need to copy and paste this key into the code (see below).

Now user needs to upload the program to ESP32 using Arduino IDE.

After uploading, open the “PRIVATE VIEW” icon on the ThingSpeak website and observe the monitored temperature value on the graph as shown below.

Source Code/Program:

In this section, you need to enter your ThingSpeak server write an API key and provide the SSID and password of your hotspot.

// Set our wifi name and password
const char* ssid = "Devilal";
const char* password = "1234567891";

// Your thingspeak channel url with api_key query
String serverName = "https://api.thingspeak.com/update?api_key=ZEUKZ4D2EH58XP30";

Source code for IoT based temperature monitoring system project using ESP32. Simply copy and paste the code into your Arduino IDE, then compile and upload it to the “ESP32 Dev Module” board.

Final Code

// Import nessesary libraries
#include <WiFi.h>
#include <HTTPClient.h>

#define ADC_VREF_mV    3300.0 // in millivolt
#define ADC_RESOLUTION 4096.0
#define PIN_LM35       35 // ESP32 pin GIOP36 (ADC0) connected to LM35


// Set our wifi name and password
const char* ssid = "Devilal";
const char* password = "1234567891";

// Your thingspeak channel url with api_key query
String serverName = "https://api.thingspeak.com/update?api_key=ZEUKZ4D2EH58XP30";

// Assign some variables to allow us read and send data every minute
unsigned long lastTime = 0;
unsigned long timerDelay = 15000;


void setup() {
  Serial.begin(9600); // Opens up the serial port with a baudrate of 9600 bits per second


  WiFi.begin(ssid, password); // Attempt to connect to wifi with our password
  Serial.println("Connecting"); // Print our status to the serial monitor
  // Wait for wifi to connect
  while(WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.print("Connected to WiFi network with IP Address: ");
  Serial.println(WiFi.localIP());
}


void loop() {
 int adcVal = analogRead(PIN_LM35);
   float milliVolt = adcVal * (ADC_VREF_mV / ADC_RESOLUTION);
     float tempC = milliVolt / 10;
       float tempF = tempC * 9 / 5 + 32;
    Serial.print("Temperature: ");
  Serial.print(tempC);   // print the temperature in °C
  Serial.print("°C");
  Serial.print("  ~  "); // separator between °C and °F
  Serial.print(tempF);   // print the temperature in °F
  Serial.println("°F");

  
  if ((millis() - lastTime) > timerDelay) { // Check if its been a minute
    if(WiFi.status()== WL_CONNECTED){ // Check to make sure wifi is still connected
      sendData(tempC, tempF); // Call the sendData function defined below
    }
    else {
      Serial.println("WiFi Disconnected");
    }
    lastTime = millis();
  }
}


void sendData(float temp, float pres){
  HTTPClient http; // Initialize our HTTP client


  String url = serverName + "&field1=" + temp + "&field2=" + pres; // Define our entire url
      
  http.begin(url.c_str()); // Initialize our HTTP request
      
  int httpResponseCode = http.GET(); // Send HTTP request
      
  if (httpResponseCode > 0){ // Check for good HTTP status code
    Serial.print("HTTP Response code: ");
    Serial.println(httpResponseCode);
  }else{
    Serial.print("Error code: ");
    Serial.println(httpResponseCode);
  }
  http.end();
}

Exit mobile version