Hello everybody! I just registered here and hope to find some help. I have the Arduino Uno R3 from SainSmart and the WiFi shield. What I want to accomplish is the option to control a RC tank over the internet and in my own network. I use LEDs to show me how the motors would work, and they work very nice. But a problem is that I need to communicate with the Arduino at least all 200ms or so to create a stable movement that's not too laggy.
I used the SimpleWebServerWiFi I found in the examples folder and modified it according to my needs. My Arduino receives commands relatively fast, but only sometimes. Sometimes it takes 100ms, then 1000ms or 2000ms. What also happened is that it switched between being fast and slow every second command. When I send the command with my AutoIT code all 200ms, the Arduino freezes. But if I wait one or two seconds between each command it works.
Here some informations about everything:
- Arduino IDE 1.0.3 (Because 1.0.5 screw up my code while compiling, a known problem with 1.0.5)
- AutoIT code to send the commands in this form: http:\[IP of Arduino]:[port][SPEED LEFT]x[SPEED RIGHT]
The code:
#include <SPI.h>
#include <WiFi.h>
char ssid[] = "My network at home"; // your network SSID (name)
char pass[] = "And the password"; // your network password
char ssid2[]="Arduino Tank"; // My hotspot on my laptop to be able to control the tank everywhere
char pass2[]="wowmuchawesome"; // The password for that
WiFiServer server(3178);
int pin1=3; //LED for the transistor that controls the forward speed of the left motor
int pin2=5; //LED for the transistor that controls the backward speed of the left motor
int pin3=6; //Same like pin1 but for right
int pin4=9; //Same like pin2 but for right
int netz=0; // which network, 0-none, 1-homenetwork, 2-Arduino Tank (hotspot)
boolean go=false; // So it only starts to add chars when I already got a "G" because of "GET"
int time=0; // for timeout
int run=0; // I don't want to check the connection all the time
String relativex=""; //Left speed
String relativey=""; //Right speed (variable names stayed although I changed the way I control it, ignore)
String currentLine=""; //The current received line
int status = WL_IDLE_STATUS; // the Wifi radio's status
char floatbuf[64]; //For converting chars in flaot
float speedlinks=0.0; //speed left
float speedrechts=0.0; //speed right
void setup() {
Serial.begin(9600);
pinMode(pin1, OUTPUT);
pinMode(pin2, OUTPUT); // I just see that I forgot to add the other two pins, how the hell could the work?!
tryconnect(); // first connection
}
void loop() {
if (run>60000) { // only test all 60 seconds
run=0; //sets the countdown back to 0
Serial.println("Run");
if (WiFi.status()!=WL_CONNECTED) { // and only if it's not connected
tryconnect(); //tries to connect
}
}
run+=1; //countup
delay(1);
webserver(); //checks for connection and tries to receive data
}
void tryconnect() { // works like a charm
Serial.println("Trying to connect.");
if (int num=WiFi.scanNetworks()>(-1)) {
for (int thisNet = 0; thisNet<num; thisNet++) {
if (WiFi.SSID(thisNet)=="NerdtechCorporation") {
netz=1;
} else if (WiFi.SSID(thisNet)=="Arduino Tank") {
netz=2;
} else {
netz=0;
}
}
if (WiFi.status()!=WL_CONNECTED) {
Serial.println("Attempting to connect to Arduino Tank...");
if (netz==2) {
status = WiFi.begin(ssid2, pass2);
}
if ( status != WL_CONNECTED) {
Serial.println("Couldn't connect to Arduino Tank.");
Serial.println("Attempting to connect to NerdtechCorporation...");
if (netz=1) {
status = WiFi.begin(ssid, pass);
}
if ( status != WL_CONNECTED) {
Serial.println("Couldn't connect to NerdtechCorporation.");
} else {
Serial.println("Connected to NerdtechCorporation.");
server.begin();
printWifiStatus();
}
} else {
Serial.println("Connected to Arduino Tank.");
server.begin();
printWifiStatus();
}
} else {
Serial.println("Already connected");
}
}
}
void webserver() {
WiFiClient client = server.available();
if (client) {
time=millis(); //stores the beginning time
Serial.println("new client");
currentLine = "";
while (client.connected()) {
if (millis()-time>5000) { // if it took 5 seconds timeout so stop everything and go back to loop()
Serial.println("Timeout.");
client.flush();
client.stop();
return;
}
if (client.available()) {
char c = client.read(); // reads incoming stuff
if (c == '\n') {
if (currentLine.length() == 0) {
client.println();
client.println();
break;
}
else {
currentLine = "";
Serial.println("Blanked current line");
}
}
else if (c != '\r') {
currentLine += c;
}
if (currentLine.substring(0,5)=="GET /"&¤tLine.indexOf("HTTP")!=-1) { // if the beginning is "GET /" and it contains "HTTP", it also contains the speed of the motors
currentLine=currentLine.substring(5,currentLine.length()-1);
Serial.println(currentLine);
currentLine=currentLine.substring(0,currentLine.indexOf(" "));
Serial.println(currentLine);
relativex=(currentLine.substring(0,currentLine.indexOf("x")));
relativey=(currentLine.substring(currentLine.indexOf("x")+1,currentLine.length()));
Serial.println(relativex);
Serial.println(relativey);
relativex.toCharArray(floatbuf,sizeof(floatbuf));
speedlinks=atof(floatbuf)*255;
relativey.toCharArray(floatbuf,sizeof(floatbuf));
speedrechts=atof(floatbuf)*255;
Serial.println(speedlinks);
Serial.println(speedrechts);
if (speedrechts<0) { // if it's backwards, stop the forward transistor (or so) and start the backwards one
analogWrite(pin3,0);
analogWrite(pin4,speedrechts*(-1));
} else { //else stop the backwards one and start the forwards one
analogWrite(pin4,0);
analogWrite(pin3,speedrechts);
}
if (speedlinks<0) { //same for left
analogWrite(pin1,0);
analogWrite(pin2,speedlinks*(-1));
} else {
analogWrite(pin2,0);
analogWrite(pin1,speedlinks);
}
Serial.println("Finished speed thing");
client.println();
client.println();
Serial.println("Sent response");
client.flush();
Serial.println(millis());
client.stop();
Serial.println(millis());
delay(10);
Serial.println("Delay over");
return;
}
}
}
Serial.println("Going to stop client manually");
client.stop();
Serial.println("client disconnected");
}
}
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");
// print where to go in a browser:
Serial.print("To see this page in action, open a browser to http://");
Serial.println(ip);
}
I know my code is damn ugly, but I would love to know why it doesn't work like it should or how I can make a better connection. I'm a not that good with Arduinos so I decided to learn by doing, but this is something I'm stuck for a long time now.
Thanks in advance, Lutan