first commit
This commit is contained in:
136
src/main.cpp
Normal file
136
src/main.cpp
Normal file
@@ -0,0 +1,136 @@
|
||||
#include <Arduino.h>
|
||||
#include <Wire.h>
|
||||
#include <HTTPClient.h>
|
||||
#include <MFRC522v2.h>
|
||||
#include <MFRC522DriverSPI.h>
|
||||
#include <MFRC522DriverPinSimple.h>
|
||||
#include <MFRC522Debug.h>
|
||||
#include <WiFi.h>
|
||||
#include <NTPClient.h>
|
||||
#include <WiFiUdp.h>
|
||||
|
||||
constexpr bool DEBUG = true;
|
||||
|
||||
// pins
|
||||
constexpr int redLedPin = 2;
|
||||
constexpr int greenLedPin = 4;
|
||||
constexpr int cardLedPin = 12;
|
||||
constexpr int buzzerPin = 13;
|
||||
|
||||
// wifi
|
||||
auto ssid = "ElaZereVsetko2g";
|
||||
auto password = "CoJeElaToJeEla";
|
||||
|
||||
// NTP client
|
||||
WiFiUDP ntpUDP;
|
||||
// Europe/Prague timezone and update every 60 seconds
|
||||
NTPClient timeClient(ntpUDP, "pool.ntp.org", 3600, 60000);
|
||||
|
||||
// vykradnuty kod netusim co to robi ale robi to
|
||||
MFRC522DriverPinSimple ss_pin(5); // Create digital pin driver
|
||||
MFRC522DriverSPI driver{ss_pin}; // Create SPI driver
|
||||
MFRC522 mfrc522{driver}; // Create MFRC522 instance
|
||||
|
||||
void debugPrint(String const& msg, const bool newLine = true) {
|
||||
if (DEBUG) {
|
||||
if (newLine) {
|
||||
Serial.println(msg);
|
||||
} else {
|
||||
Serial.print(msg);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void setup() {
|
||||
Serial.begin(115200);
|
||||
// cakaj na serial port
|
||||
while (!Serial) {
|
||||
delay(10);
|
||||
}
|
||||
|
||||
// init pins
|
||||
pinMode(redLedPin, OUTPUT);
|
||||
pinMode(greenLedPin, OUTPUT);
|
||||
pinMode(cardLedPin, OUTPUT);
|
||||
pinMode(buzzerPin, OUTPUT);
|
||||
|
||||
// pripoj sa na wifi
|
||||
debugPrint("Connecting to WiFi...");
|
||||
|
||||
WiFi.begin(ssid, password);
|
||||
while (WiFi.status() != WL_CONNECTED) {
|
||||
delay(1000);
|
||||
debugPrint(".", false);
|
||||
}
|
||||
if (DEBUG) {
|
||||
debugPrint(" connected!");
|
||||
}
|
||||
|
||||
// Turn off the red LED and turn on the green LED
|
||||
digitalWrite(redLedPin, LOW);
|
||||
digitalWrite(greenLedPin, HIGH);
|
||||
|
||||
// Initialize NTP client
|
||||
timeClient.begin();
|
||||
timeClient.update();
|
||||
|
||||
// Print the current time
|
||||
debugPrint("Current time: " + timeClient.getFormattedTime());
|
||||
|
||||
mfrc522.PCD_Init();
|
||||
|
||||
debugPrint("Cakam na kartu...");
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// Reset the loop if no new card present on the sensor/reader. This saves the entire process when idle.
|
||||
if (!mfrc522.PICC_IsNewCardPresent()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Select one of the cards.
|
||||
if (!mfrc522.PICC_ReadCardSerial()) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Get the current time in Unix time with microseconds
|
||||
timeClient.update();
|
||||
const unsigned long currentTime = timeClient.getEpochTime();
|
||||
const unsigned long microseconds = micros() % 1000000;
|
||||
|
||||
// Format the time with microseconds
|
||||
char timeWithMicroseconds[30];
|
||||
snprintf(timeWithMicroseconds, sizeof(timeWithMicroseconds), "%lu.%06lu", currentTime, microseconds);
|
||||
|
||||
// Format the UID
|
||||
String uidString = "";
|
||||
for (byte i = 0; i < mfrc522.uid.size; i++) {
|
||||
uidString += String(mfrc522.uid.uidByte[i], HEX);
|
||||
}
|
||||
|
||||
debugPrint("send: " + uidString);
|
||||
// Create the URL
|
||||
String url = "http://192.168.50.87:8111/api/timer/write/12345/" + uidString + "/" + String(timeWithMicroseconds) + "/";
|
||||
debugPrint("to: " + url);
|
||||
// Send the HTTP GET request
|
||||
HTTPClient http;
|
||||
http.begin(url);
|
||||
int httpResponseCode = http.GET();
|
||||
if (httpResponseCode > 0) {
|
||||
debugPrint("HTTP Response code: " + String(httpResponseCode));
|
||||
} else {
|
||||
debugPrint("Error on HTTP request");
|
||||
}
|
||||
http.end();
|
||||
|
||||
// Turn on the card LED and buzzer
|
||||
digitalWrite(cardLedPin, HIGH);
|
||||
tone(buzzerPin, 2000); // Generate 2000Hz tone
|
||||
delay(500); // Beep and light for 500 milliseconds
|
||||
digitalWrite(cardLedPin, LOW);
|
||||
noTone(buzzerPin); // Stop the tone
|
||||
|
||||
|
||||
delay(2000);
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user