Před par lety jsem skladal arduino uno a Ethernet Shield W5100.
Našel jsem ještě nějake poznamky a přiklady a u tohoto jsem měl napsane odzkoušene a funkční.
Kód: Vybrat vše
#include <SPI.h>
#include <Ethernet.h>
boolean zacatekCteni = false;
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
IPAddress ip(192, 168, 1, 15);
EthernetServer mujSvr = EthernetServer(80);
String header;
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
mujSvr.begin();
Serial.println(Ethernet.localIP());
}
void loop() {
String poslane = "";
EthernetClient client = mujSvr.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
header += c;
if(zacatekCteni && c == ' '){zacatekCteni = false;} //ukončí čtení
if(c == '?'){zacatekCteni = true;} //začatek čtení
if(zacatekCteni){poslane = poslane + char(c);}
if (c == '\n' && currentLineIsBlank) {
if(header.indexOf("YWRtaW46YWRtaW4=") >= 0) {
// při úspěšném přihlášení
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("hello world!");
Serial.print(poslane);
}
else {
// špatně zadane user/pass
client.println("HTTP/1.0 401 Authorization Required");
client.println("HTTP/1.1 401 Unauthorized");
client.println("WWW-Authenticate: Basic realm=\"Secure\"");
client.println("Content-Type: text/html");
client.println();
client.println("<html>Text to send if user hits Cancel button</html>"); // really need this for the popup!
}
header = "";
break;
}
if (c == '\n') {currentLineIsBlank = true;}
else
if (c != '\r') {currentLineIsBlank = false;}
}
}
// give the web browser time to receive the data
delay(1);
client.stop();
}
}
Zkouším totež s arduinem nano a Ethernet Shield ENC28J60.
Uplně to same, jenom misto knihovny Ethernet.h jsem použil UIPEthernet.h
Kód: Vybrat vše
#include <UIPEthernet.h>
#include <SPI.h>
boolean zacatekCteni = false;
String header;
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x78, 0xEE };
IPAddress ip(192, 168, 1, 115);
EthernetServer server(80);
void setup() {
Serial.begin(9600);
Ethernet.begin(mac, ip);
server.begin();
Serial.print("IP Address: ");
Serial.println(Ethernet.localIP());
}
void loop() {
String poslane = "";
EthernetClient client = server.available();
if (client) {
boolean currentLineIsBlank = true;
while (client.connected()) {
if (client.available()) {
char c = client.read();
header += c;
if(zacatekCteni && c == ' '){zacatekCteni = false;} //ukončí čtení
if(c == '?'){zacatekCteni = true;} //začatek čtení
if(zacatekCteni){poslane = poslane + char(c);}
if (c == '\n' && currentLineIsBlank) {
if(header.indexOf("YWRtaW46YWRtaW4=") >= 0) {
// při úspěšném přihlášení
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: close");
client.println();
client.println("hello world!");
Serial.print(poslane);
}
else {
// špatně zadane user/pass
Serial.print("nespravne heslo");
client.println("HTTP/1.0 401 Authorization Required");
client.println("HTTP/1.1 401 Unauthorized");
client.println("WWW-Authenticate: Basic realm=\"Secure\"");
client.println("Content-Type: text/html");
client.println();
client.println("<html>Text to send if user hits Cancel button</html>"); // really need this for the popup!
}
header = "";
break;
}
if (c == '\n') {currentLineIsBlank = true;}
else
if (c != '\r') {currentLineIsBlank = false;}
}
}
delay(1);
client.stop();
}
}
Heslo to nesežere.
Proč? Mam tam někde kopnec?
Jmeno a heslo by se mělo zašifrovat do base64 ve tvaru admin:admin
https://www.base64encode.org/
YWRtaW46YWRtaW4=
Děkují.
