Introduction: Temperature Measurement Using Esp32 and Thingsio.ai Platform

In this tutorial I am going to explain about measuring the temperature in the surroundings by using inbuilt temperature sensor present in esp32 development board. esp32 has many inbuilt sensors like the hall sensor used as the proximity sensor,touch sensor,temperature sensor. In this tutorial we are going to discuss about the temperature sensor.

Step 1: COMPONENTS:

Hardware :

1.esp32 development board

Software:

1.Arduino IDE

2.Thingsio.ai

Step 2: BLOCK DIAGRAM REPRESENTATION OF Esp32

Step 3: CODING:

#include <WiFi.h>
#include <WiFiMulti,h>
#include <WiFiClientSecure.h>
#include <time.h>
#include <sys/time.h>
#ifdef __cplusplus
extern "C" {
#endif
uint8_t temprature_sens_read();
//uint8_t g_phyFuns;
#ifdef __cplusplus
}
#endif
int count=0,i,m,j,k;
int t,t1,t2,t3;
int outputpin= A0;//ds18b20 
//////////////////////////////////////// ALL DECLARATIONS
for CLOUD //////////////////////////////
const char* host = "api.thingsai.io";                                 // OR host =
devapi2.thethingscloud.com
const char* post_url = "/devices/deviceData";       //
OR /api/v2/thingscloud2/_table/data_ac
const char* time_server =
"baas.thethingscloud.com";            
//this is to convert timestamp
const int httpPort = 80;
const int httpsPort = 443;
const char*  server =
"api.thingsai.io";  // Server
URL
char timestamp[10];
WiFiMulti WiFiMulti;
// Use WiFiClient class to create TCP connections
   WiFiClient client;
/////////////////////////////////////// TIMESTAMP
CALCULATION function///////////////////////////////////////
int GiveMeTimestamp()
{
  unsigned long
timeout = millis();
 // WiFiClient client;
  while
(client.available() == 0)
  {
    if (millis() -
timeout > 50000)
    {
      client.stop();
      return 0;
    }
  }
while (client.available())
      {
        String line =
client.readStringUntil('\r');                   
//indexOf() is a funtion to search for smthng , it returns -1 if not
found
        int pos =
line.indexOf("\"timestamp\"");                       //search for
"\"timestamp\"" from beginning of response got and copy all
data after that , it'll be your timestamp
        if (pos >=
0)                                                    
        {
          int j = 0;
         
for(j=0;j<10;j++)
          {
           
timestamp[j] = line[pos + 12 + j];
          }
        }
      }
}  
////////////////////////////////////////////////////////////////////////////////////////////////////////
void setup()
{
   
Serial.begin(115200);
    delay(10);
     tzset();
    // We start by
connecting to a WiFi network
   
WiFiMulti.addAP("dlink_DWR-113", "madhukar66");
    Serial.println();
    Serial.println();
   
Serial.print("Wait for WiFi... ");
   
while(WiFiMulti.run() != WL_CONNECTED) {
       
Serial.print(".");
        delay(500);
    }
   
Serial.println("");
   
Serial.println("WiFi connected");
   
Serial.println("IP address: ");
   
Serial.println(WiFi.localIP());
    delay(500);
}
uint8_t temp_farenheit;
float temp_celsius;
char strftime_buf[64];
time_t now = 0;
struct tm timeinfo;
char buf[256];
void loop() 
{
 // int analogValue =
analogRead(outputpin);
{
/////////////////////////////////////// SEND THE QUERY AND
RECEIVE THE RESPONSE/////////////////////// 
//       
t1=(analogValue/1024.0) *5000;
//       
Serial.print("temp: ");
//        Serial.println(t1);
/
/        t2=(t1/10);
//       
Serial.print("celc: ");
//       
Serial.println(t2);
/
/        t3=((t2 *
9)/5 + 32);
//       
Serial.print("fhrn: ");
//       
Serial.println(t3);       
//break;
      
localtime_r(&now, &timeinfo);
   
strftime(strftime_buf, sizeof(strftime_buf), "%c",
&timeinfo);
   
sprintf(buf,"scan start %02d:%02d:%02d
",timeinfo.tm_hour,timeinfo.tm_min,timeinfo.tm_sec);
    Serial.print
(buf);
    temp_farenheit=
temprature_sens_read();
    temp_celsius = (
temp_farenheit - 32 ) / 1.8;
   
Serial.print("Temp onBoard ");
   
Serial.print(temp_farenheit);
   
Serial.print("°F ");
   
Serial.print(temp_celsius);
   
Serial.println("°C");
    delay(1000);
    now++;
  Serial.print("connecting
to ");
 
Serial.println(host);                          //defined upside :-
host = devapi2.thethingscloud.com or 139.59.26.117
///////////////////////////////////// TIMESTAMP CODE SNIPPET
/////////////////////////
Serial.println("inside get timestamp\n");
  if
(!client.connect(time_server, httpPort)) 
  {
    return;                                                       
//*-*-*-*-*-*-*-*-*-*
  }
 
client.println("GET /api/timestamp HTTP/1.1");                            //Whats this part
doing, i didnt get
 
client.println("Host: baas.thethingscloud.com");
 
client.println("Cache-Control: no-cache");
 
client.println("Postman-Token:
ea3c18c6-09ba-d049-ccf3-369a22a284b8");
  client.println();
GiveMeTimestamp();                        //it'll call the
function which will get the timestamp response from the server
Serial.println("timestamp receieved");
Serial.println(timestamp);
 
Serial.println("inside ThingsCloudPost");
  String PostValue =
"{\"device_id\": 61121695920, \"slave_id\": 2";
        PostValue =
PostValue + ",\"dts\":" +timestamp;
   PostValue =
PostValue +",\"data\":{\"TEMP\":" +
temp_farenheit +"}"+"}";
 
Serial.println(PostValue);
/* create an instance of WiFiClientSecure */
WiFiClientSecure client;
   
Serial.println("Connect to server via port 443");
    if
(!client.connect(server, 443)){
       
Serial.println("Connection failed!");
    } else {
       
Serial.println("Connected to server!");
        /* create HTTP
request */
       
client.println("POST /devices/deviceData HTTP/1.1");
       
client.println("Host: api.thingsai.io");
       
//client.println("Connection: close");
       
client.println("Content-Type: application/json");
       
client.println("cache-control: no-cache");
       
client.println("Authorization: Bearer
eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.IjVhMzBkZDFkN2QwYjNhNGQzODkwYzQ4OSI.kaY6OMj5cYlWNqC2PNTkXs9PKy6_m9tdW5AG7ajfVlY");
       
client.print("Content-Length: ");
       
client.println(PostValue.length());
       
client.println();
       
client.println(PostValue);
//////////////////////////////////POSTING the data on to the
cloud is done and now get the response form cloud server//////////////////
 Serial.print("Waiting
for response ");
        while
(!client.available()){
            delay(50);
//
           
Serial.print(".");
        }  
        /* if data is
available then receive and print to Terminal */
        while
(client.available()) {
            char c =
client.read();
           
Serial.write(c);
        }
        /* if the
server disconnected, stop the client */
        if
(!client.connected()) {
           
Serial.println();
           
Serial.println("Server disconnected");
            client.stop();
        }
    }
Serial.println("//////////////////////    THE END    
/////////////////////");
delay(3000);
}
}

Step 4: OUTPUT:

Here's the output of values of temperature read from the sensor.

Step 5: GRAPHS:

The graphical representation of the values read from the onboard temperature sensor.

Step 6: