我不知道为什么我的草图无法正常工作。我想构建一个服务器来从我的Arduino(带有AJAX请求)获取模拟输入值。我有带SD读卡器的Ethernet Shield。在我的SD卡上,我有“index.htm”文件,其中包含我的网页内容(它包含带有AJAX请求发送功能的HTML和JavaScript)。这是我的代码:
#include <SPI.h>
#include <Ethernet.h>
#include <SD.h>
// MAC address from Ethernet shield sticker under board
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
IPAddress ip(192,168,1,101); // IP address, may need to change depending on network
EthernetServer server(80); // create a server at port 80
File pageFile;
String HttpReq = String();
void setup()
{
Serial.begin(9600);
// initialize SD card
Serial.println("Initializing SD card...");
if (!SD.begin(4)) {
Serial.println("ERROR - SD card initialization failed!");
return; // init failed
}
Serial.println("SUCCESS - SD card initialized.");
// check for index.htm file
if (!SD.exists("index.htm")) {
Serial.println("ERROR - Can't find index.htm file!");
return; // can't find index file
}
Serial.println("SUCCESS - Found index.htm fileeee.");
pageFile = SD.open("index.htm", FILE_READ);
if (pageFile)
{
Serial.println("SETUP: Reading file...");
char c;
while(pageFile.available())
{
c = pageFile.read();
Serial.print(c);
}
Serial.println("SETUP: End of file reading - OK");
} else Serial.println("SETUP: Can't read file");
pageFile.close();
Ethernet.begin(mac, ip); // initialize Ethernet device
server.begin(); // start to listen for clients
Serial.print("server is at ");
Serial.println(Ethernet.localIP());
}
void loop()
{
EthernetClient client = server.available(); // try to get client
if (client)
{ // got client?
Serial.println("New client:");
boolean currentLineIsBlank = true;
while (client.connected())
{
if (client.available())
{ // client data available to read
char c = client.read(); // read 1 byte (character) from client
// last line of client request is blank and ends with \n
// respond to client only after last line received
HttpReq += c;
Serial.print(c);
if (c == '\n' && currentLineIsBlank) //end of client request, now have to send server response
{
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connection: keep-alive");
client.println();
// send web page
Serial.print("Position of ajax_down: ");
Serial.println(HttpReq.indexOf("ajax_down"));
if (HttpReq.indexOf("ajax_down") > -1)
{
Serial.println("!!!!! Here should be ajax_down string:");
Serial.println(HttpReq);
Serial.println("!!!!! END");
GetResults(client);
}
else //it wasn't ajax_down request
{
pageFile = SD.open("index.htm", FILE_READ);
if (pageFile)
{
Serial.println("Reading...");
while(pageFile.available())
{
char c = pageFile.read();
client.print(c);
Serial.print(c);
}
pageFile.close();
}
else
{
Serial.println("Can't read file!");
client.print("Can't get results!");
}
}
HttpReq = "";
break;
}
// every line of text received from the client ends with \r\n
if (c == '\n') {
// last character on line of received text
// starting new line with next character read
currentLineIsBlank = true;
}
else if (c != '\r') {
// a text character was received from client
currentLineIsBlank = false;
}
} // end if (client.available())
} // end while (client.connected())
delay(1); // give the web browser time to receive the data
client.stop(); // close the connection
} // end if (client)
}
void GetResults(EthernetClient cl)
{
Serial.print("Getting results...");
int temperatury = 0;
for (int i=0; i<20; i++)
{
temperatury += analogRead(0);
}
int odczytTemperatury = (temperatury/20)*(5000/1023);
int odczytSwiatla = analogRead(5);
cl.print(odczytTemperatury);
cl.print(" ");
cl.print(odczytSwiatla);
Serial.println("OK");
}
在setup()
函数中,我检查SD卡上是否有“index.htm”文件,然后打开它将其内容打印到Serial。我在COM8上得到以下结果:
Initializing SD card...
SUCCESS - SD card initialized.
SUCCESS - Found index.htm fileeee.
SETUP: Reading file...
<!doctype html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8"/>
<title>Arduino odczyt</title>
</head>
<body>
...
</body>
</html>SETUP: End of file reading - OK
server is at 192.168.1.101
...
是我的代码。我也在setup()
开始服务器。这没关系。
在loop()
我等待客户。如果连接了客户端,我会通过char读取他的请求char:
char c = client.read(); // read 1 byte (character) from client
HttpReq += c;
当请求结束时(if (c == '\n' && currentLineIsBlank)
)服务器开始响应。首先,它检查它是否是“ajax_down”请求:if (HttpReq.indexOf("ajax_down") > -1)
这里我有第一个问题:当HttpReq没有“ajax_down”字符串时,它返回0时应返回-1({ {3}})。
在Web浏览器上转到192.168.1.101时输出COM8:
New client:
GET / HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Position of ajax_down: 0
!!!!! Here should be ajax_down string:
GET / HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Ge
!!!!! END
Getting results...OK
New client:
GET /favicon.ico HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Position of ajax_down: 0
!!!!! Here should be ajax_down string:
GET /favicon.ico HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-La
!!!!! END
Getting results...OK
它表现得好像在HttpReq的开头有“ajax_down”,但没有!然后它调用GetResults(client);
,所以在我的浏览器窗口中我可以看到两个数字(模拟输入值)。
但好吧,我们假设它不是从0开始计数而是从1开始计数,所以当我改变时
if (HttpReq.indexOf("ajax_down") > -1)
的
if (HttpReq.indexOf("ajax_down") > 0)
这里会得到else
:
else //it wasn't ajax_down request
并且正在读取SD卡中的文件(与setup()
中的代码相同!!!)但现在我无法读取文件(但首先阅读{{ 1}}工作)。
COM8:
setup()
我得到“无法得到结果!”在我的浏览器中:/
我不知道为什么我第二次无法打开文件(当我从New client:
GET / HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Cache-Control: max-age=0
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Position of ajax_down: 0
**Can't read file!**
New client:
GET /favicon.ico HTTP/1.1
Host: 192.168.1.101
Connection: keep-alive
Accept: */*
User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/30.0.1599.101 Safari/537.36
Accept-Encoding: gzip,deflate,sdch
Accept-Language: pl-PL,pl;q=0.8,en-US;q=0.6,en;q=0.4
Position of ajax_down: 0
**Can't read file!**
删除阅读时它也不起作用)。也许是因为一些内存泄漏造成的?草图的二进制大小为24 568字节(最大值:32 256)但我不知道在这种情况下是否有话要说。
答案 0 :(得分:0)
不确定我是否可以,但是如果你想在设置部分声明indexOf文件,我认为应该也需要在Loop中声明。
此致
答案 1 :(得分:0)
草图的二进制大小为24 568字节(最大值:32 256)
也许是因为一些内存泄漏造成的?
您是否在ATMega168上运行代码?
在大多数情况下,由于RAM很小,168无法处理SD。
您应该使用ATMega328(Uno)或更高版本来处理SD。