#include <WiFi.h>
#include <HTTPClient.h>
#include <DHT.h>
#include <LiquidCrystal_I2C.h>
#include <ArduinoJson.h>
#include <Arduino.h>
#include "time.h"
#include <Keypad.h>
const byte ROWS = 4;
const byte COLS = 4;
char keys[ROWS][COLS] = {
{'1','2','3','A'},
{'4','5','6','B'},
{'7','8','9','C'},
{'*','0','#','D'}
};
byte rowPins[ROWS] = {14,27,26,25}; // Connect to the row pinouts of the keypad
byte colPins[COLS] = {33,32,35,34}; // Connect to the column pinouts of the keypad
Keypad keypad = Keypad(makeKeymap(keys), rowPins, colPins, ROWS, COLS);
LiquidCrystal_I2C LCD = LiquidCrystal_I2C(0x27, 16, 2);
#define DHT_PIN 2
DHT dht(DHT_PIN, DHT22);
unsigned long epochTime;
unsigned long dataMillis = 0;
WiFiClient client;
HTTPClient http;
const char* ssid = "Wokwi-GUEST";
const char* pass = "";
const char * ntpServer = "pool.ntp.org";
const char* serverName = "https://us-east-1.aws.data.mongodb-api.com/app/application-0-vqinw/endpoint/sensor";
StaticJsonDocument<500> doc;
void setup()
{
Serial.begin(115200);
dht.begin();
WiFi.begin(ssid, pass);
Serial.print("Connecting to Wi-Fi");
LCD.init();
LCD.backlight();
LCD.setCursor(0, 0);
LCD.print("Connecting wifi ");
while (WiFi.status() != WL_CONNECTED)
{
Serial.print(".");
delay(300);
}
Serial.println();
Serial.print("Connected with IP: ");
LCD.setCursor(0, 1);
LCD.print("WiFi connected ");
Serial.println(WiFi.localIP());
Serial.println();
configTime(0, 0, ntpServer);
}
void loop()
{
char key = keypad.getKey();
if (key) {
// Handle the keypress
switch(key) {
case '1':
Serial.println("post");
POSTData();
break;
case '2':
Serial.println("get");
getData();
break;
// Add more cases for other keys
}
}
delay(100);
}
// Function that gets current epoch time
unsigned long getTime() {
time_t now;
struct tm timeinfo;
if (!getLocalTime(&timeinfo)) {
return(0);
}
time(&now);
return now;
}
void POSTData()
{
if (millis() - dataMillis > 15000 || dataMillis == 0)
{
dataMillis = millis();
epochTime = getTime();
Serial.print("Epoch Time: ");
Serial.println(epochTime);
float temperature = dht.readTemperature();
float humidity = dht.readHumidity();
LCD.setCursor(0, 0);
LCD.print("Humidity: ");
LCD.print(humidity);
LCD.print("% ");
LCD.setCursor(0, 1);
LCD.print("Temp: ");
LCD.print(temperature);
LCD.print("C ");
Serial.print("Temperature: ");
Serial.print(String(temperature));
Serial.print(" C\nHumidity: ");
Serial.print(String(humidity));
doc["sensors"]["temperature"] = temperature;
doc["sensors"]["humidity"] = humidity;
doc["sensors"]["timestamp"] = epochTime;
Serial.println("Uploading data... ");
}
if(WiFi.status()== WL_CONNECTED){
HTTPClient http;
http.begin(serverName);
http.addHeader("Content-Type", "application/json");
http.addHeader("Accept", "application/json");
// http.GET();
http.addHeader("X-Forwarded-Proto", "http");
http.addHeader("apiKey", "Mk3zDQj9hna4tDj7ykSgaKWMv7h34ik3mc6smpYTQYK7c4WTV9lIBOhPjvmeL5PM");
String json;
serializeJson(doc, json);
Serial.println(json);
int httpResponseCode = http.POST( json);
Serial.println(httpResponseCode);
//int receive = http.GET();
if (httpResponseCode == 200) {
Serial.println("Data uploaded.");
delay(100);
} else {
Serial.println("ERROR: Couldn't upload data.");
delay(100);
}
}
}
void getData() {
HTTPClient http;
String id = "654dabb06bd319918676a20f";
String url = "https://us-east-1.aws.data.mongodb-api.com/app/application-0-vqinw/endpoint/MQ?id=" + id;
http.begin(url);
// Gửi yêu cầu GET và đợi phản hồi
int httpResponseCode = http.GET();
// DynamicJsonDocument doc(1024);
if (httpResponseCode == HTTP_CODE_OK) {
// Đọc dữ liệu từ phản hồi
String response = http.getString();
// Xử lý dữ liệu nhận được
Serial.println("Received data:");
Serial.println(response);
} else {
Serial.print("Error accessing URL. HTTP Response code: ");
Serial.println(httpResponseCode);
}
http.end();
}