Lê Đức Thiện
MSSV: 20242201M
a) Gửi dữ liệu HTTP get request, dữ liệu đóng gói url-encoded
Code:
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
String serverName = "[Link] // Use the GET
endpoint
// Pin Definitions
#define LED_PIN 14
#define DHT_PIN 4
#define PIR_SENSOR 13
// LCD setup (I2C address, 20 columns, 4 rows)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// DHT Sensor setup
DHTesp dhtSensor;
void setup() {
// Setup for LED
pinMode(LED_PIN, OUTPUT);
// Setup for Serial communication
[Link](9600);
[Link]("ESP32 collecting sensors data");
[Link]("Connecting to WiFi");
// Setup for WiFi connection
[Link]("Wokwi-GUEST", "", 6);
while ([Link]() != WL_CONNECTED) {
delay(100);
[Link](".");
}
[Link]("WiFi Connected!");
// Setup for DHT sensor
[Link](DHT_PIN, DHTesp::DHT22);
// Setup for PIR sensor
pinMode(PIR_SENSOR, INPUT);
// LCD initialization
[Link]();
[Link]();
[Link](1, 0);
[Link]("ESP32 collecting data ...");
delay(1000);
}
void loop() {
// Read temperature and humidity data
TempAndHumidity data = [Link]();
int temp = [Link];
int humid = [Link];
String stemp = String(temp) + "C";
String shumid = String(humid) + "%";
// Display temperature and humidity on the LCD
[Link]();
[Link](0, 0);
[Link]("Temp: " + stemp);
[Link](0, 1);
[Link]("Humidity: " + shumid);
delay(1000);
if ([Link]() == WL_CONNECTED) {
HTTPClient http;
float x = temp; // Get temperature value
float y = humid; // Get humidity value
// Construct URL with query parameters
String url = serverName + "?temp=" + String(x) + "&humid=" +
String(y);
[Link](url);
[Link](url); // Make GET request to the constructed URL
int httpResponseCode = [Link](); // Use GET instead of POST
if (httpResponseCode > 0) {
[Link]("HTTP Response code: ");
[Link](httpResponseCode);
String payload = [Link]();
[Link](payload);
}
else {
[Link]("Error code: "); [Link](httpResponseCode);
}
[Link]();
}
else {
[Link]("WiFi Disconnected");
}
delay(3000); // Wait for 3 seconds before sending again
// PIR sensor: Detect motion
int pir_value = digitalRead(PIR_SENSOR);
if (pir_value == 1) {
[Link]("Motion detected");
} else {
[Link]("Motion ended");
}
// Control LED and print status to Serial Monitor
digitalWrite(LED_PIN, HIGH);
[Link]("LED ON");
delay(500);
digitalWrite(LED_PIN, LOW);
[Link]("LED OFF");
delay(500);
}
Link wokwi: [Link]
b) Gửi dữ liệu HTTP POST request, dữ liệu đóng gói url-encoded
Code:
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
String serverName = "[Link]
// Pin Definitions
#define LED_PIN 14
#define DHT_PIN 4
#define PIR_SENSOR 13
// LCD setup (I2C address, 20 columns, 4 rows)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// DHT Sensor setup
DHTesp dhtSensor;
void setup() {
// Setup for LED
pinMode(LED_PIN, OUTPUT);
// Setup for Serial communication
[Link](9600);
[Link]("ESP32 collecting sensors data");
[Link]("Connecting to WiFi");
//setup for WiFi connection
[Link]("Wokwi-GUEST", "", 6);
while ([Link]() != WL_CONNECTED) {
delay(100);
[Link](".");
}
[Link]("WiFi Connected!");
// Setup for DHT sensor
[Link](DHT_PIN, DHTesp::DHT22);
// Setup for PIR sensor
pinMode(PIR_SENSOR, INPUT);
// LCD initialization
[Link]();
[Link]();
[Link](1, 0);
[Link]("ESP32 collecting data ...");
delay(1000);
}
void loop() {
// Read temperature and humidity data
TempAndHumidity data = [Link]();
int temp = [Link];
int humid = [Link];
String stemp = String(temp) + "C";
String shumid = String(humid) + "%";
// Display temperature and humidity on the LCD
[Link]();
[Link](0, 0);
[Link]("Temp: " + stemp);
[Link](0, 1);
[Link]("Humidity: " + shumid);
delay(1000);
if ([Link]() == WL_CONNECTED) {
HTTPClient http;
float x = temp; //get temperature value
float y = humid; //get humidity value
[Link](serverName);
[Link]("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "&temp=" + String(x) + "&humid=" +
String(y);
[Link](httpRequestData);
int httpResponseCode = [Link](httpRequestData);
if (httpResponseCode > 0) {
[Link]("HTTP Response code: ");
[Link](httpResponseCode);
String payload = [Link]();
[Link](payload);
}
else {
[Link]("Error code: "); [Link](httpResponseCode);
}
[Link]();
}
else {
[Link]("WiFi Disconnected");
}
delay(3000);
// PIR sensor: Detect motion
int pir_value = digitalRead(PIR_SENSOR);
if (pir_value == 1) {
[Link]("Motion detected");
} else {
[Link]("Motion ended");
}
// Control LED and print status to Serial Monitor
digitalWrite(LED_PIN, HIGH);
[Link]("LED ON");
delay(500);
digitalWrite(LED_PIN, LOW);
[Link]("LED OFF");
delay(500);
}
Link wokwi: [Link]
c) Gửi dữ liệu qua HTTP POST request, dữ liệu đóng gói json trong body request
Code:
#include <LiquidCrystal_I2C.h>
#include "DHTesp.h"
#include <WiFi.h>
#include <HTTPClient.h>
String serverName = "[Link]
// Pin Definitions
#define LED_PIN 14
#define DHT_PIN 4
#define PIR_SENSOR 13
// LCD setup (I2C address, 20 columns, 4 rows)
LiquidCrystal_I2C lcd(0x27, 20, 4);
// DHT Sensor setup
DHTesp dhtSensor;
void setup() {
// Setup for LED
pinMode(LED_PIN, OUTPUT);
// Setup for Serial communication
[Link](9600);
[Link]("ESP32 collecting sensors data");
[Link]("Connecting to WiFi");
//setup for WiFi connection
[Link]("Wokwi-GUEST", "", 6);
while ([Link]() != WL_CONNECTED) {
delay(100);
[Link](".");
}
[Link]("WiFi Connected!");
// Setup for DHT sensor
[Link](DHT_PIN, DHTesp::DHT22);
// Setup for PIR sensor
pinMode(PIR_SENSOR, INPUT);
// LCD initialization
[Link]();
[Link]();
[Link](1, 0);
[Link]("ESP32 collecting data ...");
delay(1000);
}
void loop() {
// Read temperature and humidity data
TempAndHumidity data = [Link]();
int temp = [Link];
int humid = [Link];
String stemp = String(temp) + "C";
String shumid = String(humid) + "%";
// Display temperature and humidity on the LCD
[Link]();
[Link](0, 0);
[Link]("Temp: " + stemp);
[Link](0, 1);
[Link]("Humidity: " + shumid);
delay(1000);
if ([Link]() == WL_CONNECTED) {
HTTPClient http;
float x = temp; //get temperature value
float y = humid; //get humidity value
[Link](serverName);
[Link]("Content-Type", "application/json");
String httpRequestData = "{\"temp\": " + String(x) +",\"humid\": " +
String(y) + "}";
[Link](httpRequestData);
int httpResponseCode = [Link](httpRequestData);
if (httpResponseCode > 0) {
[Link]("HTTP Response code: ");
[Link](httpResponseCode);
String payload = [Link]();
[Link](payload);
}
else {
[Link]("Error code: "); [Link](httpResponseCode);
}
[Link]();
}
else {
[Link]("WiFi Disconnected");
}
delay(3000);
// PIR sensor: Detect motion
int pir_value = digitalRead(PIR_SENSOR);
if (pir_value == 1) {
[Link]("Motion detected");
} else {
[Link]("Motion ended");
}
// Control LED and print status to Serial Monitor
digitalWrite(LED_PIN, HIGH);
[Link]("LED ON");
delay(500);
digitalWrite(LED_PIN, LOW);
[Link]("LED OFF");
delay(500);
Link Wokwi: [Link]