0% found this document useful (0 votes)
1 views6 pages

Experiment 10

Uploaded by

om raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views6 pages

Experiment 10

Uploaded by

om raj
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

To Designing Weather station by HTTP, GET REQUEST-RESPONSE using NodeMCU

SOFTWARES USED 2210040117


OM RAJ
ESP8266 BOARD (Arduino IDE)
APPARATUS/ COMPONENTS REQUIRED

1. ESP8266
2. Connecting Cables
3. DC 5V, 1A Power Adaptor

THEORY:
The Hypertext Transfer Protocol (HTTP) is designed to enable communications between clients
and servers. HTTP works as a request-response protocol between a client and server. Each
Hypertext Transfer Protocol (HTTP) message is either a request or a response. A server listens
on a connection for a request, parses each message received, interprets the message semantics in
relation to the identified request target, and responds to that request with one or more response
messages. A client constructs request messages to communicate specific intentions, examines
received responses to see if the intentions were carried out, and determines how to interpret the
results. A web browser may be the client, and an application on a computer that hosts a web site
may be the server.
Example: A client (browser) submits an HTTP request to the server; then the server returns a
response to the client. The response contains status information about the request and may also
contain the requested content
Two commonly used methods for a request-response between a client and server are: GET and
POST.
 GET – Requests data from a specified resource
 POST – Submits data to be processed to a specified resource

HARDWARE CONNECTION:
OpenWeather platform is a set of elegant and widely recognisable APIs. Powered by
convolutional machine learning solutions, it is capable of delivering all the weather information
necessary for decision-making for any location on the globe to start using our APIs, please sign
up here https://home.openweathermap.org/users/sign_in
CODE/PROGRAM:-

#include <ESP8266WiFi.h>
#include <ESP8266HTTPClient.h>
#include <WiFiClient.h>
#include <Arduino_JSON.h>

const char* ssid = "YOUR SSID";


const char* password = "YOUR PASSWRD";

// Your Domain name with URL path or IP address with path


String openWeatherMapApiKey = "YOUR OPEN WEATHER MAP KEY";
// Example:
//String openWeatherMapApiKey = "bd939aa3d23ff33d3c8f5dd1dd4";

// Replace with your country code and city


String city = "Vijayawada";
String countryCode = "IN";

// THE DEFAULT TIMER IS SET TO 10 SECONDS FOR TESTING PURPOSES


// For a final application, check the API call limits per hour/minute to avoid getting
blocked/banned
unsigned long lastTime = 0;
// Timer set to 10 minutes (600000)
//unsigned long timerDelay = 600000;
// Set timer to 10 seconds (10000)
unsigned long timerDelay = 10000;

String jsonBuffer;

void setup() {
Serial.begin(115200);

WiFi.begin(ssid, password);
Serial.println("Connecting");
while(WiFi.status() != WL_CONNECTED) {
delay(500);
Serial.print(".");
}
Serial.println("");
Serial.print("Connected to WiFi network with IP Address: ");
Serial.println(WiFi.localIP());

Serial.println("Timer set to 10 seconds (timerDelay variable), it will take 10 seconds before


publishing the first reading.");
}

void loop() {
// Send an HTTP GET request
if ((millis() - lastTime) > timerDelay) {
// Check WiFi connection status
if(WiFi.status()== WL_CONNECTED){
String serverPath = "http://api.openweathermap.org/data/2.5/weather?q=" + city + "," +
countryCode + "&APPID=" + openWeatherMapApiKey;

jsonBuffer = httpGETRequest(serverPath.c_str());
Serial.println(jsonBuffer);
JSONVar myObject = JSON.parse(jsonBuffer);

// JSON.typeof(jsonVar) can be used to get the type of the var


if (JSON.typeof(myObject) == "undefined") {
Serial.println("Parsing input failed!");
return;
}

Serial.print("JSON object = ");


Serial.println(myObject);
Serial.print("Temperature: ");
Serial.println(myObject["main"]["temp"]);
Serial.print("Pressure: ");
Serial.println(myObject["main"]["pressure"]);
Serial.print("Humidity: ");
Serial.println(myObject["main"]["humidity"]);
Serial.print("Wind Speed: ");
Serial.println(myObject["wind"]["speed"]);
}
else {
Serial.println("WiFi Disconnected");
}
lastTime = millis();
}
}

String httpGETRequest(const char* serverName) {


WiFiClient client;
HTTPClient http;

// Your IP address with path or Domain name with URL path


http.begin(client, serverName);

// Send HTTP POST request


int httpResponseCode = http.GET();

String payload = "{}";

if (httpResponseCode>0) {
Serial.print("HTTP Response code: ");
Serial.println(httpResponseCode);
payload = http.getString();
}
else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
// Free resources
http.end();

return payload;
}
PRECAUTIONS
1. Use insulation tools while you connect the Circuit.
2. Wear Good Insulating Shoes.
3. Avoid contacting circuits with wet hands or wet materials.
4. Maintain a work space clear of extraneous material such as books, papers, and clothes.
5. Be careful while connecting any actuators in 230V AC Mains.

Results

The NodeMCU was programmed using the Arduino IDE to establish communication with
weather APIs through HTTP GET requests. The station effectively retrieved weather data such
as temperature, humidity, and atmospheric pressure from external sources.

OUTPUT :

You might also like