Arduino Pro Mini Зависает Web сервер
- Войдите на сайт для отправки комментариев
Чт, 27/04/2017 - 22:30
#include <EtherCard.h>
// MAC Address должен быть уникальным в вашей сети. Можно менять.
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
// ip статический / постоянный Address нашей Web страницы.
//static byte myip[] = {
//192,168,1,177 };
// Буфер, чем больше данных на Web странице, тем больше понадобится значения буфера.
byte Ethernet::buffer[1200];
BufferFiller bfill;
// Массив задействованных номеров Pins Arduino, для управления например 8 реле.
int LedPins[] = {
2,3,4,5,6,7,8,9};
// Массив для фиксации изменений.
boolean PinStatus[] = {
1,2,3,4,5,6,7,8};
//-------------
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"
"<!DOCTYPE html>"
"<html>"
"<head>"
"<title>ArduinoPIN Webserver</title>"
"<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css\">"
"<link rel=\"stylesheet\" href=\"https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap-theme.min.css\">"
"</head>"
"<div class=\"row\">"
"<div class=\"col-xs-12 col-sm-6\">"
"<h1 style=\"font-size: 64px;\">Rozette 1: <a class=\"btn btn-default btn-lg\" href=\"?ArduinoPIN1=$F\">$F</a><br /><br />"
"Rozette 2: <a class=\"btn btn-default btn-lg\" href=\"?ArduinoPIN2=$F\">$F</a><br /><br />"
"Rozette 3: <a class=\"btn btn-default btn-lg\" href=\"?ArduinoPIN3=$F\">$F</a><br /><br />"
"Rozette 4: <a class=\"btn btn-default btn-lg\" href=\"?ArduinoPIN4=$F\">$F</a><br /><h1><br />"
"</div></div>"
"</html>"),
http_OK,
PinStatus[1]?PSTR("off"):PSTR("on"),
PinStatus[1]?PSTR("<font color=\"green\" style=\"font-size: 64px;\"><b>VIKLUCHENO</b></font>"):PSTR("<font color=\"red\" style=\"font-size: 64px;\">VKLUCHENO</font>"),
PinStatus[2]?PSTR("off"):PSTR("on"),
PinStatus[2]?PSTR("<font color=\"green\" style=\"font-size: 64px;\"><b>VIKLUCHENO</b></font>"):PSTR("<font color=\"red\" style=\"font-size: 64px;\">VKLUCHENO</font>"),
PinStatus[3]?PSTR("off"):PSTR("on"),
PinStatus[3]?PSTR("<font color=\"green\" style=\"font-size: 64px;\"><b>VIKLUCHENO</b></font>"):PSTR("<font color=\"red\" style=\"font-size: 64px;\">VKLUCHENO</font>"),
PinStatus[4]?PSTR("off"):PSTR("on"),
PinStatus[4]?PSTR("<font color=\"green\" style=\"font-size: 64px;\"><b>VIKLUCHENO</b></font>"):PSTR("<font color=\"red\" style=\"font-size: 64px;\">VKLUCHENO</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);
Serial.println(F("connect difused"));
if(!ether.dhcpSetup())
Serial.println(F("Error DHCP"));
ether.printIp("My IP: ", ether.myip);
ether.printIp("Netmask: ", ether.netmask);
ether.printIp("GW IP: ", ether.gwip);
ether.printIp("DNS IP: ", ether.dnsip);
for(int i = 0; i <= 8; i++)
{
pinMode(LedPins[i],OUTPUT);
PinStatus[i]=true;
digitalWrite(LedPins[i], true);
}
}
// --------------------------------------
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 <= 7; 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("?ArduinoPIN5=on ", data, 16) == 0) {
PinStatus[5] = true;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN6=on ", data, 16) == 0) {
PinStatus[6] = true;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN7=on ", data, 16) == 0) {
PinStatus[7] = true;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN8=on ", data, 16) == 0) {
PinStatus[8] = 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 if (strncmp("?ArduinoPIN5=off ", data, 17) == 0) {
PinStatus[5] = false;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN6=off ", data, 17) == 0) {
PinStatus[6] = false;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN7=off ", data, 17) == 0) {
PinStatus[7] = false;
bfill.emit_p(http_Found);
}
else if (strncmp("?ArduinoPIN8=off ", data, 17) == 0) {
PinStatus[8] = false;
bfill.emit_p(http_Found);
}
//---------------------------
else {
// Page not found
bfill.emit_p(http_Unauthorized);
}
}
ether.httpServerReply(bfill.position()); // send http response
}
}
Работает примерно около дня. Потом зависает. На роутере так-же отсутствует.
Используется ENC28J60 в качестве Ethernet модуля.