【问题标题】:Get client IP address with Arduino使用 Arduino 获取客户端 IP 地址
【发布时间】:2013-09-30 03:45:18
【问题描述】:

我有一个Arduino Uno,使用 Ethernet Shield 作为服务器,我通过 Internet 在 Arduino 上发出请求。我使用了两个库(Ethernet.h 和 SPI.h)。

我想检查客户端 IP address,所以我只接受来自已知 IP 地址(例如,50.50.50.50)的 HTTP 请求,这是我办公室的静态 IP 地址。如何获取 Arduino 上的客户端 IP 地址?

【问题讨论】:

  • 客户端IP地址“已知”是什么意思?可以在 DNS 中查找吗?
  • 您使用什么硬件连接到 Internet(名称、供应商)?没有附加软件,Arduino Uno 无法与 Internet 通信。
  • 您正在使用哪些软件/软件库(例如,用于额外硬件和 HTTP 服务器)?

标签: arduino


【解决方案1】:

看看下面,这适用于 TCP:

http://forum.arduino.cc/index.php?PHPSESSID=jh6t8omt7vrb8nget5c9j5dbk4&/topic,82416.0.html

以下引用作者的帖子,我只是复制优秀作品:

为了让它发挥作用,我做了以下工作:

我在 EthernetClient.cpp 文件的末尾添加了以下几行:

uint8_t *EthernetClient::getRemoteIP(uint8_t remoteIP[])
{
  W5100.readSnDIPR(_sock, remoteIP);
  return remoteIP;
}

然后我在 EthernetClient.h 文件中添加了以下行(在 virtual void stop(); 行下):

uint8_t *getRemoteIP(uint8_t RemoteIP[]);//adds remote ip address

最后我在我的草图中使用了以下代码来访问远程 IP:

client.getRemoteIP(rip); // where rip is defined as byte rip[] = {0,0,0,0 };

为了在串口监视器中显示 IP,我使用了:

for (int bcount= 0; bcount < 4; bcount++)
     {
        Serial.print(rip[bcount], DEC);
        if (bcount<3) Serial.print(".");
     }

【讨论】:

【解决方案2】:

我已经使用 UDP 完成了这项工作,希望这会对您有所帮助。

在此处从 Google 获取 UDP.h:UDP.h

代码:

#include <SPI.h>
#include <Ethernet.h>
#include <Udp.h> 

// *****  ETHERNET VARS *****
// MAC address and IP for arduino
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192,168,1,98};
unsigned int localPort = 8888;      // local port to listen on

// SenderIP and SenderPort are set when message is received
byte SenderIP[IP_LENGTH];        // holds received packet's originating IP
unsigned int SenderPort;        // holds received packet's originating port

// buffer for receiving data
char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet
int packetSize = 0;

void setup() 
{
  Ethernet.begin(mac,ip);  //start Ethernet
  Udp.begin(localPort);    //start UDP
}

void loop()
{
  if(NewPortMessage())
  {
      // Do stuff, SenderIP is the IP where the UDP message was received from
  }
}

boolean NewPortMessage()
{
  packetSize = Udp.available();
  if(packetSize > 0)
  {
    packetSize -= 8; //subtract UDP 8-byte header
    // read the packet into packetBufffer and get the senders IP addr and port number
    Udp.readPacket(packetBuffer,UDP_TX_PACKET_MAX_SIZE, SenderIP, SenderPort);
    return true;
  }
  clearPacketBuffer();
  return false;
}

void clearPacketBuffer()
{
  for(int i=0; i < packetSize; i++)
    packetBuffer[i] = 0;
}

【讨论】:

    【解决方案3】:

    改变方法怎么样?你可以使用 SOA,你可以让你的 arduino 成为一个 web 客户端而不是 web 服务器......然后你可以在托管你的 web 服务的 web 服务器中处理所有这些限制,这个 web 服务将是核心你的应用程序,这样你就可以从任何你想要的移动设备上调用它:D

    只是一个想法 arduino 网络服务器不是很有用,通过这种方法,您可以使用互联网而不是仅使用 LAN

    祝你的项目好运

    【讨论】:

      猜你喜欢
      • 2019-01-14
      • 2013-07-07
      • 2011-05-26
      • 2019-11-13
      • 1970-01-01
      • 1970-01-01
      • 2011-01-08
      • 2014-12-29
      • 2015-05-05
      相关资源
      最近更新 更多