Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <SoftwareSerial.h>
- #include <TinyGPS.h>
- TinyGPS gps;
- SoftwareSerial ss(8, 9);
- SoftwareSerial SIM900( 4, 5);
- unsigned long czas_pracy; // zmienna do odlicania minut do zapisu stanu gry
- unsigned long lastmil; // zmienna do odlicania minut do zapisu stanu gry
- unsigned long lastmil2; // zmienna do odlicania minut do zapisu stanu gry
- unsigned long days;
- unsigned long hours;
- unsigned long mins;
- unsigned long secs;
- String Dane; // String w ktory beda wpisane kto posiada i jaki jest czas aktualny przejecia kopalni
- String Nazwa = "Fabryka_1";
- String Posiadanie = "TEST";
- String Zegar = "zegar1";
- String Adres_zapis = "http://sag-wataha.ugu.pl/dane_zapis.php?";
- String Adres_konfiguracja = "http://sag-wataha.ugu.pl/config.php?config=zegary";
- String ZiC = "00:00:00:00";
- String NC = "00:00:00:00";
- String ZoC = "00:00:00:00";
- String FC = "00:00:00:00";
- String napiecie = "13.00";
- String CSQ = "";
- String COPS = "";
- static char Dlugosc[15];
- static char Szerokosc[15];
- //String Dlugosc = "";
- //String Szerokosc = "";
- //float Dlugosc;
- //float Szerokosc;
- //GPS
- //unsigned long UPTIME ;
- String odczyt_serial = "";
- boolean koniec_odczytu = false;
- void setup() {
- Serial.begin(115200); /* Define baud rate for serial communication */
- ss.begin(9600);
- SIM900.begin(9600); /* Define baud rate for software serial communication */
- Serial.println("SETUP");
- sim();
- }
- void loop() {
- czas_pracy = millis();
- //Serial.println(czas_pracy);
- if (millis() - lastmil >= 60000) { // Wyznaczony czas 60000 odpowiada 2,5minuty 600000 odpowiada 10minutom 1800000 odpowiada 30minutom
- //wyslij_dane();
- delay(1000);
- lastmil = millis(); // Reset zegara
- }
- if (millis() - lastmil2 >= 13000) { // Wyznaczony czas 60000 odpowiada 2,5minuty 600000 odpowiada 10minutom 1800000 odpowiada 30minutom
- //odbierz_dane();
- delay(1000);
- lastmil2 = millis(); // Reset zegara
- }
- ReadGPS();
- Serial.println("");
- Serial.print(Szerokosc);
- Serial.print(",");
- Serial.println(Dlugosc);
- // wyslij_dane();
- }
- void wyslij_dane() {
- Serial.println("Rozpoczynam zbieranie danych");
- //("AT+HTTPPARA=\"URL\",\"http://sag-wataha.ugu.pl/dane_zapis.php?T=zegar1&N=GSM&P=Niebiescy\""); /* Set parameters for HTTP session */
- Dane = "AT+HTTPPARA";
- // Serial.println(Dane);
- Dane += "=";
- Dane += "\"";
- Dane += "URL\"";
- Dane += ",";
- Dane += "\"";
- Dane += Adres_zapis;
- // Serial.println(Dane);
- //Dane = Adres_zapis;
- Dane += "T=";
- //Dane += "T=";
- Dane += Zegar ;
- Dane += "&N=";
- Dane += Nazwa ;
- Dane += "&P=";
- Dane += Posiadanie ;
- Dane += "&ZiC=" ;
- Dane += ZiC ;
- Dane += "&NC=" ;
- Dane += NC ;
- Dane += "&ZoC=" ;
- Dane += ZoC ;
- Dane += "&FC=" ;
- Dane += FC ;
- Dane += "&SB=" ;
- Dane += napiecie;
- Dane += "_V";
- Dane += "&CSQ=" ;
- Serial.println(Dane);
- delay(5000);
- wyslij();
- }
- void odbierz_dane() {
- Serial.println("Rozpoczynam pobieranie danych");
- Dane = "AT+HTTPPARA";
- Dane += "=";
- Dane += "\"";
- Dane += "URL\"";
- Dane += ",";
- Dane += "\"";
- Dane += Adres_konfiguracja;
- Dane += "\"" ;
- Serial.println(Dane);
- SIM900.println(Dane);
- delay(10000);
- Serial.println("AT + HTTPACTION = 0\\r\\n");
- Serial.println("");
- SIM900.println("AT + HTTPACTION = 0");
- delay(10000);
- ShowSerialData();
- delay(10000);
- Serial.println("AT+HTTPREAD\\r\\n");
- SIM900.println("AT+HTTPREAD");
- delay(5000);
- ReadWEB ();
- delay(25000);
- // ShowSerialData();
- }
- void sim() {
- Serial.println("Aktywacja SIM");
- Serial.println("HTTP get method :");
- Serial.println("AT\\r\\n");
- SIM900.println("AT");
- delay(5000);
- ShowSerialData();
- //delay(5000);
- //Serial.println("AT+COPS?\\r\\n");
- SIM900.println("AT+COPS?");
- delay(5000);
- ReadCOPS ();
- Serial.println(COPS);
- //Serial.print("AT+CSQ\\r\\n");
- SIM900.println("AT+CSQ");
- delay(5000);
- ShowSerialData();
- //delay(5000);
- //Serial.print("AT + SAPBR = 3, 1, \"CONTYPE\",\"GPRS\"\\r\\n");
- SIM900.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
- delay(5000);
- ShowSerialData();
- //delay(5000);
- //Serial.print("AT+SAPBR=3,1,\"APN\",\"internet\"\\r\\n");
- SIM900.println("AT+SAPBR=3,1,\"APN\",\"internet\"");
- delay(5000);
- ShowSerialData();
- delay(5000);
- //Serial.print("AT+SAPBR=1,1\\r\\n");
- SIM900.println("AT+SAPBR=1,1");
- delay(5000);
- ShowSerialData();
- //delay(5000);
- //Serial.print("AT+SAPBR=2,1\\r\\n");
- SIM900.println("AT+SAPBR=2,1");
- delay(5000);
- ShowSerialData();
- //delay(5000);
- //Serial.print("AT+HTTPINIT\\r\\n");
- SIM900.println("AT+HTTPINIT");
- delay(5000);
- ShowSerialData();
- //delay(5000);
- //Serial.print("AT+HTTPPARA=\"CID\",1\\r\\n");
- SIM900.println("AT+HTTPPARA=\"CID\",1");
- delay(5000);
- ShowSerialData();
- //Serial.print("AT+CIPGSMLOC=2,1\\r\\n");
- SIM900.println("AT+CIPGSMLOC=2,1");
- delay(8000);
- ShowSerialData();
- //Serial.print("AT+CIPGSMLOC=1,1\\r\\n");
- SIM900.println("AT+CIPGSMLOC=1,1");
- delay(8000);
- ShowSerialData();
- delay(8000);
- SIM900.println("AT+CIPGSMLOC=2,1");
- delay(10000);
- ShowSerialData();
- delay(5000);
- }
- void wyslij() {
- Serial.println("Wyslij Dane :");
- // Serial.println("AT+CSQ\\r\\n");
- SIM900.println("AT+CSQ");
- delay(5000);
- ReadSignalPower ();
- //Serial.println(CSQ);
- //Serial.println(Dane);
- uptime();
- Dane += CSQ;
- Dane += "&U=";
- 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;
- }
- // Serial.println(Dane);
- // Serial.println("AT+COPS?\\r\\n");
- SIM900.println("AT+COPS?");
- delay(5000);
- // ShowSerialData();
- ReadCOPS ();
- // Serial.println(COPS);
- Dane += "&C=" ;
- Dane += COPS ;
- // SIM900.println("AT+CIPGSMLOC=1,1");
- // delay(10000);
- // ReadGPS ();
- //ShowSerialData();
- Serial.println("");
- delay(15000);
- Dane += "&D=" ;
- Dane += Dlugosc ;
- Dane += "&S=" ;
- Dane += Szerokosc ;
- Dane += "\"" ;
- Serial.println(Dane);
- SIM900.println(Dane);
- delay(5000);
- CSQ = "";
- ShowSerialData();
- delay(5000);
- //Serial.println("AT+CIPGSMLOC=2,1\\r\\n");
- //SIM900.println("AT+CIPGSMLOC=2,1");
- // delay(10000);
- // ShowSerialData();
- // delay(10000);
- // Serial.println("AT+CIPGSMLOC=1,1\\r\\n");
- // ShowSerialData();
- // ShowSerialData();
- // delay(5000);
- Serial.println("AT + HTTPACTION = 0\\r\\n");
- Serial.println("");
- SIM900.println("AT + HTTPACTION = 0");
- delay(10000);
- ShowSerialData();
- }
- void ShowSerialData()
- {
- while (SIM900.available() != 0) /* If data is available on serial port */
- Serial.write(char (SIM900.read())); /* Print character received on to the serial monitor */
- }
- void ReadCOPS ()
- {
- String dane = "";
- while (SIM900.available() != 0) {
- dane += char (SIM900.read());
- }
- COPS = (dane.substring(dane.indexOf('"') + 1, dane.length() - 9));
- // COPS = dane;
- // Serial.print("dane : ");
- // Serial.println(dane);
- // Serial.print("cops : ");
- // Serial.println(COPS);
- }
- void ReadGPS ()
- {
- bool newData = false;
- unsigned long chars;
- unsigned short sentences, failed;
- while (ss.available())
- {
- char c = ss.read();
- // Serial.print(c);
- // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
- if (gps.encode(c)) // Did a new valid sentence come in?
- newData = true;
- }
- if (newData)
- {
- float flat, flon;
- unsigned long age;
- gps.f_get_position(&flat, &flon, &age);
- // Serial.print("SZEROKOSC=");
- //Serial.println(" ");
- // Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
- //Serial.print(" ");
- // Serial.print(" DLUGOSC=");
- // Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
- dtostrf(flon, 6, 6, Dlugosc);
- //Dlugosc = String(flon);
- dtostrf(flat, 6, 6, Szerokosc);
- // Szerokosc = String(flat);
- // Dlugosc = flon;
- // Serial.println("");
- // Serial.print(Szerokosc);
- // Serial.print(",");
- // Serial.println(Dlugosc);
- //Serial.print(" ILOSC SATELITOW=");
- //Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
- //Serial.print(" PREC=");
- //Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
- //Serial.println("");
- }
- }
- //void ReadGPS ()
- //{
- // String dane = "";
- //
- // while (SIM900.available() != 0) {
- // dane += char (SIM900.read());
- // }
- // Dlugosc = (dane.substring(dane.indexOf(':') + 4, dane.length() - 20));
- // Szerokosc = (dane.substring(dane.indexOf(':') + 14, dane.length() - 10));
- //
- // // COPS = dane;
- // Serial.print("Odczyt : ");
- // Serial.println(dane);
- // Serial.print("dlugosc : ");
- // Serial.println(Dlugosc);
- // Serial.print("szerokosc : ");
- // Serial.println(Szerokosc);
- //
- //}
- void ReadSignalPower ()
- {
- String dane = "";
- while (SIM900.available() != 0) {
- dane += char (SIM900.read());
- }
- CSQ = (dane.substring(dane.indexOf(':') + 2, dane.length() - 8));
- // COPS = dane;
- Serial.print("Odczyt : ");
- Serial.println(dane);
- Serial.print("csq : ");
- Serial.println(CSQ);
- }
- void ReadWEB ()
- {
- String dane = "";
- while (SIM900.available() != 0) {
- dane += char (SIM900.read());
- }
- // CSQ = (dane.substring(dane.indexOf(':') + 2, dane.length() - 8));
- // COPS = dane;
- Serial.print("Odczyt : ");
- Serial.println(dane);
- // Serial.print("csq : ");
- // Serial.println(CSQ);
- }
- 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
- //Display results
- // Serial.print(days);
- // Serial.print(" days and : ");
- // Serial.print(hours);
- // Serial.print(": ");
- // Serial.print(mins);
- // Serial.print(": ");
- // Serial.print(secs);
- // Serial.println("");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement