我正在嘗試使用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屏蔽的字符串?
如此做Arduino的代碼不會在Arduino的素描像這樣您的設置()函數之前聲明一個名爲「readString」串工作?目前的行爲是什麼? – angelatlarge
@angelatlarge現在發送到IP地址的字符串不會被打印到屏幕上,所以「char c」沒有被寫入。 –
你有沒有嘗試從telnet程序連接到你的arduino,沒有Android?這是做什麼的? – angelatlarge