arduino
2015年4月1日 星期三
2015年3月10日 星期二
enc28j60 的 ip 問題
剛開始使用enc28j60起server,
扒文找到的都是說設定一個與你自己內聯網沒衝突的ip,還要在結尾加句 goodluck!
左試右試,時得時吾得,搞左兩日才發現 ip 跟 mac address 有關,除了要設定跟你 路由器 相符合的 ip (192.168.0.xxx or 192.168.1.xxx or ...)外,mac address都會有影響,即系 ip 不一定是你設定的那一個,會隨mac arrdess 而改變,所以.......搞左我兩日,所以最穩陣的做法是,在setup() 加入顯示 ip
ether.printIp("My Router IP: ", ether.myip);
咁就吾使靠估喇
/*
-----------------------
Ïîäêëþ÷àºìî ï³íè "ENC28J60 Module" äî Arduino Uno.
VCC - 3.3V
GND - GND
SCK - Pin 13
SO - Pin 12
SI - Pin 11
CS - Pin 10 Ìîæíà âèáðàòè áóäü-ÿêèé.
------------------
*/
#include <EtherCard.h> // Ïîäêëþ÷àºì ñêà÷àíó á³áë³îòåêó. https://yadi.sk/d/R57sVoglbhTRN
// MAC Address ìຠáóòè óí³êàëüíèì ó âàø³é ìåðåæ³. Ìîæíà çì³íèòè.
//static byte mymac[] = { 0x5A,0x5A,0x5A,0x5A,0x5A,0x5A };
static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
// ip ñòàòè÷íèé
static byte myip[] = { 192,168,0,102 };
// Áóôåð, ÷èì á³ëüøå äàíèõ íà Web ñòîð³íö³, òèì á³ëüøå ïîòð³áíå çíà÷åííÿ áóôåðà.
byte Ethernet::buffer[900];
BufferFiller bfill;
// Ìàñèâ çàä³ÿíèõ íîìåð³â ï³í³â Arduino, äëÿ êåðóâàííÿ 4 ðåëå.
int LedPins[] = {46,47,48,49};
// Ìàñèâ äëÿ ô³êñàö³¿ çì³í.
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><font size=10px color=\"blue\"><center>"
"Relay 1: <a href=\"?ArduinoPIN1=$F\">$F</a><font size=10px color=\"blue\"><center><br />"
"Relay 2: <a href=\"?ArduinoPIN2=$F\">$F</a><font size=10px color=\"blue\"><center><br />"
"Relay 3: <a href=\"?ArduinoPIN3=$F\">$F</a><font size=10px color=\"blue\"><center><br />"
"Relay 4: <a href=\"?ArduinoPIN4=$F\">$F</a><font size=10px color=\"blue\"><center>"),
http_OK,
PinStatus[1]?PSTR("off"):PSTR("on"),
PinStatus[1]?PSTR("<font size=10px color=\"green\"><b>ON</b></font>"):PSTR("<font size=10px color=\"red\">OFF</font>"),
PinStatus[2]?PSTR("off"):PSTR("on"),
PinStatus[2]?PSTR("<font size=10px color=\"green\"><b>ON</b></font>"):PSTR("<font size=10px color=\"red\">OFF</font>"),
PinStatus[3]?PSTR("off"):PSTR("on"),
PinStatus[3]?PSTR("<font size=10px color=\"green\"><b>ON</b></font>"):PSTR("<font size=10px color=\"red\">OFF</font>"),
PinStatus[4]?PSTR("off"):PSTR("on"),
PinStatus[4]?PSTR("<font size=10px color=\"green\"><b>ON</b></font>"):PSTR("<font size=10px 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,53) == 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
}
}
手機效果
扒文找到的都是說設定一個與你自己內聯網沒衝突的ip,還要在結尾加句 goodluck!
左試右試,時得時吾得,搞左兩日才發現 ip 跟 mac address 有關,除了要設定跟你 路由器 相符合的 ip (192.168.0.xxx or 192.168.1.xxx or ...)外,mac address都會有影響,即系 ip 不一定是你設定的那一個,會隨mac arrdess 而改變,所以.......搞左我兩日,所以最穩陣的做法是,在setup() 加入顯示 ip
ether.printIp("My Router IP: ", ether.myip);
咁就吾使靠估喇
暫時找到最好用的http://smart-home.te.ua/aurduino-ethernet-modul-enc28j60-yly-web-upravlenyya-rele-arduino-webserver/?lang=ru我都吾知注釋系乜文,好用岩用就得,
/*
-----------------------
Ïîäêëþ÷àºìî ï³íè "ENC28J60 Module" äî Arduino Uno.
VCC - 3.3V
GND - GND
SCK - Pin 13
SO - Pin 12
SI - Pin 11
CS - Pin 10 Ìîæíà âèáðàòè áóäü-ÿêèé.
------------------
*/
#include <EtherCard.h> // Ïîäêëþ÷àºì ñêà÷àíó á³áë³îòåêó. https://yadi.sk/d/R57sVoglbhTRN
// MAC Address ìຠáóòè óí³êàëüíèì ó âàø³é ìåðåæ³. Ìîæíà çì³íèòè.
//static byte mymac[] = { 0x5A,0x5A,0x5A,0x5A,0x5A,0x5A };
static byte mymac[] = {0xDD,0xDD,0xDD,0x00,0x00,0x01};
// ip ñòàòè÷íèé
static byte myip[] = { 192,168,0,102 };
// Áóôåð, ÷èì á³ëüøå äàíèõ íà Web ñòîð³íö³, òèì á³ëüøå ïîòð³áíå çíà÷åííÿ áóôåðà.
byte Ethernet::buffer[900];
BufferFiller bfill;
// Ìàñèâ çàä³ÿíèõ íîìåð³â ï³í³â Arduino, äëÿ êåðóâàííÿ 4 ðåëå.
int LedPins[] = {46,47,48,49};
// Ìàñèâ äëÿ ô³êñàö³¿ çì³í.
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><font size=10px color=\"blue\"><center>"
"Relay 1: <a href=\"?ArduinoPIN1=$F\">$F</a><font size=10px color=\"blue\"><center><br />"
"Relay 2: <a href=\"?ArduinoPIN2=$F\">$F</a><font size=10px color=\"blue\"><center><br />"
"Relay 3: <a href=\"?ArduinoPIN3=$F\">$F</a><font size=10px color=\"blue\"><center><br />"
"Relay 4: <a href=\"?ArduinoPIN4=$F\">$F</a><font size=10px color=\"blue\"><center>"),
http_OK,
PinStatus[1]?PSTR("off"):PSTR("on"),
PinStatus[1]?PSTR("<font size=10px color=\"green\"><b>ON</b></font>"):PSTR("<font size=10px color=\"red\">OFF</font>"),
PinStatus[2]?PSTR("off"):PSTR("on"),
PinStatus[2]?PSTR("<font size=10px color=\"green\"><b>ON</b></font>"):PSTR("<font size=10px color=\"red\">OFF</font>"),
PinStatus[3]?PSTR("off"):PSTR("on"),
PinStatus[3]?PSTR("<font size=10px color=\"green\"><b>ON</b></font>"):PSTR("<font size=10px color=\"red\">OFF</font>"),
PinStatus[4]?PSTR("off"):PSTR("on"),
PinStatus[4]?PSTR("<font size=10px color=\"green\"><b>ON</b></font>"):PSTR("<font size=10px 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,53) == 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
}
}
手機效果
桌面電腦效果
訂閱:
文章 (Atom)