Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ArduinoJson.h>
- #include "Adafruit_Thermal.h"
- // Here's the new syntax when using SoftwareSerial (e.g. Arduino Uno) ----
- // If using hardware serial instead, comment out or remove these lines:
- #include "SoftwareSerial.h"
- #define TX_PIN 6 // Arduino transmit YELLOW WIRE labeled RX on printer
- #define RX_PIN 5 // Arduino receive GREEN WIRE labeled TX on printer
- SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
- Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor
- // Then see setup() function regarding serial & printer begin() calls.
- // Memory pool for JSON object tree.
- //
- // Inside the brackets, 200 is the size of the pool in bytes.
- // Don't forget to change this value to match your JSON document.
- // Use arduinojson.org/assistant to compute the capacity.
- /*
- SerialEvent occurs whenever a new data comes in the hardware serial RX. This
- routine is run between each time loop() runs, so using delay inside loop can
- delay response. Multiple bytes of data may be available.
- */
- String inputString = ""; // a String to hold incoming data
- bool stringComplete = false; // whether the string is complete
- String inData = "";
- char json[257];
- void serialEvent() {
- while (Serial.available()) {
- // get the new byte:
- char inChar = (char)Serial.read();
- // add it to the inputString:
- inputString += inChar;
- // if the incoming character is a newline, set a flag so the main loop can
- // do something about it:
- if (inChar == '\n') {
- stringComplete = true;
- }
- }
- }
- /* The print receipt method to */
- void printReceipt(int bedrag, char* ATMNaam, char* IBAN, char* DT) {
- Serial.end();
- mySerial.listen();
- //printer is 32 karakters breed
- //hier de code die we gaan gebruiken om te printen
- printer.doubleHeightOn();
- printer.justify('C');
- printer.println(F("________________________________"));
- printer.println(F("Withdraw receipt"));
- printer.doubleHeightOff();
- printer.justify('L');
- printer.println(F("________________________________"));
- printer.println(F("Bank naam: Olympia Bank"));
- printer.println("ATM naam:" + String(ATMNaam));
- printer.println("IBAN:" + String(IBAN));
- printer.println("Bedrag: " + String(bedrag));
- printer.println("Datum:" + String(DT));
- printer.println(F("________________________________"));
- printer.println(F(""));
- printer.sleep(); // Tell printer to sleep
- //delay(3000L); // Sleep for 3 seconds
- printer.wake(); // MUST wake() before printing again, even if reset
- printer.setDefault(); // Restore printer to defaults
- Serial.begin(9600);
- }
- void setup() {
- //bedrag = "Bedrag: " + b + " EUR";
- // This line is for compatibility with the Adafruit IotP project pack,
- // which uses pin 7 as a spare grounding point. You only need this if
- // wired up the same way (w/3-pin header into pins 5/6/7):
- pinMode(7, OUTPUT); digitalWrite(7, LOW);
- inputString.reserve(1000);
- Serial.begin(9600);
- // NOTE: SOME PRINTERS NEED 9600 BAUD instead of 19200, check test page.
- mySerial.begin(9600); // Initialize SoftwareSerial
- printer.begin(); // Init printer (same regardless of serial type)
- printer.wake(); // MUST wake() before printing again, even if reset
- printer.setDefault(); // Restore printer to defaults
- }
- void loop() {
- char* cmd = "";
- if (stringComplete) {
- Serial.println(inputString);
- // ArduinoJson 6
- DynamicJsonDocument root(2024);
- deserializeJson(root, inputString);
- // DeserializationError error = deserializeJson(root, inputString);
- // if (error)
- // Fetch values.
- // Most of the time, you can rely on the implicit casts.
- // In other case, you can do root["time"].as<long>();
- cmd = root[0]["command"];
- char* ATMname = root[0]["ATMnaam"];
- char* IBAN = root[0]["IBAN"];
- char* bedrag = root[0]["bedrag"];
- char* dt = root[0]["date"];
- Serial.println("CMD: " + (String) cmd);
- Serial.println("IBAN: " + (String) IBAN);
- Serial.println("Bedrag: " + (String) bedrag);
- if (cmd == "print") {
- printReceipt(bedrag, ATMname, IBAN, dt);
- }
- // clear the string:
- inputString = "";
- stringComplete = false;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement