我有一台以太网屏蔽的Arduino Uno作为服务器,我通过互联网在Arduino上发出请求。我使用两个库(Ethernet.h和SPI.h)来完成此任务。
我想检查客户端IP address,因此我只接受来自已知IP地址(例如,50.50.50.50)的HTTP请求,该地址是我办公室的静态IP地址。如何获取Arduino上的客户端IP地址?
发布于 2013-01-05 03:58:48
我已经使用UDP实现了这一点,希望这能对你有所帮助。
从谷歌获取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;
}发布于 2013-09-30 03:45:19
请看以下内容,这适用于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;
}然后,我将以下行(在虚拟void stop();行下)添加到EthernetClient.h文件中:
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(".");
}发布于 2013-01-08 08:36:20
那么改变一下方法呢?您可以使用SOA,您可以使您的arduino成为web客户端而不是web server.......then您可以在托管web服务的web服务器中处理所有这些限制,此web服务将是您的应用程序的核心,这样您就可以从任何想要的移动设备调用它:D
arduino web服务器只是一个想法,并不是很有用,使用这种方法,您可以使用internet,而不仅仅是LAN
祝你的项目好运
https://stackoverflow.com/questions/13960902
复制相似问题