Advertisement
doublemintben

arduino.ino

Mar 16th, 2017
171
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.79 KB | None | 0 0
  1. #include <Arduino.h>
  2. #include <PLDuino.h>
  3. #define ESP_BAUD 115200
  4. #define BAUD3 9600
  5.  
  6. String getResponse()
  7. {
  8.   String response = "";
  9.   for(int i=0; i<10; ++i)
  10.   {
  11.     while(Serial3.available())
  12.       response += char(Serial3.read());
  13.     delay(100);
  14.   }
  15.   return response;
  16. }
  17.  
  18. String getESPResponse()
  19. {
  20.   String response = "";
  21.   for(int i=0; i<10; ++i)
  22.   {
  23.     while(Serial2.available())
  24.       response += char(Serial2.read());
  25.     delay(100);
  26.   }
  27.   return response;
  28. }
  29.  
  30. bool waitUntilStringReceived (String ptn, int timeout)
  31. {
  32.   String response = "";
  33.   long start_time = millis();
  34.   while(millis() - start_time < timeout)
  35.   {
  36.     if (Serial2.available())
  37.     {
  38.       response += (char)Serial2.read();
  39.       if (response.length() >= ptn.length())
  40.       {
  41.         response = response.substring(response.length() - ptn.length());
  42.         if (response == ptn) return true;
  43.       }
  44.     }
  45.   }
  46.   return false;
  47. }
  48.  
  49. void cancelWiFiTestStartup()
  50. {
  51.   if (waitUntilStringReceived("type \"qw\"", 10000))
  52.   {
  53.     Serial.println("sending qw...");
  54.     Serial2.println("qwqwqw;\n");
  55.     Serial.flush();
  56.   }
  57.   Serial2.flush();
  58. }
  59.  
  60. void setup()
  61. {
  62.   using namespace PLDuino;
  63.  
  64.   PLDuino::init();
  65.   enableESP();
  66.   Serial2.begin(ESP_BAUD);
  67.   Serial.begin(ESP_BAUD);
  68.   Serial3.begin(BAUD3);
  69.   cancelWiFiTestStartup();
  70. }
  71.  
  72. void loop()
  73. {
  74.   String response = "";
  75.   String ESPResponse = "";
  76.   response = getResponse();
  77.   ESPResponse = getESPResponse();
  78.  
  79.   if(response != "")
  80.   {
  81.     Serial2.println(response);
  82.     //Serial2.println("12345");
  83.     Serial.println(response);
  84.     Serial.flush();
  85.     Serial2.flush();
  86.     Serial3.flush();
  87.   }
  88.  
  89.   if(ESPResponse != "")
  90.   {
  91.     Serial.println(ESPResponse);
  92.     Serial.flush();
  93.     Serial2.flush();
  94.     Serial3.flush();
  95.   }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement