Advertisement
andrewmovic

123RSEND

Oct 11th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.31 KB | None | 0 0
  1. // SENDING READED FILES TO SERVER USING API HTTP
  2. boolean readSend(char* fn) {
  3.    TinyGsmClient client;
  4.    boolean readedData = false;
  5.    boolean requested = false;
  6.    boolean statusReply = false;
  7.    byte savedReply = 0;
  8.    String dt = "";
  9.    byte ci = 0;           // cursor
  10.    byte authReply ;
  11.    
  12.    // Read sdcard
  13.    File myFile = SD.open(fn,FILE_READ);
  14.    if(!myFile) {
  15.       return;
  16.    }
  17.    // While files are available
  18.    while(myFile.available()){
  19.        dt = myFile.readStringUntil('\n');
  20.        readedData = true;
  21.    }
  22.  
  23.    // debug readed local file
  24.    Serial.print(dt);
  25.    
  26.    // send it to server
  27.    if(client.connect(server,80)) {
  28.       client.print("GET /feed/api2.php?K=");
  29.       client.print(myKey);
  30.       client.print("&");
  31.       client.print(dt);
  32.       client.print(" HTTP/1.0\r\n\r\n");
  33.       client.print("Host: ");
  34.       client.println(server);
  35.       client.print("User-Agent: Arduino-agrieye\n");
  36.       client.println("Connection: close");
  37.       //client.println("Content-Type: text/html");
  38.       client.println();
  39.       requested = true;
  40.    } else {
  41.      myFile.close();
  42.      client.stop();
  43.      return false;
  44.    }
  45.    
  46.   // check the feedback
  47.   if(requested) {
  48.     Serial.print("Requested2\n");
  49.     boolean fin = false;
  50.     unsigned long timeoutRead = millis();
  51.     while(client.connected() && millis() - timeoutRead < 70000) {
  52.         while(client.available() > 0) {
  53.             char inChar = client.read();
  54.             Serial.print(inChar);
  55.                    
  56.             // find the # two times
  57.             if(inChar == '#') {
  58.                ci++;
  59.             }
  60.  
  61.    
  62.             if((ci == 2) && (fin == false)) {
  63.               // read value using String
  64.               String ar = client.readStringUntil('\n');
  65.               String sr = client.readStringUntil('\n');
  66.              
  67.               Serial.print("ar: ");
  68.               Serial.println(ar);
  69.               Serial.print("sr: ");
  70.               Serial.println(sr);
  71.              
  72.               // convert the value into number
  73.               authReply = ar.toInt();
  74.               savedReply = sr.toInt();
  75.               statusReply = true;
  76.               fin = true;
  77.             }
  78.                            
  79.           }
  80.          
  81.         }
  82.         // close client
  83.         client.stop();
  84.     }
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement