PDA

View Full Version : WIZ820io Ethernet and 2nd power supply with teensy 3.0



manitou
02-05-2013, 04:37 PM
Hardware isn't my speciality. I'm trying to get wiznet WIZ820io to work with teensy 3.0. I've run it on UNO, maple, and DUE without problems, but they have enough power to drive it (150ma+). For the teensy hooked to USB, i provided a breadboard power adaptor to provide power to the wiznet.
https://www.sparkfun.com/products/114
I measured output voltage (3.3v) and even checked current flow (150ma), and I have a commn ground. LEDs are working on the wiznet module, but the SPI logic isn't working??? I have a simple test to just read some of the ROM on the wiznet and it just returns 0's. As noted, same code/module works on UNO/maple/DUE.

what hardware magic am i missing??

Headroom
02-16-2013, 12:57 AM
You may want to post some form of a schematic so we have a better idea how things are wired.

PaulStoffregen
02-16-2013, 01:17 AM
Or a photo, even a which camera phone pic if it's not too blurry to see the wires....

manitou
02-17-2013, 03:44 PM
I have made some progress with the teensy 3.0 and the WIZ820io (W5200) ethernet module. I am using a 3.3v breadboard power supply for the wiznet chip (it can draw up to 175ma). I share ground with the teensy GND pin. I use only one of the wiznet Grnd and 3.3v pins. Teensy pins 10-13 are connected to CS,MOSI,MISO, and SCK of the wiznet module. Since I don't have a header on the teensy Reset, I hook teensy pin 7 to the wiz reset pin and use the sketch to control RESET on the wiznet. By changing the duration of HIGH to 100us on pin 7 and using a delay of 500ms before starting the wiznet initialization, I managed to get good values via SPI from the wiznet module. I added the W5200 mods to the 1.12 IDE Ethernet library.

The W5200 can run up to 33mbs (megabits/sec) as compared to 0.33 mbs for the W5100. The W5200 supports auto-increment on SPI read/writes. I used a simple SPI read/write test to the wiznet buffer area to test SPI speed using Paul's SPI library (AVR emulation). I was able to get 6mbs when clocking the SPI at 24MHz. I got errors for SPI speeds of 4MHz and less -- it appears the data is shifted by 4 bits??

I ran some simple UDP tests (8-byte echo latency, 10 1000-byte packe writes, and 20 1000-byte packet lossless reads) at various SPI speeds. The results of these Ethernet tests along with similar tests on the maple, UNO, and DUE are in the file wizperf.txt at the following github site

https://github.com/manitou48/DUEZoo

The maple and DUE results include SPI+DMA results. I hope to test a teensy version using the SdFat SPI library (FIFO and 16-bit frames) with the wiznet library.

I plan on using a logic analyzer to investigate the problems at 4Mhz and slower ...

Headroom
02-17-2013, 05:04 PM
Interesting observations. I've had my own problems with the WIZ820. I have also updated my Ethernet library to work with the W5200 based WIZ820io and got it to be recognized on the network via Bonjour and send and receive OSC messages. Somehow, however, it was very slow compared to the same setup using the WIZ812. Power supply problems did cross my mind but I dismissed them. I guess I may have to give that a closer look!
I did not use a Teensy 3 but a Teensy++2.

manitou
02-18-2013, 10:16 PM
I hacked a version of w5100.cpp to include the SdFat SPI routines which utilize 16-bit frames and the SPI FIFO. With the SPI clocked at 24MHz, the w5200 buffer read/write test achieved 21.6 mbs (megabits/sec) and UDP write's ran at 14.2 mbs. More results including UDP tests in file wizperf.txt at

https://github.com/manitou48/DUEZoo

maujabur
02-27-2013, 04:46 PM
Hi!
I'm trying to use the WIZ820io module with the Teensy 3.0 just as you did! (@manitou)

I already sorted out the connections and the power supply, but now I'm missing the tweaked library. I took a quik look at https://github.com/manitou48/DUEZoo but couldn't figure out how to do it.

Can you help me?

Thanks!

maujabur
03-06-2013, 09:25 PM
Ok! I get what I have to do, but I cannot find a folder hardware/arduino/sam in arduino 1.0.3. I'm using windows 7. Anybody could help me?

Anyway, I'll give a try to arduino 1.5 beta and see if teensyduino installs.


Hi!
I'm trying to use the WIZ820io module with the Teensy 3.0 just as you did! (@manitou)

I already sorted out the connections and the power supply, but now I'm missing the tweaked library. I took a quik look at https://github.com/manitou48/DUEZoo but couldn't figure out how to do it.

Can you help me?

Thanks!

Headroom
03-06-2013, 09:47 PM
That folder is part of Arduino 1.5.2. You won't find it in 1.0.3.

maujabur
03-06-2013, 10:12 PM
That folder is part of Arduino 1.5.2. You won't find it in 1.0.3.

Thanks!

I installed arduino 1.5.2 and found it.

Now my problem is divided in two:

1. I didn't find yet how to install teensyduino on the 1.5.2, so I can't use the ethernet.h an .cpp files with teensy 3.0

or

2. I don't know how to install those files on arduino 1.0.3 patched with teensyduino. When I put them on the ethernet folder, the first problem when compiling on the function read_data.

Are you able to use the wiz820io with teensy 3.0? how?

Thanks again

Headroom
03-06-2013, 11:25 PM
teensyduino is not compatible with 1.5.2 :p

No I have not worked wit the Teensy 3 yet. However, I don't understand why you would need to go into the hardware folder anyway. The Ethernet Library is a "standard" library. There should be no need to copy anything to the "hardware" folder.
Perhaps you can post a more detailed error report from your compilation attempt.

maujabur
03-07-2013, 12:58 AM
teensyduino is not compatible with 1.5.2 :p

No I have not worked wit the Teensy 3 yet. However, I don't understand why you would need to go into the hardware folder anyway. The Ethernet Library is a "standard" library. There should be no need to copy anything to the "hardware" folder.
Perhaps you can post a more detailed error report from your compilation attempt.


I tried to find the hardware folder because it said on the readme.txt at duezoo...

When I switched the files at the Ethernet on the libraries folder I got the following error:

D:\work\arduino-1.0.3\libraries\Ethernet\utility\socket.cpp: In function 'uint16_t recvfrom(SOCKET, uint8_t*, uint16_t, uint8_t*, uint16_t*)':
D:\work\arduino-1.0.3\libraries\Ethernet\utility\socket.cpp:259:52 : error: invalid conversion from 'uint8_t* {aka unsigned char*}' to 'uint16_t {aka short unsigned int}' [-fpermissive]
In file included from...

But now I will make a test, later I'll let you know if it worked. Thanks!

maujabur
03-07-2013, 01:09 AM
No, it didn't. I tried using the Ethernet and SPI libraries from 1.5.2 on 1.0.3, but it tried to import variants.h. Later I'll read the code for the ethernet library and try to solve it.

I didn't find yet somebody that was able to run the wiz820io with the teensy 3.0, but I'm still looking. Maybe I have to take a look at the SPI as well.

manitou
03-07-2013, 05:27 PM
The git DUEZoo files are for DUE and not for teensy 3. Here are the changed files for teensy 3.0

https://github.com/manitou48/teensy3


They should be applied to arduino-1.0.3/libraries/Ethernet/utility

The Ethernet.h file is in arduino-1.0.3/libraries/Ethernet

good luck.

maujabur
03-07-2013, 05:44 PM
The git DUEZoo files are for DUE and not for teensy 3. Here are the changed files for teensy 3.0

https://github.com/manitou48/teensy3

good luck.


Thank you!

Gonna try it tomorrow!

maujabur
03-07-2013, 08:21 PM
The git DUEZoo files are for DUE and not for teensy 3. Here are the changed files for teensy 3.0

https://github.com/manitou48/teensy3



@manitou Thanks a lot for the files!

Please check the file w5100.cpp , i guess it's missing.

Anyway, gonna try with the w5100.cpp.fat for the sdfat library

Thanks again!

manitou
03-07-2013, 11:18 PM
@manitou Thanks a lot for the files!

Please check the file w5100.cpp , i guess it's missing.
Thanks again!

oops, fixed.

Headroom
06-13-2013, 04:23 PM
Has anyone got the WIZ820io to work with the Teensy3 ?
With "to work" I mean established a network connection. I've gotten the WIZ820io to work with a basic sketch establishing a DHCP connection using the the library files manitou provided on an Arduino UNO out of the box and over the X-Mas timeframe remember I had it also working on a Teensy++2.

However, on a Teesny3 it appears not to be working. I am not at my home machine so can't post the sketch right now, but will do so if needed.
I follwed manitou's description using pin 7 and the sketch to reset the WIZ820io. After compiling/uploading I can see it restarting and the led on the Teensy3 flickering indicating some communication (I assume) but then that stops and no Ethernet connection is made.

manitou
06-13-2013, 06:55 PM
Are you powering WIZ with separate power (plus common ground)? I think it consumes more than teensy 3.0 can supply. You might also try using static IP instead of DHCP. I think I got more consistent results with static IP. On Maple and teensy I often had stalls and such -- I don't know if it was my breadboard wiring, or if my WIZ unit was capricious. I did get both UDP and TCP to work, at least for simple performance tests .... I often just did simple SPI read/write's to the WIZ's RAM, but even those tests would sometimes fail.
good luck

Headroom
06-13-2013, 07:56 PM
Manitou,

Thanks for the feedback. Yes I am powering the WIZ820io from a different 3.3V power supply (http://www.adafruit.com/products/1066)connected to the Teesny3 with a common ground. I've seen your posts on the maple forum and have not yet tried connect the PDWN pin to GND.

I can try it with a static IP but doubt this will make much of a difference. I am more suspecting a SPI peculiarity on the Teensy3 side, because with a regular Arduino UNO it woked in the first attempt and I bet if I try it with my Teensy++2 it'll work as well. I had a prototype Adapter PCB made at OSH Park to connect the Teesny++2 to the WIZ820io a little time before X-Mas 2012 and before you posted your library mods I had modified the Ethernet and Bonjour library mmyself and got ArdOSC working with the WIZ820io.

However, on the the Teesny3 side I've not been so lucky yet. That set-up is also still on a breadboard.

manitou
06-13-2013, 08:42 PM
FWIW, I've added wizfat.ino to https://github.com/manitou48/teensy3
The sketch is a hack for doing various udp/tcp tests against a linux box with ttcp and a rate-controlled UDP test code. It also has code for writing/reading WIZ RAM (and examining the low bytes command-and-control area)

Headroom
06-14-2013, 07:11 PM
Connecting PDWN on the WIZ820io to to GND has remedied the problem. At least so far.
A DHCP connection is generated and the script feeds back the Teesny3 IP address.
Now it's on to get Bonjour and OSC working again.

jpatrick62
01-31-2014, 10:16 PM
Has anyone got the Teensy 3/Wiz820 combo to do anything but get a DHCP request? I can successfully get the request and addressing information, but
if I try to connect to the Ethernet server, it just sits and hangs. I am using a Teensy 3.1 with the Teensyduino 1.17 library and have connected
the PWDN to ground as well. On the other hand, the same sketch using the same Teensyduino version works great with the Wiz812.

jimmayhugh
01-31-2014, 10:41 PM
I've got the Teensy3.x/WIZ820 combo acting as an UDP server, using either DCHP or static IP addressing. I also just recently added EthernetBonjour code that allows me to interrogate the setup using avahi-discover or avahi-browse.

I've got my own board (http://oshpark.com/shared_projects/x5kRJJ1U) that also has 1-wire and I2C running for my TeensyNet Project (http://www.teensynet.com/).

I made my board using D23 for the WIZ820io reset (this was before Paul and the gang started using D9), so I had to modify some of the ethernet library code to support that, as well as increasing the size of the UDP buffer to a more usable size. I'll fix that with my next ineration of the board.

Headroom
01-31-2014, 11:48 PM
I also created my own Frankenduino (http://forum.pjrc.com/threads/23904-teensy3-WIZ820io-adapter-And-then-some?highlight=frankie) adapter board that uses the WIZ820io and can confirm that this works wit the Teensy 3 out of the box.
Please post you sketch so we have something to work with.

Since Teensyduino 1.17 the Ethernet library contains an auto detect so it will automatically detect whether you have a W5100 or w5200 based Ethernet module connected.

jpatrick62
02-20-2014, 04:37 AM
Here's my code - I'm using TeensyDuino Loader 1.17 with Wiz8210 - it will occasionally get a DHCP address, but it never responds to a web page request. Most often, it just hangs waiting for an IP address.
The same code works well for the Wiz812.


#include <SPI.h>
#include <Ethernet.h>
#include <Dns.h>
#include <mac.h>

//consts
const int MAX_LEN = 85;
const int REQ_BUF_SZ = 140;
const int SCRATCH_BUFFER = 25;
const int MAX_RES = 10;
const int WEB_REQUEST_TIMEOUT = 2000;
//+++++++++ LCD Serial Commands++++++++++++++++++++++++++++
const int LCD_CLEAR = 12;
const int LCD_BACKLIGHT_ON = 17;
const int LCD_FF = 13;
const int LCD_BACKLIGHT_OFF = 18;
const int LCD_LF = 10;
const int LCD_NOTE_A = 220;
//------------PINS------------
const int reset_pin = 9; // Pin to monitor for manual Ethernet reset
//-----------PINS-------------
//--------DNS client---------
DNSClient dnsClient;
//--------address struct------
typedef struct
{
char szMacAddress[SCRATCH_BUFFER] = {0}; // MAC Address String
char szLcdMacAddress[SCRATCH_BUFFER]={0}; // MAC Address on LCD (no '-' chars...
char szIpAddress[SCRATCH_BUFFER]={0}; // Ip Address String
char szDnsAddress[SCRATCH_BUFFER]={0}; // DNS Address String
char szGatewayAddress[SCRATCH_BUFFER]={0}; // Default Gateway Address String
char szWebServiceAddress[SCRATCH_BUFFER]={0}; // Web Service Address
} _ADDRESSES;

_ADDRESSES address; // lenox laser string address structure
//-------------------------------

char strPageLine[MAX_LEN]={0};
char req_index = 0; // index into HTTP_req buffer
char szBuffer[SCRATCH_BUFFER] = {0};
char szResult[SCRATCH_BUFFER]={0}; // Numerical operation result
char szTemp[SCRATCH_BUFFER]={0}; // buffer for debug display
char szHostName[SCRATCH_BUFFER]={0}; // buffer for IP host name
//Values of illumination LEDs
int iRetVal = 0;
//timing
EthernetServer server(80); // create a server at port 80
char HTTP_req[REQ_BUF_SZ] = {0}; // stores the HTTP request

// +++++++++++++++Web Pages in Flash++++++++++++++++++++++
//page common - common html stuff
const char pc1 [] = "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>";
const char pc2 [] = "</title></head>\r\n";
const char pc_mainpage[] = "<p>Go back to <a href=\"index.htm\">main page</a></p>\r\n";
const char pce[] = "</body>\r\n</html>\r\n";

const char p1_body2[] = "<p><a href=\"net.htm\">net configuration</a></p>\r\n";
const char p1_close[] = "</html>\r\n";

const int P1_ELEMENTS = 4;
const char* p1[] = {pc1, pc2, p1_body2, p1_close}; //p1
const char* p2[] = {pc1, pc2, pc_mainpage, pce}; //p2

// +++++++++++++++End Web Pages in Flash++++++++++++++++++++++

void setup()
{
Serial3.begin(9600);
//pin setup
pinMode(reset_pin, INPUT); // Ethernet reset

delay(5000);

//Use DHCP if possible
if(MakeDHCPRequest(true)==true)
{
// print the Ethernet board/shield's IP address:
DisplayAddressInfo();
LCD_Display("IP Address:", address.szIpAddress);
server.begin();
}
else
{
LCD_Display("DHCP Request", "-FAILED-");
Serial.print("--Error! - Failed DHCP Attempt---");
}
}
///////////////////////////// The main loop ////////////////////////////
void loop()
{
CheckResetPin();
EthernetClient client = server.available(); // try to get client
if (client)
{ // got client?
boolean currentLineIsBlank = true;

while (client.connected())
{
if (client.available())
{ // client data available to read
char c = client.read(); // read 1 byte (character) from client
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1))
{
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
Serial.print(c); // print HTTP request character to serial monitor
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();

// open requested web page file
if ((strstr(HTTP_req, "GET /index.htm"))||(strstr(HTTP_req, "GET / ")))
{
Serial.println("------------ Request for web service recieved --------------------");
sendPage1(client);
}
else if (strstr(HTTP_req, "GET /net.htm"))
{
sendPage2(client);
}

// reset buffer index and all buffer elements to 0
req_index = 0;
memset(HTTP_req, 0, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n')
{
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r')
{
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
/*------------------------------------------------------------------
dnsResolveToString
Resolve host string to string ip address
IN: pointer to a host string
IN/OUT: pointer to a string IP Address
------------------------------------------------------------------*/
void dnsResolveToString(char* pHost, char* szBuffer)
{
IPAddress ip;
dnsClient.getHostByName(pHost, ip);
ConvertAddress(szBuffer, ip);
Serial.print(pHost);
Serial.print(": ");
Serial.println(szBuffer);
}
/*------------------------------------------------------------------
LCD_Display
Display contents of a buffer on teh 16x2 LCD grid. The first line
is used to identify Lenox Laser's controller, while the 2nd line
can be used for custom display.
IN: pointer to a char buffer for line 1 (16 chars max)
IN: pointer to a char buffer for line 2 (16 chars max)
Returns: void
------------------------------------------------------------------*/
void LCD_Display(char* szLine1, char* szLine2)
{
Serial3.write(LCD_CLEAR);
if(szLine1)
{
Serial3.println(szLine1);
}
if(szLine2)
{
Serial3.write(LCD_LF);
Serial3.println(szLine2);
}
Serial3.write(LCD_NOTE_A);
}
/*---------------------------------------------------------------
MakeDHCPRequest()

Attempts a DHCP request for network connectivity.
Returns: Boolean result
---------------------------------------------------------------*/
bool MakeDHCPRequest(bool bLCD)
{
if(bLCD == true)
LCD_Display("Initializing", "Network...");
Serial.println("Getting address information, please wait...");
if (Ethernet.begin(GetFirmwareMAC()))
{
Serial3.write(LCD_NOTE_A);
dnsClient.begin(Ethernet.dnsServerIP());
return true;
}
else
{
Serial.println("Failed to get address from DHCP server");
Serial3.write(LCD_CLEAR);
Serial3.println("Address FAIL!");
Serial3.write(LCD_NOTE_A);
return false;
}
}
/*------------------------------------------------------------------
DisplayAddressInfo


------------------------------------------------------------------*/
void DisplayAddressInfo()
{
Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS: ");
Serial.println(Ethernet.dnsServerIP());
Serial.print("MAC: " );
Serial.println(address.szMacAddress);
ConvertAddress(address.szDnsAddress, Ethernet.dnsServerIP()); // convert the 32 bit dns address
ConvertAddress(address.szIpAddress, Ethernet.localIP()); // convert the 32 bit IP address
}
/*------------------------------------------------------------------
GetFirmwareMAC
Reads the firmware MAC burned into teensy 3.0+. Needs the mac.h
header...
IN: void
Returns: a pointer to a SRAM byte buffer that can be used by the
Ethernet object's begin() method.
-----------------------------------------------------------------*/
byte* GetFirmwareMAC()
{
read_mac();
sprintf(address.szMacAddress, "%02x-%02x-%02x-%02x-%02x-%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(address.szLcdMacAddress, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(szHostName, "WIZnet0%01x%02x%02x", mac[3], mac[4], mac[5]);
return mac;
}
/*---------------------------------------------------------
sendPage1
This function is called to display the initial choices given
to the browser. These choices include network settings, LED
value modifications and settings, flow tests, etc.
IN: Reference to an EthernetClient object
Returns: void
---------------------------------------------------------*/
void sendPage1(EthernetClient & client)
{
// send HTML header
client.println("");
for(int i= 0; i<P1_ELEMENTS; i++)
{
strcpy_P(strPageLine, (char*)p1[i]);
client.print(strPageLine);
}
memset(strPageLine, 0, MAX_LEN);
}
/*---------------------------------------------------------
sendPage2
This function is called to display in the browser the current
IP network settings for the Microprocessor.
IN: Reference to an EthernetClient object
Returns: void
---------------------------------------------------------*/
void sendPage2(EthernetClient & client)
{
// send HTML header
client.println("");
strcpy_P(strPageLine, (char*)p2[0]);
client.print(strPageLine);
client.print("Network Configuration");
strcpy_P(strPageLine, (char*)p2[1]);
client.print(strPageLine);
//
sendConfigurationInfo(client);
//
strcpy_P(strPageLine, (char*)p2[2]);
client.print(strPageLine);
strcpy_P(strPageLine, (char*)p2[3]);
client.print(strPageLine);
memset(strPageLine, 0, MAX_LEN);
}
/*--------------------------------------------------
sendConfigurationInfo
Send the current IP address information to the client
IN: Reference to EthernetClient object
Returns: void
---------------------------------------------------*/
void sendConfigurationInfo(EthernetClient & client)
{
client.print("<h1>Network Configuration</h1>\r\n");
//method
client.print(szBuffer);
client.print("<table border=\"1\">\r\n<tr>\r\n<th>Network Item</th>\r\n<th>Value</th>\r\n</tr>\r\n");
//ip address
client.print("<tr>\r\n<td>IP Address</td>\r\n");
client.print("<td>");
client.print(Ethernet.localIP());
client.print("</td>\r\n</tr>\r\n");
//Ethernet.subnetMask()
client.print("<tr>\r\n<td>Subnet Mask</td>\r\n");
client.print("<td>");
client.print(Ethernet.subnetMask());
client.print("</td>\r\n</tr>\r\n");
//mac
client.print("<tr>\r\n<td>MAC Address</td>\r\n");
client.print("<td>");
client.print(address.szMacAddress);
client.print("</td>\r\n</tr>\r\n");
//default gateway
client.print("<tr>\r\n<td>Default Gateway</td>\r\n");
client.print("<td>");
client.print(Ethernet.gatewayIP());
client.print("</td>\r\n</tr>\r\n");
//dns
client.print("<tr>\r\n<td>DNS Server</td>\r\n");
client.print("<td>");
client.print(Ethernet.dnsServerIP());
client.print("</td>\r\n</tr>\r\n</table>\r\n");
}
/*------------------------------------------------------------
ConvertAddress
This method converts an unsigned integer (32 bit) address format
into a format that can be used as a character string.

IN: szAddress - pointer to a char string buffer
IN: _uiaddress - 32 bit ip address format
Returns: void
------------------------------------------------------------*/
void ConvertAddress(char* szAddress, uint32_t _uiaddress)
{
union
{
uint32_t unsigned_int;
uint8_t bytes[4];
} Address;

Address.unsigned_int = _uiaddress;
sprintf(szAddress, "%d.%d.%d.%d", Address.bytes[0], Address.bytes[1], Address.bytes[2], Address.bytes[3]);
}
/*-----------------------------------------------------------
CheckResetPin

Check the value of the Ethernet reset pin. If this value is low
for > 2mSecs, then we reset the Ethernet adapter. This allows us
a manual reset...
-----------------------------------------------------------*/
void CheckResetPin()
{
if(digitalRead(reset_pin) == LOW)
{
Serial.println("******* Ethernet Reset detected! ******");
LCD_Display("Resetting", "Please wait");

if(MakeDHCPRequest(true)==true)
{
// print the Ethernet board/shield's IP address:
DisplayAddressInfo();
LCD_Display("*IP Address:", address.szIpAddress); // create a web service request with our address information
}
else
{
Serial.print("--Error! - Failed DHCP Attempt---");
}
}
}

Headroom
02-20-2014, 11:53 AM
Your code would be easier to read if posted in code tags:


your code

Its the hash (#) in the advanced edit menu

jpatrick62
02-21-2014, 12:20 AM
Ok, finally ound the hash tag - thanks for the info. The code below works for the WIZ812MJ fine. I get
a DHCP address and can connect a client intot he web server. However, I cannot get the WIZ820 to work - although sometimes I will get a DHCP assigned address,
most of the times it just habgs waiting. Even when I do get an address, I can never connect to the web server. I am using Teensy 3.1 with loader 1.17.




#include <SPI.h>
#include <Ethernet.h>
#include <Dns.h>
#include <mac.h>

//consts
const int MAX_LEN = 85;
const int REQ_BUF_SZ = 140;
const int SCRATCH_BUFFER = 25;
const int MAX_RES = 10;
const int WEB_REQUEST_TIMEOUT = 2000;
//+++++++++ LCD Serial Commands++++++++++++++++++++++++++++
const int LCD_CLEAR = 12;
const int LCD_BACKLIGHT_ON = 17;
const int LCD_FF = 13;
const int LCD_BACKLIGHT_OFF = 18;
const int LCD_LF = 10;
const int LCD_NOTE_A = 220;
//------------PINS------------
const int reset_pin = 9; // Pin to monitor for manual Ethernet reset
//-----------PINS-------------
//--------DNS client---------
DNSClient dnsClient;
//--------address struct------
typedef struct
{
char szMacAddress[SCRATCH_BUFFER] = {0}; // MAC Address String
char szLcdMacAddress[SCRATCH_BUFFER]={0}; // MAC Address on LCD (no '-' chars...
char szIpAddress[SCRATCH_BUFFER]={0}; // Ip Address String
char szDnsAddress[SCRATCH_BUFFER]={0}; // DNS Address String
char szGatewayAddress[SCRATCH_BUFFER]={0}; // Default Gateway Address String
char szWebServiceAddress[SCRATCH_BUFFER]={0}; // Web Service Address
} _ADDRESSES;

_ADDRESSES address;
//-------------------------------

char strPageLine[MAX_LEN]={0};
char req_index = 0; // index into HTTP_req buffer
char szBuffer[SCRATCH_BUFFER] = {0};
char szResult[SCRATCH_BUFFER]={0}; // Numerical operation result
char szTemp[SCRATCH_BUFFER]={0}; // buffer for debug display
char szHostName[SCRATCH_BUFFER]={0}; // buffer for IP host name
//Values of illumination LEDs
int iRetVal = 0;
//timing
EthernetServer server(80); // create a server at port 80
char HTTP_req[REQ_BUF_SZ] = {0}; // stores the HTTP request

// +++++++++++++++Web Pages in Flash++++++++++++++++++++++
//page common - common html stuff
const char pc1 [] = "<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>";
const char pc2 [] = "</title></head>\r\n";
const char pc_mainpage[] = "<p>Go back to <a href=\"index.htm\">main page</a></p>\r\n";
const char pce[] = "</body>\r\n</html>\r\n";

const char p1_body2[] = "<p><a href=\"net.htm\">net configuration</a></p>\r\n";
const char p1_close[] = "</html>\r\n";

const int P1_ELEMENTS = 4;
const char* p1[] = {pc1, pc2, p1_body2, p1_close}; //p1
const char* p2[] = {pc1, pc2, pc_mainpage, pce}; //p2

// +++++++++++++++End Web Pages in Flash++++++++++++++++++++++

void setup()
{
Serial3.begin(9600);
//pin setup
pinMode(reset_pin, INPUT); // Ethernet reset

delay(5000);

//Use DHCP if possible
if(MakeDHCPRequest(true)==true)
{
// print the Ethernet board/shield's IP address:
DisplayAddressInfo();
LCD_Display("IP Address:", address.szIpAddress);
server.begin();
}
else
{
LCD_Display("DHCP Request", "-FAILED-");
Serial.print("--Error! - Failed DHCP Attempt---");
}
}
///////////////////////////// The main loop ////////////////////////////
void loop()
{
CheckResetPin();
EthernetClient client = server.available(); // try to get client
if (client)
{ // got client?
boolean currentLineIsBlank = true;

while (client.connected())
{
if (client.available())
{ // client data available to read
char c = client.read(); // read 1 byte (character) from client
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1))
{
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
Serial.print(c); // print HTTP request character to serial monitor
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();

// open requested web page file
if ((strstr(HTTP_req, "GET /index.htm"))||(strstr(HTTP_req, "GET / ")))
{
Serial.println("------------ Request for web service recieved --------------------");
sendPage1(client);
}
else if (strstr(HTTP_req, "GET /net.htm"))
{
sendPage2(client);
}

// reset buffer index and all buffer elements to 0
req_index = 0;
memset(HTTP_req, 0, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n')
{
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r')
{
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
/*------------------------------------------------------------------
dnsResolveToString
Resolve host string to string ip address
IN: pointer to a host string
IN/OUT: pointer to a string IP Address
------------------------------------------------------------------*/
void dnsResolveToString(char* pHost, char* szBuffer)
{
IPAddress ip;
dnsClient.getHostByName(pHost, ip);
ConvertAddress(szBuffer, ip);
Serial.print(pHost);
Serial.print(": ");
Serial.println(szBuffer);
}
/*------------------------------------------------------------------
LCD_Display
Display contents of a buffer on teh 16x2 LCD grid. The first line
is used to identify Lenox Laser's controller, while the 2nd line
can be used for custom display.
IN: pointer to a char buffer for line 1 (16 chars max)
IN: pointer to a char buffer for line 2 (16 chars max)
Returns: void
------------------------------------------------------------------*/
void LCD_Display(char* szLine1, char* szLine2)
{
Serial3.write(LCD_CLEAR);
if(szLine1)
{
Serial3.println(szLine1);
}
if(szLine2)
{
Serial3.write(LCD_LF);
Serial3.println(szLine2);
}
Serial3.write(LCD_NOTE_A);
}
/*---------------------------------------------------------------
MakeDHCPRequest()

Attempts a DHCP request for network connectivity.
Returns: Boolean result
---------------------------------------------------------------*/
bool MakeDHCPRequest(bool bLCD)
{
if(bLCD == true)
LCD_Display("Initializing", "Network...");
Serial.println("Getting address information, please wait...");
if (Ethernet.begin(GetFirmwareMAC()))
{
Serial3.write(LCD_NOTE_A);
dnsClient.begin(Ethernet.dnsServerIP());
return true;
}
else
{
Serial.println("Failed to get address from DHCP server");
Serial3.write(LCD_CLEAR);
Serial3.println("Address FAIL!");
Serial3.write(LCD_NOTE_A);
return false;
}
}
/*------------------------------------------------------------------
DisplayAddressInfo


------------------------------------------------------------------*/
void DisplayAddressInfo()
{
Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS: ");
Serial.println(Ethernet.dnsServerIP());
Serial.print("MAC: " );
Serial.println(address.szMacAddress);
ConvertAddress(address.szDnsAddress, Ethernet.dnsServerIP()); // convert the 32 bit dns address
ConvertAddress(address.szIpAddress, Ethernet.localIP()); // convert the 32 bit IP address
}
/*------------------------------------------------------------------
GetFirmwareMAC
Reads the firmware MAC burned into teensy 3.0+. Needs the mac.h
header...
IN: void
Returns: a pointer to a byte buffer that can be used by the
Ethernet object's begin() method.
-----------------------------------------------------------------*/
byte* GetFirmwareMAC()
{
read_mac();
sprintf(address.szMacAddress, "%02x-%02x-%02x-%02x-%02x-%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(address.szLcdMacAddress, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(szHostName, "WIZnet0%01x%02x%02x", mac[3], mac[4], mac[5]);
return mac;
}
/*---------------------------------------------------------
sendPage1
This function is called to display the initial choices given
to the browser. These choices include network settings, LED
value modifications and settings, flow tests, etc.
IN: Reference to an EthernetClient object
Returns: void
---------------------------------------------------------*/
void sendPage1(EthernetClient & client)
{
// send HTML header
client.println("");
for(int i= 0; i<P1_ELEMENTS; i++)
{
strcpy_P(strPageLine, (char*)p1[i]);
client.print(strPageLine);
}
memset(strPageLine, 0, MAX_LEN);
}
/*---------------------------------------------------------
sendPage2
This function is called to display in the browser the current
IP network settings for the Microprocessor.
IN: Reference to an EthernetClient object
Returns: void
---------------------------------------------------------*/
void sendPage2(EthernetClient & client)
{
// send HTML header
client.println("");
strcpy_P(strPageLine, (char*)p2[0]);
client.print(strPageLine);
client.print("Network Configuration");
strcpy_P(strPageLine, (char*)p2[1]);
client.print(strPageLine);
//
sendConfigurationInfo(client);
//
strcpy_P(strPageLine, (char*)p2[2]);
client.print(strPageLine);
strcpy_P(strPageLine, (char*)p2[3]);
client.print(strPageLine);
memset(strPageLine, 0, MAX_LEN);
}
/*--------------------------------------------------
sendConfigurationInfo
Send the current IP address information to the client
IN: Reference to EthernetClient object
Returns: void
---------------------------------------------------*/
void sendConfigurationInfo(EthernetClient & client)
{
client.print("<h1>Network Configuration</h1>\r\n");
//method
client.print(szBuffer);
client.print("<table border=\"1\">\r\n<tr>\r\n<th>Network Item</th>\r\n<th>Value</th>\r\n</tr>\r\n");
//ip address
client.print("<tr>\r\n<td>IP Address</td>\r\n");
client.print("<td>");
client.print(Ethernet.localIP());
client.print("</td>\r\n</tr>\r\n");
//Ethernet.subnetMask()
client.print("<tr>\r\n<td>Subnet Mask</td>\r\n");
client.print("<td>");
client.print(Ethernet.subnetMask());
client.print("</td>\r\n</tr>\r\n");
//mac
client.print("<tr>\r\n<td>MAC Address</td>\r\n");
client.print("<td>");
client.print(address.szMacAddress);
client.print("</td>\r\n</tr>\r\n");
//default gateway
client.print("<tr>\r\n<td>Default Gateway</td>\r\n");
client.print("<td>");
client.print(Ethernet.gatewayIP());
client.print("</td>\r\n</tr>\r\n");
//dns
client.print("<tr>\r\n<td>DNS Server</td>\r\n");
client.print("<td>");
client.print(Ethernet.dnsServerIP());
client.print("</td>\r\n</tr>\r\n</table>\r\n");
}
/*------------------------------------------------------------
ConvertAddress
This method converts an unsigned integer (32 bit) address format
into a format that can be used as a character string.

IN: szAddress - pointer to a char string buffer
IN: _uiaddress - 32 bit ip address format
Returns: void
------------------------------------------------------------*/
void ConvertAddress(char* szAddress, uint32_t _uiaddress)
{
union
{
uint32_t unsigned_int;
uint8_t bytes[4];
} Address;

Address.unsigned_int = _uiaddress;
sprintf(szAddress, "%d.%d.%d.%d", Address.bytes[0], Address.bytes[1], Address.bytes[2], Address.bytes[3]);
}
/*-----------------------------------------------------------
CheckResetPin

Check the value of the Ethernet reset pin. If this value is low
for > 2mSecs, then we reset the Ethernet adapter. This allows us
a manual reset...
-----------------------------------------------------------*/
void CheckResetPin()
{
if(digitalRead(reset_pin) == LOW)
{
Serial.println("******* Ethernet Reset detected! ******");
LCD_Display("Resetting", "Please wait");

if(MakeDHCPRequest(true)==true)
{
// print the Ethernet board/shield's IP address:
DisplayAddressInfo();
LCD_Display("*IP Address:", address.szIpAddress); // create a web service request with our address information
}
else
{
Serial.print("--Error! - Failed DHCP Attempt---");
}
}
}

Headroom
02-21-2014, 12:35 AM
Nice. Can you reduce the code to something small that can reproduce the problem ?
E.G. reduce it to something that I can run here on my Teensy3 <-> WIZ820io board ?

I am not sure the CheckReset Code will work as is with the WIZ820io.

jpatrick62
02-21-2014, 01:06 AM
I tried to take any extraneous "stuff" out to make a bare sketch.



#include <SPI.h>
#include <Ethernet.h>
#include <Dns.h>
#include <mac.h>

//consts
const int MAX_LEN = 85;
const int REQ_BUF_SZ = 140;
const int SCRATCH_BUFFER = 25;
const int WEB_REQUEST_TIMEOUT = 2000;
//+++++++++ LCD Serial Commands++++++++++++++++++++++++++++
const int LCD_CLEAR = 12;
const int LCD_FF = 13;
const int LCD_LF = 10;
const int LCD_NOTE_A = 220;

//--------DNS client---------
DNSClient dnsClient;
//--------address struct------
typedef struct
{
char szMacAddress[SCRATCH_BUFFER] = {0}; // MAC Address String
char szLcdMacAddress[SCRATCH_BUFFER]={0}; // MAC Address on LCD (no '-' chars...
char szIpAddress[SCRATCH_BUFFER]={0}; // Ip Address String
char szDnsAddress[SCRATCH_BUFFER]={0}; // DNS Address String
char szGatewayAddress[SCRATCH_BUFFER]={0}; // Default Gateway Address String
char szWebServiceAddress[SCRATCH_BUFFER]={0}; // Web Service Address
} _ADDRESSES;

_ADDRESSES address;
//-------------------------------

char strPageLine[MAX_LEN]={0};
char req_index = 0; // index into HTTP_req buffer
char szBuffer[SCRATCH_BUFFER] = {0};
char szResult[SCRATCH_BUFFER]={0}; // Numerical operation result
char szTemp[SCRATCH_BUFFER]={0}; // buffer for debug display
char szHostName[SCRATCH_BUFFER]={0}; // buffer for IP host name
//Values of illumination LEDs
int iRetVal = 0;
//timing
EthernetServer server(80); // create a server at port 80
char HTTP_req[REQ_BUF_SZ] = {0}; // stores the HTTP request

/////////////////////////////// Setup /////////////////////

void setup()
{
Serial3.begin(9600);

delay(5000);

//Use DHCP if possible
if(MakeDHCPRequest(true)==true)
{
// print the Ethernet board/shield's IP address:
DisplayAddressInfo();
LCD_Display("IP Address:", address.szIpAddress);
server.begin();
}
else
{
LCD_Display("DHCP Request", "-FAILED-");
Serial.print("--Error! - Failed DHCP Attempt---");
}
}
///////////////////////////// The main loop ////////////////////////////
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client)
{ // got client?
boolean currentLineIsBlank = true;

while (client.connected())
{
if (client.available())
{ // client data available to read
char c = client.read(); // read 1 byte (character) from client
// buffer first part of HTTP request in HTTP_req array (string)
// leave last element in array as 0 to null terminate string (REQ_BUF_SZ - 1)
if (req_index < (REQ_BUF_SZ - 1))
{
HTTP_req[req_index] = c; // save HTTP request character
req_index++;
}
Serial.print(c); // print HTTP request character to serial monitor
// last line of client request is blank and ends with \n
// respond to client only after last line received
if (c == '\n' && currentLineIsBlank)
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();

// open requested web page file
if ((strstr(HTTP_req, "GET /index.htm"))||(strstr(HTTP_req, "GET / ")))
{
Serial.println("------------ Request for web service recieved --------------------");
sendPage(client);
}
// reset buffer index and all buffer elements to 0
req_index = 0;
memset(HTTP_req, 0, REQ_BUF_SZ);
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n')
{
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r')
{
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
/*------------------------------------------------------------------
dnsResolveToString
Resolve host string to string ip address
IN: pointer to a host string
IN/OUT: pointer to a string IP Address
------------------------------------------------------------------*/
void dnsResolveToString(char* pHost, char* szBuffer)
{
IPAddress ip;
dnsClient.getHostByName(pHost, ip);
ConvertAddress(szBuffer, ip);
Serial.print(pHost);
Serial.print(": ");
Serial.println(szBuffer);
}
/*------------------------------------------------------------------
LCD_Display
Display contents of a buffer on teh 16x2 LCD grid. The first line
is used to identify Lenox Laser's controller, while the 2nd line
can be used for custom display.
IN: pointer to a char buffer for line 1 (16 chars max)
IN: pointer to a char buffer for line 2 (16 chars max)
Returns: void
------------------------------------------------------------------*/
void LCD_Display(char* szLine1, char* szLine2)
{
Serial3.write(LCD_CLEAR);
if(szLine1)
{
Serial3.println(szLine1);
}
if(szLine2)
{
Serial3.write(LCD_LF);
Serial3.println(szLine2);
}
Serial3.write(LCD_NOTE_A);
}
/*---------------------------------------------------------------
MakeDHCPRequest()

Attempts a DHCP request for network connectivity.
Returns: Boolean result
---------------------------------------------------------------*/
bool MakeDHCPRequest(bool bLCD)
{
if(bLCD == true)
LCD_Display("Initializing", "Network...");
Serial.println("Getting address information, please wait...");
if (Ethernet.begin(GetFirmwareMAC()))
{
Serial3.write(LCD_NOTE_A);
dnsClient.begin(Ethernet.dnsServerIP());
return true;
}
else
{
Serial.println("Failed to get address from DHCP server");
Serial3.write(LCD_CLEAR);
Serial3.println("Address FAIL!");
Serial3.write(LCD_NOTE_A);
return false;
}
}
/*------------------------------------------------------------------
DisplayAddressInfo


------------------------------------------------------------------*/
void DisplayAddressInfo()
{
Serial.print("IP address: ");
Serial.println(Ethernet.localIP());
Serial.print("Subnet Mask: ");
Serial.println(Ethernet.subnetMask());
Serial.print("Default Gateway: ");
Serial.println(Ethernet.gatewayIP());
Serial.print("DNS: ");
Serial.println(Ethernet.dnsServerIP());
Serial.print("MAC: " );
Serial.println(address.szMacAddress);
ConvertAddress(address.szDnsAddress, Ethernet.dnsServerIP()); // convert the 32 bit dns address
ConvertAddress(address.szIpAddress, Ethernet.localIP()); // convert the 32 bit IP address
}
/*------------------------------------------------------------------
GetFirmwareMAC
Reads the firmware MAC burned into teensy 3.0+. Needs the mac.h
header...
IN: void
Returns: a pointer to a SRAM byte buffer that can be used by the
Ethernet object's begin() method.
-----------------------------------------------------------------*/
byte* GetFirmwareMAC()
{
read_mac();
sprintf(address.szMacAddress, "%02x-%02x-%02x-%02x-%02x-%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(address.szLcdMacAddress, "%02x%02x%02x%02x%02x%02x", mac[0], mac[1], mac[2], mac[3], mac[4], mac[5]);
sprintf(szHostName, "WIZnet0%01x%02x%02x", mac[3], mac[4], mac[5]);
return mac;
}
/*---------------------------------------------------------
sendPage
This function is called to display a Hello
Returns: void
---------------------------------------------------------*/
void sendPage(EthernetClient & client)
{
// send Hello msg
client.println("");
client.println("<!DOCTYPE html>\r\n<html>\r\n<head>\r\n<title>");
client.println("Hello</title>\r\n<body>\r\n");
client.println("<p>Hello from Teensy 3.1 and WIZ820io</p>\r\n");
client.println("</body>\r\n</html>\r\n");
memset(strPageLine, 0, MAX_LEN);
}
/*------------------------------------------------------------
ConvertAddress
This method converts an unsigned integer (32 bit) address format
into a format that can be used as a character string.

IN: szAddress - pointer to a char string buffer
IN: _uiaddress - 32 bit ip address format
Returns: void
------------------------------------------------------------*/
void ConvertAddress(char* szAddress, uint32_t _uiaddress)
{
union
{
uint32_t unsigned_int;
uint8_t bytes[4];
} Address;

Address.unsigned_int = _uiaddress;
sprintf(szAddress, "%d.%d.%d.%d", Address.bytes[0], Address.bytes[1], Address.bytes[2], Address.bytes[3]);
}

jpatrick62
02-21-2014, 01:34 AM
Headroom, thanks for your help - I got it going. I changed the sendpage function to this and now I get a page served.



void sendPage(EthernetClient & client)
{
// send Hello msg
client.println("<html>\r\n<head>\r\n<title>");
client.println("Hello</title>\r\n</head>\r\n");
client.println("<body><h1>Hello from Teensy 3.1 and WIZ820io</h1>\r\n");
client.println("</body>\r\n</html>\r\n");
}


I'll find out what code the 812MJ could tolerate but the 820io would not and post it. Thanks again!