LDR Using Thingsai Cloud

HEY.....today we are gonna learn about measuring the intensity of light and posting the values into the THINGSAI IOT cloud platform by using ESP32 .

The requirements to do this project are

1. ESP32 Development Board ( I've used ESP32 DEVKIT V1)

2. LDR sensor

3. Jumper Wires

4. Account in THINGSAI IOT PLATFORM

Step 1: Creating and Logging in to the THINGSIO Platform

Login to the THINGS AI account. If you are new then register to the account by pressing the register button and fill all the credentials. Your account will be created and from then you will be able to work on the cloud platform and create your custom project

Step 2: Creating New Project

After logging in to the account , to create a project just click on the new project and then give the project name.

Step 3: Creating a New Device

after creating the project the next thing you have to do is create a new device.Give the device name and enter the device ID either manually or system generated.

Step 4: Defining Device Parameter

Give the device parameter and then select the type of parameter

Step 5: Updating the Device

Select the parameter and then update the device

Step 6: Coding

From the sample codes select the code of esp32 copy it and then paste it in the arduino IDE and make the necessary changes according to the requirement. I've given the code below

#include <Wifi.h>
#include<WifiMulti.h>

#include<WifiClientSecure.h>

int count=0,i,m,j,k;

int t ; int outputpin= A0;//ds18b20

int sensorvalue;

//////////////////////////////////////// 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);

// We start by connecting to a WiFi network

WiFiMulti.addAP("wifi", "pswrd");

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);

}

void loop()

{

int analogValue = analogRead(outputpin);

{ /////////////////////////////////////// SEND THE QUERY AND RECEIVE THE RESPONSE///////////////////////

sensorvalue = analogRead(A0); // read analog input pin 0

sensorvalue=sensorvalue/100;

Serial.print(sensorvalue, DEC); // prints the value read

Serial.print(" \n"); // prints a space between the numbers

delay(1000); // wait 100ms for next reading

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\": 61121695844, \"slave_id\": 2";

PostValue = PostValue + ",\"dts\":" +timestamp;

PostValue = PostValue +",\"data\":{\"INTENSITY\":" + \sensorvalue +"}"+"}";

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"); cl

ient.println("Content-Type: application/json");

client.println("cache-control: no-cache");

client.println("Authorization: BearereyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.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 7: Selection of Board and Com Port

From tools select the board and then select the com port

Step 8: Circuit Connections

Coding is done then make the following connections as mentioned below

CONNECTIONS:

GND of esp32 to GND of the LDR sensor

3V3 0f esp32 to the Vcc of the LDR

VP of esp32 to the A0 of the LDR

Step 9: Compile and Upload

compile and upload the code into the esp32 and then read the readings from the serial monitor. That would show a result something like this

Step 10: Serial Monitor

The values are obtained on the serial monitor and then they are sent into the THINGSAI IOT Cloud platform.

Step 11: Readings

This shows the values obtained from the esp32 board.

Step 12: Graphical Representation

This is the graphical representation of the obtained values . That's the end of the tutorial. Hope you've understood. Thank You

Step 13:

Share

    Recommendations

    • Epilog X Contest

      Epilog X Contest
    • Organization Contest

      Organization Contest
    • Faux-Real Contest

      Faux-Real Contest

    Discussions

    0
    None

    Great first Instructable. Thanks for sharing with the community.