Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <EtherCard.h> // Подключаєм скачану бібліотеку. https://yadi.sk/d/R57sVoglbhTRN
- //settaggio dei valori statici
- static byte mymac[] = {0x00, 0x19, 0xCB, 0xF4, 0x03, 0x01};
- static byte myip[] = {192, 168, 0, 26};
- static byte gwip[] = {192, 168, 0, 1};
- static byte netmask[] = {255, 255, 255, 0};
- static byte dnsip[] = {8, 8, 8, 8};
- // Буфер, чим більше даних на Web сторінці, тим більше потрібне значення буфера.
- byte Ethernet::buffer[900];
- BufferFiller bfill;
- // Масив задіяних номерів пінів Arduino, для керування 4 реле.
- int LedPins[] = {
- 2, 3, 4, 5
- };
- // Масив для фіксації змін.
- boolean PinStatus[] = {
- 1, 2, 3, 4
- };
- //-------------
- const char http_OK[] PROGMEM =
- "HTTP/1.0 200 OK\r\n"
- "Content-Type: text/html\r\n"
- "Pragma: no-cache\r\n\r\n";
- const char http_Found[] PROGMEM =
- "HTTP/1.0 302 Found\r\n"
- "Location: /\r\n\r\n";
- const char http_Unauthorized[] PROGMEM =
- "HTTP/1.0 401 Unauthorized\r\n"
- "Content-Type: text/html\r\n\r\n"
- "<h1>401 Unauthorized</h1>";
- //------------
- // Робимо функцію для оформлення нашої Web сторінки.
- void homePage()
- {
- bfill.emit_p(PSTR("$F"
- "<title>ArduinoPIN Webserver</title>"
- "Relay 1: <a href=\"?ArduinoPIN1=$F\">$F</a><br />"
- "Relay 2: <a href=\"?ArduinoPIN2=$F\">$F</a><br />"
- "Relay 3: <a href=\"?ArduinoPIN3=$F\">$F</a><br />"
- "Relay 4: <a href=\"?ArduinoPIN4=$F\">$F</a>"),
- http_OK,
- PinStatus[1] ? PSTR("off") : PSTR("on"),
- PinStatus[1] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"),
- PinStatus[2] ? PSTR("off") : PSTR("on"),
- PinStatus[2] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"),
- PinStatus[3] ? PSTR("off") : PSTR("on"),
- PinStatus[3] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"),
- PinStatus[4] ? PSTR("off") : PSTR("on"),
- PinStatus[4] ? PSTR("<font color=\"green\"><b>ON</b></font>") : PSTR("<font color=\"red\">OFF</font>"));
- }
- //------------------------
- void setup()
- {
- Serial.begin(9600);
- // По замовчуванню в бібліотеці "ethercard" (CS-pin) = № 8.
- // if (ether.begin(sizeof Ethernet::buffer, mymac) == 0).
- // and change it to: Змінюємо (CS-pin) на 10.
- // if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0).
- if (ether.begin(sizeof Ethernet::buffer, mymac, 10) == 0);
- if (!ether.dhcpSetup());
- // Виводимо в Serial монітор IP адресу, яку нам автоматично присвоїв наш Router.
- // Динамічна IP адреса незручна, бо періодично змінюється.
- // Нам доведеться часто дізнаватися, яка адреса нашої Web сторінки.
- ether.printIp("My Router IP: ", ether.myip); // Виводимо в Serial монітор IP адресу, яку нам присвоїв Router.
- //Також ми можемо встановити статичну IP адресу нашої сторінки і звертатися до Arduino по ній
- //ether.staticSetup(myip);
- ether.printIp("My SET IP: ", ether.myip); // Виводимо в Serial монітор статичну IP адресу.
- //-----
- for (int i = 0; i <= 4; i++)
- {
- pinMode(LedPins[i], OUTPUT);
- digitalWrite (LedPins[i], HIGH);
- PinStatus[i] = false;
- }
- }
- // --------------------------------------
- void loop()
- {
- delay(1); // Смикаєм мікроконтролер.
- word len = ether.packetReceive(); // check for ethernet packet / перевірити ethernet пакети.
- word pos = ether.packetLoop(len); // check for tcp packet / перевірити TCP пакети.
- if (pos) {
- bfill = ether.tcpOffset();
- char *data = (char *) Ethernet::buffer + pos;
- if (strncmp("GET /", data, 5) != 0) {
- bfill.emit_p(http_Unauthorized);
- }
- else {
- data += 5;
- if (data[0] == ' ') {
- homePage(); // Return home page Якщо виявлено зміни на сторінці, запускаємо функцію.
- for (int i = 0; i <= 3; i++)digitalWrite(LedPins[i], !PinStatus[i + 1]);
- }
- // "16" = кількість символів "?ArduinoPIN1=on ".
- else if (strncmp("?ArduinoPIN1=on ", data, 16) == 0) {
- PinStatus[1] = true;
- bfill.emit_p(http_Found);
- }
- else if (strncmp("?ArduinoPIN2=on ", data, 16) == 0) {
- PinStatus[2] = true;
- bfill.emit_p(http_Found);
- }
- else if (strncmp("?ArduinoPIN3=on ", data, 16) == 0) {
- PinStatus[3] = true;
- bfill.emit_p(http_Found);
- }
- else if (strncmp("?ArduinoPIN4=on ", data, 16) == 0) {
- PinStatus[4] = true;
- bfill.emit_p(http_Found);
- }
- //------------------------------------------------------
- else if (strncmp("?ArduinoPIN1=off ", data, 17) == 0) {
- PinStatus[1] = false;
- bfill.emit_p(http_Found);
- }
- else if (strncmp("?ArduinoPIN2=off ", data, 17) == 0) {
- PinStatus[2] = false;
- bfill.emit_p(http_Found);
- }
- else if (strncmp("?ArduinoPIN3=off ", data, 17) == 0) {
- PinStatus[3] = false;
- bfill.emit_p(http_Found);
- }
- else if (strncmp("?ArduinoPIN4=off ", data, 17) == 0) {
- PinStatus[4] = false;
- bfill.emit_p(http_Found);
- }
- //---------------------------
- else {
- // Page not found
- bfill.emit_p(http_Unauthorized);
- }
- }
- ether.httpServerReply(bfill.position()); // send http response
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement