Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Biblioteki
- #include <Wire.h>
- #include <LiquidCrystal_I2C.h>
- #include <LcdBarGraphX.h>
- #include <SPI.h>
- #include <MFRC522.h>
- #include "SoftwareSerial.h"
- #define LCD_ADDRESS 0x3F
- #define DEVICE_ADDRESS_1 0x50 // Adres I2C zapasowy EEPROMU 24LC256 256K
- #define DEVICE_ADDRESS_3 0x68 // Adres I2C RTC
- #define TX_PIN 7 // Arduino transmit YELLOW WIRE labeled RX on printer
- #define RX_PIN 8 // Arduino receive GREEN WIRE labeled TX on printer
- #define SS_PIN 10
- #define RST_PIN 9
- //uruchomienia
- LiquidCrystal_I2C lcd(LCD_ADDRESS, 2, 1, 0, 4, 5, 6, 7, 3, POSITIVE); // -- creating LCD instance
- SoftwareSerial gsm(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
- MFRC522 rfid(SS_PIN, RST_PIN); // Instance of the class
- MFRC522::MIFARE_Key key;
- byte lcdNumCols = 6; // -- number of columns in the LCD
- // -- creating a 4 chars wide bars
- LcdBarGraphX lbg0(&lcd, 6, 0, 0); // -- First line at column 0
- //byte i0 = 0;
- int codeRead = 0;
- String uidString;
- String Dane; // String w ktory beda wpisane kto posiada i jaki jest czas aktualny przejecia kopalni
- //String Nazwa = "rfit";
- String Adres_zapis1 = "https://sag-wataha.org.pl/mapa/dane_zapis.php?";
- String Adres_zapis2 = "http://zegary.ugu.pl/dane_zapis.php?";
- String Adres_odczytu = "http://watacha.tk/czytnik/?get";
- String Zegar = "rfid";
- String Adres_test = "https://api.myip.com/";
- String Strona_Konfliktu = "1";
- unsigned long czas_pracy; // zmienna do odlicania minut do zapisu stanu gry
- unsigned long days;
- unsigned long hours;
- unsigned long mins;
- unsigned long secs;
- 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
- };
- 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 setup() {
- gsm.begin(9600); // Initialize SoftwareSerial
- Serial.begin(9600);
- SPI.begin(); // Init SPI bus
- lcd.begin(1, lcdNumCols);
- lcd.clear();
- rfid.PCD_Init(); // Init MFRC522
- Serial.println(F("Tokeny"));
- for (int i = 0; i < 255 ; i++)
- {
- lbg0.drawValue( i, 255);
- delay(25);
- }
- lcd.clear();
- lcd.setCursor(0, 0); // Go to column 0, row 0
- lcd.print("START");
- delay(1000);
- lcd.clear();
- sim();
- }
- void loop() {
- czas_pracy = millis();
- for (int i = 0; i < 1 ; i++)
- {
- lcd.print("SCANN");
- delay(500);
- lcd.clear();
- lcd.print("GIFT");
- delay(500);
- lcd.clear();
- }
- if ( rfid.PICC_IsNewCardPresent())
- {
- readRFID();
- }
- delay(100);
- }
- void readRFID()
- {
- rfid.PICC_ReadCardSerial();
- uidString = String(rfid.uid.uidByte[0]) + String(rfid.uid.uidByte[1]) + String(rfid.uid.uidByte[2]) + String(rfid.uid.uidByte[3]);
- Serial.println(uidString);
- for (int i = 0; i < 4 ; i++)
- {
- lcd.clear();
- lcd.print(rfid.uid.uidByte[i]);
- delay(500);
- lcd.print(rfid.uid.uidByte[i + 1]);
- delay(500);
- lcd.clear();
- }
- //
- lcd.clear();
- lcd.print("SEND..");
- Serial.println("UGU");
- wyslij_dane();
- rfid.PICC_HaltA();
- rfid.PCD_StopCrypto1();
- }
- void printDec(byte *buffer, byte bufferSize) {
- for (byte i = 0; i < bufferSize; i++) {
- Serial.print(buffer[i] < 0x10 ? " 0" : " ");
- Serial.print(buffer[i], DEC);
- }
- }
- void uptime()
- {
- secs = czas_pracy / 1000; //convect milliseconds to seconds
- mins = secs / 60; //convert seconds to minutes
- hours = mins / 60; //convert minutes to hours
- days = hours / 24; //convert hours to days
- secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
- mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
- hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
- }
- void wyslij_dane() {
- Serial.println("Rozpoczynam zbieranie danych");
- Dane = "AT+HTTPPARA";
- Dane += "=";
- Dane += "\"";
- Dane += "URL\"";
- Dane += ",";
- Dane += "\"";
- Dane += Adres_zapis2;
- // Serial.println(Dane);
- Dane += "T=";
- Dane += Zegar ;
- Dane += "&P=";
- Dane += Strona_Konfliktu ;
- Dane += "&U=";
- uptime();
- if (days < 10) {
- Dane += "0";
- Dane += days;
- Dane += ":" ;
- }
- else
- {
- Dane += days;
- Dane += ":" ;
- }
- if (hours < 10) {
- Dane += "0";
- Dane += hours;
- Dane += ":" ;
- }
- else
- {
- Dane += hours;
- Dane += ":" ;
- }
- if (mins < 10) {
- Dane += "0";
- Dane += mins;
- Dane += ":" ;
- }
- else
- {
- Dane += mins;
- Dane += ":" ;
- }
- if (secs < 10) {
- Dane += "0";
- Dane += secs;
- }
- else
- {
- Dane += secs;
- }
- Dane += "&UID=";
- Dane += uidString;
- Dane += "\"" ;
- Serial.println(Dane);
- delay(5000);
- wyslij();
- }
- void wyslij() {
- // lcd.clear();
- lcd.setCursor(2, 0); // Ustawienie pozycji kursora na wyswietlaczu
- lcd.print("WYSYLAM DANE");
- gsm.println(Dane);
- delay(5000);
- ShowSerialData();
- delay(5000);
- Serial.println("AT + HTTPACTION = 0\\r\\n");
- Serial.println("");
- gsm.println("AT + HTTPACTION = 0");
- delay(10000);
- ShowSerialData();
- }
- void ShowSerialData()
- {
- while (gsm.available() != 0) /* If data is available on serial port */
- Serial.write(char (gsm.read())); /* Print character received on to the serial monitor */
- }
- void sendGSM(String msg, int waitMs = 500) {
- gsm.println(msg);
- delay(waitMs);
- while (gsm.available()) {
- parseATText(gsm.read());
- }
- }
- void parseATText(byte b) {
- buffer[pos++] = b;
- if ( pos >= sizeof(buffer) )
- resetBuffer(); // just to be safe
- 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' ) {
- 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
- gsm.print("AT+HTTPREAD=0,");
- gsm.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();
- }
- }
- break;
- }
- }
- void sim() {
- sendGSM("AT");
- sendGSM("AT+CSQ");
- sendGSM("AT+CFUN=1");
- sendGSM("AT+CGATT=1");
- sendGSM("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
- sendGSM("AT+SAPBR=3,1,\"APN\",\"internet\"");
- sendGSM("AT+SAPBR=1,1", 3000);
- sendGSM("AT+SAPBR=2,1");
- sendGSM("AT+HTTPINIT");
- sendGSM("AT+HTTPPARA=\"CID\",1");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement