Hi,
I am using an ENC82J60 module with my Nano, I've downloaded a working web server and i'm using the EtherCard.h library. I can turn an LED on/off etc. by looking for a specific string in the HTTP header with strstr.
I would now like to parse the header so I can read querystrings etc.
For my project I wish to take a querystring parameter and display it on an LCD screen.
I have tried casting the Ethernet::buffer to a string so I can do this but I've had no success.
char* chArray = (char *)Ethernet::buffer + pos;
The chArray pointer contains the following:
GET /?status=0 HTTP/1.1
Host: 192.168.7.221
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/42.0.2311.135 Safari/537.36
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en-GB,en-US;q=0.8,en;q=0.6
How can I cast this to a string so that I can parse it?
Regards
Dan
I have tried casting the Ethernet::buffer to a string so I can do this but I've had no success.
I recommend that you visit http://snippets-r-us.com for help with your snippets.
Hi Paul, here's the full code:
#include <EtherCard.h>
#define STATIC 0 // set to 1 to disable DHCP (adjust myip/gwip values below)
// mac address
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ethernet interface ip address
static byte myip[] = { 192,168,0,200 };
// gateway ip address
static byte gwip[] = { 192,168,0,1 };
// LED to control output
int ledPin10 = 10;
byte Ethernet::buffer[700];
char page[] PROGMEM =
"HTTP/1.0 503 Service Unavailable\r\n"
"Content-Type: text/html\r\n"
"Retry-After: 600\r\n"
"\r\n"
"<html>"
"<head><title>"
"Service Temporarily Unavailable"
"</title></head>"
"<body>"
"<h3>This service is currently unavailable</h3>"
"<p><em>"
"The main server is currently off-line.
"
"Please try again later."
"</em></p>"
"</body>"
"</html>"
;
void setup () {
pinMode(ledPin10, OUTPUT);
Serial.begin(9600);
Serial.println("Trying to get an IP...");
Serial.print("MAC: ");
for (byte i = 0; i < 6; ++i) {
Serial.print(mymac[i], HEX);
if (i < 5)
Serial.print(':');
}
Serial.println();
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println( "Failed to access Ethernet controller");
#if STATIC
Serial.println( "Getting static IP.");
if (!ether.staticSetup(myip, gwip)){
Serial.println( "could not get a static IP");
blinkLed(); // blink forever to indicate a problem
}
#else
Serial.println("Setting up DHCP");
if (!ether.dhcpSetup()){
Serial.println( "DHCP failed");
blinkLed(); // blink forever to indicate a problem
}
#endif
ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.mymask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
}
void loop () {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
char* chArray = (char *)Ethernet::buffer + pos;
// IF LED10=ON turn it ON
if(strstr(chArray, "GET /?LED10=ON") != 0) {
Serial.println("Received ON command");
digitalWrite(ledPin10, HIGH);
}
// IF LED10=OFF turn it OFF
if(strstr(chArray, "GET /?LED10=OFF") != 0) {
Serial.println("Received OFF command");
digitalWrite(ledPin10, LOW);
}
// show some data to the user
memcpy_P(ether.tcpOffset(), page, sizeof page);
ether.httpServerReply(sizeof page - 1);
}
void blinkLed(){
while (true){
digitalWrite(ledPin10, HIGH);
delay(500);
digitalWrite(ledPin10, LOW);
delay(500);
}
}
Have you tried using parentheses:
char* chArray = (char *)(Ethernet::buffer + pos);
When you say that you've had no success, that really conveys almost no information. Did the code not compile? Did it compile, but not do what you want? If that is the case, what DID it do?