chipKIT® Development Platform

Inspired by Arduino™

HTTP Requests with WiFi shield

Created Mon, 25 Feb 2013 02:13:12 +0000 by domtech809


domtech809

Mon, 25 Feb 2013 02:13:12 +0000

Hello,

I have been trying to follow the Arduino example http://arduino.cc/en/Tutorial/WiFiWebClientRepeating to send an HTTP request and print the results on the terminal. I've modified the WiFiTCPEchoClient sketch no see if I can get it going but it's a no go. If any one can share some info or if anyone has done this or something similar please share :). I have confirmed that I can connect to the network and obtain and IP from my router. The scan sketch works fine as well.

//This is for the MRF24WBxx on a pmodWiFi or WiFiShield
#include <WiFiShieldOrPmodWiFi.h>
//This is for the MRF24WGxx on a pmodWiFi or WiFiShield
//#include <WiFiShieldOrPmodWiFi_G.h> 

/***********************************************************/
/*       Required libraries, Do NOT comment out            */
/***********************************************************/
#include <DNETcK.h>
#include <DWIFIcK.h>

/************************************************************/                                                                  
/*       SET THESE VALUES FOR YOUR NETWORK                  */
/************************************************************/
char * szIPServer = "www.arduino.cc";    //server to connect to
//unsigned short portServer = DNETcK::iPersonalPorts44 + 300;     // port 44300
unsigned short portServer = DNETcK::iWellKnownPorts + 79;         //port 80 
// Specify the SSID
const char * szSsid = "chipKitNTW";

// select 1 for the security you want, or none for no security
#define USE_WPA2_PASSPHRASE

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

    const char * szPassPhrase = "WiFiShield";
    #define WiFiConnectMacro() DWIFIcK::connect(szSsid, szPassPhrase, &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 = 0,
    WRITE,
    READ,
    CLOSE,
    DONE,
} STATE;

STATE state = WRITE;

unsigned tStart = 0;
unsigned tWait = 5000;

TcpClient tcpClient;
byte rgbRead[1024];
int cbRead = 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);
   
    MAC rgbMAC; 
    IPv4 ipMy;
    IPv4 ipGateway;
    IPv4 subnetMask;
    IPv4 ipDns1;
    IPv4 ipDns2;
    
void setup() {
    DNETcK::STATUS status;
    int conID = DWIFIcK::INVALID_CONNECTION_ID;
    
    Serial.begin(9600);
    Serial.println("WiFiTCPEchoClient 1.0");
    Serial.println("Digilent, Copyright 2012");
    Serial.println("");
    
    if((conID = WiFiConnectMacro()) != DWIFIcK::INVALID_CONNECTION_ID)
    {
        Serial.print("Connection Created, ConID = ");
        Serial.println(conID, DEC);
        state = WRITE;
    }
    else
    {
        Serial.print("Unable to connection, status: ");
        Serial.println(status, DEC);
        state = CLOSE;
    }

    // use DHCP to get our IP and network addresses
    DNETcK::begin();

    // make a connection to our echo server
    tcpClient.connect(szIPServer, portServer);

}

void loop() {
    int cbRead = 0;

    switch(state)
    {

       // write out the strings  
       case WRITE:
            if(tcpClient.isConnected())
                {     
                Serial.println("Got Connection");
                    DNETcK::getMyMac(&rgbMAC);
                    DNETcK::getMyIP(&ipMy);
                    DNETcK::getGateway(&ipGateway);
                    DNETcK::getSubnetMask(&subnetMask);
                    DNETcK::getDns1(&ipDns1);
                    DNETcK::getDns2(&ipDns2);           
     
    printMAC(rgbMAC);
    printIP(ipMy);
    printIP(ipGateway);
    printIP(subnetMask);
    
               // tcpClient.writeStream(rgbWriteStream, cbWriteStream);
 
                // check that print() and println() work
                tcpClient.print("GET /latest.txt HTTP/1.1");
                tcpClient.println("Host: www.arduino.cc");
                tcpClient.print("User-Agent: arduino-ethernet");
                tcpClient.println("Connection: close");
                
                printWrite(tcpClient);

                Serial.println("Bytes Read Back:");
                state = READ;
                tStart = (unsigned) millis();
                }
            break;

        // look for the echo back
         case READ:

            // see if we got anything to read
            if((cbRead = tcpClient.available()) > 0)
            {
                cbRead = cbRead < sizeof(rgbRead) ? cbRead : sizeof(rgbRead);
                cbRead = tcpClient.readStream(rgbRead, cbRead);
 
                for(int i=0; i < cbRead; i++) 
                {
                    Serial.print(rgbRead[i], BYTE);
                }
            }

            // give us some time to get everything echo'ed back
            else if( (((unsigned) millis()) - tStart) > tWait )
            {
                Serial.println("");
                state = CLOSE;
            }
            break;

        // done, so close up the tcpClient
        case CLOSE:
            tcpClient.close();
            Serial.println("Closing TcpClient, Done with sketch.");
            state = DONE;
            break;

        case DONE:
        default:
            break;
    }

    // keep the stack alive each pass through the loop()
    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 printMAC(MAC& mac)
{
    Serial.print("MAC: ");
    printNumb(mac.rgbMAC, 6, ':');
    Serial.println(" ");
}


void printIP(IPv4& ip)
{
    Serial.print("IP: ");
    printNumb(ip.rgbIP, 4, '.');
    Serial.println(" ");
}

void printNumb(byte * rgb, int cb, char chDelim)
{
    for(int i=0; i<cb; i++)
    {
        if(chDelim == ':' && rgb[i] < 16)
        {
            Serial.print(0, DEC);
        }
    
        if(chDelim == ':')
        {
            Serial.print(rgb[i], HEX);
        }
        else
        {
            Serial.print(rgb[i], DEC);
        }  
    
        if(i < cb-1)
        {
            Serial.print(chDelim);  
        }
    }
}

riceman744

Mon, 24 Aug 2015 19:48:53 +0000

I too am having the same problem. Did you ever figure out how to get it working?


Lukas

Mon, 07 Sep 2015 11:54:08 +0000

Just an idea: for my shield i need #include <WiFiShieldOrPmodWiFi_G.h>. From the digilent website:

Note: Early versions of the WiFi Shield use the Microchip MRF24WB0MA WiFi module. Later versions use the improved MRF24WG0MA WiFi module.