Advertisement
proximus2

Untitled

Jun 28th, 2017
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.15 KB | None | 0 0
  1. #include "SIM900.h"
  2. #include <SoftwareSerial.h>
  3.  
  4. #include "sms.h"
  5. SMSGSM sms;
  6.  
  7. int numdata;
  8. boolean started=false;
  9. char smsposition;
  10. char sms_text[160];
  11. char phone_num[20];
  12.  
  13. char primalac[256] = "+381655231300";
  14.  
  15. char Kredit[30];
  16.  
  17. //control pins of relay.
  18. int ventil_1=4;
  19. int ventil_2=5;
  20.  
  21. long trajanje_trenutnog_zalivanja = 0L;
  22. unsigned long vreme_poslednje_akcije = 0;
  23. int trenutna_akcija = 0; // 0 = svi zatvoreni, 1 = upalio prvi i drugi
  24.  
  25. void setup()
  26. {
  27. Serial.begin(9600);
  28. Serial.println("GSM Shield testing.");
  29. //Start configuration of shield with baudrate.
  30. //For http uses is raccomanded to use 4800 or slower.
  31. if (gsm.begin(2400)){
  32. Serial.println("\nstatus=READY");
  33. started=true;
  34. }
  35. else Serial.println("\nstatus=IDLE");
  36.  
  37. // Initialize PINs
  38. pinMode( 4, OUTPUT );
  39. pinMode( 5, OUTPUT );
  40. digitalWrite( 4, HIGH );
  41. digitalWrite( 5, HIGH );
  42. delay(200);
  43. }
  44.  
  45. void loop(){
  46.  
  47. proveriVentile();
  48.  
  49. Serial.println(millis());
  50.  
  51. if(started){
  52. smsposition = sms.IsSMSPresent(SMS_UNREAD);
  53. if (smsposition) {
  54. // there is new SMS => read it
  55. sms.GetSMS(smsposition, phone_num, 20, sms_text, 160);
  56. sms.DeleteSMS(smsposition);
  57. Serial.println("\nRead SMS");
  58. Serial.println(phone_num);
  59. Serial.println(sms_text);
  60. ProcessSms(sms_text, phone_num);
  61. //delay(1000);
  62.  
  63. }
  64. }
  65. }
  66. // EN: Make action based on the content of the SMS.
  67. // Notice than SMS content is the result of the processing of several GPRS shield messages.
  68.  
  69. void ProcessSms( String smsbuffer, String number ){
  70. strcpy(primalac,number.c_str());
  71. if( smsbuffer.indexOf("#stop") >= 0 ){
  72. digitalWrite( 4, HIGH );
  73. delay(200);
  74. digitalWrite( 5, HIGH );
  75. delay(200);
  76. trenutna_akcija = 0;
  77. trajanje_trenutnog_zalivanja = 0;
  78. delay(200);
  79.  
  80. if(started){
  81. sms.SendSMS(primalac, "Zalivanje je zaustavljeno.");
  82. }
  83. }
  84. if( smsbuffer.indexOf("#start") >= 0 ){
  85. if (trenutna_akcija == 0) {
  86. int spaceIndex = smsbuffer.indexOf(' ');
  87. String minutesString = smsbuffer.substring(spaceIndex);
  88. int minutes = minutesString.toInt();
  89. Serial.println(minutes);
  90. if (minutes == 0) {
  91. if(started){
  92. sms.SendSMS(primalac, "Unesite trajanje zalivanja po zoni u minutima npr. #start 10");
  93. }
  94. } else { // start sa periodom zalivanja, npr. #start 120
  95. trajanje_trenutnog_zalivanja = minutes;
  96. digitalWrite( ventil_1, LOW );
  97. vreme_poslednje_akcije = millis();
  98. trenutna_akcija = 1;
  99. char smsmessage[160];
  100. sprintf(smsmessage,"%s%d%s","Zalivanje u trajanju od ", trajanje_trenutnog_zalivanja, "min po zoni je zapoceto.");
  101. Serial.println(smsmessage);
  102. if(started){
  103. sms.SendSMS(primalac, smsmessage);
  104. }
  105. }
  106. } else {
  107. char smsmessage[160];
  108. sprintf(smsmessage,"%s%d%s","Trenutno je u toku zalivanje ", trenutna_akcija , " zone, nije moguce zapoceti novo zalivanje.");
  109. Serial.println(smsmessage);
  110. if(started){
  111. sms.SendSMS(primalac, smsmessage);
  112. }
  113. }
  114. }
  115. if( smsbuffer.indexOf("#kredit") >= 0 ){
  116. gsm.SimpleWriteln("AT+CUSD=1,\"*100#\""); //sending AT+CUSD=1,"*100#"
  117. delay(5000);
  118. char resp[125];
  119. gsm.read(resp, 125); //this command will send the response to the serial port and, at the same time, copy to "resp" string
  120. //Serial.println(resp);
  121. izvuciKredit(resp, Kredit);
  122. Serial.println(Kredit);
  123. if(started){
  124. sms.SendSMS(primalac, Kredit);
  125. }
  126. }
  127.  
  128. if( smsbuffer.indexOf("#promenibroj") >= 0 ){
  129. char tmp[256];
  130. int i, j;
  131. tmp[0] = '+';
  132. for(i=0;smsbuffer[i];i++) {
  133. j=1;
  134. while(smsbuffer[i]>='0' && smsbuffer[i]<='9') {
  135. tmp[j]=smsbuffer[i];
  136. i++;
  137. j++;
  138. }
  139. tmp[j]=0;
  140. }
  141. strncpy(primalac, tmp, 256);;
  142. Serial.println("Novi primalac je ");
  143. Serial.println(primalac);
  144. if (sms.SendSMS(primalac, "Uspesno je izvrsena promena broja.")) {
  145. Serial.println("\nSMS sent OK");
  146. }
  147. }
  148.  
  149. }
  150.  
  151. void proveriVentile() {
  152. if ((trenutna_akcija == 1) && !(millis() - vreme_poslednje_akcije < trajanje_trenutnog_zalivanja * 60000L)) {
  153. digitalWrite( ventil_1, HIGH );
  154. digitalWrite( ventil_2, LOW );
  155. trenutna_akcija = 2;
  156. vreme_poslednje_akcije = millis();
  157. Serial.println("Zalivanje prve zone je zavrseno, zapoceto zalivanje druge zone.");
  158. } else if ((trenutna_akcija == 2) && !(millis() - vreme_poslednje_akcije < trajanje_trenutnog_zalivanja * 60000L)) {
  159. digitalWrite( ventil_2, HIGH );
  160. trenutna_akcija = 0;
  161. vreme_poslednje_akcije = millis();
  162. Serial.println("Zalivanje je završeno.");
  163. if(started){
  164. sms.SendSMS(primalac, "Zalivanje je zavrseno.");
  165. }
  166. }
  167. }
  168.  
  169. int izvuciKredit (char* porukaUSSD, char* preostaloKredita)
  170. {
  171. int duzinaPoruke=0;
  172. int i=0, j=0, pocetak=-1, kraj=-1;
  173.  
  174. // your code goes here.
  175. duzinaPoruke=strlen(porukaUSSD);
  176. //Serial.print(porukaUSSD);
  177. //Serial.print("\nDuzina stringa: ");
  178. //Serial.print("%d\n",duzinaPoruke);
  179.  
  180. for(j=0; j<duzinaPoruke-5;j++)
  181. if(porukaUSSD[j]=='K')
  182. if(porukaUSSD[j+1]=='r')
  183. if(porukaUSSD[j+2]=='e')
  184. if(porukaUSSD[j+3]=='d')
  185. if(porukaUSSD[j+4]=='i')
  186. if(porukaUSSD[j+5]=='t')
  187. pocetak=j; // pocetak reci Kredit u velikoj poruci
  188.  
  189. if(pocetak==-1) return 1; // Pretrazen ceo niz i nema reci "Kredit", pa je to greska
  190.  
  191.  
  192. //Serial.print("\nPocetak Kredita: ");
  193. //Serial.print("%d\n",pocetak);
  194.  
  195. /* Ranije MTS slao din. ,a sad vise nema TACKE nego samo din pa ovo vise ne vazi
  196. for(j=0; j<duzinaPoruke-3;j++)
  197. if(porukaUSSD[j]=='d')
  198. if(porukaUSSD[j+1]=='i')
  199. if(porukaUSSD[j+2]=='n')
  200. if(porukaUSSD[j+3]=='.')
  201. kraj=j+4; // kraj reci din. u velikoj poruci
  202. */
  203.  
  204. // Novi format MTS odgovora je samo din bez tacke i ovo radi za oba slucaja dok ne udjemo u EU
  205. for(j=0; j<duzinaPoruke-2;j++)
  206. if(porukaUSSD[j]=='d')
  207. if(porukaUSSD[j+1]=='i')
  208. if(porukaUSSD[j+2]=='n')
  209. kraj=j+3; // kraj reci din u velikoj poruci
  210.  
  211. if(kraj==-1) return 2; // Pretrazen ceo niz i nema reci "din." , pa je to greska
  212.  
  213. //Serial.print("\nKraj Kredita: ");
  214. //printf("%d\n",kraj);
  215.  
  216. if(kraj<pocetak) return 3; // Moze to i bolje, nije pokriven bas svaki slucaj
  217.  
  218. for(i=0; i<kraj-pocetak; i++)
  219. preostaloKredita[i]=porukaUSSD[pocetak+i];
  220.  
  221. preostaloKredita[i]='\0';
  222.  
  223. //Serial.print("\nPreostalo kredita: ");
  224. //Serial.print("%s\n",Kredit);
  225.  
  226. return 0;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement