/* @Title: Doorbell Project
* @Members: Leyte,
* @Date 05-16-24 05:00:36 GMT +08:00
* @Description: Gi Turbo ni Paklay 0w0 mga kadlawon, erase lang when done
* Sayu kayo sla ning chat, sayu pas kabuntagon hahhahah
*/
// ERROR STATUS GUIDE
//#define HTTPC_ERROR_CONNECTION_REFUSED (-1)
//#define HTTPC_ERROR_SEND_HEADER_FAILED (-2)
//#define HTTPC_ERROR_SEND_PAYLOAD_FAILED (-3)
//#define HTTPC_ERROR_NOT_CONNECTED (-4)
//#define HTTPC_ERROR_CONNECTION_LOST (-5)
//#define HTTPC_ERROR_NO_STREAM (-6)
//#define HTTPC_ERROR_NO_HTTP_SERVER (-7)
//#define HTTPC_ERROR_TOO_LESS_RAM (-8)
//#define HTTPC_ERROR_ENCODING (-9)
//#define HTTPC_ERROR_STREAM_WRITE (-10)
//#define HTTPC_ERROR_READ_TIMEOUT (-11)
#include <WiFi.h>
#include <WiFiClient.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
// URL
const String serverUrl = "https://ap-southeast-1.aws.data.mongodb-api.com/app/data-uxjtwvh/endpoint/data/v1/action/insertOne";
const String apiKey = "OtE9RMH4EdXpMVVQHhNJh9LMpsOLogNQ4kpyzMiGAgKI35LLxVgbnAStaQUIr0Pr";
//DATABASE CREDENTIALS
const String database = "esp_doorbell"; //db
const String collection = "passerby"; // tbl
const String dataSource = "espcluster"; //tbl
//WIFI CREDENTIALS
#define _SSID "Wokwi-GUEST"
#define _PASS ""
// BUTTON SETTINGS
#define DEBOUNCE_DELAY 100
const int BTN_PIN = 2;
int prevBtnState = HIGH;
unsigned long lastDebounceTime = 0;
int pushedTimes = 0;
// BUZZER
const int BUZZ_PIN = 4;
WiFiClient client;
HTTPClient http;
void setup ()
{
Serial.begin(115200);
pinMode(BTN_PIN, INPUT_PULLUP);
pinMode(BUZZ_PIN, OUTPUT);
WiFi.mode(WIFI_STA);
WiFi.begin(_SSID, _PASS);
while (WiFi.status() != WL_CONNECTED)
{
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.print("Connected to Wi-Fi network with IP Address: ");
Serial.println(WiFi.localIP());
http.setReuse(true);
}
void loop()
{
int btnRead = digitalRead(BTN_PIN);
unsigned long currTime = millis();
if (btnRead == LOW && prevBtnState == HIGH)
{
if ((currTime - lastDebounceTime) >= DEBOUNCE_DELAY)
{
pushedTimes++;
sendOnButtonPress(String(pushedTimes));
digitalWrite(BUZZ_PIN, HIGH); // Buzz kung button is pressed
delay(300);
digitalWrite(BUZZ_PIN, LOW);
lastDebounceTime = currTime;
}
}
prevBtnState = btnRead;
delay(10);
}
void sendOnButtonPress(const String& notice)
{
http.begin(serverUrl);
http.addHeader("Connection", "Keep-Alive");
http.addHeader("Content-Type", "application/json");
http.addHeader("Access-Control-Request-Headers", "*");
http.addHeader("api-key", apiKey);
StaticJsonDocument<200> doc;
doc["collection"] = collection;
doc["database"] = database;
doc["dataSource"] = dataSource;
JsonObject document = doc.createNestedObject("document");
document["pressedTimes"] = notice;
String payload;
serializeJson(doc, payload);
int httpResponseCode = http.POST(payload);
if (httpResponseCode > 0)
{
Serial.println("Successfully received data from server.");
String response = http.getString();
Serial.println("Response: ");
Serial.println(response);
Serial.println(payload);
} else {
Serial.print("Error code: ");
Serial.println(httpResponseCode);
}
http.end();
}