Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SPI.h>
- #include <MFRC522.h>
- #include "Adafruit_Thermal.h"
- #include <LiquidCrystal_I2C.h> // Biblioteka LCD na szynie I2C
- #include <Wire.h> // Biblioteka I2C
- Adafruit_Thermal printer(&Serial1);// Pass addr to //printer constructor
- #define LCD_ADDRESS 0x27
- #define BACKLIGHT_PIN 3 // Dziala podświetlenie :]
- LiquidCrystal_I2C lcd(LCD_ADDRESS, 2, 1, 0, 4, 5, 6, 7);
- #define SS_PIN 10
- #define RST_PIN 9
- MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
- MFRC522::MIFARE_Key key;
- //String zadanie_web;
- int codeRead = 0;
- String uidString;
- String Dane; // String w ktory beda wpisane kto posiada i jaki jest czas aktualny przejecia kopalni
- String Nazwa = "Drukarka";
- String Adres_zapis = "http://zegary.ugu.pl/dane_zapis.php?";
- String Adres_odczytu = "http://watacha.tk/czytnik/?get,";
- enum _parseState {
- PS_DETECT_MSG_TYPE,
- PS_IGNORING_COMMAND_ECHO,
- PS_HTTPACTION_TYPE,
- PS_HTTPACTION_RESULT,
- PS_HTTPACTION_LENGTH,
- PS_HTTPREAD_LENGTH,
- PS_HTTPREAD_CONTENT
- };
- enum _actionState {
- AS_IDLE,
- AS_WAITING_FOR_RESPONSE
- };
- byte actionState = AS_IDLE;
- unsigned long lastActionTime = 0;
- byte parseState = PS_DETECT_MSG_TYPE;
- char buffer[80];
- byte pos = 0;
- int contentLength = 0;
- void resetBuffer() {
- memset(buffer, 0, sizeof(buffer));
- pos = 0;
- }
- //void sendSerial2(const char* msg, int waitMs = 500) {
- void sendSerial2(String msg, int waitMs = 500) {
- Serial2.println(msg);
- while (Serial2.available()) {
- parseATText(Serial2.read());
- }
- delay(waitMs);
- }
- void setup()
- {
- Serial.begin(9600); // Główny Serial
- Serial1.begin(19200);
- // printer.begin(19200);
- printer.begin();
- Serial2.begin(9600); // Serial GSM
- lcd.begin(20, 4); // Uruchom LCD
- lcd.setBacklightPin(BACKLIGHT_PIN, POSITIVE); // Podświetlenie lcd to działa
- lcd.setBacklight(HIGH); // Podświetlenie lcd to działa
- SPI.begin(); // Init SPI bus
- rfid.PCD_Init(); // Init MFRC522
- lcd.setCursor(3, 0);
- lcd.print("AIRSOFT PRINTER");
- lcd.setCursor(9, 1);
- lcd.print("BY");
- lcd.setCursor(2, 2);
- lcd.print("VIKTOR VAN DARMAN");
- lcd.setCursor(11, 3);
- lcd.print("ver 1.0.0");
- // sim();
- Serial.println(F("Arduino RFID tutorial"));
- sendSerial2("AT+SAPBR=3,1,\"APN\",\"internet\"");
- sendSerial2("AT+SAPBR=1,1", 3000);
- sendSerial2("AT+HTTPINIT");
- sendSerial2("AT+HTTPPARA=\"CID\",1");
- sendSerial2("AT+HTTPPARA=\"URL\",\"http://watacha.tk/czytnik/?get,2191962300\"");
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Przyluz brylok z ");
- lcd.setCursor(0, 1);
- lcd.print("zadaniem do czytnika");
- }
- void loop() {
- // Serial.println("loop");
- if ( rfid.PICC_IsNewCardPresent())
- {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Przyluz brylok z ");
- lcd.setCursor(0, 1);
- lcd.print("zadaniem do czytnika");
- // lcd.setCursor(0, 2);
- // lcd.print(buffer);
- Serial.println("Odczyt RFID");
- readRFID();
- delay(5000);
- lcd.setCursor(0, 3);
- lcd.print(" ");
- }
- }
- void odczyt() {
- Serial.println("odczyt");
- unsigned long now = millis();
- if ( actionState == AS_IDLE ) {
- if ( now > lastActionTime + 5000 ) {
- Serial.println("Rozpoczynam pobieranie danych");
- Dane = "AT+HTTPPARA";
- Dane += "=";
- Dane += "\"";
- Dane += "URL\"";
- Dane += ",";
- Dane += "\"";
- Dane += Adres_odczytu;
- Dane += uidString;
- //Dane += "2191962300";
- Dane += "\"" ;
- Serial.println(Dane);
- sendSerial2(Dane);
- // sendSerial2("AT+HTTPPARA=\"URL\",\"http://watacha.tk/czytnik/?get,2191962300\"");
- delay(10000);
- sendSerial2("AT+HTTPACTION=0");
- lastActionTime = now;
- actionState = AS_WAITING_FOR_RESPONSE;
- }
- }
- while (Serial2.available()) {
- // Serial.println("odczyt gsm");
- lastActionTime = now;
- parseATText(Serial2.read());
- }
- }
- void parseATText(byte b) {
- buffer[pos++] = b;
- if ( pos >= sizeof(buffer) )
- resetBuffer(); // just to be safe
- /*
- // Detailed debugging
- Serial.println();
- // Serial.print("state = ");
- // Serial.println(state);
- Serial.print("b = ");
- Serial.println(b);
- Serial.print("pos = ");
- Serial.println(pos);
- Serial.print("buffer = ");
- Serial.println(buffer);
- */
- switch (parseState) {
- case PS_DETECT_MSG_TYPE:
- {
- if ( b == '\n' )
- resetBuffer();
- else {
- if ( pos == 3 && strcmp(buffer, "AT+") == 0 ) {
- parseState = PS_IGNORING_COMMAND_ECHO;
- }
- else if ( b == ':' ) {
- //Serial.print("Checking message type: ");
- //Serial.println(buffer);
- if ( strcmp(buffer, "+HTTPACTION:") == 0 ) {
- Serial.println("Received HTTPACTION");
- parseState = PS_HTTPACTION_TYPE;
- }
- else if ( strcmp(buffer, "+HTTPREAD:") == 0 ) {
- Serial.println("Received HTTPREAD");
- parseState = PS_HTTPREAD_LENGTH;
- }
- resetBuffer();
- }
- }
- }
- break;
- case PS_IGNORING_COMMAND_ECHO:
- {
- if ( b == '\n' ) {
- lcd.clear();
- lcd.setCursor(0, 0);
- lcd.print("Uruchamiam GSM");
- lcd.setCursor(0, 1);
- lcd.print(buffer);
- Serial.print("Ignoring echo: ");
- Serial.println(buffer);
- parseState = PS_DETECT_MSG_TYPE;
- resetBuffer();
- }
- }
- break;
- case PS_HTTPACTION_TYPE:
- {
- if ( b == ',' ) {
- Serial.print("HTTPACTION type is ");
- Serial.println(buffer);
- parseState = PS_HTTPACTION_RESULT;
- resetBuffer();
- }
- }
- break;
- case PS_HTTPACTION_RESULT:
- {
- if ( b == ',' ) {
- Serial.print("HTTPACTION result is ");
- Serial.println(buffer);
- parseState = PS_HTTPACTION_LENGTH;
- resetBuffer();
- }
- }
- break;
- case PS_HTTPACTION_LENGTH:
- {
- if ( b == '\n' ) {
- Serial.print("HTTPACTION length is ");
- Serial.println(buffer);
- // now request content
- Serial2.print("AT+HTTPREAD=0,");
- Serial2.println(buffer);
- parseState = PS_DETECT_MSG_TYPE;
- resetBuffer();
- }
- }
- break;
- case PS_HTTPREAD_LENGTH:
- {
- if ( b == '\n' ) {
- contentLength = atoi(buffer);
- Serial.print("HTTPREAD length is ");
- Serial.println(contentLength);
- Serial.print("HTTPREAD content: ");
- parseState = PS_HTTPREAD_CONTENT;
- resetBuffer();
- }
- }
- break;
- case PS_HTTPREAD_CONTENT:
- {
- // for this demo I'm just showing the content bytes in the serial monitor
- Serial.write(b);
- contentLength--;
- if ( contentLength <= 0 ) {
- // all content bytes have now been read
- parseState = PS_DETECT_MSG_TYPE;
- resetBuffer();
- Serial.print("\n\n\n");
- actionState = AS_IDLE;
- }
- }
- break;
- }
- }
- void readRFID()
- {
- rfid.PICC_ReadCardSerial();
- Serial.print(F("\nPICC type: "));
- MFRC522::PICC_Type piccType = rfid.PICC_GetType(rfid.uid.sak);
- Serial.println(rfid.PICC_GetTypeName(piccType));
- // Check is the PICC of Classic MIFARE type
- if (piccType != MFRC522::PICC_TYPE_MIFARE_MINI &&
- piccType != MFRC522::PICC_TYPE_MIFARE_1K &&
- piccType != MFRC522::PICC_TYPE_MIFARE_4K) {
- Serial.println(F("Your tag is not of type MIFARE Classic."));
- return;
- }
- Serial.println("Scanned PICC's UID:");
- // printDec(rfid.uid.uidByte, rfid.uid.size);
- //uidString = String(rfid.uid.uidByte[0]) + " " + String(rfid.uid.uidByte[1]) + " " + String(rfid.uid.uidByte[2]) + " " + String(rfid.uid.uidByte[3]);
- uidString = String(rfid.uid.uidByte[0]) + String(rfid.uid.uidByte[1]) + String(rfid.uid.uidByte[2]) + String(rfid.uid.uidByte[3]);
- // drukarka_zadan = ODBIERZ_DANE;
- // if (in) {
- lcd.setCursor(0, 3);
- lcd.print(uidString);
- Serial.println(uidString);
- // odczyt();
- // naglowek();
- // odbierz_dane();
- // Serial.println(in);
- ////printer.println(in);
- // stopka();
- // }
- }
- void printDec(byte * buffer, byte bufferSize) {
- for (byte i = 0; i < bufferSize; i++) {
- Serial.print(buffer[i] < 0x10 ? " 0" : " ");
- Serial.print(buffer[i], DEC);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement