Go Down

Topic: Programming using USR-WIFI232-G (Read 983 times) previous topic - next topic

aerouta

Hello all, does anyone have any experience using the WIFI Module. I am attempting to get through the basic configuration. I am having some problems interfacing with the module on the board. I have attached the code I am using. Along with the schematic showing how the WIFI module to attached, and the page from the WIFI module manual.

The manual walks walks through how to interface with the WIFI module. The first step works fine. I send it "+++" and I get back "a". However when I send it "a" I don't get back anything. I should get back "+ok" according to the manual. Please take a look at the code and let me know if I am missing something.

Code: [Select]
void setup(){
       Serial.begin(9600);
       Serial1.begin(115200);
}


String inputdata;  // data coming in on serial port
String outputdata;  // data coming out of Module

void loop() {
       if (Serial.available()){
              inputdata = Serial.readStringUntil('\n'); //read data from serial
              Serial.print("Input: ");   Serial.println(inputdata);
              Serial1.print(inputdata); // Send dat to WIFI Module
       }
       if (Serial1.available()) {
              // read the incoming byte:
              outputdata = Serial1.readStringUntil('\n');
              // say what you got:
              Serial.print("WIFI: ");
              Serial.println(outputdata);
       }
}


RESULTS

Input: +++
WIFI: a

Input: a

pylon

It depends a bit on how you send the serial data. Most terminal programs send '\r\n' when the return key is pressed. In this case you send '+++' to the module and it answers with a 'a' but the next character sent is a '\r' and not a 'a' so it doesn't change to AT+ mode.

aerouta

Thanks for the info, do you have any suggestions?

pylon

Yes, change your program to:

Code: [Select]
void setup(){
       Serial.begin(9600);
       Serial1.begin(115200);
}


void loop() {
       if (Serial.available()){
             Serial1.write(Serial.read());
       }
       if (Serial1.available()) {
              Serial.write(Serial1.read());
       }
}


and try again.

aerouta

