Advertisement
albertbrucelee

Untitled

Apr 24th, 2019
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.34 KB | None | 0 0
  1. //tambahin fungsi:
  2. //fungsi send data to esp
  3. boolean sendData(String command, char response[], const int timeout) {
  4.     esp8266.print(command); // send the read character to the esp8266
  5.     Serial.println(">"+command);
  6.     long int time = millis();
  7.     while( (time+timeout) > millis()) {
  8.         while(esp8266.available()) {
  9.             if(esp8266.find(response)){
  10.                 Serial.println("Response found");
  11.                 return true;
  12.             }
  13.             else if(esp8266.find("busy")){    //If device is busy...
  14.                 Serial.println("busy...");
  15.                 wifi_reset();
  16.                 waitResponse("ready",5000);
  17.                 return false;
  18.             }
  19.         }  
  20.     }
  21.     Serial.println("Response NOT found");
  22.     return false;
  23. }
  24.  
  25. //## reset the esp8266 by digitalwrite pin
  26. void wifi_reset(){
  27.     digitalWrite(RST,LOW);
  28.     delay(100);
  29.     digitalWrite(RST,HIGH);
  30.     delay(1000);
  31.     Serial.println("##### ESP8266 Reset #####");
  32. }
  33.  
  34.  
  35. //kode untuk kirim data di main:
  36. main(){
  37.     ...
  38.     String cmd="AT+CIPSEND=";
  39.     cmd+= getStr.length();
  40.     cmd+= "\r\n";
  41.  
  42.     //timeout 10000
  43.     if(sendData(cmd,">",10000)){
  44.         //berhasil
  45.     } else {
  46.         //gagal
  47.         Serial.println("connection timeout");
  48.         sendData("AT+CIPCLOSE\r\n","OK",1000);
  49.         return;
  50.     }
  51.     ...
  52. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement