3

我正在尝试使用 Arduino、Android 应用程序和路由器制作无线灯光控制设备(开/关/调光)。

我正在使用路由器将 Arduino 设置为静态 IP 192.168.1.2。我正在从 Android 应用程序向 IP 地址 192.168.1.2 发送字符串(“1”-关闭、“2”-降低亮度、“3”-增加亮度、“4”-打开)。我已经使用Arduino Wi-Fi shield将 Arduino连接到 Internet,并使用以下代码设置了 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 shield 获取字符串?

4

1 回答 1

1

您可以将字符读入完整的字符串,而不是将每个单独的字符读入串行监视器,如上面的示例所示。

在您的示例中,这段代码将从客户端 TCP 会话中读取每个字符并将其打印到串行监视器,从而在串行控制台中显示 HTTP 请求。

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

试试这样的东西。在 Arduino 草图中的 setup() 函数之前声明一个名为“readString”的字符串,如下所示:

    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 = "";
  }
于 2013-05-05T14:02:27.143 回答