chipKIT® Development Platform

Inspired by Arduino™

Wifi Server Connection

Created Wed, 13 Mar 2013 14:40:40 +0000 by aredman


aredman

Wed, 13 Mar 2013 14:40:40 +0000

Hi Guys,

I am using Max32 board with Digilent Wifi module. I have modified the EchoServer sketch for project and I am trying to get it to connect to a client application written in C# on my computer. The C# client application works perfectly well through my wifi router with its own C# server application so I know that this end works. I can connect to the wifi and it gives me the static ip I request on the router, however, the programs seem to stall when the server is waiting for the connection of the client. I have add the code below. I believe it is getting stuck around if((count = tcpServer.availableClients()) > 0)

Has anyone else had a similar problem or have any directions to take this. I haven't got another setup to perform the echoServer echoClient setup but from this forum I assume that it does work ok. Sorry to post the whole code but you never know with these types of setups where the error lies.

Thanks

Aaron

#include <WiFiShieldOrPmodWiFi.h>
#include <DNETcK.h>
#include <DWIFIcK.h>

IPv4 ipServer = {192,168,1,110};
unsigned short portServer = 8001;//DNETcK::iPersonalPorts44 + 300;     // port 44300 

// Specify the SSID
const char * szSsid = "Robot Wifi";

// select 1 for the security you want, or none for no security
//#define USE_WPA2_PASSPHRASE
//#define USE_WPA2_KEY
//#define USE_WEP40
//#define USE_WEP104
//#define USE_WF_CONFIG_H

// modify the security key to what you have.
#if defined(USE_WPA2_PASSPHRASE)

    const char * szPassPhrase = "aredman";
    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, szPassPhrase, &status)

#elif defined(USE_WPA2_KEY)

    DWIFIcK::WPA2KEY key = { 'A', 'a', 'c', 'j', '2', '1', '0', '7'}; 
                            //0x3B, 0xAD, 0x79, 0xF7, 0x1D, 0xC4, 0xB9, 0x05, 
                            //0x7A, 0x34, 0x4C, 0x3E, 0xB5, 0xFA, 0x38, 0xC2, 
                            //0x0F, 0x0A, 0xB0, 0x90, 0xDC, 0x62, 0xAD, 0x58 };
    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, key, &status)

#elif defined(USE_WEP40)

    const int iWEPKey = 0;
    DWIFIcK::WEP40KEY keySet = {    0x7B, 0x6B, 0xD3, 0xA8, 0xF0,     // Key 0
                                    //0xBE, 0xC9, 0x58, 0x06, 0x97,     // Key 0
                                    0x00, 0x00, 0x00, 0x00, 0x00,     // Key 1
                                    0x00, 0x00, 0x00, 0x00, 0x00,     // Key 2
                                    0x00, 0x00, 0x00, 0x00, 0x00 };   // Key 3
    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, keySet, iWEPKey, &status)

#elif defined(USE_WEP104)

    const int iWEPKey = 0;
    DWIFIcK::WEP104KEY keySet = {   0x3E, 0xCD, 0x30, 0xB2, 0x55, 0x2D, 0x3C, 0x50, 0x52, 0x71, 0xE8, 0x83, 0x91,   // Key 0
                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 1
                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,   // Key 2
                                    0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // Key 3
    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, keySet, iWEPKey, &status)

#elif defined(USE_WF_CONFIG_H)

    #define WiFiConnectMacro() DWIFIcK::connect(0, &status)

#else   // no security - OPEN

    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, &status)

#endif
   
//******************************************************************************************
//******************************************************************************************
//***************************** END OF CONFIGURATION ***************************************
//******************************************************************************************
//******************************************************************************************

typedef enum
{
    NONE,
    SETUP,
    INITIALISATION,
    RUNNING,
    LISTEN,
    ISLISTENING,
    AVAILABLECLIENT,
    ACCEPTCLIENT,
    CLOSE,
    DONE,
} STATE;

STATE state = SETUP;

unsigned tStart = 0;
unsigned tWait = 5000;

TcpServer tcpServer(3);
TcpClient tcpClient;

byte rgbRead[1024];
int cbRead = 0;
int count = 0;

// this is for Print.write to print
byte rgbWrite[] = {'*','W','r','o','t','e',' ','f','r','o','m',' ','p','r','i','n','t','.','w','r','i','t','e','*','\n'};
int cbWrite = sizeof(rgbWrite);

// this is for tcpClient.writeStream to print
byte rgbWriteStream[] = {'*','W','r','o','t','e',' ','f','r','o','m',' ','t','c','p','C','l','i','e','n','t','.','w','r','i','t','e','S','t','r','e','a','m','*','\n'};
int cbWriteStream = sizeof(rgbWriteStream);
DNETcK::STATUS status;


void setup() {

    int conID = DWIFIcK::INVALID_CONNECTION_ID;

    Serial.begin(9600);
    Serial.println("ECU Robot V1.0");
    Serial.println("Aaron Redman, Edith Cowan University");
    Serial.println("");
    
    
    if((conID = WiFiConnectMacro()) != DWIFIcK::INVALID_CONNECTION_ID)
    {
        state = INITIALISATION;
    }
    else
    {
        Serial.print("Unable to connection, status: ");
        Serial.println(status, DEC);
        state = CLOSE;
    }
    DNETcK::begin(ipServer);
}

