We live in this 21 century, and I still find that borrow books from the library can be complicated and time-consuming. Today we're making this minimal Library book management system, try to solve the issue and make the world a better place (*^▽^*).
In this project, two M5StackFire and one 701 module thermal printers were used, one of which was connected to the RFID panel for swiping and displaying user information, and the other was used to forward data to control the printer while the screen was on for display billing information. Another thing is, because the thermal printer is easy to drive under Arduino, and UIFlow is convenient for interface and database processing, the code combines UIflow and Arduino for data processing, reducing the overall complexity and debugging.
Considering the actual situation of the MakeFaire site, I didn't use NTP to get the network time. Since there is no RTC, I can only write a function to count the time. Fortunately, there is a timer module in UIFlow. It can be called directly. We can manually calculate the time through the timer. In order to facilitate quick debugging and verification, I set the unit conversion of time to decimal 2, which is 2 seconds = 1 minute, 2 minutes = 1 hour, 2 hours = 1 day.
First, a simple UI interface design
The program is more complicated. The following is a brief description of the key points. Because of the database view function, you need a simple page switch and mark it with page.
Related variables are initialized
First scan the book number and look it up from the database. In order to avoid misidentification, add the flag tag and read it only once.
Start counting date intervals if a target is found in the database.
The card deduction fee screen displays relevant information, deletes this record from the database, and generates JSON data.
The serial port sends JSON and changes flag.
If no records are found, save the card related information to the database and display it.
Data is written to the card and sent to JSON.
Print database content when switching to the data view page.
The button callback A key formats and recharges the card, and the B and C keys are set to query and system operation respectively.
The printer console uses two serial ports for communication (701 printer default 9600 baud rate, other rates garbled) using ArduinoJson for decoding.
#include <M5Stack.h>
#include "Adafruit_Thermal.h"
#include <ArduinoJson.h>
HardwareSerial Serialn(1);
HardwareSerial mySerial(2); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor
String com = "";
// -----------------------------------------------------------------------
void setup() {
M5.begin();
mySerial.begin(9600); //
Serialn.begin(115200, SERIAL_8N1, 26, 36); //
printer.begin();
M5.Lcd.setCursor(110, 10, 4);
M5.Lcd.print("PRINTER");
}
void loop() {
if(Serialn.available()){
com = "";
while(Serialn.available()){
char ch = Serialn.read();
if(ch != '\n'){
com += ch;
}
}
printer.wake();
M5.Lcd.clear(BLACK);
Serial.println(com);
StaticJsonDocument<200> doc;
DeserializationError error = deserializeJson(doc, com);
if (error) {
Serial.print(F("deserializeJson() failed: "));
Serial.println(error.c_str());
return;
}
String CardNum = doc["CardNum"];
String Overage = doc["Overage"];
String Record = doc["Record"];
String Book = doc["Book"];
String Borrowing = doc["Borrowing"];
String Sendback = doc["Sendback"];
M5.Lcd.setCursor(70, 10, 4);
printer.inverseOn();
printer.justify('C');
mySerial.println(F(" Loan receipt "));
printer.inverseOff();
M5.Lcd.println(" Loan receipt ");
M5.Lcd.println("");
M5.Lcd.setCursor(0, 35, 2);
printer.justify('L');
printer.println("--------------------------");
printer.boldOn();
printer.setSize('L');
printer.print(F("CardNum:"));
printer.boldOff();
printer.setSize('M');
printer.println(CardNum);
M5.Lcd.println("---------------------------------------------------");
M5.Lcd.print("CardNum: ");
M5.Lcd.println(CardNum);
printer.boldOn();
printer.setSize('L');
printer.print(F("Overage:"));
printer.boldOff();
printer.setSize('M');
printer.println(Overage);
M5.Lcd.println("----------------------------------------------------");
M5.Lcd.print("Overage: ");
M5.Lcd.println(Overage);
printer.boldOn();
printer.setSize('L');
printer.print(F("Record:"));
printer.boldOff();
printer.setSize('M');
printer.println(Record);
M5.Lcd.println("-----------------------------------------------------");
M5.Lcd.print("Record: ");
M5.Lcd.println(Record);
printer.boldOn();
printer.setSize('L');
printer.print(F("Book:"));
printer.boldOff();
printer.setSize('M');
printer.println(Book);
M5.Lcd.println("-----------------------------------------------------");
M5.Lcd.print("Book: ");
M5.Lcd.println(Book);
M5.Lcd.setCursor(250, 220);
printer.justify('R');
if(Borrowing == "null"){
printer.print(Sendback);
M5.Lcd.println(Sendback);
}else{
printer.print(Borrowing);
M5.Lcd.println(Borrowing);
}
printer.feed(5);
delay(2000);
printer.sleep();
}
delay(3000);
M5.Lcd.setCursor(110, 10, 4);
M5.Lcd.print("PRINTER");
}
Enjoy!
Comments