Hi Good Day guys! I need help with my project.
I need to control something using a webpage.
Here is a sample program for a web switch using EtherCard.h library.
#include <EtherCard.h>
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = {192,168,1,2};
byte Ethernet::buffer[700];
const int ledPin = 2;
boolean ledStatus;
char* on = "ON";
char* off = "OFF";
char* statusLabel;
char* buttonLabel;
void setup () {
Serial.begin(600);
Serial.println("WebLed Demo");
if (!ether.begin(sizeof Ethernet::buffer, mymac, 10))
Serial.println( "Failed to access Ethernet controller");
else
Serial.println("Ethernet controller initialized");
if (!ether.staticSetup(myip))
Serial.println("Failed to set IP address");
Serial.println();
pinMode(ledPin, OUTPUT);
digitalWrite(ledPin, LOW);
ledStatus = false;
}
void loop() {
word len = ether.packetReceive();
word pos = ether.packetLoop(len);
if(pos) {
if(strstr((char *)Ethernet::buffer + pos, "GET /?status=ON") != 0) {
Serial.println("Received ON command");
ledStatus = true;
}
if(strstr((char *)Ethernet::buffer + pos, "GET /?status=OFF") != 0) {
Serial.println("Received OFF command");
ledStatus = false;
}
if(ledStatus) {
digitalWrite(ledPin, HIGH);
statusLabel = on;
buttonLabel = off;
} else {
digitalWrite(ledPin, LOW);
statusLabel = off;
buttonLabel = on;
}
BufferFiller bfill = ether.tcpOffset();
bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n"
"Content-Type: text/html\r\nPragma: no-cache\r\n\r\n"
"<html><head><title>WebLed</title></head>"
"<body>LED Status: $S "
"<a href=\"/?status=$S\"><input type=\"button\" value=\"$S\"></a>"
"</body></html>"
), statusLabel, buttonLabel, buttonLabel);
ether.httpServerReply(bfill.position());
}
}
I slightly understand the code. but I need to clear out some things so that I can full understand it. Here are my questions.
what does this mean?
strstr((char *)Ethernet::buffer + pos, "GET /?status=ON")
I tried printing
(char *)Ethernet::buffer + pos
and the serial monitor prints this
GET /?status=ON HTTP/1.1
Host: 192.168.1.2
Connection: keep-alive
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,/;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.56 Safari/537.17
Referer: http://192.168.1.2/?status=OFF
Accept-Encoding: gzip,deflate,sdch
Accept-Language: en-US,en;q=0.8
Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.3
So what is this for? Why is there a char* on it? What is strstr for? and why does it test if its !=0?
Also if Im not mistaken
bfill.emit_p(PSTR
prints the content of the webpage....what is PSTR for?
After I understand this completely, Instead of putting the html inside the bfill.emit_p, I want to put my html on an SD card and load it on it. Is this possible? If yes, how?
Also instead of putting hyperlinks on buttons I want to put it on images that are on my SD card. how is this possible?
THANKS and more power to the community!