Advertisement
ViktorVanDarma

sim900a

May 25th, 2019
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 10.84 KB | None | 0 0
  1. #include <SoftwareSerial.h>
  2. #include <TinyGPS.h>
  3.  
  4. TinyGPS gps;
  5. SoftwareSerial ss(8, 9);
  6.  
  7. SoftwareSerial SIM900( 4, 5);
  8.  
  9. unsigned long czas_pracy; // zmienna do odlicania minut do zapisu stanu gry
  10.  
  11. unsigned long lastmil; // zmienna do odlicania minut do zapisu stanu gry
  12. unsigned long lastmil2; // zmienna do odlicania minut do zapisu stanu gry
  13.  
  14.  
  15. unsigned long days;
  16. unsigned long hours;
  17. unsigned long mins;
  18. unsigned long secs;
  19.  
  20.  
  21.  
  22.  
  23. String Dane; // String w ktory beda wpisane kto posiada i jaki jest czas aktualny przejecia kopalni
  24. String Nazwa = "Fabryka_1";
  25. String Posiadanie = "TEST";
  26. String Zegar = "zegar1";
  27. String Adres_zapis = "http://sag-wataha.ugu.pl/dane_zapis.php?";
  28. String Adres_konfiguracja = "http://sag-wataha.ugu.pl/config.php?config=zegary";
  29.  
  30.  
  31. String ZiC = "00:00:00:00";
  32. String NC = "00:00:00:00";
  33. String ZoC = "00:00:00:00";
  34. String FC = "00:00:00:00";
  35. String napiecie = "13.00";
  36.  
  37. String CSQ = "";
  38. String COPS = "";
  39. static char Dlugosc[15];
  40. static char Szerokosc[15];
  41. //String Dlugosc = "";
  42. //String Szerokosc = "";
  43. //float Dlugosc;
  44. //float Szerokosc;
  45. //GPS
  46.  
  47.  
  48. //unsigned long UPTIME ;
  49.  
  50. String odczyt_serial = "";
  51. boolean koniec_odczytu = false;
  52.  
  53. void setup() {
  54. Serial.begin(115200); /* Define baud rate for serial communication */
  55.  
  56. ss.begin(9600);
  57. SIM900.begin(9600); /* Define baud rate for software serial communication */
  58.  
  59. Serial.println("SETUP");
  60. sim();
  61. }
  62.  
  63. void loop() {
  64. czas_pracy = millis();
  65. //Serial.println(czas_pracy);
  66.  
  67. if (millis() - lastmil >= 60000) { // Wyznaczony czas 60000 odpowiada 2,5minuty 600000 odpowiada 10minutom 1800000 odpowiada 30minutom
  68. //wyslij_dane();
  69. delay(1000);
  70. lastmil = millis(); // Reset zegara
  71.  
  72. }
  73. if (millis() - lastmil2 >= 13000) { // Wyznaczony czas 60000 odpowiada 2,5minuty 600000 odpowiada 10minutom 1800000 odpowiada 30minutom
  74. //odbierz_dane();
  75. delay(1000);
  76. lastmil2 = millis(); // Reset zegara
  77.  
  78. }
  79. ReadGPS();
  80. Serial.println("");
  81. Serial.print(Szerokosc);
  82. Serial.print(",");
  83. Serial.println(Dlugosc);
  84.  
  85. // wyslij_dane();
  86.  
  87. }
  88.  
  89.  
  90. void wyslij_dane() {
  91. Serial.println("Rozpoczynam zbieranie danych");
  92. //("AT+HTTPPARA=\"URL\",\"http://sag-wataha.ugu.pl/dane_zapis.php?T=zegar1&N=GSM&P=Niebiescy\""); /* Set parameters for HTTP session */
  93. Dane = "AT+HTTPPARA";
  94. // Serial.println(Dane);
  95.  
  96. Dane += "=";
  97. Dane += "\"";
  98. Dane += "URL\"";
  99. Dane += ",";
  100. Dane += "\"";
  101. Dane += Adres_zapis;
  102.  
  103. // Serial.println(Dane);
  104.  
  105. //Dane = Adres_zapis;
  106. Dane += "T=";
  107. //Dane += "T=";
  108. Dane += Zegar ;
  109. Dane += "&N=";
  110. Dane += Nazwa ;
  111. Dane += "&P=";
  112. Dane += Posiadanie ;
  113. Dane += "&ZiC=" ;
  114. Dane += ZiC ;
  115. Dane += "&NC=" ;
  116. Dane += NC ;
  117. Dane += "&ZoC=" ;
  118. Dane += ZoC ;
  119. Dane += "&FC=" ;
  120. Dane += FC ;
  121. Dane += "&SB=" ;
  122. Dane += napiecie;
  123. Dane += "_V";
  124.  
  125. Dane += "&CSQ=" ;
  126.  
  127.  
  128. Serial.println(Dane);
  129. delay(5000);
  130. wyslij();
  131. }
  132. void odbierz_dane() {
  133. Serial.println("Rozpoczynam pobieranie danych");
  134. Dane = "AT+HTTPPARA";
  135. Dane += "=";
  136. Dane += "\"";
  137. Dane += "URL\"";
  138. Dane += ",";
  139. Dane += "\"";
  140. Dane += Adres_konfiguracja;
  141. Dane += "\"" ;
  142.  
  143. Serial.println(Dane);
  144.  
  145. SIM900.println(Dane);
  146.  
  147. delay(10000);
  148. Serial.println("AT + HTTPACTION = 0\\r\\n");
  149. Serial.println("");
  150. SIM900.println("AT + HTTPACTION = 0");
  151. delay(10000);
  152.  
  153. ShowSerialData();
  154. delay(10000);
  155. Serial.println("AT+HTTPREAD\\r\\n");
  156. SIM900.println("AT+HTTPREAD");
  157. delay(5000);
  158. ReadWEB ();
  159. delay(25000);
  160. // ShowSerialData();
  161.  
  162. }
  163.  
  164. void sim() {
  165. Serial.println("Aktywacja SIM");
  166. Serial.println("HTTP get method :");
  167. Serial.println("AT\\r\\n");
  168. SIM900.println("AT");
  169. delay(5000);
  170. ShowSerialData();
  171. //delay(5000);
  172. //Serial.println("AT+COPS?\\r\\n");
  173. SIM900.println("AT+COPS?");
  174. delay(5000);
  175. ReadCOPS ();
  176. Serial.println(COPS);
  177. //Serial.print("AT+CSQ\\r\\n");
  178. SIM900.println("AT+CSQ");
  179. delay(5000);
  180. ShowSerialData();
  181. //delay(5000);
  182. //Serial.print("AT + SAPBR = 3, 1, \"CONTYPE\",\"GPRS\"\\r\\n");
  183. SIM900.println("AT+SAPBR=3,1,\"CONTYPE\",\"GPRS\"");
  184. delay(5000);
  185. ShowSerialData();
  186. //delay(5000);
  187. //Serial.print("AT+SAPBR=3,1,\"APN\",\"internet\"\\r\\n");
  188. SIM900.println("AT+SAPBR=3,1,\"APN\",\"internet\"");
  189. delay(5000);
  190. ShowSerialData();
  191. delay(5000);
  192. //Serial.print("AT+SAPBR=1,1\\r\\n");
  193. SIM900.println("AT+SAPBR=1,1");
  194. delay(5000);
  195. ShowSerialData();
  196. //delay(5000);
  197. //Serial.print("AT+SAPBR=2,1\\r\\n");
  198. SIM900.println("AT+SAPBR=2,1");
  199. delay(5000);
  200. ShowSerialData();
  201. //delay(5000);
  202. //Serial.print("AT+HTTPINIT\\r\\n");
  203. SIM900.println("AT+HTTPINIT");
  204. delay(5000);
  205. ShowSerialData();
  206. //delay(5000);
  207. //Serial.print("AT+HTTPPARA=\"CID\",1\\r\\n");
  208. SIM900.println("AT+HTTPPARA=\"CID\",1");
  209. delay(5000);
  210. ShowSerialData();
  211. //Serial.print("AT+CIPGSMLOC=2,1\\r\\n");
  212. SIM900.println("AT+CIPGSMLOC=2,1");
  213. delay(8000);
  214. ShowSerialData();
  215.  
  216.  
  217. //Serial.print("AT+CIPGSMLOC=1,1\\r\\n");
  218. SIM900.println("AT+CIPGSMLOC=1,1");
  219. delay(8000);
  220. ShowSerialData();
  221. delay(8000);
  222.  
  223. SIM900.println("AT+CIPGSMLOC=2,1");
  224. delay(10000);
  225. ShowSerialData();
  226.  
  227.  
  228. delay(5000);
  229. }
  230. void wyslij() {
  231. Serial.println("Wyslij Dane :");
  232. // Serial.println("AT+CSQ\\r\\n");
  233. SIM900.println("AT+CSQ");
  234. delay(5000);
  235. ReadSignalPower ();
  236. //Serial.println(CSQ);
  237.  
  238. //Serial.println(Dane);
  239. uptime();
  240. Dane += CSQ;
  241. Dane += "&U=";
  242. if (days < 10) {
  243. Dane += "0";
  244. Dane += days;
  245. Dane += ":" ;
  246. }
  247. else
  248. {
  249. Dane += days;
  250. Dane += ":" ;
  251.  
  252. }
  253. if (hours < 10) {
  254.  
  255. Dane += "0";
  256. Dane += hours;
  257. Dane += ":" ;
  258.  
  259.  
  260. }
  261. else
  262. {
  263. Dane += hours;
  264. Dane += ":" ;
  265.  
  266. }
  267. if (mins < 10) {
  268.  
  269. Dane += "0";
  270. Dane += mins;
  271. Dane += ":" ;
  272. }
  273. else
  274. {
  275. Dane += mins;
  276. Dane += ":" ;
  277. }
  278. if (secs < 10) {
  279.  
  280. Dane += "0";
  281. Dane += secs;
  282.  
  283. }
  284. else
  285. {
  286. Dane += secs;
  287. }
  288. // Serial.println(Dane);
  289.  
  290.  
  291. // Serial.println("AT+COPS?\\r\\n");
  292. SIM900.println("AT+COPS?");
  293. delay(5000);
  294. // ShowSerialData();
  295. ReadCOPS ();
  296. // Serial.println(COPS);
  297.  
  298. Dane += "&C=" ;
  299. Dane += COPS ;
  300.  
  301. // SIM900.println("AT+CIPGSMLOC=1,1");
  302. // delay(10000);
  303. // ReadGPS ();
  304.  
  305. //ShowSerialData();
  306. Serial.println("");
  307. delay(15000);
  308.  
  309. Dane += "&D=" ;
  310. Dane += Dlugosc ;
  311. Dane += "&S=" ;
  312. Dane += Szerokosc ;
  313.  
  314.  
  315. Dane += "\"" ;
  316.  
  317.  
  318.  
  319. Serial.println(Dane);
  320.  
  321. SIM900.println(Dane);
  322.  
  323. delay(5000);
  324. CSQ = "";
  325. ShowSerialData();
  326. delay(5000);
  327. //Serial.println("AT+CIPGSMLOC=2,1\\r\\n");
  328. //SIM900.println("AT+CIPGSMLOC=2,1");
  329. // delay(10000);
  330. // ShowSerialData();
  331. // delay(10000);
  332. // Serial.println("AT+CIPGSMLOC=1,1\\r\\n");
  333.  
  334. // ShowSerialData();
  335. // ShowSerialData();
  336. // delay(5000);
  337.  
  338. Serial.println("AT + HTTPACTION = 0\\r\\n");
  339. Serial.println("");
  340. SIM900.println("AT + HTTPACTION = 0");
  341. delay(10000);
  342. ShowSerialData();
  343.  
  344. }
  345.  
  346.  
  347.  
  348.  
  349.  
  350. void ShowSerialData()
  351. {
  352. while (SIM900.available() != 0) /* If data is available on serial port */
  353.  
  354. Serial.write(char (SIM900.read())); /* Print character received on to the serial monitor */
  355.  
  356.  
  357. }
  358.  
  359.  
  360.  
  361.  
  362.  
  363.  
  364.  
  365.  
  366. void ReadCOPS ()
  367. {
  368. String dane = "";
  369.  
  370. while (SIM900.available() != 0) {
  371. dane += char (SIM900.read());
  372. }
  373. COPS = (dane.substring(dane.indexOf('"') + 1, dane.length() - 9));
  374.  
  375. // COPS = dane;
  376.  
  377. // Serial.print("dane : ");
  378. // Serial.println(dane);
  379. // Serial.print("cops : ");
  380. // Serial.println(COPS);
  381.  
  382. }
  383.  
  384. void ReadGPS ()
  385. {
  386.  
  387. bool newData = false;
  388. unsigned long chars;
  389. unsigned short sentences, failed;
  390.  
  391. while (ss.available())
  392. {
  393. char c = ss.read();
  394. // Serial.print(c);
  395. // Serial.write(c); // uncomment this line if you want to see the GPS data flowing
  396. if (gps.encode(c)) // Did a new valid sentence come in?
  397. newData = true;
  398. }
  399.  
  400.  
  401.  
  402. if (newData)
  403. {
  404. float flat, flon;
  405. unsigned long age;
  406. gps.f_get_position(&flat, &flon, &age);
  407. // Serial.print("SZEROKOSC=");
  408. //Serial.println(" ");
  409. // Serial.print(flat == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flat, 6);
  410. //Serial.print(" ");
  411. // Serial.print(" DLUGOSC=");
  412. // Serial.print(flon == TinyGPS::GPS_INVALID_F_ANGLE ? 0.0 : flon, 6);
  413. dtostrf(flon, 6, 6, Dlugosc);
  414. //Dlugosc = String(flon);
  415.  
  416. dtostrf(flat, 6, 6, Szerokosc);
  417.  
  418. // Szerokosc = String(flat);
  419. // Dlugosc = flon;
  420.  
  421. // Serial.println("");
  422. // Serial.print(Szerokosc);
  423. // Serial.print(",");
  424. // Serial.println(Dlugosc);
  425. //Serial.print(" ILOSC SATELITOW=");
  426. //Serial.print(gps.satellites() == TinyGPS::GPS_INVALID_SATELLITES ? 0 : gps.satellites());
  427. //Serial.print(" PREC=");
  428. //Serial.print(gps.hdop() == TinyGPS::GPS_INVALID_HDOP ? 0 : gps.hdop());
  429. //Serial.println("");
  430. }
  431. }
  432. //void ReadGPS ()
  433. //{
  434. // String dane = "";
  435. //
  436. // while (SIM900.available() != 0) {
  437. // dane += char (SIM900.read());
  438. // }
  439. // Dlugosc = (dane.substring(dane.indexOf(':') + 4, dane.length() - 20));
  440. // Szerokosc = (dane.substring(dane.indexOf(':') + 14, dane.length() - 10));
  441. //
  442. // // COPS = dane;
  443. // Serial.print("Odczyt : ");
  444. // Serial.println(dane);
  445. // Serial.print("dlugosc : ");
  446. // Serial.println(Dlugosc);
  447. // Serial.print("szerokosc : ");
  448. // Serial.println(Szerokosc);
  449. //
  450. //}
  451.  
  452. void ReadSignalPower ()
  453. {
  454. String dane = "";
  455.  
  456. while (SIM900.available() != 0) {
  457. dane += char (SIM900.read());
  458. }
  459. CSQ = (dane.substring(dane.indexOf(':') + 2, dane.length() - 8));
  460.  
  461. // COPS = dane;
  462. Serial.print("Odczyt : ");
  463. Serial.println(dane);
  464. Serial.print("csq : ");
  465. Serial.println(CSQ);
  466.  
  467. }
  468.  
  469. void ReadWEB ()
  470. {
  471. String dane = "";
  472.  
  473. while (SIM900.available() != 0) {
  474. dane += char (SIM900.read());
  475. }
  476. // CSQ = (dane.substring(dane.indexOf(':') + 2, dane.length() - 8));
  477.  
  478. // COPS = dane;
  479. Serial.print("Odczyt : ");
  480. Serial.println(dane);
  481. // Serial.print("csq : ");
  482. // Serial.println(CSQ);
  483.  
  484. }
  485.  
  486. void uptime()
  487. {
  488. secs = czas_pracy / 1000; //convect milliseconds to seconds
  489. mins = secs / 60; //convert seconds to minutes
  490. hours = mins / 60; //convert minutes to hours
  491. days = hours / 24; //convert hours to days
  492. secs = secs - (mins * 60); //subtract the coverted seconds to minutes in order to display 59 secs max
  493. mins = mins - (hours * 60); //subtract the coverted minutes to hours in order to display 59 minutes max
  494. hours = hours - (days * 24); //subtract the coverted hours to days in order to display 23 hours max
  495. //Display results
  496. // Serial.print(days);
  497. // Serial.print(" days and : ");
  498. // Serial.print(hours);
  499. // Serial.print(": ");
  500. // Serial.print(mins);
  501. // Serial.print(": ");
  502. // Serial.print(secs);
  503. // Serial.println("");
  504. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement