C++ 如何通过we浏览器访问Spark core?
我希望核心像web服务器一样,在通过webrowser访问时显示文本 使用此命令>>>client.printlnSorry;它应该在浏览器中显示抱歉,对吗? 但当我在浏览器中输入内核的IP时。它说这个网页不可用 谢谢你的阅读C++ 如何通过we浏览器访问Spark core?,c++,arduino,sparkcore,C++,Arduino,Sparkcore,我希望核心像web服务器一样,在通过webrowser访问时显示文本 使用此命令>>>client.printlnSorry;它应该在浏览器中显示抱歉,对吗? 但当我在浏览器中输入内核的IP时。它说这个网页不可用 谢谢你的阅读 TCPServer server = TCPServer(80); TCPClient client; void setup() { // start listening for clients server.begin(
TCPServer server = TCPServer(80);
TCPClient client;
void setup()
{
// start listening for clients
server.begin();
// Make sure your Serial Terminal app is closed before powering your Core
Serial.begin(9600);
// Now open your Serial Terminal, and hit any key to continue!
while(!Serial.available()) SPARK_WLAN_Loop();
Serial.println(WiFi.localIP());
Serial.println(WiFi.subnetMask());
Serial.println(WiFi.gatewayIP());
Serial.println(WiFi.SSID());
}
void loop()
{
if (client.connected()) {
// echo all available bytes back to the client
while (client.available()) {
server.write(client.read());
server.write("Hello world!");
client.println("Sorry"); //<<<<<<<<<<<<<<should show this
}
} else {
// if no client is yet connected, check for a new connection
client = server.available();
}
}
这是我在arduino和ethernet sheild中使用的代码,它工作正常
#include <SPI.h>
#include <Ethernet.h>
//Enter a new MAC address from your computer MAC plus one.
byte mac[] = { 0x00, 0xAA, 0xBB, 0xCC, 0xDE, 0x02 };
EthernetServer server(80); // Create new server object at selected port
void setup() {
Serial.begin(9600);
Serial.println("Requesting ip address ...");
Ethernet.begin(mac,ip,gateway,subnet);
Serial.print("My IP address: ");
Serial.print(Ethernet.localIP());
server.begin(); // begin listening
}
void loop() {
EthernetClient client = server.available(); // Get connection
if(server.available()){ // check if there are connection
Serial.println("new client connection");
while (client.connected()) { // check if the client still alive
if (client.available()) { // check if the connection are still alive
char c = client.read(); // read data from Ethernet buffer
if(c=='='){ p = client.read(); }
Serial.write(c);
if(c == '\n'){ // check if there are terminate character
client.println("sorry");
break;
}
}
}
client.stop(); // disconnect from client
Serial.println("client disconnected");
}
delay(1000);
}///// credit P'O ITE
浏览器对您所做的不会很满意。返回到浏览器的任何内容都不符合HTTP协议规范。您应该使用telnet客户端在端口80上打开到核心IP地址的连接。然后你会看到一些更接近你期望的东西 使用SparkCore和它的适度功能返回网页是可能的,但您必须严格控制大小。若要返回网页,浏览器会很满意,您可以忽略该请求并始终返回以下内容:
HTTP/1.1 200 OK
Date: Tue, 31 Mar 2015 22:38:34 GMT
Server: SparkCore
Content-Type: text/html; charset=UTF-8
Content-Length: 138
Accept-Ranges: bytes
Connection: close
<html>
<head>
<title>An Example Page</title>
</head>
<body>
Hello World, this is a very simple HTML document.
</body>
</html>
这是一个最小的HTTP响应。我还没有测试过它,但我很有信心它会起作用。要了解有关HTTP的更多信息以及您需要做什么,请查看维基百科:
我还在community.spark.io上找到了一个很好的帖子:
祝你好运