Advertisement
Utshaw

Serial print e prob.need flush

Jul 15th, 2018
87
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. #include<SoftwareSerial.h>
  2. #include<stdio.h>
  3. #include<string.h>
  4.  
  5. SoftwareSerial GPRS(7,8); //Rx , Tx
  6.  
  7. int lockPin = 12;
  8. int unlockPin = 11;
  9. unsigned char buffer[64];
  10. unsigned char bufferReader[64];
  11. int count = 0;
  12. int bufferReaderCounter = 0;
  13.  
  14. void GSMInit(){
  15. char * writeQuery = "AT+HTTPPARA=\"URL\",\"http://198.211.96.87/v1/index_Cycle.php/locstate?lat=29.64&lng=11.33&bk=1\"\r\n";
  16. GPRS.write(writeQuery);
  17. delay(500);
  18.  
  19. GPRS.write("AT+CREG?\r\n");
  20. delay(500);
  21.  
  22. GPRS.write("AT+CGATT=1\r\n"); // Attach to GPRS
  23. delay(500);
  24.  
  25. GPRS.write("AT+SAPBER=3,1,\"APN\",\"gpinternet\"\r\n"); // To query the GPRS context
  26. delay(200);
  27.  
  28. GPRS.write("AT+SAPBR=1,1\r\n"); // Open a GPRS context
  29. delay(500);
  30.  
  31. GPRS.write("AT+HTTPACTION=1\r\n");
  32. delay(1000);
  33.  
  34.  
  35. while(GPRS.available())
  36. {
  37. buffer[count++] = GPRS.read();
  38. if(count == 64) break;
  39. }
  40. //Serial.println(buffer);
  41. //Serial.write(buffer , count);
  42. clearBufferArray();
  43. count = 0;
  44. //Serial.write("\r\n ... ... \r\n");
  45. // delay(1000);
  46.  
  47. /*while(1)
  48. {
  49. if(GPRS.available()){
  50. if(GPRS.read()!='+'){continue;}
  51. else{
  52. buffer[count++] = GPRS.read();
  53. Serial.write("Something");
  54. break;
  55. }
  56. if(count == 64) break;
  57. }
  58. }
  59. Serial.write(buffer , count);
  60. //clearBufferArray();
  61. count = 0;
  62.  
  63. */
  64. // Serial.write("\r\n ... ... \r\n");
  65.  
  66.  
  67. //GPRS.write("AT+HTTPREAD=0,7\r\n"); // Open a GPRS context
  68. // delay(700);
  69. }
  70.  
  71. void setup() {
  72.  
  73. pinMode(LED_BUILTIN, OUTPUT);//blink led for testing
  74. pinMode(lockPin, OUTPUT);
  75. pinMode(unlockPin, OUTPUT);
  76.  
  77. // put your setup code here, to run once:
  78. GPRS.begin(9600);
  79. Serial.begin(9600);
  80.  
  81. GPRS.write("AT\r\n");
  82. delay(200);
  83.  
  84. GPRS.write("AT+HTTPINIT\r\n");
  85. delay(200);
  86.  
  87. GPRS.write("AT+HTTPPARA=\"CID\",1\r\n");
  88. delay(500);
  89. //https://onlinesohopathi.com/v1/index_Cycle.php/updateloc?lat=29.64&lng=11.33&bk=1
  90.  
  91.  
  92.  
  93. }
  94.  
  95. void loop() {
  96. Serial.println("------------------MAIN LOOP---------------------------------------------------");
  97.  
  98. GSMInit();
  99. delay(1000);
  100. while(1){
  101.  
  102. Serial.println("MASTER SANDWICH-----------------------------------------------------------------------------------------------");
  103. count=0;
  104. bufferReaderCounter = 0;
  105.  
  106. while(GPRS.available())
  107. {
  108. //Serial.println("Available --> ");
  109. //buffer[count++] = GPRS.read();
  110. //Serial.println(buffer[count-1]);
  111. if(GPRS.read() == '+'){
  112. // Serial.println("--------------Found plus ------------");
  113. while(GPRS.available()){
  114. buffer[count++] = GPRS.read();
  115. // Serial.println(buffer[count-1]);
  116. }
  117. }
  118. else{
  119. continue;
  120. }
  121. if(count == 64) break;
  122. }
  123.  
  124. //Serial.println("Wrinting from buffer ..........................");
  125.  
  126. char *p;
  127. p = strtok(buffer, ",");
  128. p = strtok(NULL, ",");
  129. p = strtok(NULL, ",");
  130.  
  131.  
  132.  
  133. if(buffer[0] != NULL){
  134. //int i;
  135. //sscanf(p, "%d", &i);
  136. //Serial.print("Found buffer array size: ");
  137. //Serial.println(i);
  138.  
  139. char * readContent = "AT+HTTPREAD=0,";
  140. strcat(p, "\r\n");
  141. strcat(readContent, p);
  142. GPRS.write(readContent);
  143. delay(200);
  144.  
  145.  
  146. char flag = 'f';
  147. while(flag == 'f' && !GPRS.available()){
  148.  
  149. }
  150. flag = 't';
  151.  
  152. char temp[20];
  153. char newTemp[33];
  154. int ntemp = 0;
  155. int itemp = 0;
  156. while(GPRS.available()){
  157. char rr = GPRS.read();
  158. if(rr >= 'a' && rr <= 'z'){
  159. newTemp[ntemp++] = rr;
  160. }
  161. }
  162. /*
  163. while(GPRS.available()){
  164. char rr = GPRS.read();
  165. if(rr >= 'a' && rr <= 'z'){
  166. newTemp[ntemp++] = rr;
  167. }
  168. if(rr == '\r' || rr == '\n'){
  169. continue;
  170. }
  171.  
  172. flag = 't';
  173. bufferReader[bufferReaderCounter++] = rr;
  174. if(rr == 'u' && flag == 't'){
  175. temp[itemp++] = rr;
  176. while(GPRS.available()){
  177. rr = GPRS.read();
  178. if(rr == '\r' || rr == '\n'){
  179. continue;
  180. }
  181. bufferReader[bufferReaderCounter++] = rr;
  182. temp[itemp++] = rr;
  183. if(rr == 'k'){
  184. flag = 'f';
  185. break;
  186. }
  187. }
  188. }
  189. else if(rr == 'l' && flag == 't'){
  190. temp[itemp++] = rr;
  191. while(GPRS.available()){
  192. rr = GPRS.read();
  193. if(rr == '\r' || rr == '\n'){
  194. continue;
  195. }
  196. bufferReader[bufferReaderCounter++] = rr;
  197. temp[itemp++] = rr;
  198. if(rr == 'k'){
  199. flag = 'f';
  200. break;
  201. }
  202. }
  203. }
  204. }*/
  205. newTemp[ntemp] = '\0';
  206. temp[itemp] = '\0';
  207. bufferReader[bufferReaderCounter] = '\0';
  208.  
  209. //Serial.println("DEF");
  210. //Serial.write(bufferReader,bufferReaderCounter);
  211. //Serial.println("DEF");
  212. char* pch = strstr(bufferReader,"unlock"); // find "unlock" inside bufferReader
  213.  
  214.  
  215.  
  216. if( strstr(bufferReader,"unlock") != NULL){
  217. //Serial.println("Got unlock inside bufferedReader ");
  218. digitalWrite(LED_BUILTIN, HIGH); // turn the LED on (HIGH is the voltage level)
  219. //delay(1000); // wait for a second
  220. digitalWrite(LED_BUILTIN, LOW); // turn the LED off by making the voltage LOW
  221. //delay(1000); // wait for a second
  222. }
  223.  
  224. //Serial.println("ABCDEF");
  225. // Serial.write(bufferReader,bufferReaderCounter);
  226. // Serial.println("ABCDEF");
  227. // Serial.println(temp);
  228. // Serial.println("ABCDEF");
  229. if(temp[0] == 'u'){
  230. // Serial.println("Unlock hoiseeeeeeee");
  231. }else if(temp[0] == 'l'){
  232. // Serial.println("Lock hoiseeeeeeee");
  233. }else if(temp[0] == 'n'){
  234. // Serial.println("No changed hoiseeeeeeee");
  235. }
  236. // Serial.println("ABCDEF");
  237.  
  238. // Serial.println("SANDWICH");
  239. if(strcmp(temp, "unlock") == 0){
  240. // Serial.println("Sandwiched unlock");
  241. digitalWrite(unlockPin, HIGH);
  242. digitalWrite(lockPin, LOW);
  243.  
  244. }else if(strcmp(temp, "lock") == 0){
  245. // Serial.println("Sandwiched lock");
  246.  
  247. digitalWrite(unlockPin, LOW);
  248. digitalWrite(lockPin, HIGH);
  249.  
  250.  
  251. }else if(strcmp(temp, "nock") == 0){
  252. // Serial.println("Sandwiched nock");
  253. }
  254. // Serial.println("SANDWICH");
  255.  
  256.  
  257.  
  258.  
  259. Serial.println("EXTRA SANDWICH");
  260. Serial.write(newTemp, ntemp);
  261. if(strcmp(newTemp, "unlock") == 0){
  262. Serial.println("Sandwiched unlock");
  263. digitalWrite(unlockPin, HIGH);
  264. digitalWrite(lockPin, LOW);
  265. break;
  266.  
  267. }else if(strcmp(newTemp, "lock") == 0){
  268. Serial.println("Sandwiched lock");
  269.  
  270. digitalWrite(unlockPin, LOW);
  271. digitalWrite(lockPin, HIGH);
  272. break;
  273.  
  274. }else if(strcmp(newTemp, "nock") == 0){
  275. Serial.println("Sandwiched nock");
  276. break;
  277. }
  278. Serial.println("EXTRA SANDWICH");
  279.  
  280. // Serial.println("-----T ");
  281. // Serial.println("A----");
  282. //clearBufferReaderArray();
  283. //bufferReaderCounter = 0;
  284.  
  285. }
  286. // Serial.write(buffer , count);
  287. // Serial.println(i);
  288. clearBufferArray();
  289. count = 0;
  290.  
  291.  
  292.  
  293. Serial.println("MASTER SANDWICH END----------------------------------------------THE END :( -------------------------------------------------");
  294. }
  295. delay(1000);
  296. }
  297.  
  298. void clearBufferArray()
  299. {
  300. for(int i = 0; i< count ; i++)
  301. {
  302. buffer[i] = NULL;
  303.  
  304. }
  305.  
  306. }
  307.  
  308.  
  309.  
  310. void clearBufferReaderArray()
  311. {
  312. for(int i = 0; i< bufferReaderCounter ; i++)
  313. {
  314. bufferReader[i] = NULL;
  315.  
  316. }
  317.  
  318. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement