WIZ820io Ethernet and 2nd power supply with teensy 3.0

Status
Not open for further replies.
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);
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---");
}
}
}
 
Your code would be easier to read if posted in code tags:

Code:
 your code

Its the hash (#) in the advanced edit menu
 
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.


Code:
#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---");
    }
  }
}
 
Last edited:
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.
 
I tried to take any extraneous "stuff" out to make a bare sketch.

Code:
#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]);
}
 
Headroom, thanks for your help - I got it going. I changed the sendpage function to this and now I get a page served.

Code:
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!
 
Status
Not open for further replies.
Back
Top