wifi

Hi,
I'm trying to make my wifi module running correctly. I started with a simple get request and used the code I found on the internet in tutorials and with some modification, had the module running. The thing is that after some trials where it sends get request correctly and I have the response back, eventually I get a 'connection timeout' of my code (as following). To get the whole back running, I must restart the module and Arduino and connect to wifi again. First thing came to my mind is that maybe I must "flash" some thing in memory somewhere after a get request? Please help

#include <SoftwareSerial.h>
#define SSID "mySSID" 
#define PASS "myPASS"


SoftwareSerial sser(2, 3); // RX, TX for debugging
int led = 13;                // the pin that the LED is atteched to
int sensor = 7;              // the pin that the sensor is atteched to
int state = LOW;             // by default, no motion detected
int val = 0;   

void setup() {
  // put your setup code here, to run once:
sser.begin(9600);  
Serial.begin(9600);
sser.println("AT+RST");
while(!(sser.find("ready"))){
delay(1000);
    Serial.println("waiting...");
}


    Serial.println("WiFi - Module is ready");


  // try to connect to wifi
  boolean connected=false;
  for(int i=0;i<5;i++){
    if(connectWiFi()){
      connected = true;
    Serial.println("connecting wifi...");
      break;
    }
  }
  if (!connected){
    Serial.println("wifi connection failed!");
    while(1);
  }
  Serial.println("wifi connection ok wait...");
  delay(5000);
  sser.println("AT+CIPMUX=0"); // set to single connection mode
  Serial.println("ok wait...");
  delay(1000);
  pinMode(led, OUTPUT);      // initalize LED as an output
  pinMode(sensor, INPUT);   
}

void loop()
{
  Serial.println("loop cycling..."); 
  val = digitalRead(sensor);   // read sensor value
  if (val == HIGH) {           // check if the sensor is HIGH
    digitalWrite(led, HIGH);   // turn LED ON
    delay(100);                // delay 100 milliseconds 
    
    if (state == LOW) {
      Serial.println("Motion detected!"); 
      state = HIGH;       // update variable state to HIGH
      fireit(); //inform via wifi
    }
  } 
  else {
      digitalWrite(led, LOW); // turn LED OFF
      delay(200);             // delay 200 milliseconds 
      
      if (state == HIGH){
        Serial.println("Motion stopped!");
        state = LOW;       // update variable state to LOW
    }
  }
}


void fireit(){
  String cmd = "AT+CIPSTART=\"TCP\",\"";
  cmd += "mysite.com";
  cmd += "\",80";
  sser.println(cmd);
  Serial.println(cmd);
  delay(1000);
  if(sser.find("Error")){
    Serial.println("got error, sorry!");
    return;
  } 
  cmd = "GET /me/112233 HTTP/1.0\r\nHost: mysite.com\r\n\r\n";
  sser.print("AT+CIPSEND=");
  sser.println(cmd.length());
  delay(1000);
  if(sser.find(">")){
    Serial.print(">");
  }else{
    sser.println("AT+CIPCLOSE");
    Serial.println("connection timeout");
    delay(1000);
    //fireit();//recursive call until getting the result
    return;
  }
  sser.print(cmd);
  Serial.print(cmd);
  Serial.print("going long delay...");
delay(3000);
delay(3000);
//sser.println("AT+CIPCLOSE");
  while (1){
     Serial.println("in while  1 ...");
    if(sser.available()){
      char c = (char)sser.read();
      Serial.print(c);
//      if(c=='\r') Serial.print('\n');
//      else Serial.print(c);
      delay(10);
      break;
    }
  }
}

boolean connectWiFi()
{
  sser.println("AT+CWMODE=1");
  String cmd="AT+CWJAP=\"";
  cmd+=SSID;
  cmd+="\",\"";
  cmd+=PASS;
  cmd+="\"";

  Serial.println(cmd);
    sser.println(cmd);
  delay(2000);
  
  if(sser.find("OK")){
    Serial.println("OK, Connected to WiFi.");
    return true;
  }else{
    Serial.println("Can not connect to the WiFi.");
    return false;
  }
}

Well it was all about power issues!

I simply changed my power supply and now everything is working stable!

Please do good filtering when working with wifi modules :smiley: