Hallo zusammen,
ich habe mir vor kurzem das Arduino WiFi Shield zugelegt. Ich nutze es mit dem Arduino Uno. Das Board ist mit einem 9 Volt Netzteil für die Stromversorgung verbunden.
Ich will nun mit meinem Android Phone eine TCP Verbindung zum Board aufbauen und so einen Servo ansteuern.
Das funktioniert auch ganz gut, das Board empfängt die Daten vom Smartphone und stellt den Servo auf den entsprechenden Winkel.
Allerdings bricht mir die Verbindung jedes mal nach ca. 10 Sekunden ab. Ich muss dann das Board neustarten, damit ich mich wieder für die nächsten 10 Sekunden verbinden kann. Es ist dabei egal wieviele Daten ich in der Zeit sende.
Hier mal der Arduino Code:
#include <SPI.h>
#include <WiFi.h>
#include <Servo.h>
char ssid[] = "xxxx"; // your network SSID (name)
char pass[] = "xxxx"; // your network password
int serverPort=8888;
int servoPin=3;
// Initialize the WiFi server library
WiFiServer server(serverPort);
int status = WL_IDLE_STATUS;
//Initialise the Servo Library
Servo myservo;
void setup()
{
// start the serial for debugging
Serial.begin(9600);
// start the WiFi connection and the server:
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);
}
server.begin();
Serial.println("Connected");
printWifiStatus();
myservo.attach(servoPin); // attache the servo on the given servoPin
}
void loop()
{
// listen for incoming clients
WiFiClient client = server.available();
if (client) {
String commandStr ="";//Connamdstring where incoming commands are stored
while (client.connected()) {//if a client is connected
if (client.available()) {//and available
//reading the inputs from the client
char c = client.read();
commandStr+=c;//and ands them to the commands String
if (c == '\n') { //if a newline character is sent (commandline is fully recieved)
Serial.println("Command:"+commandStr); //output the command
if(commandStr.indexOf("set:")==0){ //if the command begins with "set:
String value=commandStr; //store the command into a new String
value.replace("set:", " "); //replace the "set:" from the command string
Serial.println("Set a Servo:"+value); //output the servo value
myservo.write(convertToInt(value)); //set the servo to the recieved value
}
commandStr=""; //reset the commandline String
}
}
}
// give the client time to receive the data
delay(1);
// close the connection:
client.stop();
}
}
int convertToInt(String value){
char buf[value.length()];
value.toCharArray(buf,value.length());
return atoi(buf);
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your WiFi shield's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
Warum wird die Verbindung zum Client nach 10 Sekunden unterbrochen und beim nächsten Durchlauf von loop() nicht wieder hergestellt?
Ich bin noch relativ neu auf dem Gebiet der Arduino Programmierung deswegen könnte es auch sein, dass ich einen Anfängerfehler gemacht habe. Wenn mir jemand helfen könnte, wäre ich euch sehr dankbar.
Der Code stammt größtenteils aus diesem Tutorial:
http://www.lauridmeyer.com/2012/04/controlling-a-servo-with-an-android-phone-using-the-arduino-and-an-ethernetshield/
Viele Grüße
RubberPoach