Advertisement
ViktorVanDarma

Drukarka_Zadan

Sep 21st, 2019
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.67 KB | None | 0 0
  1. #include <SPI.h>
  2. #include <MFRC522.h>
  3. #include "Adafruit_Thermal.h"
  4. #include <LiquidCrystal_I2C.h> // Biblioteka LCD na szynie I2C
  5. #include <Wire.h> // Biblioteka I2C
  6.  
  7.  
  8. Adafruit_Thermal printer(&Serial1);// Pass addr to //printer constructor
  9.  
  10. #define LCD_ADDRESS 0x27
  11. #define BACKLIGHT_PIN 3 // Dziala podświetlenie :]
  12.  
  13. LiquidCrystal_I2C lcd(LCD_ADDRESS, 2, 1, 0, 4, 5, 6, 7);
  14.  
  15. #define SS_PIN 10
  16. #define RST_PIN 9
  17.  
  18. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
  19.  
  20. MFRC522::MIFARE_Key key;
  21.  
  22.  
  23. //String zadanie_web;
  24.  
  25. int codeRead = 0;
  26. String uidString;
  27.  
  28. String Dane; // String w ktory beda wpisane kto posiada i jaki jest czas aktualny przejecia kopalni
  29. String Nazwa = "Drukarka";
  30. String Adres_zapis = "http://zegary.ugu.pl/dane_zapis.php?";
  31. String Adres_odczytu = "http://watacha.tk/czytnik/?get,";
  32.  
  33.  
  34.  
  35. enum _parseState {
  36. PS_DETECT_MSG_TYPE,
  37.  
  38. PS_IGNORING_COMMAND_ECHO,
  39.  
  40. PS_HTTPACTION_TYPE,
  41. PS_HTTPACTION_RESULT,
  42. PS_HTTPACTION_LENGTH,
  43.  
  44. PS_HTTPREAD_LENGTH,
  45. PS_HTTPREAD_CONTENT
  46. };
  47.  
  48. enum _actionState {
  49. AS_IDLE,
  50. AS_WAITING_FOR_RESPONSE
  51. };
  52.  
  53. byte actionState = AS_IDLE;
  54. unsigned long lastActionTime = 0;
  55.  
  56. byte parseState = PS_DETECT_MSG_TYPE;
  57. char buffer[80];
  58. byte pos = 0;
  59.  
  60. int contentLength = 0;
  61.  
  62. void resetBuffer() {
  63. memset(buffer, 0, sizeof(buffer));
  64. pos = 0;
  65. }
  66.  
  67. //void sendSerial2(const char* msg, int waitMs = 500) {
  68. void sendSerial2(String msg, int waitMs = 500) {
  69. Serial2.println(msg);
  70. while (Serial2.available()) {
  71. parseATText(Serial2.read());
  72. }
  73. delay(waitMs);
  74. }
  75.  
  76. void setup()
  77. {
  78. Serial.begin(9600); // Główny Serial
  79. Serial1.begin(19200);
  80. // printer.begin(19200);
  81. printer.begin();
  82.  
  83. Serial2.begin(9600); // Serial GSM
  84.  
  85.  
  86. lcd.begin(20, 4); // Uruchom LCD
  87. lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE); // Podświetlenie lcd to działa
  88. lcd.setBacklight(HIGH); // Podświetlenie lcd to działa
  89.  
  90. SPI.begin(); // Init SPI bus
  91. rfid.PCD_Init(); // Init MFRC522
  92.  
  93.  
  94.  
  95.  
  96. lcd.setCursor(3, 0);
  97. lcd.print("AIRSOFT PRINTER");
  98. lcd.setCursor(9, 1);
  99. lcd.print("BY");
  100. lcd.setCursor(2, 2);
  101. lcd.print("VIKTOR VAN DARMAN");
  102. lcd.setCursor(11, 3);
  103. lcd.print("ver 1.0.0");
  104.  
  105.  
  106. // sim();
  107. Serial.println(F("Arduino RFID tutorial"));
  108.  
  109. sendSerial2("AT+SAPBR=3,1,\"APN\",\"internet\"");
  110. sendSerial2("AT+SAPBR=1,1", 3000);
  111. sendSerial2("AT+HTTPINIT");
  112. sendSerial2("AT+HTTPPARA=\"CID\",1");
  113. sendSerial2("AT+HTTPPARA=\"URL\",\"http://watacha.tk/czytnik/?get,2191962300\"");
  114. lcd.clear();
  115. lcd.setCursor(0, 0);
  116. lcd.print("Przyluz brylok z ");
  117. lcd.setCursor(0, 1);
  118. lcd.print("zadaniem do czytnika");
  119.  
  120. }
  121.  
  122. void loop() {
  123. // Serial.println("loop");
  124. if ( rfid.PICC_IsNewCardPresent())
  125. {
  126.  
  127. lcd.clear();
  128. lcd.setCursor(0, 0);
  129. lcd.print("Przyluz brylok z ");
  130. lcd.setCursor(0, 1);
  131. lcd.print("zadaniem do czytnika");
  132. // lcd.setCursor(0, 2);
  133. // lcd.print(buffer);
  134. Serial.println("Odczyt RFID");
  135. readRFID();
  136. delay(5000);
  137. lcd.setCursor(0, 3);
  138. lcd.print(" ");
  139.  
  140. }
  141.  
  142. }
  143. void odczyt() {
  144. Serial.println("odczyt");
  145. unsigned long now = millis();
  146.  
  147. if ( actionState == AS_IDLE ) {
  148. if ( now > lastActionTime + 5000 ) {
  149.  
  150. Serial.println("Rozpoczynam pobieranie danych");
  151. Dane = "AT+HTTPPARA";
  152. Dane += "=";
  153. Dane += "\"";
  154. Dane += "URL\"";
  155. Dane += ",";
  156. Dane += "\"";
  157. Dane += Adres_odczytu;
  158. Dane += uidString;
  159. //Dane += "2191962300";
  160. Dane += "\"" ;
  161. Serial.println(Dane);
  162. sendSerial2(Dane);
  163. // sendSerial2("AT+HTTPPARA=\"URL\",\"http://watacha.tk/czytnik/?get,2191962300\"");
  164. delay(10000);
  165. sendSerial2("AT+HTTPACTION=0");
  166. lastActionTime = now;
  167. actionState = AS_WAITING_FOR_RESPONSE;
  168. }
  169. }
  170.  
  171. while (Serial2.available()) {
  172. // Serial.println("odczyt gsm");
  173. lastActionTime = now;
  174. parseATText(Serial2.read());
  175. }
  176. }
  177.  
  178. void parseATText(byte b) {
  179.  
  180. buffer[pos++] = b;
  181.  
  182. if ( pos >= sizeof(buffer) )
  183. resetBuffer(); // just to be safe
  184.  
  185. /*
  186. // Detailed debugging
  187. Serial.println();
  188. // Serial.print("state = ");
  189. // Serial.println(state);
  190. Serial.print("b = ");
  191. Serial.println(b);
  192. Serial.print("pos = ");
  193. Serial.println(pos);
  194. Serial.print("buffer = ");
  195. Serial.println(buffer);
  196. */
  197. switch (parseState) {
  198. case PS_DETECT_MSG_TYPE:
  199. {
  200. if ( b == '\n' )
  201. resetBuffer();
  202. else {
  203. if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
  204. parseState = PS_IGNORING_COMMAND_ECHO;
  205. }
  206. else if ( b == ':' ) {
  207. //Serial.print("Checking message type: ");
  208. //Serial.println(buffer);
  209.  
  210. if ( strcmp(buffer, "+HTTPACTION:") == 0 ) {
  211. Serial.println("Received HTTPACTION");
  212. parseState = PS_HTTPACTION_TYPE;
  213. }
  214. else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) {
  215. Serial.println("Received HTTPREAD");
  216. parseState = PS_HTTPREAD_LENGTH;
  217. }
  218. resetBuffer();
  219. }
  220. }
  221. }
  222. break;
  223.  
  224. case PS_IGNORING_COMMAND_ECHO:
  225. {
  226. if ( b == '\n' ) {
  227.  
  228. lcd.clear();
  229. lcd.setCursor(0, 0);
  230. lcd.print("Uruchamiam GSM");
  231. lcd.setCursor(0, 1);
  232. lcd.print(buffer);
  233. Serial.print("Ignoring echo: ");
  234.  
  235. Serial.println(buffer);
  236. parseState = PS_DETECT_MSG_TYPE;
  237. resetBuffer();
  238. }
  239. }
  240. break;
  241.  
  242. case PS_HTTPACTION_TYPE:
  243. {
  244. if ( b == ',' ) {
  245. Serial.print("HTTPACTION type is ");
  246. Serial.println(buffer);
  247. parseState = PS_HTTPACTION_RESULT;
  248. resetBuffer();
  249. }
  250. }
  251. break;
  252.  
  253. case PS_HTTPACTION_RESULT:
  254. {
  255. if ( b == ',' ) {
  256. Serial.print("HTTPACTION result is ");
  257. Serial.println(buffer);
  258. parseState = PS_HTTPACTION_LENGTH;
  259. resetBuffer();
  260. }
  261. }
  262. break;
  263.  
  264. case PS_HTTPACTION_LENGTH:
  265. {
  266. if ( b == '\n' ) {
  267. Serial.print("HTTPACTION length is ");
  268. Serial.println(buffer);
  269.  
  270. // now request content
  271. Serial2.print("AT+HTTPREAD=0,");
  272. Serial2.println(buffer);
  273.  
  274. parseState = PS_DETECT_MSG_TYPE;
  275. resetBuffer();
  276. }
  277. }
  278. break;
  279.  
  280. case PS_HTTPREAD_LENGTH:
  281. {
  282. if ( b == '\n' ) {
  283. contentLength = atoi(buffer);
  284. Serial.print("HTTPREAD length is ");
  285. Serial.println(contentLength);
  286.  
  287. Serial.print("HTTPREAD content: ");
  288.  
  289. parseState = PS_HTTPREAD_CONTENT;
  290. resetBuffer();
  291. }
  292. }
  293. break;
  294.  
  295. case PS_HTTPREAD_CONTENT:
  296. {
  297. // for this demo I'm just showing the content bytes in the serial monitor
  298. Serial.write(b);
  299.  
  300. contentLength--;
  301.  
  302. if ( contentLength <= 0 ) {
  303.  
  304. // all content bytes have now been read
  305.  
  306. parseState = PS_DETECT_MSG_TYPE;
  307. resetBuffer();
  308.  
  309. Serial.print("\n\n\n");
  310.  
  311. actionState = AS_IDLE;
  312. }
  313. }
  314. break;
  315. }
  316. }
  317.  
  318.  
  319.  
  320.  
  321. void readRFID()
  322. {
  323.  
  324. rfid.PICC_ReadCardSerial();
  325. Serial.print(F("\nPICC type: "));
  326. MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
  327. Serial.println(rfid.PICC_GetTypeName(piccType));
  328.  
  329. // Check is the PICC of Classic MIFARE type
  330. if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
  331. piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
  332. piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
  333. Serial.println(F("Your tag is not of type MIFARE Classic."));
  334. return;
  335. }
  336.  
  337.  
  338. Serial.println("Scanned PICC's UID:");
  339. // printDec(rfid.uid.uidByte, rfid.uid.size);
  340.  
  341. //uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]);
  342. uidString = String(rfid.uid.uidByte[0]) + String(rfid.uid.uidByte[1]) + String(rfid.uid.uidByte[2]) + String(rfid.uid.uidByte[3]);
  343.  
  344. // drukarka_zadan = ODBIERZ_DANE;
  345. // if (in) {
  346. lcd.setCursor(0, 3);
  347. lcd.print(uidString);
  348.  
  349. Serial.println(uidString);
  350. // odczyt();
  351. // naglowek();
  352.  
  353. // odbierz_dane();
  354.  
  355. // Serial.println(in);
  356.  
  357.  
  358. ////printer.println(in);
  359.  
  360. // stopka();
  361.  
  362. // }
  363.  
  364.  
  365. }
  366.  
  367.  
  368.  
  369. void printDec(byte * buffer, byte bufferSize) {
  370. for (byte i = 0; i < bufferSize; i++) {
  371. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  372. Serial.print(buffer[i], DEC);
  373.  
  374. }
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement