From 1b9da2fe8bd20fe321f86daa394391e9e40f24d5 Mon Sep 17 00:00:00 2001 From: Aaron Lee Date: Mon, 1 Jan 2024 00:34:45 +0800 Subject: [PATCH] add arduino --- arduino/SavageTracker/SavageTracker.ino | 172 ++++++++++++++++++++++++ 1 file changed, 172 insertions(+) create mode 100644 arduino/SavageTracker/SavageTracker.ino diff --git a/arduino/SavageTracker/SavageTracker.ino b/arduino/SavageTracker/SavageTracker.ino new file mode 100644 index 0000000..4310f0f --- /dev/null +++ b/arduino/SavageTracker/SavageTracker.ino @@ -0,0 +1,172 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +LiquidCrystal_I2C lcd(0x27, 16, 2); // set the LCD address to 0x27 for a 16 chars and 2 line display + +#define SS_PIN D8 +#define RST_PIN D3 +#define BEEPER_PIN D0 + +#define WIFI_SSID "SEA" +#define WIFI_PASS "sara9914" +#define RECURL "http://192.168.0.11:5000/api/rfid" +#define KEY "secret" + +MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class + +bool processingReq = false; +WiFiClient wifiClient; + +int cursorPos = 0; + +void setup() { + Serial.begin(9600); + + lcd.init(); + lcd.backlight(); + lcdOut(0, "Savage Tumaz"); + + lcdOut("init SPI..."); + SPI.begin(); // Init SPI bus + + lcdOut("init MFRC522..."); + rfid.PCD_SetAntennaGain(255); + rfid.PCD_Init(); // Init MFRC522 + lcd.setCursor(14, 1); + lcd.print("ok"); + + pinMode(BEEPER_PIN, OUTPUT); + + connectWifi(); + + delay(1500); + + lcdOut("Please Scan"); +} + +void loop() { + if (processingReq || !rfid.PICC_IsNewCardPresent() || !rfid.PICC_ReadCardSerial()) { + return; + } + + char uid[8]; + sprintf(uid, "%02X%02X%02X%02X", rfid.uid.uidByte[0], rfid.uid.uidByte[1], rfid.uid.uidByte[2], rfid.uid.uidByte[3]); + Serial.println(uid); + + rfid.PICC_HaltA(); + rfid.PCD_StopCrypto1(); + + digitalWrite(BEEPER_PIN, HIGH); + delay(50); + digitalWrite(BEEPER_PIN, LOW); + + lcdOut(0, uid); + lcdOut("Loading..."); + + DynamicJsonDocument body(200); + body["uid"] = uid; + body["key"] = KEY; + char bodych[1024]; + serializeJson(body, bodych); + Serial.println(bodych); + + HTTPClient httpClient; + httpClient.useHTTP10(true); + httpClient.begin(wifiClient, RECURL); + httpClient.addHeader("Content-Type", "application/json"); + Serial.print("RECURL: "); + Serial.println(RECURL); + + int httpCode = httpClient.POST(bodych); + Serial.print("Send POST request to RECURL: "); + Serial.println(RECURL); + + if (httpCode == HTTP_CODE_OK) { + DynamicJsonDocument doc(500); + deserializeJson(doc, httpClient.getStream()); + httpClient.end(); + + String tmpstr; + serializeJson(doc, tmpstr); + Serial.println(tmpstr); + + lcdOut(0, strdup(doc["status"])); + lcd.print(":"); + lcd.print(strdup(doc["name"])); + successTone(); + + lcdOut("Please Scan"); + } else { + Serial.println("Server Respose Code:"); + Serial.println(httpCode); + lcdOut(0, "ERROR "); + lcd.print(httpCode); + if (httpCode > 0) { + DynamicJsonDocument doc(500); + deserializeJson(doc, httpClient.getStream()); + lcdOut(strdup(doc["status"])); + } else lcdOut(httpClient.errorToString(httpCode).c_str()); + errTone(); + delay(2000); + lcdOut("Please Scan"); + } +} + +void lcdOut(const char* s) { + lcdOut(strdup(s)); +} + +void lcdOut(char* s) { + lcdOut(1, s); +} + +void lcdOut(int line, char* s) { + lcd.setCursor(0, line); + lcd.print(" "); + lcd.setCursor(0, line); + lcd.print(s); +} + +void errTone() { + twoTone(300); +} + +void successTone() { + twoTone(50); +} + +void twoTone(int duration) { + digitalWrite(BEEPER_PIN, HIGH); + delay(duration); + digitalWrite(BEEPER_PIN, LOW); + delay(duration); + digitalWrite(BEEPER_PIN, HIGH); + delay(duration); + digitalWrite(BEEPER_PIN, LOW); +} + +void connectWifi() { + WiFi.disconnect(); + WiFi.mode(WIFI_STA); + WiFi.begin(WIFI_SSID, WIFI_PASS); + Serial.printf("\nDevice is connecting to WiFi using SSID %s and Passphrase %s.\n", WIFI_SSID, WIFI_PASS); + lcdOut("WiFi Setup"); + int cnt = 0; + // Keep looping until the WiFi is not connected. + while (WiFi.status() != WL_CONNECTED) { + if (cnt++ % 5 == 0) { + lcd.setCursor(10, 1); + lcd.print(" "); + lcd.setCursor(10, 1); + } + lcd.print("."); + delay(500); + } + lcdOut("WiFi OK!"); +} \ No newline at end of file