Advertisement
ViktorVanDarma

rfid_gsm

Oct 15th, 2019
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.77 KB | None | 0 0
  1. //Biblioteki
  2. #include <Wire.h>
  3. #include <LiquidCrystal_I2C.h>
  4. #include <LcdBarGraphX.h>
  5. #include <SPI.h>
  6. #include <MFRC522.h>
  7. #include "SoftwareSerial.h"
  8.  
  9. #define LCD_ADDRESS 0x3F
  10. #define DEVICE_ADDRESS_1 0x50 // Adres I2C zapasowy EEPROMU 24LC256 256K
  11. #define DEVICE_ADDRESS_3 0x68 // Adres I2C RTC
  12. #define TX_PIN 7 // Arduino transmit YELLOW WIRE labeled RX on printer
  13. #define RX_PIN 8 // Arduino receive GREEN WIRE labeled TX on printer
  14. #define SS_PIN 10
  15. #define RST_PIN 9
  16.  
  17. //uruchomienia
  18. LiquidCrystal_I2C lcd(LCD_ADDRESS, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // -- creating LCD instance
  19. SoftwareSerial gsm(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
  20. MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
  21. MFRC522::MIFARE_Key key;
  22.  
  23. byte lcdNumCols = 6; // -- number of columns in the LCD
  24.  
  25. // -- creating a 4 chars wide bars
  26. LcdBarGraphX lbg0(&lcd, 6, 0, 0); // -- First line at column 0
  27. //byte i0 = 0;
  28.  
  29. int codeRead = 0;
  30. String uidString;
  31.  
  32. String Dane; // String w ktory beda wpisane kto posiada i jaki jest czas aktualny przejecia kopalni
  33. //String Nazwa = "rfit";
  34. String Adres_zapis1 = "https://sag-wataha.org.pl/mapa/dane_zapis.php?";
  35. String Adres_zapis2 = "http://zegary.ugu.pl/dane_zapis.php?";
  36. String Adres_odczytu = "http://watacha.tk/czytnik/?get";
  37. String Zegar = "rfid";
  38. String Adres_test = "https://api.myip.com/";
  39. String Strona_Konfliktu = "1";
  40.  
  41. unsigned long czas_pracy; // zmienna do odlicania minut do zapisu stanu gry
  42.  
  43. unsigned long days;
  44. unsigned long hours;
  45. unsigned long mins;
  46. unsigned long secs;
  47.  
  48. enum _parseState {
  49. PS_DETECT_MSG_TYPE,
  50.  
  51. PS_IGNORING_COMMAND_ECHO,
  52.  
  53. PS_HTTPACTION_TYPE,
  54. PS_HTTPACTION_RESULT,
  55. PS_HTTPACTION_LENGTH,
  56.  
  57. PS_HTTPREAD_LENGTH,
  58. PS_HTTPREAD_CONTENT
  59. };
  60.  
  61. byte parseState = PS_DETECT_MSG_TYPE;
  62. char buffer[80];
  63. byte pos = 0;
  64.  
  65. int contentLength = 0;
  66.  
  67. void resetBuffer() {
  68. memset(buffer, 0, sizeof(buffer));
  69. pos = 0;
  70. }
  71.  
  72. void setup() {
  73. gsm.begin(9600); // Initialize SoftwareSerial
  74. Serial.begin(9600);
  75. SPI.begin(); // Init SPI bus
  76.  
  77. lcd.begin(1, lcdNumCols);
  78. lcd.clear();
  79. rfid.PCD_Init(); // Init MFRC522
  80.  
  81. Serial.println(F("Tokeny"));
  82.  
  83.  
  84. for (int i = 0; i < 255 ; i++)
  85. {
  86. lbg0.drawValue( i, 255);
  87. delay(25);
  88. }
  89.  
  90.  
  91. lcd.clear();
  92. lcd.setCursor(0, 0); // Go to column 0, row 0
  93. lcd.print("START");
  94. delay(1000);
  95. lcd.clear();
  96. sim();
  97. }
  98.  
  99. void loop() {
  100. czas_pracy = millis();
  101.  
  102. for (int i = 0; i < 1 ; i++)
  103. {
  104. lcd.print("SCANN");
  105. delay(500);
  106. lcd.clear();
  107.  
  108. lcd.print("GIFT");
  109. delay(500);
  110. lcd.clear();
  111.  
  112. }
  113.  
  114. if ( rfid.PICC_IsNewCardPresent())
  115. {
  116. readRFID();
  117. }
  118. delay(100);
  119.  
  120. }
  121.  
  122. void readRFID()
  123. {
  124.  
  125. rfid.PICC_ReadCardSerial();
  126. uidString = String(rfid.uid.uidByte[0]) + String(rfid.uid.uidByte[1]) + String(rfid.uid.uidByte[2]) + String(rfid.uid.uidByte[3]);
  127. Serial.println(uidString);
  128. for (int i = 0; i < 4 ; i++)
  129. {
  130. lcd.clear();
  131. lcd.print(rfid.uid.uidByte[i]);
  132. delay(500);
  133. lcd.print(rfid.uid.uidByte[i + 1]);
  134. delay(500);
  135. lcd.clear();
  136. }
  137. //
  138. lcd.clear();
  139. lcd.print("SEND..");
  140. Serial.println("UGU");
  141. wyslij_dane();
  142.  
  143. rfid.PICC_HaltA();
  144. rfid.PCD_StopCrypto1();
  145. }
  146.  
  147. void printDec(byte *buffer, byte bufferSize) {
  148. for (byte i = 0; i < bufferSize; i++) {
  149. Serial.print(buffer[i] < 0x10 ? " 0" : " ");
  150. Serial.print(buffer[i], DEC);
  151.  
  152. }
  153. }
  154.  
  155. void uptime()
  156. {
  157. secs = czas_pracy / 1000; //convect milliseconds to seconds
  158. mins = secs / 60; //convert seconds to minutes
  159. hours = mins / 60; //convert minutes to hours
  160. days = hours / 24; //convert hours to days
  161. secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  162. mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  163. hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  164. }
  165.  
  166.  
  167. void wyslij_dane() {
  168. Serial.println("Rozpoczynam zbieranie danych");
  169. Dane = "AT+HTTPPARA";
  170.  
  171. Dane += "=";
  172. Dane += "\"";
  173. Dane += "URL\"";
  174. Dane += ",";
  175. Dane += "\"";
  176. Dane += Adres_zapis2;
  177.  
  178. // Serial.println(Dane);
  179.  
  180. Dane += "T=";
  181. Dane += Zegar ;
  182. Dane += "&P=";
  183. Dane += Strona_Konfliktu ;
  184. Dane += "&U=";
  185. uptime();
  186.  
  187. if (days < 10) {
  188. Dane += "0";
  189. Dane += days;
  190. Dane += ":" ;
  191. }
  192. else
  193. {
  194. Dane += days;
  195. Dane += ":" ;
  196.  
  197. }
  198. if (hours < 10) {
  199.  
  200. Dane += "0";
  201. Dane += hours;
  202. Dane += ":" ;
  203. }
  204. else
  205. {
  206. Dane += hours;
  207. Dane += ":" ;
  208. }
  209. if (mins < 10) {
  210.  
  211. Dane += "0";
  212. Dane += mins;
  213. Dane += ":" ;
  214. }
  215. else
  216. {
  217. Dane += mins;
  218. Dane += ":" ;
  219. }
  220. if (secs < 10) {
  221.  
  222. Dane += "0";
  223. Dane += secs;
  224.  
  225. }
  226. else
  227. {
  228. Dane += secs;
  229. }
  230. Dane += "&UID=";
  231. Dane += uidString;
  232. Dane += "\"" ;
  233.  
  234.  
  235. Serial.println(Dane);
  236. delay(5000);
  237.  
  238. wyslij();
  239. }
  240.  
  241. void wyslij() {
  242. // lcd.clear();
  243. lcd.setCursor(2, 0); // Ustawienie pozycji kursora na wyswietlaczu
  244. lcd.print("WYSYLAM DANE");
  245.  
  246. gsm.println(Dane);
  247.  
  248. delay(5000);
  249. ShowSerialData();
  250. delay(5000);
  251. Serial.println("AT + HTTPACTION = 0\\r\\n");
  252. Serial.println("");
  253. gsm.println("AT + HTTPACTION = 0");
  254. delay(10000);
  255. ShowSerialData();
  256.  
  257. }
  258.  
  259.  
  260. void ShowSerialData()
  261. {
  262. while (gsm.available() != 0) /* If data is available on serial port */
  263.  
  264. Serial.write(char (gsm.read())); /* Print character received on to the serial monitor */
  265.  
  266.  
  267. }
  268.  
  269.  
  270. void sendGSM(String msg, int waitMs = 500) {
  271. gsm.println(msg);
  272. delay(waitMs);
  273. while (gsm.available()) {
  274. parseATText(gsm.read());
  275. }
  276. }
  277.  
  278.  
  279. void parseATText(byte b) {
  280.  
  281. buffer[pos++] = b;
  282.  
  283. if ( pos >= sizeof(buffer) )
  284. resetBuffer(); // just to be safe
  285.  
  286. switch (parseState) {
  287. case PS_DETECT_MSG_TYPE:
  288. {
  289. if ( b == '\n' )
  290. resetBuffer();
  291. else {
  292. if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
  293. parseState = PS_IGNORING_COMMAND_ECHO;
  294. }
  295. else if ( b == ':' ) {
  296. //Serial.print("Checking message type: ");
  297. //Serial.println(buffer);
  298.  
  299. if ( strcmp(buffer, "+HTTPACTION:") == 0 ) {
  300. Serial.println("Received HTTPACTION");
  301. parseState = PS_HTTPACTION_TYPE;
  302. }
  303. else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) {
  304. Serial.println("Received HTTPREAD");
  305. parseState = PS_HTTPREAD_LENGTH;
  306. }
  307. resetBuffer();
  308. }
  309. }
  310. }
  311. break;
  312.  
  313. case PS_IGNORING_COMMAND_ECHO:
  314. {
  315. if ( b == '\n' ) {
  316. Serial.print("Ignoring echo: ");
  317. Serial.println(buffer);
  318. parseState = PS_DETECT_MSG_TYPE;
  319. resetBuffer();
  320. }
  321. }
  322. break;
  323.  
  324. case PS_HTTPACTION_TYPE:
  325. {
  326. if ( b == ',' ) {
  327. Serial.print("HTTPACTION type is ");
  328. Serial.println(buffer);
  329. parseState = PS_HTTPACTION_RESULT;
  330. resetBuffer();
  331. }
  332. }
  333. break;
  334.  
  335. case PS_HTTPACTION_RESULT:
  336. {
  337. if ( b == ',' ) {
  338. Serial.print("HTTPACTION result is ");
  339. Serial.println(buffer);
  340. parseState = PS_HTTPACTION_LENGTH;
  341. resetBuffer();
  342. }
  343. }
  344. break;
  345.  
  346. case PS_HTTPACTION_LENGTH:
  347. {
  348. if ( b == '\n' ) {
  349. Serial.print("HTTPACTION length is ");
  350. Serial.println(buffer);
  351.  
  352. // now request content
  353. gsm.print("AT+HTTPREAD=0,");
  354. gsm.println(buffer);
  355.  
  356. parseState = PS_DETECT_MSG_TYPE;
  357. resetBuffer();
  358. }
  359. }
  360. break;
  361.  
  362. case PS_HTTPREAD_LENGTH:
  363. {
  364. if ( b == '\n' ) {
  365. contentLength = atoi(buffer);
  366. Serial.print("HTTPREAD length is ");
  367. Serial.println(contentLength);
  368.  
  369. Serial.print("HTTPREAD content: ");
  370.  
  371. parseState = PS_HTTPREAD_CONTENT;
  372. resetBuffer();
  373. }
  374. }
  375. break;
  376.  
  377. case PS_HTTPREAD_CONTENT:
  378. {
  379. // for this demo I'm just showing the content bytes in the serial monitor
  380. Serial.write(b);
  381.  
  382. contentLength--;
  383.  
  384. if ( contentLength <= 0 ) {
  385.  
  386. // all content bytes have now been read
  387.  
  388. parseState = PS_DETECT_MSG_TYPE;
  389. resetBuffer();
  390. }
  391. }
  392. break;
  393. }
  394. }
  395.  
  396.  
  397.  
  398.  
  399. void sim() {
  400. sendGSM("AT");
  401. sendGSM("AT+CSQ");
  402. sendGSM("AT+CFUN=1");
  403. sendGSM("AT+CGATT=1");
  404.  
  405. sendGSM("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  406. sendGSM("AT+SAPBR=3,1,\"APN\",\"internet\"");
  407.  
  408. sendGSM("AT+SAPBR=1,1", 3000);
  409. sendGSM("AT+SAPBR=2,1");
  410.  
  411. sendGSM("AT+HTTPINIT");
  412. sendGSM("AT+HTTPPARA=\"CID\",1");
  413. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement