Hello i am a newbie and i am making an android application.I need to connect arduino with wifi but i dont know how to do that....any tips???
Thanks for your time!
//#include <SoftwareSerial.h>
// if you want to skip the messages on the serial monitor comment the macro below
//#define _DEBUG_MESSAGE
//#define BUTTON_PRESSED (!(PINE & 0x04))
#define LED_OFF 0
#define LED_ON 1
#define LED_TOGGLE 2
//#define RXPIN 0
//#define TXPIN 1
//SoftwareSerial Serial1(RXPIN,TXPIN);
void Connect_to_WiFi_Network (char Net[], char Pass[])
{
String Command;
Command = "AT+CWJAP=\"";
Command = Command + Net;
Command = Command + "\",\"";
Command = Command + Pass;
Command = Command + "\"\r";
Serial.print (Command); //1
}
void Disconnect_from_WiFi_Network (void)
{
Serial.print ("AT+CWQAP\r"); //1
}
// using PIC-WEB as a server
// the default server IP address is 192.168.0.130
// if you want to change it edit the macro below
#define IP_Server "192.168.0.130"
void Get_Server_Page ()
{
String Cmd, IP;
long int Timeout = 10000000;
// Serial1.print("AT+CIPMUX=0\r");
IP = "AT+CIPSTART=\"TCP\",\"";
IP += IP_Server;
IP += "\",8080"; //8080
// IP = "AT+CIPSTART=\"UDP\",\"";
// IP += IP_Server;
// IP += "\",80,10000,2"; //8080
Serial.println (IP);
#ifdef _DEBUG_MESSAGE
// Serial1.println (IP); // debug purpose
#endif
if (Serial.find ("Error"))
return;
delay(20000);
// by default index.htm (/<empty>) otherwise should be the subpage name
// for example ("GET /forms.htm HTTP/1.0\r\n\r\n"
Cmd = "GET /forms.htm&field1=1&field2=3&field3=3 HTTP/1.0\r\n\r\n";
Serial.print("AT+CIPSEND="); //1
Serial.println (Cmd.length()); //1
// delay(10000);
if (Serial.find (">"))
{
Serial.print(Cmd); //1
#ifdef _DEBUG_MESSAGE
// Serial1.print ('>');
#endif
// delay(1000);
}
else
{
// Serial1.println ("AT+CIPCLOSE"); //1
#ifdef _DEBUG_MESSAGE
// Serial1.println ("Connection Timeout!");
#endif
delay (1000);
return;
}
// original but delay is too long and during that time the data
// sent by the server to the ESP module are lost;
// replaced by timeout instead with value 100K
// updated after each character
// delay (2000);
while (Timeout--)
{
static bool FirstTime=true;
int Number=0, i=0;
// filtering metadata so only page data will be printed
if (FirstTime)
FirstTime=false;
else
Serial.find ("OK"); //1
Serial.find ("+IPD,"); //1
delay (1);
while (1)
{
char c=0; //1
if (Serial.available())
{
c = Serial.read (); //1
if (c == ':')
break;
else
Number = Number * 10 + (c & 0x0F);
}
else
{
Timeout--;
if (!Timeout)
break;
}
}
for (i=0; i<Number;)
if (Serial.available ())
{
//#ifdef _DEBUG_MESSAGE
// Serial1.print ((char)Serial.read ()); //1
//#endif
Timeout = 100000;
i++;
}
} //while(Timeout)
#ifdef _DEBUG_MESSAGE
// Serial1.println ("Transfer complete!");
#endif
}
void setup()
{
// LEDs on Olimexino set as outputs
//pinMode (7, OUTPUT);
//digitalWrite (7, LOW);
pinMode (9, OUTPUT);
digitalWrite (9, LOW);
// pinMode(TXPIN,OUTPUT);
// pinMode(RXPIN,INPUT);
// DDRE = DDRE & (~(1<<2)); // Button - input
// pinMode (8, OUTPUT); // UEXT Power
// digitalWrite (8, LOW); // UEXT Power turned on
#ifdef _DEBUG_MESSAGE
// Serial1.begin (115200);
#endif
Serial.begin (115200); //1
delay (1000);
Serial.print ("AT+CWMODE=3\r"); //1
delay (1000);
Serial.print ("AT+RST\r"); //1
Disconnect_from_WiFi_Network ();
//delay(1000);
Connect_to_WiFi_Network ("Net", "password");
//delay(1000);
}
bool Button = true; // this variable is used to avoid command repetition due to holding the button
int Val=0;
void loop ()
{
// if (BUTTON_PRESSED && Button)
// {
// toggling LEDs on the Olimexino board indicates that button is pressed
// digitalWrite (7, Val);
digitalWrite (9, !Val);
Val = !Val;
// sending command to the server
//delay (10000);
//Serial1.print ("AT+RST\r\n"); //1
//Disconnect_from_WiFi_Network ();
//delay(1000);
//Connect_to_WiFi_Network ("Net", "password");
//delay(10000);
Get_Server_Page ();
// Button = false; // mark button as pressed
// }
// if (!BUTTON_PRESSED)
// Button = true; // mark button as released
}