thanks for the suggestion, I gave this a try but I get the same results.   =(


Yes, change your program to:

Code: [Select]
void setup(){
       Serial.begin(9600);
       Serial1.begin(115200);
}


void loop() {
       if (Serial.available()){
             Serial1.write(Serial.read());
       }
       if (Serial1.available()) {
              Serial.write(Serial1.read());
       }
}


and try again.

pylon

Quote
thanks for the suggestion, I gave this a try but I get the same results.


Almost impossible that you get exactly the same results. Please post the exact results you get!

aerouta


Pretty close. Instead of the new input displaying on the next line it is on the same row. So the original code, if I sent "+++" twice I would receive the following :

a
a

With the code you sent, if I send "+++" twice I receive

aa


However, the result when I send "a" is the same..... Nothing.


Quote
thanks for the suggestion, I gave this a try but I get the same results.


Almost impossible that you get exactly the same results. Please post the exact results you get!

pylon

What time is between the a from the module and you're 'a' being sent to the module? Often these things have a more or less strict timing behavior, although the manual says absolutely nothing about it.

aerouta

It varies from immediately to a few seconds.


What time is between the a from the module and you're 'a' being sent to the module? Often these things have a more or less strict timing behavior, although the manual says absolutely nothing about it.

pylon

The newer version of the manual (v1.4) says that no more than 3 seconds must be between the last "+" and the "a". Have you tried that?

What serial console are you using on the PC? If you use the Serial Monitor of the IDE, what settings did you choose there?

Change the Serial.begin(9600) to Serial.begin(115200) to be as fast as the other side of the communication.

If that doesn't help, try sending an "A" instead of the "a".

aerouta

SUCCESS!!! It must have been a timing issue. Never been so happy to see "+ok"

Now if I can could only remember what I was trying to do in the first place!

aerouta

I have not seen code on the web that works with the Arduino Mega and the USR-WIFI232 so I set out to develop my own and post it. After several failed attempts I decided hire a developer to help and this is as far as he is willing to go.

I would like some help trouble shooting this code. I am attempting to connect the board to a websocket server. I have successfully connected to websocket.org. However, it takes several attempts (20+ times through the while loop).  Also, the main goal is to connect to a socket.io server which I have not successfully accomplished.

The two functions which are used for establishing a connection are "Connect()" and "ConnectToWebSckServer()".  The rest of the code (see attachment) preceding these functions deals with initializing the WIFI module, this part works, but I am sure can be improved.

I am looking for suggestions in improving the board connectivity or at least an explanation on why it fails to connect after several attempts. And also, how to properly send the handshake and connect to a socket.io server.


Code: [Select]

==============================================================================
//Connect() function will send commands to WiFi module to Connect to WebSocke
//Server.
//==============================================================================
void Connect()
{
  String Response = String();
  Serial.print("Connecting to Web Socket Server......");

  Serial1.print("AT+TCPDIS=on\r");   //Turn On TCP Link
  DelayinMS(200);
  while(Serial1.available())
  {
    Response += (char )Serial1.read();
    DelayinMS(20);
  }
  //while(ConnectToWebSckServer("/socket.io/1/","52.0.167.93:3000","http://52.0.167.93:3000"));
  while(ConnectToWebSckServer("/","echo.websocket.org","http://echo.websocket.org"));

  digitalWrite(CONNECTED, HIGH);
  Serial.println("..... Connected.");
}
//------------------------------------------------------------------------------

//==============================================================================
//ConnectToWebSckServer() function
//==============================================================================
int ConnectToWebSckServer(String Path, String Host, String Origin)
{
   String HandShake = String();
   String Comm = String();
   String Response = String(); 
   //Prepare header for connecting to WebSocekt Server
   HandShake = "GET ";
   HandShake += Path;
   HandShake += " HTTP/1.1\r\n";
   HandShake +="Upgrade: WebSocket\r\n";
   HandShake+="Connection: Upgrade\r\n";
   HandShake +="Host: ";
   HandShake += Host;
   HandShake +="\r\nOrigin: ";
   HandShake += Origin;
   HandShake += "\r\n\r\n";
   //Serial.println(HandShake);
   
   do{
     Response = String();
     Comm = String("AT+SEND=") + String(HandShake.length()) + String("\r");
     while(!SendComm(Comm, ">")) DelayinMS(1000);
     DelayinMS(300);
     Serial1.print(HandShake);
 Serial.println(HandShake);

     DelayinMS(3000);
     while(Serial1.available())
     {
       Response += (char )Serial1.read();
       DelayinMS(20);
     }
 

     Response = Response.substring(4, (Response.length()-4));
 Serial.println(Response);
     }while(!Response.equals("120"));// 135 for socket.io??
   //Receive response from the server
   Serial1.print("AT+RECV=1000\r");
   DelayinMS(5000);
   Response = String();
   while(Serial1.available())
   {
     Response += (char )Serial1.read();
     DelayinMS(20);
   }
   //Check if the response has proper data
   if(Response.indexOf("HTTP/1.1 101") >= 0) return 0;
   else return 1;
}
//------------------------------------------------------------------------------

//==============================================================================

//==============================================================================
//SendComm(ATComm, ExpRes) function accepts two strings as parameter. ATComm is
//the command that is to be sent to WiFi module and ExpRes is the expected
//response from the WiFi module. The function returns 1 if the response from the
//module is as per expected.
//Value Returned : Integer    1: Command Executed Successfully.
//                            0: Command not Executed Successfully.
//==============================================================================
int SendComm(String ATComm, String ExpRes)
{
  String Response = String();
  Serial1.print(ATComm);
  DelayinMS(200);
  while(Serial1.available())
  {
    Response += (char )Serial1.read();
    DelayinMS(20);
  }
  if(!(Response.indexOf(ExpRes) >= 0)) return 0;    //Check if Module's response
                                                    //is as expected.
  //Response = Response.substring(ATComm.length(), (Response.length()-4));
  //if(Response != ExpRes) return 0;
  return 1;
}
//------------------------------------------------------------------------------

//SendData()Function will send the string data received in the parameter "Data"
//to the WebSocket Server
//Value Returned : Integer    1: If data sent successfully.
//                            0: If any error in Data Sending
//==============================================================================
int SendData(String Data)
{
  String Response = String();
  String Comm = String();
  int i;
  char ch;
 
  Comm = String("AT+SEND=") + String(Data.length()+2) + String("\r");
  while(!SendComm(Comm, ">")) DelayinMS(50);
  DelayinMS(200);
  ch=0x00;
  //Comm = String(ch) + Data;
  Serial1.write(ch);
  for(i=0; i < Data.length(); i++) Serial1.write(Data.charAt(i));
  ch=0xFF;
  //Comm = Comm + String(ch);
  Serial1.write(ch);
  //Serial1.print(Comm);
  DelayinMS(2000);
  while(Serial1.available())
  {
    Response += (char )Serial1.read();
    DelayinMS(20);
  }
  Response = Response.substring(4, (Response.length()-4));
  if(Response.equals(String(Data.length()+2))) {
    Serial.print("Data Sent - ");
    Serial.println(Data);
    return 1;
  }
  Serial.print("Error in Sending Data - ");
  Serial.println(Data);
  return 0;
}
//------------------------------------------------------------------------------


//==============================================================================
//ReceiveData() Function will receive data (maximum 1000 bytes at a time)
//Return Value : String Received from WebSocket Server.
//==============================================================================
String ReceiveData()
{
  String Response = String();
  Serial1.print("AT+RECV=1000\r");
  DelayinMS(5000);
  while(Serial1.available())
  {
    //Serial.write(Serial1.read());
    Response += (char )Serial1.read();
  }
  Response = Response.substring(22, (Response.length()-5));
  Serial.print("Data Recd - "); 
  return Response;
}
//------------------------------------------------------------------------------

aerouta


Go Up