Advertisement
Guest User

Untitled

a guest
Apr 23rd, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.15 KB | None | 0 0
  1. #include <ArduinoJson.h>
  2. #include "Adafruit_Thermal.h"
  3.  
  4. // Here's the new syntax when using SoftwareSerial (e.g. Arduino Uno) ----
  5. // If using hardware serial instead, comment out or remove these lines:
  6.  
  7. #include "SoftwareSerial.h"
  8. #define TX_PIN 6 // Arduino transmit YELLOW WIRE labeled RX on printer
  9. #define RX_PIN 5 // Arduino receive GREEN WIRE labeled TX on printer
  10.  
  11.  
  12.  
  13. SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
  14. Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor
  15. // Then see setup() function regarding serial & printer begin() calls.
  16.  
  17. // Memory pool for JSON object tree.
  18. //
  19. // Inside the brackets, 200 is the size of the pool in bytes.
  20. // Don't forget to change this value to match your JSON document.
  21. // Use arduinojson.org/assistant to compute the capacity.
  22.  
  23. /*
  24. SerialEvent occurs whenever a new data comes in the hardware serial RX. This
  25. routine is run between each time loop() runs, so using delay inside loop can
  26. delay response. Multiple bytes of data may be available.
  27. */
  28. String inputString = ""; // a String to hold incoming data
  29. bool stringComplete = false; // whether the string is complete
  30.  
  31. String inData = "";
  32. char json[257];
  33. void serialEvent() {
  34. while (Serial.available()) {
  35. // get the new byte:
  36. char inChar = (char)Serial.read();
  37. // add it to the inputString:
  38. inputString += inChar;
  39. // if the incoming character is a newline, set a flag so the main loop can
  40. // do something about it:
  41. if (inChar == '\n') {
  42. stringComplete = true;
  43. }
  44. }
  45. }
  46.  
  47. /* The print receipt method to */
  48. void printReceipt(int bedrag, char* ATMNaam, char* IBAN, char* DT) {
  49. Serial.end();
  50. mySerial.listen();
  51. //printer is 32 karakters breed
  52. //hier de code die we gaan gebruiken om te printen
  53. printer.doubleHeightOn();
  54. printer.justify('C');
  55. printer.println(F("________________________________"));
  56. printer.println(F("Withdraw receipt"));
  57. printer.doubleHeightOff();
  58. printer.justify('L');
  59. printer.println(F("________________________________"));
  60. printer.println(F("Bank naam: Olympia Bank"));
  61. printer.println("ATM naam:" + String(ATMNaam));
  62. printer.println("IBAN:" + String(IBAN));
  63. printer.println("Bedrag: " + String(bedrag));
  64. printer.println("Datum:" + String(DT));
  65. printer.println(F("________________________________"));
  66. printer.println(F(""));
  67.  
  68.  
  69. printer.sleep(); // Tell printer to sleep
  70. //delay(3000L); // Sleep for 3 seconds
  71. printer.wake(); // MUST wake() before printing again, even if reset
  72. printer.setDefault(); // Restore printer to defaults
  73. Serial.begin(9600);
  74. }
  75.  
  76. void setup() {
  77. //bedrag = "Bedrag: " + b + " EUR";
  78.  
  79. // This line is for compatibility with the Adafruit IotP project pack,
  80. // which uses pin 7 as a spare grounding point. You only need this if
  81. // wired up the same way (w/3-pin header into pins 5/6/7):
  82. pinMode(7, OUTPUT); digitalWrite(7, LOW);
  83. inputString.reserve(1000);
  84. Serial.begin(9600);
  85.  
  86. // NOTE: SOME PRINTERS NEED 9600 BAUD instead of 19200, check test page.
  87. mySerial.begin(9600); // Initialize SoftwareSerial
  88. printer.begin(); // Init printer (same regardless of serial type)
  89. printer.wake(); // MUST wake() before printing again, even if reset
  90. printer.setDefault(); // Restore printer to defaults
  91. }
  92.  
  93. void loop() {
  94.  
  95. char* cmd = "";
  96. if (stringComplete) {
  97. Serial.println(inputString);
  98.  
  99. // ArduinoJson 6
  100. DynamicJsonDocument root(2024);
  101. deserializeJson(root, inputString);
  102. // DeserializationError error = deserializeJson(root, inputString);
  103. // if (error)
  104.  
  105.  
  106. // Fetch values.
  107. // Most of the time, you can rely on the implicit casts.
  108. // In other case, you can do root["time"].as<long>();
  109. cmd = root[0]["command"];
  110. char* ATMname = root[0]["ATMnaam"];
  111. char* IBAN = root[0]["IBAN"];
  112. char* bedrag = root[0]["bedrag"];
  113. char* dt = root[0]["date"];
  114.  
  115. Serial.println("CMD: " + (String) cmd);
  116.  
  117. Serial.println("IBAN: " + (String) IBAN);
  118. Serial.println("Bedrag: " + (String) bedrag);
  119.  
  120.  
  121. if (cmd == "print") {
  122. printReceipt(bedrag, ATMname, IBAN, dt);
  123. }
  124.  
  125.  
  126. // clear the string:
  127. inputString = "";
  128. stringComplete = false;
  129. }
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement