#include <SoftwareSerial.h>
#include <Arduino.h>
SoftwareSerial mySerial(2,3); // RX, TX
void setup()
{
pinMode(9,OUTPUT);
delay(10);
Serial.begin(9600);
delay(10);
mySerial.begin(115200);
delay(1000);
while(mySerial.available())
{
mySerial.println('AT+CWMODE=1');
delay(1000);
mySerial.println('AT+CWLAP');
delay(1000);
mySerial.println('AT+CIPMUX=1');
delay(1000);
mySerial.println('AT+CWJAP="Swaraj_WLAN","internet"');
delay(1000);
mySerial.println('AT+CIPSERVER=1,4444');
delay(1000);
mySerial.println('AT+CIFSR');
}
}
void loop()
{
if (mySerial.available())
{
String a;
a=mySerial.readString();
Serial.println(a);
char b[15];
a.toCharArray(b,15);
Serial.println(b[9]);
if(b[11]=='o'&&b[12]=='n')
{
Serial.print("This segment ram!");
pinMode(9,OUTPUT);
digitalWrite(9,HIGH);
}
if(b[11]=='o'&&b[12]=='f'&&b[13]=='f')
{
Serial.print("This 2 segment ram!");
digitalWrite(9,LOW);
}
}
if(Serial.available())
{
mySerial.write(Serial.read());
}
}
/*void Check_Protocol(char inStr[])
{
Serial.print("Command: ");
Serial.print(inStr);
Serial.println("-");
Serial.println("Check_Protocol");
if(inStr[0]=='o'&&inStr[1]=='n')
{
Serial.println("on");
digitalWrite(9, HIGH);
}
if(inStr[0]=='o'&&inStr[1]=='f'&&inStr[2]=='f')
{
Serial.println("on");
digitalWrite(9, HIGH);
}
}*/
The above code worked like a charm for the first time.But the next day i was surprised it was not working.I reuploaded some other code ,and then reuploaded this same code in arduino.Later after experimenting I found out that , i need to manually enter the commands “AT+CWLAP”,“AT+CIPMUX=1” etc then after even if I close the arduino ide and the console it works.
But, if i unplug the usb cable used to power the arduino and plug it again it is not connecting to wifi.