Professional Documents
Culture Documents
Báo cáo Bai5. ESP32 gửi dữ liệu HTTP
Báo cáo Bai5. ESP32 gửi dữ liệu HTTP
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/
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>
#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);
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) + "%";
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);
}
#include <WiFi.h>
#include <HTTPClient.h>
#include <Wire.h>
#include "DHT.h"
#include <LiquidCrystal_I2C.h>
#include <PubSubClient.h>
WiFiClient espClient;
PubSubClient client(espClient);
#define LED_PIN 33
#define DHT_PIN 17 // DHT sensor connecting to GPIO17
5
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");
void setup() {
//setup for serial communication
Serial.begin(9600);
setupWiFi();
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) + "%";
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>
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");
void setup() {
//setup for serial communication
Serial.begin(9600);
setupWiFi();
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
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);
}