คือผมลองทำแล้ว หลอดที่1 กดแล้วไม่ทำงานตามที่กำหนดครับ หลอดอื่น ทำตามปกติ
// This is a demo of the RBBB running as webserver with the Ether Card
// 2010-05-28 <jc@wippler.nl> http://opensource.org/licenses/mit-license.php
#include <EtherCard.h>
// ethernet interface mac address, must be unique on the LAN
static byte mymac[] = { 0x74,0x69,0x69,0x2D,0x30,0x31 };
static byte myip[] = { 192,168,1,106 };
byte Ethernet::buffer[500];
BufferFiller bfill;
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 () {
if (ether.begin(sizeof Ethernet::buffer, mymac) == 0)
Serial.println(F("Failed to access Ethernet controller"));
ether.staticSetup(myip);
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
}
}
กรณีนี้แสดงว่าทำได้แล้ว โคดผมอ่านแล้วไม่น่าผิด ลองเช็คขาการต่อ หรือเปลี่ยนขา led ดูครับ
ผมลองสลับขาไปมา ทำยังไง มันก็ เหมือนว่าจะทำงาน แบบเดิม
ผมลยลอง เปลี่ยนค่า for(int i = 0; i <= 4; i++)
กับ
for (int i = 0; i <= 3; i++)digitalWrite(LedPins[i],!PinStatus[i+1]);
เปลี่ยนตรง <= ไปมาหลายค่า แต่ที่ได้คือ เหมือว่าRelay จะทำงานแค่ 3 ตัว พอ IN1-3 ทำงานได้ IN4 ก็จะไม่ทำงาน บางครั้ง ทำงาน ได้ 4ตัว แต่ พอกดเปิด/ปิด IN จะเปิด/ปิดได้3ตัว in กดเปิด/ปิด ก็ไม่ทำงานตามที่กด
ผมเลยลอง
เพิ่ม int LedPins[] = {
2,3,4,5,6};
เปลี่ยน for (int i = 0; i <= 4; i++)digitalWrite(LedPins[i],!PinStatus[i]);
และลองเหมื่อนว่าจะได้ แล้ว แต่คือ ผมไม่เข้าใจตรง for (int i = 0; i <= 4; i++)d ครับ และทำไมผมต้องเพิ่ม เอาต์พุตด้วยทำไม
โคดผมดูแล้วไม่น่าผิดครับ
ถ้าดูจากข้อมูลแล้ว แสดงว่าค่าเริ่มจาก 1-4 ครับ
ลองเปลี่ยน for (int i = 1; i <= 4; i++)digitalWrite(LedPins[i],!PinStatus[i]); หรือลองเช็ค/เลื่อนขา led ครับ