Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 10

1

Báo Cáo Bài 5. ESP32 gửi dữ liệu HTTP request


Vũ Duy Hiệp – 20194279
Mã lớp: 144947 - IT4735

Mục đích:
Lập trình cho ESP32 gửi dữ liệu (nhiệt độ, độ ẩm như đã thu thập ở bài 4) lên server
Sử dụng công cụ mô phỏng wokwi https://wokwi.com/

Hoạt động của chương trình:


a) Gửi dữ liệu HTTP get request, dữ liệu đóng gói url-encoded
b) Gửi dữ liệu HTTP post request, dữ liệu đóng gói url-encoded
c) Gửi dữ liệu HTTP post request, dữ liệu đóng gói json (trong request body)
Sử dụng server có sẵn:
https://postman-echo.com/get

https://postman-echo.com/post

Kết quả
a) Gửi dữ liệu HTTP get request, dữ liệu đóng gói url-encoded

#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
2

#include "DHT.h"
#include <LiquidCrystal_I2C.h>

String serverName = "https://postman-echo.com/get";

#define LED_PIN 33
#define DHT_PIN 17 // DHT sensor connecting to GPIO17
#define PIR_SENSOR 13 // PIR sensor connecting to GPIO13
DHT dhtSensor(DHT_PIN, DHT22);

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars
and 2 line display

void setup() {
//setup for serial communication
Serial.begin(9600);

dhtSensor.begin(); // Initialize DHT sensor


pinMode(PIR_SENSOR, INPUT); // Set PIR sensor as an input data source for ESP32
pinMode(DHT_PIN, INPUT); // Set PIR sensor as an input data source for ESP32
pinMode(LED_PIN, OUTPUT);

lcd.init(); // initialize the lcd


// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1, 0);
Serial.print("Connecting to WiFi");
//setup for WiFi connection
WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("WiFi Connected!");
}

void loop() {
int temp = dhtSensor.readTemperature(); // Read temperature data from dht sensor
int humid = dhtSensor.readHumidity(); // Read humidity data from dht sensor
String str_temp = String(temp) + "C";
String str_humid = String(temp) + "%";

lcd.clear(); // Clean content on LCD screen


lcd.setCursor(0, 0); // Set cursor to line 1
lcd.print("Temp: " + str_temp); // Print temperature data on LCD screen
3

lcd.setCursor(0, 1); // Set cursor to line 2


lcd.print("Humidity: " + str_humid); // Print humidity data on LCD screen

String msg = "1{\"temp\": " + str_temp + ",\"humid\": " + str_humid + "}";

Serial.print("Publish message: ");


Serial.println(msg.c_str());
HTTPClient http;
String serverPath = serverName + "?temp=" + String(temp) +
"&humid="+String(temp);
http.begin(serverPath.c_str()); // Send HTTP GET request
int httpResponseCode = http.GET();

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

Link Wokwin demo: https://wokwi.com/projects/382995790214109185


Hình ảnh kết quả:
4

b) Gửi dữ liệu HTTP POST request, dữ liệu đóng gói url-encoded

#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#include <PubSubClient.h>

String serverName = "https://postman-echo.com/post";

WiFiClient espClient;
PubSubClient client(espClient);

#define LED_PIN 33
#define DHT_PIN 17 // DHT sensor connecting to GPIO17
5

#define PIR_SENSOR 13 // PIR sensor connecting to GPIO13


DHT dhtSensor(DHT_PIN, DHT22);

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars
and 2 line display

void setupWiFi() {
Serial.print("Connecting to WiFi");

//setup for WiFi connection


WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("WiFi Connected!");
}

void setup() {
//setup for serial communication
Serial.begin(9600);

setupWiFi();

dhtSensor.begin(); // Initialize DHT sensor


pinMode(PIR_SENSOR, INPUT); // Set PIR sensor as an input data source for ESP32
pinMode(DHT_PIN, INPUT); // Set PIR sensor as an input data source for ESP32
pinMode(LED_PIN, OUTPUT);
lcd.init(); // initialize the lcd
// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1, 0);
}

