2013-03-31 75 views
3

我正在嘗試使用Arduino,Android應用程序和路由器製作無線燈光控制設備(開/關/調光)。Android to Arduino Uno + Wi-Fi屏蔽字符串通訊

我使用路由器將Arduino設置爲靜態IP 192.168.1.2。我從Android應用程序向IP地址192.168.1.2發送字符串(「1」 - off,「2」 - 降低亮度,「3」 - 增加亮度,「4」-on)。我已經連接的Arduino使用Arduino Wi-Fi shield互聯網和使用下面的代碼建立WifiServer:

char ssid[] = "NAME"; // Your network SSID (name) 
char pass[] = "PASS"; // Your network password (use for WPA, or use as key for WEP) 

int keyIndex = 0;  // Your network key Index number (needed only for WEP) 

int status = WL_IDLE_STATUS; 

WiFiServer server(23); 

boolean alreadyConnected = false; // Whether or not the client was connected previously. 

void setup() { 
    // Start serial port: 
    Serial.begin(9600); 

    // Attempt to connect to Wi-Fi network: 
    while (status != WL_CONNECTED) { 
     Serial.print("Attempting to connect to SSID: "); 
     Serial.println(ssid); 
     status = WiFi.begin(ssid, pass); 
     // Wait 10 seconds for connection: 
     delay(10000); 
    } 
    // Start the server: 
    server.begin(); 
    // You're connected now, so print out the status: 
    printWifiStatus(); 
} 

我遇到的主要問題是如何接受並從Android設備打印出來的字符串。當前的代碼我要做到這一點是:

// Listen for incoming clients 
    WiFiClient client = server.available(); 
    if (client) { 
     // An HTTP request ends with a blank line 
     boolean newLine = true; 
     String line = ""; 

     while (client.connected() && client.available()) { 
      char c = client.read(); 
      Serial.print(c); 

      // If you've gotten to the end of the line (received a newline 
      // character) and the line is blank, the HTTP request has ended, 
      // so you can send a reply. 
      if (c == '\n' && newLine) { 
       // Send a standard HTTP response header 
       //client.println("HTTP/1.1 200 OK"); 
       //client.println("Content-Type: text/html"); 
       //client.println(); 
      } 
      if (c == '\n') { 
       // You're starting a new line 
       newLine = true; 
       Serial.println(line); 
       line = ""; 
      } 
      else if (c != '\r') { 
       // You've gotten a character on the current line 
       newLine = false; 
       line += c; 
      } 
     } 
     Serial.println(line); 

     // Give the web browser time to receive the data 
     delay(1); 

     // Close the connection: 
     //client.stop(); 
    } 
} 

我立足這個代碼從博客文章Android Arduino Switch with a TinyWebDB hack的,但是這個代碼是用於以太網屏蔽。 Android應用程序是使用MIT App Inventor製作的,該應用程序類似於發現該博客文章的應用程序。

TLDR,我怎樣才能得到使用Arduino Wi-Fi屏蔽的字符串?

+0

如此做Arduino的代碼不會在Arduino的素描像這樣您的設置()函數之前聲明一個名爲「readString」串工作?目前的行爲是什麼? – angelatlarge

+0

@angelatlarge現在發送到IP地址的字符串不會被打印到屏幕上,所以「char c」沒有被寫入。 –

+0

你有沒有嘗試從telnet程序連接到你的arduino,沒有Android?這是做什麼的? – angelatlarge

回答

1

您可以將字符讀入完整的字符串,而不是像上例中那樣將每個字符讀入串行監視器。

在您的示例中,該位代碼將從客戶端TCP會話中讀取每個字符並將其打印到串行監視器,從而在串行控制檯中顯示HTTP請求。

char c = client.read(); 
    Serial.print(c); 

請試試這樣的代替。

String readString; 

    void setup() { 
     //setup code 
    } 

    void loop() { 

     // Try this when you're reading inside the while client.connected loop instead of the above: 

     if (readString.length() < 100) { 
      readString += c; 
      Serial.print(c); 
     } 

這裏是環()的工作示例:

void loop() { 

    // Listen for incoming clients 
    WiFiClient client = server.available(); 

    if (client) { 
     Serial.println("new client"); 

     // An HTTP request ends with a blank line 
     boolean currentLineIsBlank = true; 
     while (client.connected()) { 
      if (client.available()) { 
       char c = client.read(); 
       //Serial.write(c); 
       // If you've gotten to the end of the line (received a newline 
       // character) and the line is blank, the HTTP request has ended, 
       // so you can send a reply. 
       if (readString.length() < 100) { 
        readString += c; 
        Serial.print(c); 
       } 
       if (c == '\n' && currentLineIsBlank) { 
        client.println("HTTP/1.1 200 OK"); 
        client.println("Content-Type: text/html"); 
        client.println("Connnection: close"); 
        client.println(); 
        client.println("<!DOCTYPE HTML>"); 
        //send the HTML stuff 
        client.println("<html><head><title>Admin Web</title><style type=\"text/css\">"); 
        client.println("body { font-family: sans-serif }"); 
        client.println("h1 { font-size: 14pt; }"); 
        client.println("p { font-size: 10pt; }"); 
        client.println("a { color: #2020FF; }"); 
        client.println("</style>"); 
        client.println("</head><body text=\"#A0A0A0\" bgcolor=\"#080808\">"); 
        client.println("<h1>Arduino Control Panel</h1><br/>"); 
        client.println("<form method=\"link\" action=\"/unlockdoor\"><input type=\"submit\" value=\"Unlock Door!\"></form>"); 
        client.println("<br/>"); 
        client.println("</body></html>"); 
        break; 
       } 
       if (c == '\n') { 
        // You're starting a new line. 
        currentLineIsBlank = true; 
       } 
       else if (c != '\r') { 
        // You've gotten a character on the current line. 
        currentLineIsBlank = false; 
       } 
      } 
     } 
     // Give the web browser time to receive the data. 
     delay(1); 
      // Close the connection: 
      client.stop(); 

      Serial.println("client disonnected"); 
      if (readString.indexOf("/unlockdoor") > 0) 
      { 
      unlockdoor(); 
      Serial.println("Unlocked the door!"); 
      } 
      readString = ""; 
    }