I’m a noob at Arduino’s/forums, so I’ll try to be as detailed as possible. Forgive my noobness.
I’m trying to control 2 servo motors (for a robot) via WiFi. I have the official Arduino WiFi shield stacked on top of an Arduino Uno R3. I have it working on Serial, and now I’ve converted the code for WiFi (changed pins to 5,6 so that they don’t clash with the WiFi shield).
When I SSH through Putty, I connect to the WiFi shield (it says connected to client - as shown in the screen shot), but I can’t seem to give any commands as it keeps looping back and posting the same message ('connected to client) till it gives me the error (software caused connection to abort - screenshot 2). I also get some garbage character returned to me (screenshot).
My code is copied below (it’s kind of long - apologies). Please let me know what I’m doing wrong. Or if there’s any other detail you want me to post.
Thanks,
Mustafa
screenshot1: http://bit.ly/XRzdXo
screenshot2: http://bit.ly/TOFH3p
String readString;
#include <Servo.h>
#include <WiFi.h>
Servo left; // create servo object to control a servo
Servo right;
char ssid[] = "Your Network"; // the name of your network –hiding it for posting online
int status = WL_IDLE_STATUS; // the Wifi radio's status
IPAddress ip;
WiFiServer server(22);
//WiFiClient client;
void setup() {
Serial.begin(9600);
// tone(3, 3000, 1000); // Play tone for 1 second
//delay(1000); // Delay to finish tone
// attempt to connect to an open network:
Serial.println("Attempting to connect to open network...");
status = WiFi.begin(ssid);
// if you're not connected, stop here:
if ( status != WL_CONNECTED) {
Serial.println("Couldn't get a wifi connection");
while(true);
}
// if you are connected :
else {
Serial.println("Connected to wifi");
// Serial.print("Connected to the network");
server.begin();
ip = WiFi.localIP();
Serial.print("\nLocal IP is");
Serial.print (ip);
Serial.println("\nStarting Connection...");
// if (client.connect (server,22) {
// Serial.println("connected");}
}
left.writeMicroseconds(1500); //set initial servo position if desired
right.writeMicroseconds(1500);
left.attach(5); //the pin for the servo control
right.attach(6);
Serial.println("servo-test-22-dual-input"); // so I can keep track of what is loaded
}
void loop() {
WiFiClient client = server.available();
if (client) {
if (client.connected()) {
Serial.println("Connected to client"); //it gets till here. In the serial window, it says //“connected to client.
char c = client.read(); //gets one byte from serial buffer
readString += c; //makes the string readString
delay(2); //slow looping to allow buffer to fill with next character
}
}
if (readString.length() >0) {
Serial.println(readString); //so you can see the captured string
int n = readString.toInt(); //convert readString into a number
if(n== 1)
{
Serial.print("Writing moveForward code to servos");
Serial.println(n);
left.writeMicroseconds(1700);
right.writeMicroseconds (1300);
}
else if (n==2)
{
Serial.print("Writing Turn Left code to servos");
Serial.println(n);
left.writeMicroseconds(1300);
right.writeMicroseconds(1300);
}
else if (n==3)
{
Serial.print("Writing Turn Right code to servos");
Serial.println(n);
left.writeMicroseconds(1700);
right.writeMicroseconds(1700);
}
else if (n==4)
{
Serial.print("Writing moveBack code to servos)");
Serial.println(n);
left.writeMicroseconds(1300);
right.writeMicroseconds(1700);
}
readString=""; //empty for next input
}
}