4
0
Fork 0
mirror of https://github.com/zzzzDev4/IAS-Better-Tea.git synced 2025-04-21 07:31:20 +02:00

Replaced old RFID reader with PN532 that supports I2C

This commit is contained in:
zzzz 2024-05-09 18:31:39 +02:00
parent 859d6daa9e
commit f4ec4f13a1
5 changed files with 121 additions and 87 deletions

View file

@ -14,5 +14,5 @@ board = d1
framework = arduino framework = arduino
lib_deps = lib_deps =
adafruit/Adafruit SSD1306@^2.5.9 adafruit/Adafruit SSD1306@^2.5.9
miguelbalboa/MFRC522@^1.4.11
arduino-libraries/Arduino_JSON@^0.2.0 arduino-libraries/Arduino_JSON@^0.2.0
adafruit/Adafruit PN532@^1.3.3

60
src/SmartRFID.cpp Normal file
View file

@ -0,0 +1,60 @@
#include "SmartRFID.hpp"
SmartRFID::SmartRFID(Adafruit_PN532 *nfc)
{
m_nfc = nfc; // RFID with I2C
}
void SmartRFID::init()
{
m_nfc->begin();
unsigned long versiondata = m_nfc->getFirmwareVersion();
if (!versiondata)
{
Serial.print("No board found!");
while (1)
;
}
Serial.println("PN5 chip found!");
}
String SmartRFID::readTag()
{
m_nfc->SAMConfig();
Serial.println("Waiting for ISO14443A chip...");
uint8_t success;
uint8_t uid[] = {0, 0, 0, 0, 0, 0, 0}; // buffer for UID
uint8_t uidLength; // UID length (4 or 7 bytes depending on ISO14443A Card/Chip type)
success = m_nfc->readPassiveTargetID(PN532_MIFARE_ISO14443A, uid, &uidLength);
if (success)
{
Serial.println("Found an ISO14443A card");
Serial.print(" UID Length: ");
Serial.print(uidLength, DEC);
Serial.println(" bytes");
Serial.print(" UID Value: ");
m_nfc->PrintHex(uid, uidLength);
Serial.println("");
Serial.print(" compact hex code: ");
String s = string_dump_byte_array(uid, uidLength);
Serial.println(s);
return s;
}
return "error_reading_tag";
}
/**
* Helper routine to dump a byte array as hex values to a string.
*/
String SmartRFID::string_dump_byte_array(byte *buffer, byte bufferSize)
{
String result = "";
for (byte i = 0; i < bufferSize; i++)
{
result += (buffer[i] < 0x10 ? " 0" : " ");
result += String(buffer[i], HEX);
}
result.replace(" ", "");
return result;
}

20
src/SmartRFID.hpp Normal file
View file

@ -0,0 +1,20 @@
#pragma once
#include <Adafruit_PN532.h>
class SmartRFID
{
public:
SmartRFID(Adafruit_PN532 *nfc);
void init();
String readTag();
private:
String string_dump_byte_array(byte *buffer, byte bufferSize);
private:
unsigned long m_cardid;
Adafruit_PN532 *m_nfc;
};

View file