void loop() 
{
  int cbRead = 0;
  
  switch(state)
  {
    case INITIALISATION:
      if(DNETcK::isInitialized(&status))
      {
          Serial.println("IP Stack Initialized");
          state = LISTEN;
      }
      else if(DNETcK::isStatusAnError(status))
      {
          Serial.print("Error in initializing, status: ");
          Serial.println(status, DEC);
          state = CLOSE;
      }
        
      if(DWIFIcK::isConnected(1))
      {
        Serial.println("Robot Wifi Connected");
      }
      if(DNETcK::isInitialized())
      {
        if (DNETcK::getMyIP(&ipServer))
        {
          printIP();
          Serial.println("");
          state = LISTEN;//RUNNING;
          Serial.println("Robot Running");
        }
        else
        {
          Serial.println("IP error");
        }
      }
      break;
      
    //case RUNNING:
    // say to listen on the port
    case LISTEN:
        if(tcpServer.startListening(portServer))
        {
            Serial.println("Started Listening");
            state = ISLISTENING;
        }
        else
        {
            state = CLOSE;
        }
        break;

    case ISLISTENING:
        if(tcpServer.isListening(&status))
        {
            Serial.print("Listening on port: ");
            Serial.print(portServer, DEC);
            Serial.println("");
            state = AVAILABLECLIENT;
        }
        else if(DNETcK::isStatusAnError(status))
        {
            state = CLOSE;
        }
        break;

    // wait for a connection
    case AVAILABLECLIENT:
        if((count = tcpServer.availableClients()) > 0)
        {
            Serial.print("Got ");
            Serial.print(count, DEC);
            Serial.println(" clients pending");
            state = ACCEPTCLIENT;
        }
        break;

    // accept the connection
    case ACCEPTCLIENT:
        
        // probably unneeded, but just to make sure we have
        // tcpClient in the  "just constructed" state
        //tcpClient.close(); 

        // accept the client 
        if(tcpServer.acceptClient(&tcpClient))
        {
            Serial.println("Got a Connection");
            state = CLOSE;//READ;
            tStart = (unsigned) millis();
        }

        // this probably won't happen unless the connection is dropped
        // if it is, just release our socket and go back to listening
        else
        {
            state = CLOSE;
        }
        break;
 
       break;  
      
    case CLOSE:
      tcpServer.close();
      Serial.println("");
      Serial.println("Robot Shutting Down");
      state = DONE;
      break;

    case DONE:
      default:
      break;  
  }
  DNETcK::periodicTasks(); 
}   

void printWrite(Print& print)
{

    // check the print() and println() methods
    tcpClient.print("*Printed from print.print*\n");
    tcpClient.println("*Printed from print.println*");

    // While these are hidden from TcpClient
    // they should not be hidden from Print
    // these should all work.
    print.write((uint8_t) 'b');
    print.write("\n*Wrote from print.write*\n");
    print.write(rgbWrite, cbWrite);
}

void printIP(void)
{
  Serial.print("IP Address assigned: ");
  Serial.print((int)ipServer.rgbIP[0]);
  Serial.print(".");
  Serial.print((int)ipServer.rgbIP[1]);
  Serial.print(".");
  Serial.print((int)ipServer.rgbIP[2]);
  Serial.print(".");
  Serial.println((int)ipServer.rgbIP[3]);
}

aredman

Sat, 16 Mar 2013 05:38:37 +0000

New update on this. It seems to be the router as the issue. I can get the laptop and chipkit wifi talking through my phones hotspot, but not through a wireless router. I have tried 2 types, a TP-Link and D-Link. Does anyone know if there are any incompatabilities with these setups. I did get info off one of the routers that the chipkit was conntected through 11b and the laptop as 11n. Could this be an issue?


aredman

Sun, 24 Mar 2013 14:14:33 +0000

OK, Hopefully someone may be able to at least help with this. When I see the Wifi module on the router settings, it either is named as "Unknown" or simply just blank. Some routers don't like this and it may not be causing the issue. But does anyone know if the module can be given a name to be identified on the router?

Thanks


aredman

Sun, 31 Mar 2013 15:00:51 +0000

It seems the issue is with N routers. If I use a router which is G only, I can get a connection and open a port. If I use an N router and even limit it to G or B types only, the port will not open. Can anyone categorically confirm that the Digilent Wifi module can work on an N router. The datasheet states that it is N compatible. If someone from Digilent reads this post, can they please reply as I thought they wold be the ones to confirm how it has been tested. I am ready to give it in and buy another Arduino Wifi shield as this module would be pretty pointless if it can not work on an N router. I am happy to try all suggestions as this has been frustrating me for weeks!!


jferreira

Mon, 06 May 2013 16:22:58 +0000

Aredman,

I'm having the same problem as you did. Did you solved it already? If so, can you tell me how...?

I'm working with a N router also...


KeithV

Tue, 07 May 2013 04:51:13 +0000

Hey guys, read the documentation on this.... I have answered this several times now on this forum. Go look under documents in the DWIFIcK director and read about the router setup.

Short answer, yes it ususally will work with N routers, but your router MUST support 2Mbps, and you MUST broadcast your SSID. Please read the document on all of the ways to get your router to work at 2Mbps.