【问题标题】:Direct connection between laptop and arduino via ethernet通过以太网直接连接笔记本电脑和arduino
【发布时间】:2014-01-08 14:44:08
【问题描述】:

我用 Arduino 和以太网屏蔽做了一个项目。 Arduino 托管了一个网站,我可以通过笔记本电脑上的浏览器打开该网站。 Arduino 通过以太网连接到路由器。所有这些工作都很好。

现在我必须在学校展示这个项目。为了防止令人不快的意外,我想通过以太网将 Arduino 直接与笔记本电脑连接起来。我的问题是我对这个话题真的不太了解。如果可能,请告诉我应该怎么做。

【问题讨论】:

  • 您能在演讲前在学校测试一下 Arduino 的无线连接吗?
  • 对不起,我有一个错字。 Arduino 没有无线连接。对不起!

标签: connection arduino ethernet


【解决方案1】:

如果您将路由器从环路中取出,您需要:

为笔记本电脑的以太网连接分配一个手动 IP 地址,例如 192.168.0.1

子网掩码 255.255.255.0

为 Arduino 的以太网分配一个手动 IP 地址,比如 192.168.0.2

子网掩码 255.255.255.0

默认网关为空

使用交叉电缆将两者连接起来(标准的跳线将不起作用)

然后您应该能够通过笔记本电脑在http://192.168.0.2 上启动您的 Arduino 站点。

看起来很聪明 :) 在笔记本电脑上编辑主机表(Windows 为 C:\windows\system32\drivers\etc\hosts)(Linux 为 /etc/hosts) 并输入:

192.168.0.2 my.arduino

然后你可以通过http://my.arduino访问它

祝你好运

【讨论】:

  • 感谢您的详细回答和小提示;P 我会试试看的!
  • 你好。我试过了,但我仍然无法让它工作。然而,我找到了解决问题的方法。我在家里找到了一个备用路由器,会随身携带。但我知道你的答案是正确的,只是对我来说不够详细。无论如何,我将其标记为已解决的巧妙小技巧;)
【解决方案2】:

您必须为笔记本电脑和 Arduino 手动分配 IP 地址。 然后在你的草图中包含 Ethernet.h 并尝试建立以太网连接。最后,您可以通过在浏览器中输入 Arduino 的 IP 在笔记本电脑中看到您的网页。示例:

#include <SPI.h> 
#include <Ethernet.h>
/******************** ETHERNET SETTINGS ********************/
byte mac[] = { 0x90, 0xA2, 0xDA, 0x0D, 0x85, 0xD9 };   //physical mac address
byte ip[] = { 192, 168, 1, 172 };                   // ip in lan
byte subnet[] = { 255, 255, 255, 0 };              //subnet mask
byte gateway[] = { 192, 168, 1, 254 };              // default gateway
EthernetServer server(80);                       //server port
void setup()
{
Ethernet.begin(mac,ip,gateway,subnet);     // initialize Ethernet device
server.begin();                                // start to listen for clients
pinMode(8, INPUT);                            // input pin for switch
}
void loop()
{
EthernetClient client = server.available();    // look for the client
// send a standard http response header
client.println("HTTP/1.1 200 OK");
client.println("Content-Type: text/html");
client.println("Connnection: close");
client.println();
/* 
This portion is the webpage which will be
sent to client web browser one can use html , javascript
and another web markup language to make particular layout 
*/
client.println("<!DOCTYPE html>");      //web page is made using html
client.println("<html>");
client.println("<head>");
client.println("<title>Ethernet Tutorial</title>");
client.println("<meta http-equiv=\"refresh\" content=\"1\">");
/*
The above line is used to refresh the page in every 1 second
This will be sent to the browser as the following HTML code:
<meta http-equiv="refresh" content="1">
content = 1 sec i.e assign time for refresh 
*/
client.println("</head>");
client.println("<body>");
client.println("<h1>A Webserver Tutorial </h1>");
client.println("<h2>Observing State Of Switch</h2>");
client.print("<h2>Switch is:  </2>");
if (digitalRead(8))
{
client.println("<h3>ON</h3>");
}
else
{
client.println("<h3>OFF</h3>");
}
client.println("</body>");
client.println("</html>");
delay(1);         // giving time to receive the data
/*
The following line is important because it will stop the client
and look for the new connection in the next iteration i.e
EthernetClient client = server.available();
*/
client.stop();
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-22
    • 1970-01-01
    • 2021-06-26
    • 2013-02-07
    • 2022-01-25
    相关资源
    最近更新 更多