@ -3,21 +3,27 @@
#define WIRE Wire #define WIRE Wire
#define DISPLAY_PIN 4 // D2 (SDA) #define SERVO_PIN 14 // D5
#define SERVO_PIN 0 // D3
#define TOUCH_BTN_PIN 16 // D0
#define POTENTIOMETER_PIN A0 // A0 #define POTENTIOMETER_PIN A0 // A0
// RFID // buttons
#define RST_PIN 2 // D4 #define TACTILE_BTN_LEFT 0 // D3
#define SS_PIN 15 // D8 #define TACTILE_BTN_RIGHT 2 // D4
MFRC522 mfrc522(SS_PIN, RST_PIN); // create RFID instance #define TOUCH_BTN_PIN 16 // D0
MFRC522::MIFARE_Key key;
// RFID
#define PN532_IRQ 13 // D7
#define PN532_RESET 12 // D6 (not connected)
Adafruit_PN532 nfc(PN532_IRQ, PN532_RESET); // RFID with I2C
// OLED display
#define DISPLAY_PIN 4 // D2 (SDA)
auto display = Adafruit_SSD1306(128, 32, &WIRE); auto display = Adafruit_SSD1306(128, 32, &WIRE);
// util classes
ContinuousServo contServo(SERVO_PIN); ContinuousServo contServo(SERVO_PIN);
TeaTimer teaTimer(&contServo); TeaTimer teaTimer(&contServo);
SmartRFID smartRFID(&nfc);
SmartDial smartDial(POTENTIOMETER_PIN); SmartDial smartDial(POTENTIOMETER_PIN);
SmartDisplay smartDisplay(&display); SmartDisplay smartDisplay(&display);
TeaNetworking teaNet; TeaNetworking teaNet;
@ -25,7 +31,7 @@ TeaNetworking teaNet;
// networking // networking
String shuttleServer = "https://ias-tea-axum.shuttleapp.rs"; String shuttleServer = "https://ias-tea-axum.shuttleapp.rs";
TeaData currentTea; TeaData currentTea("Teafault", "c0derfid", 100, 30);
enum State enum State
{ {
@ -43,12 +49,7 @@ void setup()
teaNet.init(ssid, password); teaNet.init(ssid, password);
Serial.println("Initializing SPI..."); smartRFID.init();
SPI.begin();
Serial.println("Initializing RFID...");
mfrc522.PCD_Init();
mfrc522.PICC_HaltA();
Serial.println("Initializing OLED..."); Serial.println("Initializing OLED...");
display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // Address 0x3C for 128x32 display.begin(SSD1306_SWITCHCAPVCC, 0x3C, false); // Address 0x3C for 128x32
@ -56,14 +57,11 @@ void setup()
delay(1000); delay(1000);
// Prepare the key (used both as key A and key B)
for (byte i = 0; i < 6; i++)
{
key.keyByte[i] = 0xFF;
}
pinMode(LED_BUILTIN, OUTPUT); pinMode(LED_BUILTIN, OUTPUT);
pinMode(TOUCH_BTN_PIN, INPUT); pinMode(TOUCH_BTN_PIN, INPUT);
pinMode(TACTILE_BTN_LEFT, INPUT);
pinMode(TACTILE_BTN_RIGHT, INPUT);
} }
int steepingSeconds = 60; int steepingSeconds = 60;
@ -95,33 +93,16 @@ void loop()
case WAIT_FOR_RFID_SCAN: case WAIT_FOR_RFID_SCAN:
{ {
smartDisplay.printWaitForRFIDScreen(); smartDisplay.printWaitForRFIDScreen();
// Look for new cards
if (!mfrc522.PICC_IsNewCardPresent())
return;
// Select one of the cards String uid = smartRFID.readTag();
if (!mfrc522.PICC_ReadCardSerial())
return;
// UID is a unique four hex pairs, e.g. E6 67 2A 12 display.println(uid);
Serial.print("Card UID:");
oled_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
serial_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
display.println();
display.display(); display.display();
delay(1000); delay(1000);
Serial.println();
Serial.print("String UID:");
String uid = string_dump_byte_array(mfrc522.uid.uidByte, mfrc522.uid.size);
Serial.println(uid);
// Halt PICC
mfrc522.PICC_HaltA();
display.println("Success!"); display.println("Success!");
display.display(); display.display();
delay(1000); delay(500);
JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/teabyrfid/" + uid); JSONVar result = teaNet.httpsGETRequest(shuttleServer + "/teabyrfid/" + uid);
Serial.print("Result:"); Serial.print("Result:");
@ -138,12 +119,25 @@ void loop()
currentTea = TeaData(result["teaname"], result["rfidcode"], result["watertemp"], result["steepingSeconds"]); currentTea = TeaData(result["teaname"], result["rfidcode"], result["watertemp"], result["steepingSeconds"]);
state = State::DISPLAY_SCANNED_TEA_CONFIG; state = State::DISPLAY_SCANNED_TEA_CONFIG;
break; break;
} }
case DISPLAY_SCANNED_TEA_CONFIG: case DISPLAY_SCANNED_TEA_CONFIG:
{ {
int dir = smartDial.smoothEdit(&currentTea.m_steepingSeconds, 0, 6000); int dir = smartDial.smoothEdit(&currentTea.m_steepingSeconds, 0, 6000);
smartDisplay.printTeaConfigScreen(currentTea.m_teaName, currentTea.m_steepingSeconds, dir);
if (digitalRead(TACTILE_BTN_LEFT) == LOW)
{
printMsg("BUTTON GREEN");
}
else if (digitalRead(TACTILE_BTN_RIGHT) == LOW)
{
printMsg("BUTTON RED");
}
else
{
smartDisplay.printTeaConfigScreen(currentTea.m_teaName, currentTea.m_steepingSeconds, dir);
}
// change state if start pressed // change state if start pressed
if (isBtnPressed()) if (isBtnPressed())
@ -189,43 +183,6 @@ bool isBtnPressed()
return digitalRead(TOUCH_BTN_PIN) == HIGH; return digitalRead(TOUCH_BTN_PIN) == HIGH;
} }
/**
* Helper routine to dump a byte array as hex values to a string.
*/
String string_dump_byte_array(byte *buffer, byte bufferSize)
{
String result = "";
for (byte i = 0; i < bufferSize; i++)
{
result += (buffer[i] < 0x10 ? " 0" : " ");
result += String(buffer[i], HEX);
}
result.replace(" ", "");
return result;
}
/**
* Helper routine to dump a byte array as hex values to Serial.
*/
void serial_dump_byte_array(byte *buffer, byte bufferSize)
{
for (byte i = 0; i < bufferSize; i++)
{
Serial.print(buffer[i] < 0x10 ? " 0" : " ");
Serial.print(buffer[i], HEX);
}
}
/**
* Helper routine to dump a byte array as hex values to OLED.
*/
void oled_dump_byte_array(byte *buffer, byte bufferSize)
{
for (byte i = 0; i < bufferSize; i++)
{
display.print(buffer[i] < 0x10 ? " 0" : " ");
display.print(buffer[i], HEX);
}
}
void printMsg(const String &s) void printMsg(const String &s)
{ {
display.clearDisplay(); display.clearDisplay();

View file

@ -1,15 +1,16 @@
#include <Arduino.h> #include <Arduino.h>
#include <Arduino_JSON.h> #include <Arduino_JSON.h>
#include <SPI.h>
#include <Wire.h> #include <Wire.h>
#include <Adafruit_GFX.h> #include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h> #include <Adafruit_SSD1306.h>
#include <MFRC522.h> #include <Adafruit_PN532.h>
#include <FS.h> #include <FS.h>
#include <LittleFS.h> #include <LittleFS.h>
#include <time.h> #include <time.h>
#include "TeaTimer.hpp" #include "TeaTimer.hpp"
#include "SmartRFID.hpp"
#include "ContinuousServo.hpp" #include "ContinuousServo.hpp"
#include "SmartDial.hpp" #include "SmartDial.hpp"
#include "SmartDisplay.hpp" #include "SmartDisplay.hpp"
@ -20,7 +21,3 @@ bool toggle_led(void *);
void printMsg(const String &s); void printMsg(const String &s);
void printMsg(unsigned long l); void printMsg(unsigned long l);
bool isBtnPressed(); bool isBtnPressed();
void serial_dump_byte_array(byte *buffer, byte bufferSize);
void oled_dump_byte_array(byte *buffer, byte bufferSize);
String string_dump_byte_array(byte *buffer, byte bufferSize);