I have been trying to set up my arduino to read the IP information from an SD card. So far I have that part working. Bit I’ve having an issue with refreshing a web page. I can load the page on the first attempt but after that the page just hangs.
The file “network.txt” is read and parsed to set up the MAC, IP, Subnet Mask, Gateway and the DNS with each value on it’s own line with a blank line at the end. So far that part is working fine.
But the server portion in the loop works just once and will not serve up another page unless I reset the Arduino.
Attached is my code. Forgive me if it looks a bit messy.
Please note that I’m using the 1.0.6 IDE. It’s the only one I could find that accepts sscanf as valid which I mentioned works fine.
#include <SD.h>
#include <SPI.h>
#include <Ethernet.h>
byte myMac[6];
byte myIP[4];
byte myNM[4];
byte myGW[4];
byte myDNS[4];
EthernetServer server(80);
void setup() {
Serial.begin(9600);
pinMode(10,OUTPUT);
//digitalWrite(10,HIGH);
if(!SD.begin(4)) Serial.println("SD fail");
else Serial.println("SD ok");
File fh = SD.open("network.txt",FILE_READ);
char netBuffer[32];
if(!fh)
{
Serial.println("SD open fail");
return;
}
int chPos = 0;
int lineNo = 0;
while(fh.available())
{
char ch = fh.read();
if(ch == '\r') {
chPos = 0;
switch(lineNo) {
case 0:
sscanf(netBuffer,"%2x:%2x:%2x:%2x:%2x:%2x",&myMac[0],&myMac[1],&myMac[2],&myMac[3],&myMac[4],&myMac[5]);
break;
case 1:
sscanf(netBuffer,"%u.%u.%u.%u",&myIP[0],&myIP[1],&myIP[2],&myIP[3]);
break;
case 2:
sscanf(netBuffer,"%u.%u.%u.%u",&myNM[0],&myNM[1],&myNM[2],&myNM[3]);
break;
case 3:
sscanf(netBuffer,"%u.%u.%u.%u",&myGW[0],&myGW[1],&myGW[2],&myGW[3]);
break;
case 4:
sscanf(netBuffer,"%u.%u.%u.%u",&myDNS[0],&myDNS[1],&myDNS[2],&myDNS[3]);
break;
}
lineNo++;
}
else if(ch == '\n') {
// do nothing
}
else if(chPos < 31) {
netBuffer[chPos] = ch;
chPos++;
netBuffer[chPos] = 0;
}
}
fh.close();
int x;
Serial.print("\r\nmac ");
for(x=0;x<6;x++) {
Serial.print(myMac[x],HEX);
if(x<5) Serial.print(":");
}
Serial.print("\r\nip ");
for(x=0;x<4;x++) {
Serial.print(myIP[x],DEC);
if(x<3) Serial.print(".");
}
Serial.print("\r\nnetmask ");
for(x=0;x<4;x++) {
Serial.print(myNM[x],DEC);
if(x<3) Serial.print(".");
}
Serial.print("\r\ngateway ");
for(x=0;x<4;x++) {
Serial.print(myGW[x],DEC);
if(x<3) Serial.print(".");
}
Serial.print("\r\ndns ");
for(x=0;x<4;x++) {
Serial.print(myDNS[x],DEC);
if(x<3) Serial.print(".");
}
Serial.println("\r\nStarting ethernet");
Ethernet.begin(myMac,myIP,myDNS,myGW,myNM);
//digitalWrite(10,LOW);
server.begin();
Serial.println(Ethernet.localIP());
Serial.println("============================");
}
// ===================================================================================
// ===================================================================================
void loop()
{
// listen for incoming clients
EthernetClient client = server.available();
if (client) {
Serial.println("new client");
// an http request ends with a blank line
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
Serial.write(c);
// if you've gotten to the end of the line (received a newline
// character) and the line is blank, the http request has ended,
// so you can send a reply
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("Connection: close"); // the connection will be closed after completion of the response
client.println("Refresh: 5"); // refresh the page automatically every 5 sec
client.println();
client.println("<!DOCTYPE HTML>");
client.println("<html>");
// output the value of each analog input pin
for (int analogChannel = 0; analogChannel < 6; analogChannel++) {
int sensorReading = analogRead(analogChannel);
client.print("analog input ");
client.print(analogChannel);
client.print(" is ");
client.print(sensorReading);
client.println("
");
}
client.println("</html>");
break;
}
if (c == '\n') {
// you're starting a new line
currentLineIsBlank = true;
} else if (c != '\r') {
// you've gotten a character on the current line
currentLineIsBlank = false;
}
}
}
// give the web browser time to receive the data
delay(1);
// close the connection:
client.stop();
Serial.println("client disconnected");
Serial.println("============================");
}
}
Thank you for any help you can give me.
SP