Hi everyone,
I'm currently having an odd problem. I succeeded in doing what I was trying for some weeks, this means being able to transmit data via Wifi and making the Arduino react according to these data. So I can use my Android Phone to activate a motor, for instance. I used for that the WiFly Shield library and everything workED fine. Indeed, I just came back to my house and decided to keep working on my project, so I was planning to use the WiFi Network of my house... However, I got a hard problem over there... Here's my program :
#include "WiFly.h"
#include "Servo.h"
char passphrase[] = "a269c7a7297a99669ee443af29";
char ssid[] = "Alice-dbde";
Server server(80);
Servo myservo;
void setup() {
Serial.begin(9600);
Serial.println("Setting up");
WiFly.begin();
Serial.println("WiFly started");
if (!WiFly.join(ssid, passphrase)) {
while (1) {
Serial.print("Error");
}
}
Serial.println("Can u reach me ?");
Serial.print("IP: ");
Serial.println(WiFly.ip());
myservo.attach(5);
server.begin();
}
void loop() {
Client client = server.available();
if (client) {
// an http request ends with a blank line
boolean current_line_is_blank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.print(c);
if (c == 'a')
{
motor();
}
if (c == 'b')
{
unmotor();
}
// if we've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so we can send a reply
if (c == '\n' && current_line_is_blank) {
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println();
}
if (c == '\n') {
// we're starting a new line
current_line_is_blank = true;
} else if (c != '\r') {
// we've gotten a character on the current line
current_line_is_blank = false;
}
}
}
// give the web browser time to receive the data
delay(100);
client.stop();
}
}
void motor()
{
for(int pos = 0; pos < 180; pos += 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
void unmotor()
{
for(int pos = 180; pos > 0; pos -= 1) // goes from 0 degrees to 180 degrees
{ // in steps of 1 degree
myservo.write(pos); // tell servo to go to position in variable 'pos'
delay(15); // waits 15ms for the servo to reach the position
}
}
This worked absolutely fine at my school, but not there.
Indeed, it gets stuck here:
if (!WiFly.join(ssid, passphrase)) {
while (1) {
Serial.print("Error");
}
}
By stuck, I mean that the programm doesn't go after the if statement. But still does the Wifly.join or it wouldn't be able to join the network (it doesn't enter the while loop).
Moreover, I can see with the LEDS my Arduino is connected to my network ! I can even do a telnet on it (as I know its IP thanks to my DHCP configuration). So to sum up, it's stuck over there, but I does work. The problem is that as my program can't go after the WiFly.join(), I can't do anything else... I tried to connect manually, it perfectly work, but I'm bound to use this Library for this project (my teacher told me...). Would anyone have an idea about that odd problem ? O.o