通过连接到arduino和ethernet shield的继电器控制灯光和风扇,但html页面停止加载
我将我的灯和风扇连接到一个八通道中继板,该中继板连接到我的arduino和以太网屏蔽 html页面加载良好,所有的中继工作正常,但几个小时后html页面停止加载,直到我按下重置 这是代码,非常感谢您的帮助 检查附件中的源代码通过连接到arduino和ethernet shield的继电器控制灯光和风扇,但html页面停止加载,arduino,arduino-uno,Arduino,Arduino Uno,我将我的灯和风扇连接到一个八通道中继板,该中继板连接到我的arduino和以太网屏蔽 html页面加载良好,所有的中继工作正常,但几个小时后html页面停止加载,直到我按下重置 这是代码,非常感谢您的帮助 检查附件中的源代码 /*Induino R3 User Guide - Program 18.2 - A Simple Webserver that Controls using Links Modified original Webserver Example A simple web s
/*Induino R3 User Guide - Program 18.2 - A Simple Webserver that Controls using Links
Modified original Webserver Example
A simple web server that shows the value of the analog input pins.
using an Arduino Wiznet Ethernet shield.
Circuit:
* Ethernet shield attached to pins 10, 11, 12, 13
* Analog inputs attached to pins A0 through A5 (optional)
created 18 Dec 2009
by David A. Mellis
modified 9 Apr 2012
by Tom Igoe
*/
#include <SPI.h>
#include <Ethernet.h>
// Enter a MAC address and IP address for your controller below.
// The IP address will be dependent on your local network:
byte mac[] = {
0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,177);
// Initialize the Ethernet server library
// with the IP address and port you want to use
// (port 80 is default for HTTP):
EthernetServer server(80);
String req; // a variable to store the GET request
boolean state = 0; // a variable to store the state of the LED
boolean state1 = 0;
boolean state2 = 0;
boolean state3 = 0;
boolean state4 = 0; // a variable to store the state of the LED
boolean state5 = 0;
boolean state6 = 0;
boolean state7 = 0;
String state_val[]={"OFF","ON"}; // A Variable to store the State of the LED as String
String state_val1[]={"1OFF","1ON"};
String state_val2[]={"2OFF","2ON"};
String state_val3[]={"3OFF","3ON"};
String state_val4[]={"4OFF","4ON"}; // A Variable to store the State of the LED as String
String state_val6[]={"6OFF","6ON"};
String state_val7[]={"7OFF","7ON"};
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(9600);
while (!Serial) {
; // wait for serial port to connect. Needed for Leonardo only
}
// start the Ethernet connection and the server:
Ethernet.begin(mac, ip);
server.begin();
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
pinMode(5,OUTPUT); // RGB LED RED is on Pin 6
pinMode(6,OUTPUT);
pinMode(7,OUTPUT);
pinMode(8,OUTPUT);
pinMode(2,OUTPUT); // RGB LED RED is on Pin 6
pinMode(3,OUTPUT);
pinMode(9,OUTPUT);
pinMode(14,OUTPUT);
}
void loop() {
// listen for incoming clients
EthernetClient client = server.available();
if (client) { // Check if there is a client available
Serial.println("new client");
while (client.connected()) { // check if the client is connected
if (client.available()) { // Check if there is a request from the client
Serial.print("$$$$");
req=""; // reset the request variable
while(client.available()) // Read data from the client 1 byte at a time as long as there is data
{
char c=client.read(); // Read 1 byte of data from the client
if(c==13) // Check if the data read is a Carriage Return a.ka. Enter, This means end of first line - the line starting with GET and ending with HTTP/1.1
break; // Since we have read the first line, we need not read the remaining data for storage, so exit the while loop
req += c; // append the read data to the existing value
delayMicroseconds(1000);
}
// if the while loop above exited from the break statement, then there is data in the client buffer, which needs to be read to be removed
// we need to read this data to empty the buffer
while(client.available()) // While loop to read the data to empty buffer
{
client.read();
delayMicroseconds(1000);
}
Serial.println(req); // print the value of the request to the Serial Monitor for debugging
Serial.println("XXX");
}
if(find_string(req,"GET /?ON")) // Check if the received Request containts the String ON
{
state = 1; // Set the State Variable
digitalWrite(5,state); // Set the state to the RED LED
}
if(find_string(req,"GET /?OFF")) // Check if the received Request containts the String ON
{
state = 0; // Set the State Variable
digitalWrite(5,state);
}
if(find_string(req,"GET /?1ON")) // Check if the received Request containts the String ON
{
state1 = 1; // Set the State Variable
digitalWrite(6,state1); // Set the state to the RED LED
}
if(find_string(req,"GET /?1OFF")) // Check if the received Request containts the String ON
{
state1 = 0; // Set the State Variable
digitalWrite(6,state1);
}
if(find_string(req,"GET /?2ON")) // Check if the received Request containts the String ON
{
state2 = 1; // Set the State Variable
digitalWrite(7,state2); // Set the state to the RED LED
}
if(find_string(req,"GET /?2OFF")) // Check if the received Request containts the String ON
{
state2 = 0; // Set the State Variable
digitalWrite(7,state2);
}
if(find_string(req,"GET /?3ON")) // Check if the received Request containts the String ON
{
state3 = 1; // Set the State Variable
digitalWrite(8,state3); // Set the state to the RED LED
}
if(find_string(req,"GET /?3OFF")) // Check if the received Request containts the String ON
{
state3 = 0; // Set the State Variable
digitalWrite(8,state3);
}
if(find_string(req,"GET /?4ON")) // Check if the received Request containts the String ON
{
state4 = 1; // Set the State Variable
digitalWrite(2,state4); // Set the state to the RED LED
}
if(find_string(req,"GET /?4OFF")) // Check if the received Request containts the String ON
{
state4 = 0; // Set the State Variable
digitalWrite(2,state4);
}
if(find_string(req,"GET /?5ON")) // Check if the received Request containts the String ON
{
state5 = 1; // Set the State Variable
digitalWrite(3,state5); // Set the state to the RED LED
}
if(find_string(req,"GET /?5OFF")) // Check if the received Request containts the String ON
{
state5 = 0; // Set the State Variable
digitalWrite(3,state5);
}
if(find_string(req,"GET /?6ON")) // Check if the received Request containts the String ON
{
state6 = 1; // Set the State Variable
digitalWrite(9,state6); // Set the state to the RED LED
}
if(find_string(req,"GET /?6OFF")) // Check if the received Request containts the String ON
{
state6 = 0; // Set the State Variable
digitalWrite(9,state6);
}
if(find_string(req,"GET /?7ON")) // Check if the received Request containts the String ON
{
state7 = 1; // Set the State Variable
digitalWrite(14,state7); // Set the state to the RED LED
}
if(find_string(req,"GET /?7OFF")) // Check if the received Request containts the String ON
{
state7 = 0; // Set the State Variable
digitalWrite(14,state7);
}
Serial.println("here 1"); // for debugging
client.println("HTTP/1.1 200 OK"); // start responding with the DATA to the client & Send out the response header
client.println("Content-Type: text/html");
client.println("Connection: close"); // the connection will be closed after completion of the responsedelay(1);
client.println();
client.println("<!DOCTYPE HTML>"); // Start of the HTML Page
client.println("<html>");
client.println("<br />");
delayMicroseconds(500);
if(state) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val[1]+" \">");
}
client.println("Click Here to Switch "+state_val[!state]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state1) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val1[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val1[1]+" \">");
}
client.println("Click Here to Switch "+state_val1[!state1]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state2) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val2[0]+" \">");
}
else
}
client.println("<a href= \"/?"+state_val2[1]+" \">");
}
client.println("Click Here to Switch "+state_val2[!state2]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state3) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val3[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val3[1]+" \">");
}
client.println("Click Here to Switch "+state_val3[!state3]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state4) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val4[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val4[1]+" \">");
}
client.println("Click Here to Switch "+state_val4[!state4]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state5) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val5[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val5[1]+" \">");
}
client.println("Click Here to Switch "+state_val5[!state5]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state6) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val6[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val6[1]+" \">");
}
client.println("Click Here to Switch "+state_val6[!state6]+" -RED of RGB LED");
client.println("</a>");
client.println("<br />");
delayMicroseconds(500);
if(state7) // Check the State and print the LINK on the Page accordingly
{
client.println("<a href= \"/?"+state_val7[0]+" \">");
}
else
{
client.println("<a href= \"/?"+state_val7[1]+" \">");
}
client.println("Click Here to Switch "+state_val7[!state7]+" -RED of RGB LED");
client.println("</a>");
client.println("</html>");
break;
}
client.stop();
Serial.println("client disonnected");
}
}
// A Function to locate a given search string in a given base string
boolean find_string(String base, String search)
{
int len = search.length(); // find the length of the base string
for(int m = 0; m<((base.length()-len)+1);m++)// Iterate from the beginning of the base string till the end minus length of the substring
{
if(base.substring(m,(m+len))==search) // Check if the extracted Substring Matches the Search String
{
return true; // if it matches exit the function with a true value
}
}
return false; // if the above loop did not find any matches, control would come here and return a false value
}
/*Induino R3用户指南-程序18.2-一个使用链接进行控制的简单Web服务器
修改的原始Web服务器示例
一个简单的web服务器,显示模拟输入引脚的值。
使用Arduino Wiznet以太网屏蔽。
线路:
*连接到引脚10、11、12、13的以太网屏蔽
*连接至引脚A0至A5的模拟输入(可选)
创建于2009年12月18日
大卫·A·梅利斯
2012年4月9日修改
汤姆·伊戈
*/
#包括
#包括
//在下面输入控制器的MAC地址和IP地址。
//IP地址将取决于您的本地网络:
字节mac[]={
0xDE,0xAD,0xBE,0xEF,0xFE,0xED};
ip地址ip(192168,1177);
//初始化以太网服务器库
//具有要使用的IP地址和端口
//(HTTP的默认端口为80):
以太网络服务器(80);
字符串请求;//用于存储GET请求的变量
布尔状态=0;//用于存储LED状态的变量
布尔状态1=0;
布尔状态2=0;
布尔状态3=0;
布尔状态4=0;//用于存储LED状态的变量
布尔状态5=0;
布尔状态6=0;
布尔状态7=0;
字符串状态_val[]={“关闭”,“打开”};//将LED状态存储为字符串的变量
字符串状态_val1[]={“1OFF”,“1ON”};
字符串状态_val2[]={“2OFF”,“2ON”};
字符串状态_val3[]={“3OFF”,“3ON”};
字符串状态_val4[]={“4OFF”,“4ON”};//将LED状态存储为字符串的变量
字符串状态_val6[]={“6OFF”,“6ON”};
字符串状态_val7[]={“7OFF”,“7ON”};
无效设置(){
//打开串行通信并等待端口打开:
Serial.begin(9600);
而(!串行){
;//等待串行端口连接。仅Leonardo需要
}
//启动以太网连接和服务器:
以太网开始(mac,ip);
server.begin();
串行打印(“服务器位于”);
Serial.println(Ethernet.localIP());
引脚模式(5,输出);//RGB LED红色在引脚6上
pinMode(6,输出);
pinMode(7,输出);
pinMode(8,输出);
引脚模式(2,输出);//RGB LED红色在引脚6上
pinMode(3,输出);
pinMode(9,输出);
pinMode(14,输出);
}
void循环(){
//侦听传入的客户端
EthernetClient=server.available();
if(client){//检查是否有可用的客户端
Serial.println(“新客户”);
while(client.connected()){//检查客户端是否已连接
if(client.available()){//检查是否有来自客户端的请求
串行打印(“$$”);
req=”“;//重置请求变量
while(client.available())//只要有数据,就从客户端一次读取1个字节的数据
{
char c=client.read();//从客户端读取1字节的数据
if(c==13)//检查读取的数据是否是回车a.ka.Enter,这意味着第一行的结尾-以GET开始,以HTTP/1.1结束的行
break;//因为我们已经读取了第一行,所以不需要读取存储的剩余数据,所以退出while循环
req+=c;//将读取的数据附加到现有值
延迟微秒(1000);
}
//如果上面的while循环从break语句退出,那么客户机缓冲区中有数据,需要读取数据才能删除
//我们需要读取这些数据来清空缓冲区
while(client.available())//while循环将数据读取到空缓冲区
{
client.read();
延迟微秒(1000);
}
Serial.println(req);//将请求的值打印到串行监视器以进行调试
序列号。打印号(“XXX”);
}
if(find_string(req,“GET/?ON”)//检查收到的请求是否包含
{
state=1;//设置state变量
digitalWrite(5,状态);//将状态设置为红色LED
}
if(find_string(req,“GET/?OFF”)//检查收到的请求是否包含打开的字符串
{
state=0;//设置state变量
数字写入(5,状态);
}
if(find_string(req,“GET/?1ON”)//检查收到的请求是否包含
{
state1=1;//设置状态变量
digitalWrite(6,状态1);//将状态设置为红色LED
}
if(find_string(req,“GET/?1OFF”)//检查收到的请求是否包含打开的字符串
{
state1=0;//设置状态变量
数字写入(6,状态1);
}
if(find_string(req,“GET/?2ON”)//检查收到的请求是否包含
{
state2=1;//设置状态变量
digitalWrite(7,状态2);//将状态设置为红色LED
}
if(find_string(req,“GET/?2OFF”)//检查收到的请求是否包含打开的字符串
{
state2=0;//设置状态变量
数字写入(7,状态2);
}
if(find_string(req,“GET/?3ON”)//检查收到的请求是否包含
{
state3=1;//设置状态变量
digitalWrite(8,状态3);//将状态设置为红色LED
}
if(find_string(req,“GET/?3OFF”)//检查收到的请求是否包含打开的字符串
{
state3=0;//设置状态变量
数字写入(8,state3)