void loop() {
if (WiFi.status() == WL_CONNECTED) {
int temp = dhtSensor.readTemperature(); // Read temperature data from dht
sensor
int humid = dhtSensor.readHumidity(); // Read humidity data from dht sensor
String str_temp = String(temp) + "C";
String str_humid = String(humid) + "%";

lcd.clear(); // Clean content on LCD screen


lcd.setCursor(0, 0); // Set cursor to line 1
lcd.print("Temp: " + str_temp); // Print temperature data on LCD screen
6

lcd.setCursor(0, 1); // Set cursor to line 2


lcd.print("Humidity: " + str_humid); // Print humidity data on LCD screen

String msg = "1{\"temp\": " + str_temp + ",\"humid\": " + str_humid + "}";

Serial.print("Publish message: ");


Serial.println(msg.c_str());
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/x-www-form-urlencoded");
String httpRequestData = "&temp=" + String(temp) + "&humid=" + String(humid);
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);
if (httpResponseCode > 0) {
Serial.print("HTTP Response code: "); Serial.println(httpResponseCode);
String payload = http.getString();
Serial.println(payload);
}
else {
Serial.print("Error code: "); Serial.println(httpResponseCode);
}
http.end();
} else {
Serial.println("WiFi Disconnected");
}
delay(3000);
}

Link wokwi demo: https://wokwi.com/projects/382997799862546433


7

Hình ảnh kết quả:

c) Gửi dữ liệu qua HTTP POST request, dữ liệu đóng gói json trong body request

#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#include <PubSubClient.h>
#include <ArduinoJson.h>

String serverName = "https://postman-echo.com/post";

WiFiClient espClient;
PubSubClient client(espClient);
8

#define LED_PIN 33
#define DHT_PIN 17 // DHT sensor connecting to GPIO17
#define PIR_SENSOR 13 // PIR sensor connecting to GPIO13
DHT dhtSensor(DHT_PIN, DHT22);

LiquidCrystal_I2C lcd(0x27, 20, 4); // set the LCD address to 0x27 for a 16 chars
and 2 line display
void setupWiFi() {
Serial.print("Connecting to WiFi");

//setup for WiFi connection


WiFi.begin("Wokwi-GUEST", "", 6);
while (WiFi.status() != WL_CONNECTED) {
delay(100);
Serial.print(".");
}
Serial.println("WiFi Connected!");
}

void setup() {
//setup for serial communication
Serial.begin(9600);

setupWiFi();

dhtSensor.begin(); // Initialize DHT sensor


pinMode(PIR_SENSOR, INPUT); // Set PIR sensor as an input data source for ESP32
pinMode(DHT_PIN, INPUT); // Set PIR sensor as an input data source for ESP32
pinMode(LED_PIN, OUTPUT);

lcd.init(); // initialize the lcd


// Print a message to the LCD.
lcd.backlight();
lcd.setCursor(1, 0);
}

void loop() {

if (WiFi.status() == WL_CONNECTED) {
int temp = dhtSensor.readTemperature(); // Read temperature data from dht
sensor
int humid = dhtSensor.readHumidity(); // Read humidity data from dht sensor
String str_temp = String(temp) + "C";
9

String str_humid = String(humid) + "%";

lcd.clear(); // Clean content on LCD screen


lcd.setCursor(0, 0); // Set cursor to line 1
lcd.print("Temp: " + str_temp); // Print temperature data on LCD screen
lcd.setCursor(0, 1); // Set cursor to line 2
lcd.print("Humidity: " + str_humid); // Print humidity data on LCD screen

String msg = "1{\"temp\": " + str_temp + ",\"humid\": " + str_humid + "}";

Serial.print("Publish message: ");


Serial.println(msg.c_str());
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/json");
DynamicJsonDocument doc(1024);
String jsonstr;
JsonObject root = doc.to<JsonObject>();
root["temperature"] = temp;
root["humidity"] = humid;
serializeJson(doc, jsonstr);
String httpRequestData = jsonstr;
Serial.println(httpRequestData);
int httpResponseCode = http.POST(httpRequestData);

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

} else {
Serial.println("WiFi Disconnected");
}
delay(3000);
}

Link wokwi demo: https://wokwi.com/projects/382999030518591489


Kết quả:
10

You might also like