Created Mon, 09 Sep 2013 14:12:23 +0000 by domtech809
Mon, 09 Sep 2013 14:12:23 +0000
Hello,
I'm having problems trying to get the WiFi shield to work as a client and connect to the internet. I want to do a simple websearch on google as the Arduino examples here: http://arduino.cc/en/Tutorial/WiFiWebClient http://arduino.cc/en/Tutorial/WiFiWebClientRepeating
I tried modifying the TCPClient Example from the digilent library but was not able to get anywhere. I've verified that my Scan example works and I can run the Server example as well, but the client is not connecting to a page and returning any results.
//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);
}
}
}