3

Arduino、Android アプリ、およびルーターを使用して、ワイヤレス調光デバイス (オン/オフ/調光) を作成しようとしています。

ルーターを使用してArduinoを静的IP 192.168.1.2に設定しています。Android アプリから IP アドレス 192.168.1.2 に文字列 (「1」-オフ、「2」-輝度を下げる、「3」-輝度を上げる、「4」-オン) を送信しています。Arduino Wi-Fi シールドを使用して Arduino をインターネットに接続し、次のコードを使用して 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シールドを使用して文字列を取得するにはどうすればよいですか?

4

1 に答える 1