├── Arduino IDE 1.0.x └── Ethernet │ ├── util.h │ ├── EthernetServer.h │ ├── EthernetClient.h │ ├── Twitter.h │ ├── Dns.h │ ├── examples │ ├── DhcpAddressPrinter │ │ └── DhcpAddressPrinter.ino │ ├── ChatServer │ │ └── ChatServer.ino │ ├── Twitter_SimplePost │ │ └── Twitter_SimplePost.ino │ ├── TelnetClient │ │ └── TelnetClient.ino │ ├── DhcpChatServer │ │ └── DhcpChatServer.ino │ ├── WebClient │ │ └── WebClient.ino │ ├── Twitter_Serial_GW │ │ └── Twitter_Serial_GW.ino │ ├── WebServer │ │ └── WebServer.ino │ ├── UDPSendReceiveString │ │ └── UDPSendReceiveString.ino │ ├── WebClientRepeating │ │ └── WebClientRepeating.ino │ ├── UdpNtpClient │ │ └── UdpNtpClient.ino │ ├── XivelyClientString │ │ └── XivelyClientString.ino │ ├── XivelyClient │ │ └── XivelyClient.ino │ └── BarometricPressureWebServer │ │ └── BarometricPressureWebServer.ino │ ├── Ethernet.h │ ├── utility │ ├── socket.h │ ├── w5500.cpp │ ├── w5100.cpp │ ├── w5200.cpp │ └── socket.cpp │ ├── EthernetServer.cpp │ ├── Twitter.cpp │ ├── EthernetClient.cpp │ ├── Dhcp.h │ ├── EthernetUdp.h │ ├── Ethernet.cpp │ ├── EthernetUdp.cpp │ └── Dns.cpp ├── Arduino IDE 1.5.x └── Ethernet │ ├── src │ ├── util.h │ ├── utility │ │ ├── util.h │ │ ├── socket.h │ │ ├── w5500.cpp │ │ ├── w5100.cpp │ │ ├── w5200.cpp │ │ └── socket.cpp │ ├── EthernetServer.h │ ├── EthernetClient.h │ ├── Twitter.h │ ├── Dns.h │ ├── Ethernet.h │ ├── EthernetServer.cpp │ ├── Twitter.cpp │ ├── EthernetClient.cpp │ ├── Dhcp.h │ ├── EthernetUdp.h │ ├── Ethernet.cpp │ └── EthernetUdp.cpp │ └── library.properties ├── .gitattributes ├── README.md └── .gitignore /Arduino IDE 1.0.x/Ethernet/util.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_H 2 | #define UTIL_H 3 | 4 | #define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) ) 5 | #define ntohs(x) htons(x) 6 | 7 | #define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \ 8 | ((x)<< 8 & 0x00FF0000UL) | \ 9 | ((x)>> 8 & 0x0000FF00UL) | \ 10 | ((x)>>24 & 0x000000FFUL) ) 11 | #define ntohl(x) htonl(x) 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/util.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_H 2 | #define UTIL_H 3 | 4 | #define htons(x) ( ((x)<<8) | (((x)>>8)&0xFF) ) 5 | #define ntohs(x) htons(x) 6 | 7 | #define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \ 8 | ((x)<< 8 & 0x00FF0000UL) | \ 9 | ((x)>> 8 & 0x0000FF00UL) | \ 10 | ((x)>>24 & 0x000000FFUL) ) 11 | #define ntohl(x) htonl(x) 12 | 13 | #endif 14 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/utility/util.h: -------------------------------------------------------------------------------- 1 | #ifndef UTIL_H 2 | #define UTIL_H 3 | 4 | #define htons(x) ( ((x)<< 8 & 0xFF00) | \ 5 | ((x)>> 8 & 0x00FF) ) 6 | #define ntohs(x) htons(x) 7 | 8 | #define htonl(x) ( ((x)<<24 & 0xFF000000UL) | \ 9 | ((x)<< 8 & 0x00FF0000UL) | \ 10 | ((x)>> 8 & 0x0000FF00UL) | \ 11 | ((x)>>24 & 0x000000FFUL) ) 12 | #define ntohl(x) htonl(x) 13 | 14 | #endif 15 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/library.properties: -------------------------------------------------------------------------------- 1 | name=Ethernet 2 | version=1.0.0 3 | author=Soohwan Kim and Wiznet 4 | maintainer=Wiznet 5 | sentence=WIZnet Ethernet Library 6 | paragraph=WIZ Ethernet library is made for various Open Source Hardware Platform and support WIZnet's W5100, W5200 and W5500 chip. The Ethernet library lets you connect to the Internet or a local network. 7 | category=Communication 8 | url=https://github.com/Wiznet/WIZ_Ethernet_Library 9 | architectures=* 10 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/EthernetServer.h: -------------------------------------------------------------------------------- 1 | #ifndef ethernetserver_h 2 | #define ethernetserver_h 3 | 4 | #include "Server.h" 5 | 6 | class EthernetClient; 7 | 8 | class EthernetServer : 9 | public Server { 10 | private: 11 | uint16_t _port; 12 | void accept(); 13 | public: 14 | EthernetServer(uint16_t); 15 | EthernetClient available(); 16 | virtual void begin(); 17 | virtual size_t write(uint8_t); 18 | virtual size_t write(const uint8_t *buf, size_t size); 19 | using Print::write; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/EthernetServer.h: -------------------------------------------------------------------------------- 1 | #ifndef ethernetserver_h 2 | #define ethernetserver_h 3 | 4 | #include "Server.h" 5 | 6 | class EthernetClient; 7 | 8 | class EthernetServer : 9 | public Server { 10 | private: 11 | uint16_t _port; 12 | void accept(); 13 | public: 14 | EthernetServer(uint16_t); 15 | EthernetClient available(); 16 | virtual void begin(); 17 | virtual size_t write(uint8_t); 18 | virtual size_t write(const uint8_t *buf, size_t size); 19 | using Print::write; 20 | }; 21 | 22 | #endif 23 | -------------------------------------------------------------------------------- /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | 4 | # Custom for Visual Studio 5 | *.cs diff=csharp 6 | *.sln merge=union 7 | *.csproj merge=union 8 | *.vbproj merge=union 9 | *.fsproj merge=union 10 | *.dbproj merge=union 11 | 12 | # Standard to msysgit 13 | *.doc diff=astextplain 14 | *.DOC diff=astextplain 15 | *.docx diff=astextplain 16 | *.DOCX diff=astextplain 17 | *.dot diff=astextplain 18 | *.DOT diff=astextplain 19 | *.pdf diff=astextplain 20 | *.PDF diff=astextplain 21 | *.rtf diff=astextplain 22 | *.RTF diff=astextplain 23 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/EthernetClient.h: -------------------------------------------------------------------------------- 1 | #ifndef ethernetclient_h 2 | #define ethernetclient_h 3 | #include "Arduino.h" 4 | #include "Print.h" 5 | #include "Client.h" 6 | #include "IPAddress.h" 7 | 8 | class EthernetClient : public Client { 9 | 10 | public: 11 | EthernetClient(); 12 | EthernetClient(uint8_t sock); 13 | 14 | uint8_t status(); 15 | virtual int connect(IPAddress ip, uint16_t port); 16 | virtual int connect(const char *host, uint16_t port); 17 | virtual size_t write(uint8_t); 18 | virtual size_t write(const uint8_t *buf, size_t size); 19 | virtual int available(); 20 | virtual int read(); 21 | virtual int read(uint8_t *buf, size_t size); 22 | virtual int peek(); 23 | virtual void flush(); 24 | virtual void stop(); 25 | virtual uint8_t connected(); 26 | virtual operator bool(); 27 | 28 | friend class EthernetServer; 29 | 30 | using Print::write; 31 | 32 | private: 33 | static uint16_t _srcport; 34 | uint8_t _sock; 35 | }; 36 | 37 | #endif 38 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/EthernetClient.h: -------------------------------------------------------------------------------- 1 | #ifndef ethernetclient_h 2 | #define ethernetclient_h 3 | #include "Arduino.h" 4 | #include "Print.h" 5 | #include "Client.h" 6 | #include "IPAddress.h" 7 | 8 | class EthernetClient : public Client { 9 | 10 | public: 11 | EthernetClient(); 12 | EthernetClient(uint8_t sock); 13 | 14 | uint8_t status(); 15 | virtual int connect(IPAddress ip, uint16_t port); 16 | virtual int connect(const char *host, uint16_t port); 17 | virtual size_t write(uint8_t); 18 | virtual size_t write(const uint8_t *buf, size_t size); 19 | virtual int available(); 20 | virtual int read(); 21 | virtual int read(uint8_t *buf, size_t size); 22 | virtual int peek(); 23 | virtual void flush(); 24 | virtual void stop(); 25 | virtual uint8_t connected(); 26 | virtual operator bool(); 27 | virtual bool operator==(const EthernetClient&); 28 | virtual bool operator!=(const EthernetClient& rhs) { return !this->operator==(rhs); }; 29 | 30 | friend class EthernetServer; 31 | 32 | using Print::write; 33 | 34 | private: 35 | static uint16_t _srcport; 36 | uint8_t _sock; 37 | }; 38 | 39 | #endif 40 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/Twitter.h: -------------------------------------------------------------------------------- 1 | /* 2 | Twitter.cpp - Arduino library to Post messages to Twitter using OAuth. 3 | Copyright (c) NeoCat 2010-2011. All right reserved. 4 | 5 | This library is distributed in the hope that it will be useful, 6 | but WITHOUT ANY WARRANTY; without even the implied warranty of 7 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 8 | */ 9 | 10 | // ver1.2 - Use to support IDE 0019 or later 11 | // ver1.3 - Support IDE 1.0 12 | 13 | #ifndef TWITTER_H 14 | #define TWITTER_H 15 | 16 | #include 17 | #include 18 | #if defined(ARDUINO) && ARDUINO > 18 // Arduino 0019 or later 19 | #include 20 | #endif 21 | #include 22 | #if defined(ARDUINO) && ARDUINO < 100 // earlier than Arduino 1.0 23 | #include 24 | #endif 25 | 26 | class Twitter 27 | { 28 | private: 29 | uint8_t parseStatus; 30 | int statusCode; 31 | const char *token; 32 | #if defined(ARDUINO) && ARDUINO < 100 33 | Client client; 34 | #else 35 | EthernetClient client; 36 | #endif 37 | public: 38 | Twitter(const char *user_and_passwd); 39 | 40 | bool post(const char *msg); 41 | bool checkStatus(Print *debug = NULL); 42 | int wait(Print *debug = NULL); 43 | int status(void) { return statusCode; } 44 | }; 45 | 46 | #endif //TWITTER_H 47 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/Twitter.h: -------------------------------------------------------------------------------- 1 | /* 2 | Twitter.cpp - Arduino library to Post messages to Twitter using OAuth. 3 | Copyright (c) NeoCat 2010-2011. All right reserved. 4 | 5 | This library is distributed in the hope that it will be useful, 6 | but WITHOUT ANY WARRANTY; without even the implied warranty of 7 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 8 | */ 9 | 10 | // ver1.2 - Use to support IDE 0019 or later 11 | // ver1.3 - Support IDE 1.0 12 | 13 | #ifndef TWITTER_H 14 | #define TWITTER_H 15 | 16 | #include 17 | #include 18 | #if defined(ARDUINO) && ARDUINO > 18 // Arduino 0019 or later 19 | #include 20 | #endif 21 | #include 22 | #if defined(ARDUINO) && ARDUINO < 100 // earlier than Arduino 1.0 23 | #include 24 | #endif 25 | 26 | class Twitter 27 | { 28 | private: 29 | uint8_t parseStatus; 30 | int statusCode; 31 | const char *token; 32 | #if defined(ARDUINO) && ARDUINO < 100 33 | Client client; 34 | #else 35 | EthernetClient client; 36 | #endif 37 | public: 38 | Twitter(const char *user_and_passwd); 39 | 40 | bool post(const char *msg); 41 | bool checkStatus(Print *debug = NULL); 42 | int wait(Print *debug = NULL); 43 | int status(void) { return statusCode; } 44 | }; 45 | 46 | #endif //TWITTER_H 47 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/Dns.h: -------------------------------------------------------------------------------- 1 | // Arduino DNS client for WizNet5100-based Ethernet shield 2 | // (c) Copyright 2009-2010 MCQN Ltd. 3 | // Released under Apache License, version 2.0 4 | 5 | #ifndef DNSClient_h 6 | #define DNSClient_h 7 | 8 | #include 9 | 10 | class DNSClient 11 | { 12 | public: 13 | // ctor 14 | void begin(const IPAddress& aDNSServer); 15 | 16 | /** Convert a numeric IP address string into a four-byte IP address. 17 | @param aIPAddrString IP address to convert 18 | @param aResult IPAddress structure to store the returned IP address 19 | @result 1 if aIPAddrString was successfully converted to an IP address, 20 | else error code 21 | */ 22 | int inet_aton(const char *aIPAddrString, IPAddress& aResult); 23 | 24 | /** Resolve the given hostname to an IP address. 25 | @param aHostname Name to be resolved 26 | @param aResult IPAddress structure to store the returned IP address 27 | @result 1 if aIPAddrString was successfully converted to an IP address, 28 | else error code 29 | */ 30 | int getHostByName(const char* aHostname, IPAddress& aResult); 31 | 32 | protected: 33 | uint16_t BuildRequest(const char* aName); 34 | uint16_t ProcessResponse(uint16_t aTimeout, IPAddress& aAddress); 35 | 36 | IPAddress iDNSServer; 37 | uint16_t iRequestId; 38 | EthernetUDP iUdp; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/Dns.h: -------------------------------------------------------------------------------- 1 | // Arduino DNS client for WizNet5100-based Ethernet shield 2 | // (c) Copyright 2009-2010 MCQN Ltd. 3 | // Released under Apache License, version 2.0 4 | 5 | #ifndef DNSClient_h 6 | #define DNSClient_h 7 | 8 | #include 9 | 10 | class DNSClient 11 | { 12 | public: 13 | // ctor 14 | void begin(const IPAddress& aDNSServer); 15 | 16 | /** Convert a numeric IP address string into a four-byte IP address. 17 | @param aIPAddrString IP address to convert 18 | @param aResult IPAddress structure to store the returned IP address 19 | @result 1 if aIPAddrString was successfully converted to an IP address, 20 | else error code 21 | */ 22 | int inet_aton(const char *aIPAddrString, IPAddress& aResult); 23 | 24 | /** Resolve the given hostname to an IP address. 25 | @param aHostname Name to be resolved 26 | @param aResult IPAddress structure to store the returned IP address 27 | @result 1 if aIPAddrString was successfully converted to an IP address, 28 | else error code 29 | */ 30 | int getHostByName(const char* aHostname, IPAddress& aResult); 31 | 32 | protected: 33 | uint16_t BuildRequest(const char* aName); 34 | uint16_t ProcessResponse(uint16_t aTimeout, IPAddress& aAddress); 35 | 36 | IPAddress iDNSServer; 37 | uint16_t iRequestId; 38 | EthernetUDP iUdp; 39 | }; 40 | 41 | #endif 42 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/DhcpAddressPrinter/DhcpAddressPrinter.ino: -------------------------------------------------------------------------------- 1 | /* 2 | DHCP-based IP printer 3 | 4 | This sketch uses the DHCP extensions to the Ethernet library 5 | to get an IP address via DHCP and print the address obtained. 6 | using an Arduino Wiznet Ethernet shield. 7 | 8 | Circuit: 9 | * Ethernet shield attached to pins 10, 11, 12, 13 10 | 11 | created 12 April 2011 12 | modified 9 Apr 2012 13 | by Tom Igoe 14 | modified 12 Aug 2013 15 | by Soohwan Kim 16 | */ 17 | 18 | #include 19 | #include 20 | 21 | // Enter a MAC address for your controller below. 22 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 23 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 24 | ; 25 | #else 26 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 27 | #endif 28 | 29 | // Initialize the Ethernet client library 30 | // with the IP address and port of the server 31 | // that you want to connect to (port 80 is default for HTTP): 32 | EthernetClient client; 33 | 34 | void setup() { 35 | // Open serial communications and wait for port to open: 36 | Serial.begin(9600); 37 | // this check is only needed on the Leonardo: 38 | while (!Serial) { 39 | ; // wait for serial port to connect. Needed for Leonardo only 40 | } 41 | 42 | // start the Ethernet connection: 43 | #if defined(WIZ550io_WITH_MACADDRESS) 44 | if (Ethernet.begin() == 0) { 45 | #else 46 | if (Ethernet.begin(mac) == 0) { 47 | #endif 48 | Serial.println("Failed to configure Ethernet using DHCP"); 49 | // no point in carrying on, so do nothing forevermore: 50 | for(;;) 51 | ; 52 | } 53 | // print your local IP address: 54 | Serial.print("My IP address: "); 55 | for (byte thisByte = 0; thisByte < 4; thisByte++) { 56 | // print the value of each byte of the IP address: 57 | Serial.print(Ethernet.localIP()[thisByte], DEC); 58 | Serial.print("."); 59 | } 60 | Serial.println(); 61 | } 62 | 63 | void loop() { 64 | 65 | } 66 | 67 | 68 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/Ethernet.h: -------------------------------------------------------------------------------- 1 | /* 2 | modified 12 Aug 2013 3 | by Soohwan Kim (suhwan@wiznet.co.kr) 4 | */ 5 | #ifndef ethernet_h 6 | #define ethernet_h 7 | 8 | #include 9 | #include "./utility/w5100.h" 10 | #include "IPAddress.h" 11 | #include "EthernetClient.h" 12 | #include "EthernetServer.h" 13 | #include "Dhcp.h" 14 | 15 | 16 | 17 | class EthernetClass { 18 | private: 19 | IPAddress _dnsServerAddress; 20 | DhcpClass* _dhcp; 21 | public: 22 | static uint8_t _state[MAX_SOCK_NUM]; 23 | static uint16_t _server_port[MAX_SOCK_NUM]; 24 | // Initialise the Ethernet shield to use the provided MAC address and gain the rest of the 25 | // configuration through DHCP. 26 | // Returns 0 if the DHCP configuration failed, and 1 if it succeeded 27 | int begin(uint8_t *mac_address); 28 | void begin(uint8_t *mac_address, IPAddress local_ip); 29 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server); 30 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway); 31 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet); 32 | 33 | #if defined(WIZ550io_WITH_MACADDRESS) 34 | // Initialize function when use the ioShield serise (included WIZ550io) 35 | // WIZ550io has a MAC address which is written after reset. 36 | // Default IP, Gateway and subnet address are also writen. 37 | // so, It needs some initial time. please refer WIZ550io Datasheet in details. 38 | int begin(void); 39 | void begin(IPAddress local_ip); 40 | void begin(IPAddress local_ip, IPAddress dns_server); 41 | void begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway); 42 | void begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet); 43 | #endif 44 | 45 | int maintain(); 46 | 47 | IPAddress localIP(); 48 | IPAddress subnetMask(); 49 | IPAddress gatewayIP(); 50 | IPAddress dnsServerIP(); 51 | 52 | friend class EthernetClient; 53 | friend class EthernetServer; 54 | }; 55 | 56 | extern EthernetClass Ethernet; 57 | 58 | #endif 59 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/Ethernet.h: -------------------------------------------------------------------------------- 1 | /* 2 | modified 12 Aug 2013 3 | by Soohwan Kim (suhwan@wiznet.co.kr) 4 | */ 5 | #ifndef ethernet_h 6 | #define ethernet_h 7 | 8 | #include 9 | #include "utility/w5100.h" 10 | #include "IPAddress.h" 11 | #include "EthernetClient.h" 12 | #include "EthernetServer.h" 13 | #include "Dhcp.h" 14 | 15 | 16 | 17 | class EthernetClass { 18 | private: 19 | IPAddress _dnsServerAddress; 20 | DhcpClass* _dhcp; 21 | public: 22 | static uint8_t _state[MAX_SOCK_NUM]; 23 | static uint16_t _server_port[MAX_SOCK_NUM]; 24 | 25 | #if defined(WIZ550io_WITH_MACADDRESS) 26 | // Initialize function when use the ioShield serise (included WIZ550io) 27 | // WIZ550io has a MAC address which is written after reset. 28 | // Default IP, Gateway and subnet address are also writen. 29 | // so, It needs some initial time. please refer WIZ550io Datasheet in details. 30 | int begin(void); 31 | void begin(IPAddress local_ip); 32 | void begin(IPAddress local_ip, IPAddress dns_server); 33 | void begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway); 34 | void begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet); 35 | #else 36 | // Initialize the Ethernet shield to use the provided MAC address and gain the rest of the 37 | // configuration through DHCP. 38 | // Returns 0 if the DHCP configuration failed, and 1 if it succeeded 39 | int begin(uint8_t *mac_address); 40 | void begin(uint8_t *mac_address, IPAddress local_ip); 41 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server); 42 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway); 43 | void begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet); 44 | 45 | #endif 46 | 47 | int maintain(); 48 | 49 | IPAddress localIP(); 50 | IPAddress subnetMask(); 51 | IPAddress gatewayIP(); 52 | IPAddress dnsServerIP(); 53 | 54 | friend class EthernetClient; 55 | friend class EthernetServer; 56 | }; 57 | 58 | extern EthernetClass Ethernet; 59 | 60 | #endif 61 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/utility/socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "w5100.h" 5 | 6 | extern uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag); // Opens a socket(TCP or UDP or IP_RAW mode) 7 | extern void close(SOCKET s); // Close socket 8 | extern uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port); // Establish TCP connection (Active connection) 9 | extern void disconnect(SOCKET s); // disconnect the connection 10 | extern uint8_t listen(SOCKET s); // Establish TCP connection (Passive connection) 11 | extern uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len); // Send data (TCP) 12 | extern int16_t recv(SOCKET s, uint8_t * buf, int16_t len); // Receive data (TCP) 13 | extern uint16_t peek(SOCKET s, uint8_t *buf); 14 | extern uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW) 15 | extern uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); // Receive data (UDP/IP RAW) 16 | 17 | extern uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len); 18 | 19 | // Functions to allow buffered UDP send (i.e. where the UDP datagram is built up over a 20 | // number of calls before being sent 21 | /* 22 | @brief This function sets up a UDP datagram, the data for which will be provided by one 23 | or more calls to bufferData and then finally sent with sendUDP. 24 | @return 1 if the datagram was successfully set up, or 0 if there was an error 25 | */ 26 | extern int startUDP(SOCKET s, uint8_t* addr, uint16_t port); 27 | /* 28 | @brief This function copies up to len bytes of data from buf into a UDP datagram to be 29 | sent later by sendUDP. Allows datagrams to be built up from a series of bufferData calls. 30 | @return Number of bytes successfully buffered 31 | */ 32 | uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len); 33 | /* 34 | @brief Send a UDP datagram built up from a sequence of startUDP followed by one or more 35 | calls to bufferData. 36 | @return 1 if the datagram was successfully sent, or 0 if there was an error 37 | */ 38 | int sendUDP(SOCKET s); 39 | 40 | #endif 41 | /* _SOCKET_H_ */ 42 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/EthernetServer.cpp: -------------------------------------------------------------------------------- 1 | #include "w5100.h" 2 | #include "socket.h" 3 | extern "C" { 4 | #include "string.h" 5 | } 6 | 7 | #include "Ethernet.h" 8 | #include "EthernetClient.h" 9 | #include "EthernetServer.h" 10 | 11 | EthernetServer::EthernetServer(uint16_t port) 12 | { 13 | _port = port; 14 | } 15 | 16 | void EthernetServer::begin() 17 | { 18 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 19 | EthernetClient client(sock); 20 | if (client.status() == SnSR::CLOSED) { 21 | socket(sock, SnMR::TCP, _port, 0); 22 | listen(sock); 23 | EthernetClass::_server_port[sock] = _port; 24 | break; 25 | } 26 | } 27 | } 28 | 29 | void EthernetServer::accept() 30 | { 31 | int listening = 0; 32 | 33 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 34 | EthernetClient client(sock); 35 | 36 | if (EthernetClass::_server_port[sock] == _port) { 37 | if (client.status() == SnSR::LISTEN) { 38 | listening = 1; 39 | } 40 | else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) { 41 | client.stop(); 42 | } 43 | } 44 | } 45 | 46 | if (!listening) { 47 | begin(); 48 | } 49 | } 50 | 51 | EthernetClient EthernetServer::available() 52 | { 53 | accept(); 54 | 55 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 56 | EthernetClient client(sock); 57 | if (EthernetClass::_server_port[sock] == _port && 58 | (client.status() == SnSR::ESTABLISHED || 59 | client.status() == SnSR::CLOSE_WAIT)) { 60 | if (client.available()) { 61 | // XXX: don't always pick the lowest numbered socket. 62 | return client; 63 | } 64 | } 65 | } 66 | 67 | return EthernetClient(MAX_SOCK_NUM); 68 | } 69 | 70 | size_t EthernetServer::write(uint8_t b) 71 | { 72 | return write(&b, 1); 73 | } 74 | 75 | size_t EthernetServer::write(const uint8_t *buffer, size_t size) 76 | { 77 | size_t n = 0; 78 | 79 | accept(); 80 | 81 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 82 | EthernetClient client(sock); 83 | 84 | if (EthernetClass::_server_port[sock] == _port && 85 | client.status() == SnSR::ESTABLISHED) { 86 | n += client.write(buffer, size); 87 | } 88 | } 89 | 90 | return n; 91 | } 92 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/utility/socket.h: -------------------------------------------------------------------------------- 1 | #ifndef _SOCKET_H_ 2 | #define _SOCKET_H_ 3 | 4 | #include "utility/w5100.h" 5 | 6 | extern uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag); // Opens a socket(TCP or UDP or IP_RAW mode) 7 | extern void close(SOCKET s); // Close socket 8 | extern uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port); // Establish TCP connection (Active connection) 9 | extern void disconnect(SOCKET s); // disconnect the connection 10 | extern uint8_t listen(SOCKET s); // Establish TCP connection (Passive connection) 11 | extern uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len); // Send data (TCP) 12 | extern int16_t recv(SOCKET s, uint8_t * buf, int16_t len); // Receive data (TCP) 13 | extern uint16_t peek(SOCKET s, uint8_t *buf); 14 | extern uint16_t sendto(SOCKET s, const uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t port); // Send data (UDP/IP RAW) 15 | extern uint16_t recvfrom(SOCKET s, uint8_t * buf, uint16_t len, uint8_t * addr, uint16_t *port); // Receive data (UDP/IP RAW) 16 | extern void flush(SOCKET s); // Wait for transmission to complete 17 | 18 | extern uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len); 19 | 20 | // Functions to allow buffered UDP send (i.e. where the UDP datagram is built up over a 21 | // number of calls before being sent 22 | /* 23 | @brief This function sets up a UDP datagram, the data for which will be provided by one 24 | or more calls to bufferData and then finally sent with sendUDP. 25 | @return 1 if the datagram was successfully set up, or 0 if there was an error 26 | */ 27 | extern int startUDP(SOCKET s, uint8_t* addr, uint16_t port); 28 | /* 29 | @brief This function copies up to len bytes of data from buf into a UDP datagram to be 30 | sent later by sendUDP. Allows datagrams to be built up from a series of bufferData calls. 31 | @return Number of bytes successfully buffered 32 | */ 33 | uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len); 34 | /* 35 | @brief Send a UDP datagram built up from a sequence of startUDP followed by one or more 36 | calls to bufferData. 37 | @return 1 if the datagram was successfully sent, or 0 if there was an error 38 | */ 39 | int sendUDP(SOCKET s); 40 | 41 | #endif 42 | /* _SOCKET_H_ */ 43 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/EthernetServer.cpp: -------------------------------------------------------------------------------- 1 | #include "utility/w5100.h" 2 | #include "utility/socket.h" 3 | extern "C" { 4 | #include "string.h" 5 | } 6 | 7 | #include "Ethernet.h" 8 | #include "EthernetClient.h" 9 | #include "EthernetServer.h" 10 | 11 | EthernetServer::EthernetServer(uint16_t port) 12 | { 13 | _port = port; 14 | } 15 | 16 | void EthernetServer::begin() 17 | { 18 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 19 | EthernetClient client(sock); 20 | if (client.status() == SnSR::CLOSED) { 21 | socket(sock, SnMR::TCP, _port, 0); 22 | listen(sock); 23 | EthernetClass::_server_port[sock] = _port; 24 | break; 25 | } 26 | } 27 | } 28 | 29 | void EthernetServer::accept() 30 | { 31 | int listening = 0; 32 | 33 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 34 | EthernetClient client(sock); 35 | 36 | if (EthernetClass::_server_port[sock] == _port) { 37 | if (client.status() == SnSR::LISTEN) { 38 | listening = 1; 39 | } 40 | else if (client.status() == SnSR::CLOSE_WAIT && !client.available()) { 41 | client.stop(); 42 | } 43 | } 44 | } 45 | 46 | if (!listening) { 47 | begin(); 48 | } 49 | } 50 | 51 | EthernetClient EthernetServer::available() 52 | { 53 | accept(); 54 | 55 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 56 | EthernetClient client(sock); 57 | if (EthernetClass::_server_port[sock] == _port && 58 | (client.status() == SnSR::ESTABLISHED || 59 | client.status() == SnSR::CLOSE_WAIT)) { 60 | if (client.available()) { 61 | // XXX: don't always pick the lowest numbered socket. 62 | return client; 63 | } 64 | } 65 | } 66 | 67 | return EthernetClient(MAX_SOCK_NUM); 68 | } 69 | 70 | size_t EthernetServer::write(uint8_t b) 71 | { 72 | return write(&b, 1); 73 | } 74 | 75 | size_t EthernetServer::write(const uint8_t *buffer, size_t size) 76 | { 77 | size_t n = 0; 78 | 79 | accept(); 80 | 81 | for (int sock = 0; sock < MAX_SOCK_NUM; sock++) { 82 | EthernetClient client(sock); 83 | 84 | if (EthernetClass::_server_port[sock] == _port && 85 | client.status() == SnSR::ESTABLISHED) { 86 | n += client.write(buffer, size); 87 | } 88 | } 89 | 90 | return n; 91 | } 92 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/Twitter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Twitter.cpp - Arduino library to Post messages to Twitter using OAuth. 3 | Copyright (c) NeoCat 2010-2011. All right reserved. 4 | 5 | This library is distributed in the hope that it will be useful, 6 | but WITHOUT ANY WARRANTY; without even the implied warranty of 7 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 8 | */ 9 | 10 | // ver1.2 - Use 11 | // ver1.3 - Support IDE 1.0 12 | 13 | #include 14 | #include "Twitter.h" 15 | 16 | #define LIB_DOMAIN "arduino-tweet.appspot.com" 17 | 18 | #if defined(ARDUINO) && ARDUINO < 100 19 | static uint8_t server[] = {0,0,0,0}; // IP address of LIB_DOMAIN 20 | Twitter::Twitter(const char *token) : client(server, 80), token(token) 21 | { 22 | } 23 | #else 24 | Twitter::Twitter(const char *token) : token(token) 25 | { 26 | } 27 | #endif 28 | 29 | bool Twitter::post(const char *msg) 30 | { 31 | #if defined(ARDUINO) && ARDUINO < 100 32 | DNSError err = EthernetDNS.resolveHostName(LIB_DOMAIN, server); 33 | if (err != DNSSuccess) { 34 | return false; 35 | } 36 | #endif 37 | parseStatus = 0; 38 | statusCode = 0; 39 | #if defined(ARDUINO) && ARDUINO < 100 40 | if (client.connect()) { 41 | #else 42 | if (client.connect(LIB_DOMAIN, 80)) { 43 | #endif 44 | client.println("POST http://" LIB_DOMAIN "/update HTTP/1.0"); 45 | client.print("Content-Length: "); 46 | client.println(strlen(msg)+strlen(token)+14); 47 | client.println(); 48 | client.print("token="); 49 | client.print(token); 50 | client.print("&status="); 51 | client.println(msg); 52 | } else { 53 | return false; 54 | } 55 | return true; 56 | } 57 | 58 | bool Twitter::checkStatus(Print *debug) 59 | { 60 | if (!client.connected()) { 61 | if (debug) 62 | while(client.available()) 63 | debug->print((char)client.read()); 64 | client.flush(); 65 | client.stop(); 66 | return false; 67 | } 68 | if (!client.available()) 69 | return true; 70 | char c = client.read(); 71 | if (debug) 72 | debug->print(c); 73 | switch(parseStatus) { 74 | case 0: 75 | if (c == ' ') parseStatus++; break; // skip "HTTP/1.1 " 76 | case 1: 77 | if (c >= '0' && c <= '9') { 78 | statusCode *= 10; 79 | statusCode += c - '0'; 80 | } else { 81 | parseStatus++; 82 | } 83 | } 84 | return true; 85 | } 86 | 87 | int Twitter::wait(Print *debug) 88 | { 89 | while (checkStatus(debug)); 90 | return statusCode; 91 | } 92 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/Twitter.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | Twitter.cpp - Arduino library to Post messages to Twitter using OAuth. 3 | Copyright (c) NeoCat 2010-2011. All right reserved. 4 | 5 | This library is distributed in the hope that it will be useful, 6 | but WITHOUT ANY WARRANTY; without even the implied warranty of 7 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. 8 | */ 9 | 10 | // ver1.2 - Use 11 | // ver1.3 - Support IDE 1.0 12 | 13 | #include 14 | #include "Twitter.h" 15 | 16 | #define LIB_DOMAIN "arduino-tweet.appspot.com" 17 | 18 | #if defined(ARDUINO) && ARDUINO < 100 19 | static uint8_t server[] = {0,0,0,0}; // IP address of LIB_DOMAIN 20 | Twitter::Twitter(const char *token) : client(server, 80), token(token) 21 | { 22 | } 23 | #else 24 | Twitter::Twitter(const char *token) : token(token) 25 | { 26 | } 27 | #endif 28 | 29 | bool Twitter::post(const char *msg) 30 | { 31 | #if defined(ARDUINO) && ARDUINO < 100 32 | DNSError err = EthernetDNS.resolveHostName(LIB_DOMAIN, server); 33 | if (err != DNSSuccess) { 34 | return false; 35 | } 36 | #endif 37 | parseStatus = 0; 38 | statusCode = 0; 39 | #if defined(ARDUINO) && ARDUINO < 100 40 | if (client.connect()) { 41 | #else 42 | if (client.connect(LIB_DOMAIN, 80)) { 43 | #endif 44 | client.println("POST http://" LIB_DOMAIN "/update HTTP/1.0"); 45 | client.print("Content-Length: "); 46 | client.println(strlen(msg)+strlen(token)+14); 47 | client.println(); 48 | client.print("token="); 49 | client.print(token); 50 | client.print("&status="); 51 | client.println(msg); 52 | } else { 53 | return false; 54 | } 55 | return true; 56 | } 57 | 58 | bool Twitter::checkStatus(Print *debug) 59 | { 60 | if (!client.connected()) { 61 | if (debug) 62 | while(client.available()) 63 | debug->print((char)client.read()); 64 | client.flush(); 65 | client.stop(); 66 | return false; 67 | } 68 | if (!client.available()) 69 | return true; 70 | char c = client.read(); 71 | if (debug) 72 | debug->print(c); 73 | switch(parseStatus) { 74 | case 0: 75 | if (c == ' ') parseStatus++; break; // skip "HTTP/1.1 " 76 | case 1: 77 | if (c >= '0' && c <= '9') { 78 | statusCode *= 10; 79 | statusCode += c - '0'; 80 | } else { 81 | parseStatus++; 82 | } 83 | } 84 | return true; 85 | } 86 | 87 | int Twitter::wait(Print *debug) 88 | { 89 | while (checkStatus(debug)); 90 | return statusCode; 91 | } 92 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/ChatServer/ChatServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Chat Server 3 | 4 | A simple server that distributes any incoming messages to all 5 | connected clients. To use telnet to your device's IP address and type. 6 | You can see the client's input in the serial monitor as well. 7 | Using an Arduino Wiznet Ethernet shield. 8 | 9 | Circuit: 10 | * Ethernet shield attached to pins 10, 11, 12, 13 11 | * Analog inputs attached to pins A0 through A5 (optional) 12 | 13 | created 18 Dec 2009 14 | by David A. Mellis 15 | modified 9 Apr 2012 16 | by Tom Igoe 17 | modified 12 Aug 2013 18 | by Soohwan Kim 19 | */ 20 | 21 | #include 22 | #include 23 | 24 | // Enter a MAC address and IP address for your controller below. 25 | // The IP address will be dependent on your local network. 26 | // gateway and subnet are optional: 27 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 28 | ; 29 | #else 30 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 31 | #endif 32 | 33 | IPAddress ip(192,168,1, 177); 34 | IPAddress gateway(192,168,1, 1); 35 | IPAddress subnet(255, 255, 255, 0); 36 | 37 | 38 | // telnet defaults to port 23 39 | EthernetServer server(23); 40 | boolean alreadyConnected = false; // whether or not the client was connected previously 41 | 42 | void setup() { 43 | // initialize the ethernet device 44 | #if defined(WIZ550io_WITH_MACADDRESS) 45 | Ethernet.begin(ip, gateway, subnet); 46 | #else 47 | Ethernet.begin(mac, ip, gateway, subnet); 48 | #endif 49 | // start listening for clients 50 | server.begin(); 51 | // Open serial communications and wait for port to open: 52 | Serial.begin(9600); 53 | while (!Serial) { 54 | ; // wait for serial port to connect. Needed for Leonardo only 55 | } 56 | 57 | 58 | Serial.print("Chat server address:"); 59 | Serial.println(Ethernet.localIP()); 60 | } 61 | 62 | void loop() { 63 | // wait for a new client: 64 | EthernetClient client = server.available(); 65 | 66 | // when the client sends the first byte, say hello: 67 | if (client) { 68 | if (!alreadyConnected) { 69 | // clead out the input buffer: 70 | client.flush(); 71 | Serial.println("We have a new client"); 72 | client.println("Hello, client!"); 73 | alreadyConnected = true; 74 | } 75 | 76 | if (client.available() > 0) { 77 | // read the bytes incoming from the client: 78 | char thisChar = client.read(); 79 | // echo the bytes back to the client: 80 | server.write(thisChar); 81 | // echo the bytes to the server as well: 82 | Serial.write(thisChar); 83 | } 84 | } 85 | } 86 | 87 | 88 | 89 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | WIZ Ethernet Library 2 | ======== 3 | WIZ Ethernet library is made for various Open Source Hardware Platform and support WIZnet's W5100, W5200 and W5500 chip. The Ethernet library lets you connect to the Internet or a local network 4 | 5 | ## Supported devices 6 | * ioShield, WIZ550io (using W5500) 7 | * W5200 Ethernet Shield, WIZ820io (using W5200) 8 | * Arduino Ethernet Shield (using W5100) 9 | 10 | ## Hardware 11 | * [ioShield](http://wizwiki.net/wiki/doku.php?id=ioshield "ioShield") 12 | * [W5200 Ethernet Shield](https://github.com/Wiznet/W5200-Ethernet-Shield "W5200 Ethernet Shield") 13 | * [Ethernet Shield](http://arduino.cc/en/Main/ArduinoEthernetShield "Ethernet Shield") 14 | 15 | ## Software 16 | #### 1. Install WIZ Ethernet library 17 | ##### Arduino IDE 1.0.x 18 | 19 | Download all files and overwrite onto the "\libraries\Ethernet" folder in your project in sketch. 20 | 21 | ##### Arduino IDE 1.5.x 22 | 23 | Download all files and replace the "\libraries\Ethernet\src" folder in your Arduino IDE. This will update the "utility" folder also under "\libraries\Ethernet\src". 24 | 25 | #### 2. Select device: W5100, W5200 or W5500 26 | In the W5100.h file(\libraries\Ethernet\utility\w5100.h), uncomment the device(shield) you want to use. 27 | 28 | ```cpp 29 | #ifndef W5100_H_INCLUDED 30 | #define W5100_H_INCLUDED 31 | 32 | #include 33 | #include 34 | 35 | typedef uint8_t SOCKET; 36 | //#define W5100_ETHERNET_SHIELD 37 | //#define W5200_ETHERNET_SHIELD 38 | #define W5500_ETHERNET_SHIELD 39 | ``` 40 | By default, "WIZ550io_WITH_MACADDRESS" is commented and if you uncomment it, you can use the MAC address stored in the WIZ550io. 41 | 42 | ```cpp 43 | #if defined(W5500_ETHERNET_SHIELD) 44 | //#define WIZ550io_WITH_MACADDRESS // Use assigned MAC address of WIZ550io 45 | #include "w5500.h" 46 | #endif 47 | ``` 48 | 49 | ## How to use the WIZ Ethernet library and evaluate existing Ethernet example. 50 | All other steps are the same as the steps from the Arduino Ethernet Shield. You can find examples in the Arduino IDE, go to Files->Examples->Ethernet, open any example, then copy it to your sketch file (gr_sketch.cpp) and change configuration values properly. 51 | After that, you can check if it is work well. For example, if you choose 'WebServer', you should change IP Address first and compile and download it. Then you can access web server page through your web browser of your PC or something. 52 | 53 | ## Revision History 54 | * Initial Release : 14 August 2013 55 | * Adding function to read / write W5500 PHY configuration register : 4 December 2013 56 | * Support the Arduino Due (Arduino IDE 1.5.x). Now it support 42Mhz SPI clock ! (by Jinbuhm Kim): 28 Feb. 2014 57 | * Separate the folder for Arduino IDE 1.0.x & Arduino IDE 1.5.x 58 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/Twitter_SimplePost/Twitter_SimplePost.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Twitter SimplePost 3 | 4 | SimplePost messages to Twitter (tweet) from Aruduino with Ethernet Shield! 5 | 6 | Notice 7 | -The library uses this site as a proxy server for OAuth stuff. Your tweet may not be applied during maintenance of this site. 8 | -Please avoid sending more than 1 request per minute not to overload the server. 9 | -Twitter seems to reject repeated tweets with the same contenet (returns error 403). 10 | 11 | Reference & Lincense 12 | URL : http://arduino-tweet.appspot.com/ 13 | 14 | Circuit: 15 | * Ethernet shield attached to pins 10, 11, 12, 13 16 | 17 | created 2011 18 | by NeoCat - (ver1.3 - Support IDE 1.0) 19 | tested & 20 | modified 13 Aug 2013 21 | by Soohwan Kim - (ver1.3 - Support IDE 1.0.5) 22 | */ 23 | 24 | #include // needed in Arduino 0019 or later 25 | #include 26 | #include "./Twitter.h" // Twitter.h in library/Ethernet folder. @diffinsight 2013-08-19 27 | 28 | // The includion of EthernetDNS is not needed in Arduino IDE 1.0 or later. 29 | // Please uncomment below in Arduino IDE 0022 or earlier. 30 | //#include 31 | 32 | 33 | // Ethernet Shield Settings 34 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 35 | ; 36 | #else 37 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 38 | #endif 39 | 40 | // If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later). 41 | // fill in an available IP address on your network here, 42 | IPAddress ip(1,1,1,1); 43 | IPAddress gw(1,1,1,1); 44 | IPAddress snip(1,1,1,1); 45 | IPAddress dnsip(1,1,1,1); 46 | 47 | // Your Token to Tweet (get it from http://arduino-tweet.appspot.com/) 48 | Twitter twitter("YOUR-TOKEN-HERE"); 49 | 50 | // Message to post 51 | char msg[] = "Hello! Tweet! Tweet! Tweet! This message is written from Arduino Pro Mini!"; 52 | 53 | void setup() 54 | { 55 | delay(1000); 56 | // start the Ethernet connection: 57 | #if defined(WIZ550io_WITH_MACADDRESS) 58 | Ethernet.begin(ip, dnsip, gw,snip); 59 | #else 60 | Ethernet.begin(mac, ip, dnsip, gw,snip); 61 | #endif 62 | // or you can use DHCP for autoomatic IP address configuration. 63 | // Ethernet.begin(mac); 64 | Serial.begin(9600); 65 | 66 | Serial.println("connecting ..."); 67 | if (twitter.post(msg)) { 68 | // Specify &Serial to output received response to Serial. 69 | // If no output is required, you can just omit the argument, e.g. 70 | // int status = twitter.wait(); 71 | int status = twitter.wait(&Serial); 72 | if (status == 200) { 73 | Serial.println("OK."); 74 | } else { 75 | Serial.print("failed : code "); 76 | Serial.println(status); 77 | } 78 | } else { 79 | Serial.println("connection failed."); 80 | } 81 | } 82 | 83 | void loop() 84 | { 85 | } 86 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/TelnetClient/TelnetClient.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Telnet client 3 | 4 | This sketch connects to a a telnet server (http://www.google.com) 5 | using an Arduino Wiznet Ethernet shield. You'll need a telnet server 6 | to test this with. 7 | Processing's ChatServer example (part of the network library) works well, 8 | running on port 10002. It can be found as part of the examples 9 | in the Processing application, available at 10 | http://processing.org/ 11 | 12 | Circuit: 13 | * Ethernet shield attached to pins 10, 11, 12, 13 14 | 15 | created 14 Sep 2010 16 | modified 9 Apr 2012 17 | by Tom Igoe 18 | modified 12 Aug 2013 19 | by Soohwan Kim 20 | */ 21 | 22 | #include 23 | #include 24 | 25 | // Enter a MAC address and IP address for your controller below. 26 | // The IP address will be dependent on your local network: 27 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 28 | ; 29 | #else 30 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 31 | #endif 32 | 33 | IPAddress ip(192,168,1,177); 34 | 35 | // Enter the IP address of the server you're connecting to: 36 | IPAddress server(1,1,1,1); 37 | 38 | // Initialize the Ethernet client library 39 | // with the IP address and port of the server 40 | // that you want to connect to (port 23 is default for telnet; 41 | // if you're using Processing's ChatServer, use port 10002): 42 | EthernetClient client; 43 | 44 | void setup() { 45 | // start the Ethernet connection: 46 | #if defined(WIZ550io_WITH_MACADDRESS) 47 | Ethernet.begin(ip); 48 | #else 49 | Ethernet.begin(mac, ip); 50 | #endif 51 | // Open serial communications and wait for port to open: 52 | Serial.begin(9600); 53 | while (!Serial) { 54 | ; // wait for serial port to connect. Needed for Leonardo only 55 | } 56 | 57 | 58 | // give the Ethernet shield a second to initialize: 59 | delay(1000); 60 | Serial.println("connecting..."); 61 | 62 | // if you get a connection, report back via serial: 63 | if (client.connect(server, 10002)) { 64 | Serial.println("connected"); 65 | } 66 | else { 67 | // if you didn't get a connection to the server: 68 | Serial.println("connection failed"); 69 | } 70 | } 71 | 72 | void loop() 73 | { 74 | // if there are incoming bytes available 75 | // from the server, read them and print them: 76 | if (client.available()) { 77 | char c = client.read(); 78 | Serial.print(c); 79 | } 80 | 81 | // as long as there are bytes in the serial queue, 82 | // read them and send them out the socket if it's open: 83 | while (Serial.available() > 0) { 84 | char inChar = Serial.read(); 85 | if (client.connected()) { 86 | client.print(inChar); 87 | } 88 | } 89 | 90 | // if the server's disconnected, stop the client: 91 | if (!client.connected()) { 92 | Serial.println(); 93 | Serial.println("disconnecting."); 94 | client.stop(); 95 | // do nothing: 96 | while(true); 97 | } 98 | } 99 | 100 | 101 | 102 | 103 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/DhcpChatServer/DhcpChatServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | DHCP Chat Server 3 | 4 | A simple server that distributes any incoming messages to all 5 | connected clients. To use telnet to your device's IP address and type. 6 | You can see the client's input in the serial monitor as well. 7 | Using an Arduino Wiznet Ethernet shield. 8 | 9 | THis version attempts to get an IP address using DHCP 10 | 11 | Circuit: 12 | * Ethernet shield attached to pins 10, 11, 12, 13 13 | 14 | Based on ChatServer example by David A. Mellis 15 | created 21 May 2011 16 | modified 9 Apr 2012 17 | by Tom Igoe 18 | modified 12 Aug 2013 19 | by Soohwan Kim 20 | 21 | */ 22 | 23 | #include 24 | #include 25 | 26 | // Enter a MAC address and IP address for your controller below. 27 | // The IP address will be dependent on your local network. 28 | // gateway and subnet are optional: 29 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 30 | ; 31 | #else 32 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 33 | #endif 34 | IPAddress ip(192,168,1, 177); 35 | IPAddress gateway(192,168,1, 1); 36 | IPAddress subnet(255, 255, 0, 0); 37 | 38 | // telnet defaults to port 23 39 | EthernetServer server(23); 40 | boolean gotAMessage = false; // whether or not you got a message from the client yet 41 | 42 | void setup() { 43 | 44 | // Open serial communications and wait for port to open: 45 | Serial.begin(9600); 46 | // this check is only needed on the Leonardo: 47 | while (!Serial) { 48 | ; // wait for serial port to connect. Needed for Leonardo only 49 | } 50 | 51 | 52 | // start the Ethernet connection: 53 | Serial.println("Trying to get an IP address using DHCP"); 54 | #if defined(WIZ550io_WITH_MACADDRESS) 55 | if (Ethernet.begin() == 0) { 56 | #else 57 | if (Ethernet.begin(mac) == 0) { 58 | #endif 59 | Serial.println("Failed to configure Ethernet using DHCP"); 60 | // initialize the ethernet device not using DHCP: 61 | #if defined(WIZ550io_WITH_MACADDRESS) 62 | Ethernet.begin(ip, gateway, subnet); 63 | #else 64 | Ethernet.begin(mac, ip, gateway, subnet); 65 | #endif 66 | } 67 | // print your local IP address: 68 | Serial.print("My IP address: "); 69 | ip = Ethernet.localIP(); 70 | for (byte thisByte = 0; thisByte < 4; thisByte++) { 71 | // print the value of each byte of the IP address: 72 | Serial.print(ip[thisByte], DEC); 73 | Serial.print("."); 74 | } 75 | Serial.println(); 76 | // start listening for clients 77 | server.begin(); 78 | 79 | } 80 | 81 | void loop() { 82 | // wait for a new client: 83 | EthernetClient client = server.available(); 84 | 85 | // when the client sends the first byte, say hello: 86 | if (client) { 87 | if (!gotAMessage) { 88 | Serial.println("We have a new client"); 89 | client.println("Hello, client!"); 90 | gotAMessage = true; 91 | } 92 | 93 | // read the bytes incoming from the client: 94 | char thisChar = client.read(); 95 | // echo the bytes back to the client: 96 | server.write(thisChar); 97 | // echo the bytes to the server as well: 98 | Serial.print(thisChar); 99 | } 100 | } 101 | 102 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/WebClient/WebClient.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web client 3 | 4 | This sketch connects to a website (http://www.google.com) 5 | using an Arduino Wiznet Ethernet shield. 6 | 7 | Circuit: 8 | * Ethernet shield attached to pins 10, 11, 12, 13 9 | 10 | created 18 Dec 2009 11 | by David A. Mellis 12 | modified 9 Apr 2012 13 | by Tom Igoe, based on work by Adrian McEwen 14 | modified 12 Aug 2013 15 | by Soohwan Kim 16 | 17 | */ 18 | 19 | #include 20 | #include 21 | 22 | // Enter a MAC address for your controller below. 23 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 24 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 25 | ; 26 | #else 27 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 28 | #endif 29 | // if you don't want to use DNS (and reduce your sketch size) 30 | // use the numeric IP instead of the name for the server: 31 | //IPAddress server(74,125,232,128); // numeric IP for Google (no DNS) 32 | char server[] = "www.google.com"; // name address for Google (using DNS) 33 | 34 | // Set the static IP address to use if the DHCP fails to assign 35 | IPAddress ip(192,168,0,177); 36 | 37 | // Initialize the Ethernet client library 38 | // with the IP address and port of the server 39 | // that you want to connect to (port 80 is default for HTTP): 40 | EthernetClient client; 41 | 42 | void setup() { 43 | // Open serial communications and wait for port to open: 44 | Serial.begin(9600); 45 | while (!Serial) { 46 | ; // wait for serial port to connect. Needed for Leonardo only 47 | } 48 | 49 | // start the Ethernet connection: 50 | #if defined(WIZ550io_WITH_MACADDRESS) 51 | if (Ethernet.begin() == 0) { 52 | #else 53 | if (Ethernet.begin(mac) == 0) { 54 | #endif 55 | Serial.println("Failed to configure Ethernet using DHCP"); 56 | // no point in carrying on, so do nothing forevermore: 57 | // try to congifure using IP address instead of DHCP: 58 | #if defined(WIZ550io_WITH_MACADDRESS) 59 | Ethernet.begin(ip); 60 | #else 61 | Ethernet.begin(mac, ip); 62 | #endif 63 | } 64 | // give the Ethernet shield a second to initialize: 65 | delay(1000); 66 | Serial.println("connecting..."); 67 | 68 | // if you get a connection, report back via serial: 69 | if (client.connect(server, 80)) { 70 | Serial.println("connected"); 71 | // Make a HTTP request: 72 | client.println("GET /search?q=arduino HTTP/1.1"); 73 | client.println("Host: www.google.com"); 74 | client.println("Connection: close"); 75 | client.println(); 76 | } 77 | else { 78 | // if you didn't get a connection to the server: 79 | Serial.println("connection failed"); 80 | } 81 | } 82 | 83 | void loop() 84 | { 85 | // if there are incoming bytes available 86 | // from the server, read them and print them: 87 | if (client.available()) { 88 | char c = client.read(); 89 | Serial.print(c); 90 | } 91 | 92 | // if the server's disconnected, stop the client: 93 | if (!client.connected()) { 94 | Serial.println(); 95 | Serial.println("disconnecting."); 96 | client.stop(); 97 | 98 | // do nothing forevermore: 99 | while(true); 100 | } 101 | } 102 | 103 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/Twitter_Serial_GW/Twitter_Serial_GW.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Twitter Serial GW 3 | 4 | Sample : Serial port => Twitter gateway 5 | Run this program on Arduino and connect via some Serial port terminal software. 6 | Type a message and hit enter key, and it will be posted to Twitter. 7 | Arduino IDE Serial Monitor is not usable because it doesn't seem able to send Enter key code. 8 | 9 | Notice 10 | -The library uses this site as a proxy server for OAuth stuff. Your tweet may not be applied during maintenance of this site. 11 | -Please avoid sending more than 1 request per minute not to overload the server. 12 | -Twitter seems to reject repeated tweets with the same contenet (returns error 403). 13 | 14 | Reference & Lincense 15 | URL : http://arduino-tweet.appspot.com/ 16 | 17 | Circuit: 18 | * Ethernet shield attached to pins 10, 11, 12, 13 19 | 20 | created 2011 21 | by NeoCat - (ver1.3 - Support IDE 1.0) 22 | tested & 23 | modified 13 Aug 2013 24 | by Soohwan Kim - (ver1.3 - Support IDE 1.0.5) 25 | */ 26 | 27 | #include // needed in Arduino 0019 or later 28 | #include 29 | #include "./Twitter.h" // Twitter.h in library/Ethernet folder. @diffinsight 2013-08-19 30 | 31 | // The includion of EthernetDNS is not needed in Arduino IDE 1.0 or later. 32 | // Please uncomment below in Arduino IDE 0022 or earlier. 33 | //#include 34 | 35 | // Ethernet Shield Settings 36 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 37 | ; 38 | #else 39 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 40 | #endif 41 | 42 | // If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later). 43 | // fill in an available IP address on your network here, 44 | IPAddress ip(1,1,1,1); 45 | IPAddress gw(1,1,1,1); 46 | IPAddress snip(0,0,0,0); 47 | IPAddress dnsip(0,0,0,0); 48 | 49 | // Your Token to Tweet (get it from http://arduino-tweet.appspot.com/) 50 | //Twitter twitter("YOUR-TOKEN-HERE"); 51 | 52 | 53 | char msg[141] = ""; 54 | int len = 0; 55 | 56 | void setup() 57 | { 58 | delay(1000); 59 | // start the Ethernet connection: 60 | #if defined(WIZ550io_WITH_MACADDRESS) 61 | Ethernet.begin(ip, dnsip, gw,snip); 62 | #else 63 | Ethernet.begin(mac, ip, dnsip, gw,snip); 64 | #endif 65 | // or you can use DHCP for autoomatic IP address configuration. 66 | // start the Ethernet connection: 67 | //#if defined(WIZ550io_WITH_MACADDRESS) 68 | // Ethernet.begin(); 69 | //#else 70 | // Ethernet.begin(mac); 71 | //#endif 72 | 73 | 74 | Serial.begin(9600); 75 | Serial.print("> "); 76 | } 77 | 78 | void loop() 79 | { 80 | if (Serial.available() > 0) { 81 | char recv = msg[len++] = Serial.read(); 82 | if (recv == '\b' || recv == 127) { // Backspace 83 | if (len > 1) { 84 | len -= 2; 85 | Serial.print("\b \b"); 86 | } 87 | } 88 | else if (recv == '\r' || recv == '\n') { // send CR/LF to post 89 | Serial.print("\r\n"); 90 | len--; 91 | msg[len] = 0; 92 | if (len > 0) 93 | post(); 94 | len = 0; 95 | Serial.print("\r\n> "); 96 | } 97 | else if (len > 140) 98 | len = 140; 99 | else 100 | Serial.print(recv); 101 | } 102 | } 103 | 104 | void post() 105 | { 106 | Serial.println("connecting ..."); 107 | if (twitter.post(msg)) { 108 | int status = twitter.wait(); 109 | if (status == 200) { 110 | Serial.println("OK."); 111 | } else { 112 | Serial.print("failed : code "); 113 | Serial.println(status); 114 | } 115 | } else { 116 | Serial.println("connection failed."); 117 | } 118 | delay(1000); 119 | } 120 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/WebServer/WebServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Web Server 3 | 4 | A simple web server that shows the value of the analog input pins. 5 | using an Arduino Wiznet Ethernet shield. 6 | 7 | Circuit: 8 | * Ethernet shield attached to pins 10, 11, 12, 13 9 | * Analog inputs attached to pins A0 through A5 (optional) 10 | 11 | created 18 Dec 2009 12 | by David A. Mellis 13 | modified 9 Apr 2012 14 | by Tom Igoe 15 | modified 12 Aug 2013 16 | by Soohwan Kim 17 | 18 | */ 19 | 20 | #include 21 | #include 22 | 23 | // Enter a MAC address and IP address for your controller below. 24 | // The IP address will be dependent on your local network: 25 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 26 | ; 27 | #else 28 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 29 | #endif 30 | IPAddress ip(192,168,1,177); 31 | 32 | // Initialize the Ethernet server library 33 | // with the IP address and port you want to use 34 | // (port 80 is default for HTTP): 35 | EthernetServer server(80); 36 | 37 | void setup() { 38 | // Open serial communications and wait for port to open: 39 | Serial.begin(9600); 40 | while (!Serial) { 41 | ; // wait for serial port to connect. Needed for Leonardo only 42 | } 43 | 44 | 45 | // start the Ethernet connection and the server: 46 | #if defined(WIZ550io_WITH_MACADDRESS) 47 | Ethernet.begin(ip); 48 | #else 49 | Ethernet.begin(mac, ip); 50 | #endif 51 | server.begin(); 52 | Serial.print("server is at "); 53 | Serial.println(Ethernet.localIP()); 54 | } 55 | 56 | 57 | void loop() { 58 | // listen for incoming clients 59 | EthernetClient client = server.available(); 60 | if (client) { 61 | Serial.println("new client"); 62 | // an http request ends with a blank line 63 | boolean currentLineIsBlank = true; 64 | while (client.connected()) { 65 | if (client.available()) { 66 | char c = client.read(); 67 | Serial.write(c); 68 | // if you've gotten to the end of the line (received a newline 69 | // character) and the line is blank, the http request has ended, 70 | // so you can send a reply 71 | if (c == '\n' && currentLineIsBlank) { 72 | // send a standard http response header 73 | client.println("HTTP/1.1 200 OK"); 74 | client.println("Content-Type: text/html"); 75 | client.println("Connection: close"); // the connection will be closed after completion of the response 76 | client.println("Refresh: 5"); // refresh the page automatically every 5 sec 77 | client.println(); 78 | client.println(""); 79 | client.println(""); 80 | // output the value of each analog input pin 81 | for (int analogChannel = 0; analogChannel < 6; analogChannel++) { 82 | int sensorReading = analogRead(analogChannel); 83 | client.print("analog input "); 84 | client.print(analogChannel); 85 | client.print(" is "); 86 | client.print(sensorReading); 87 | client.println("
"); 88 | } 89 | client.println(""); 90 | break; 91 | } 92 | if (c == '\n') { 93 | // you're starting a new line 94 | currentLineIsBlank = true; 95 | } 96 | else if (c != '\r') { 97 | // you've gotten a character on the current line 98 | currentLineIsBlank = false; 99 | } 100 | } 101 | } 102 | // give the web browser time to receive the data 103 | delay(1); 104 | // close the connection: 105 | client.stop(); 106 | Serial.println("client disonnected"); 107 | } 108 | } 109 | 110 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | ################# 2 | ## Eclipse 3 | ################# 4 | 5 | *.pydevproject 6 | .project 7 | .metadata 8 | bin/ 9 | tmp/ 10 | *.tmp 11 | *.bak 12 | *.swp 13 | *~.nib 14 | local.properties 15 | .classpath 16 | .settings/ 17 | .loadpath 18 | 19 | # External tool builders 20 | .externalToolBuilders/ 21 | 22 | # Locally stored "Eclipse launch configurations" 23 | *.launch 24 | 25 | # CDT-specific 26 | .cproject 27 | 28 | # PDT-specific 29 | .buildpath 30 | 31 | 32 | ################# 33 | ## Visual Studio 34 | ################# 35 | 36 | ## Ignore Visual Studio temporary files, build results, and 37 | ## files generated by popular Visual Studio add-ons. 38 | 39 | # User-specific files 40 | *.suo 41 | *.user 42 | *.sln.docstates 43 | 44 | # Build results 45 | 46 | [Dd]ebug/ 47 | [Rr]elease/ 48 | x64/ 49 | build/ 50 | [Bb]in/ 51 | [Oo]bj/ 52 | 53 | # MSTest test Results 54 | [Tt]est[Rr]esult*/ 55 | [Bb]uild[Ll]og.* 56 | 57 | *_i.c 58 | *_p.c 59 | *.ilk 60 | *.meta 61 | *.obj 62 | *.pch 63 | *.pdb 64 | *.pgc 65 | *.pgd 66 | *.rsp 67 | *.sbr 68 | *.tlb 69 | *.tli 70 | *.tlh 71 | *.tmp 72 | *.tmp_proj 73 | *.log 74 | *.vspscc 75 | *.vssscc 76 | .builds 77 | *.pidb 78 | *.log 79 | *.scc 80 | 81 | # Visual C++ cache files 82 | ipch/ 83 | *.aps 84 | *.ncb 85 | *.opensdf 86 | *.sdf 87 | *.cachefile 88 | 89 | # Visual Studio profiler 90 | *.psess 91 | *.vsp 92 | *.vspx 93 | 94 | # Guidance Automation Toolkit 95 | *.gpState 96 | 97 | # ReSharper is a .NET coding add-in 98 | _ReSharper*/ 99 | *.[Rr]e[Ss]harper 100 | 101 | # TeamCity is a build add-in 102 | _TeamCity* 103 | 104 | # DotCover is a Code Coverage Tool 105 | *.dotCover 106 | 107 | # NCrunch 108 | *.ncrunch* 109 | .*crunch*.local.xml 110 | 111 | # Installshield output folder 112 | [Ee]xpress/ 113 | 114 | # DocProject is a documentation generator add-in 115 | DocProject/buildhelp/ 116 | DocProject/Help/*.HxT 117 | DocProject/Help/*.HxC 118 | DocProject/Help/*.hhc 119 | DocProject/Help/*.hhk 120 | DocProject/Help/*.hhp 121 | DocProject/Help/Html2 122 | DocProject/Help/html 123 | 124 | # Click-Once directory 125 | publish/ 126 | 127 | # Publish Web Output 128 | *.Publish.xml 129 | *.pubxml 130 | 131 | # NuGet Packages Directory 132 | ## TODO: If you have NuGet Package Restore enabled, uncomment the next line 133 | #packages/ 134 | 135 | # Windows Azure Build Output 136 | csx 137 | *.build.csdef 138 | 139 | # Windows Store app package directory 140 | AppPackages/ 141 | 142 | # Others 143 | sql/ 144 | *.Cache 145 | ClientBin/ 146 | [Ss]tyle[Cc]op.* 147 | ~$* 148 | *~ 149 | *.dbmdl 150 | *.[Pp]ublish.xml 151 | *.pfx 152 | *.publishsettings 153 | 154 | # RIA/Silverlight projects 155 | Generated_Code/ 156 | 157 | # Backup & report files from converting an old project file to a newer 158 | # Visual Studio version. Backup files are not needed, because we have git ;-) 159 | _UpgradeReport_Files/ 160 | Backup*/ 161 | UpgradeLog*.XML 162 | UpgradeLog*.htm 163 | 164 | # SQL Server files 165 | App_Data/*.mdf 166 | App_Data/*.ldf 167 | 168 | ############# 169 | ## Windows detritus 170 | ############# 171 | 172 | # Windows image file caches 173 | Thumbs.db 174 | ehthumbs.db 175 | 176 | # Folder config file 177 | Desktop.ini 178 | 179 | # Recycle Bin used on file shares 180 | $RECYCLE.BIN/ 181 | 182 | # Mac crap 183 | .DS_Store 184 | 185 | 186 | ############# 187 | ## Python 188 | ############# 189 | 190 | *.py[co] 191 | 192 | # Packages 193 | *.egg 194 | *.egg-info 195 | dist/ 196 | build/ 197 | eggs/ 198 | parts/ 199 | var/ 200 | sdist/ 201 | develop-eggs/ 202 | .installed.cfg 203 | 204 | # Installer logs 205 | pip-log.txt 206 | 207 | # Unit test / coverage reports 208 | .coverage 209 | .tox 210 | 211 | #Translations 212 | *.mo 213 | 214 | #Mr Developer 215 | .mr.developer.cfg 216 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/UDPSendReceiveString/UDPSendReceiveString.ino: -------------------------------------------------------------------------------- 1 | /* 2 | UDPSendReceive.pde: 3 | This sketch receives UDP message strings, prints them to the serial port 4 | and sends an "acknowledge" string back to the sender 5 | 6 | A Processing sketch is included at the end of file that can be used to send 7 | and received messages for testing with a computer. 8 | 9 | created 21 Aug 2010 10 | by Michael Margolis 11 | modified 12 Aug 2013 12 | by Soohwan Kim 13 | 14 | This code is in the public domain. 15 | */ 16 | 17 | 18 | #include // needed for Arduino versions later than 0018 19 | #include 20 | #include // UDP library from: bjoern@cs.stanford.edu 12/30/2008 21 | 22 | 23 | // Enter a MAC address and IP address for your controller below. 24 | // The IP address will be dependent on your local network: 25 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 26 | ; 27 | #else 28 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 29 | #endif 30 | IPAddress ip(192, 168, 1, 177); 31 | 32 | unsigned int localPort = 8888; // local port to listen on 33 | 34 | // buffers for receiving and sending data 35 | char packetBuffer[UDP_TX_PACKET_MAX_SIZE]; //buffer to hold incoming packet, 36 | char ReplyBuffer[] = "acknowledged"; // a string to send back 37 | 38 | // An EthernetUDP instance to let us send and receive packets over UDP 39 | EthernetUDP Udp; 40 | 41 | void setup() { 42 | // start the Ethernet and UDP: 43 | #if defined(WIZ550io_WITH_MACADDRESS) 44 | Ethernet.begin(ip); 45 | #else 46 | Ethernet.begin(mac,ip); 47 | #endif 48 | Udp.begin(localPort); 49 | 50 | Serial.begin(9600); 51 | } 52 | 53 | void loop() { 54 | // if there's data available, read a packet 55 | int packetSize = Udp.parsePacket(); 56 | if(packetSize) 57 | { 58 | Serial.print("Received packet of size "); 59 | Serial.println(packetSize); 60 | Serial.print("From "); 61 | IPAddress remote = Udp.remoteIP(); 62 | for (int i =0; i < 4; i++) 63 | { 64 | Serial.print(remote[i], DEC); 65 | if (i < 3) 66 | { 67 | Serial.print("."); 68 | } 69 | } 70 | Serial.print(", port "); 71 | Serial.println(Udp.remotePort()); 72 | 73 | // read the packet into packetBufffer 74 | Udp.read(packetBuffer,UDP_TX_PACKET_MAX_SIZE); 75 | Serial.println("Contents:"); 76 | Serial.println(packetBuffer); 77 | 78 | // send a reply, to the IP address and port that sent us the packet we received 79 | Udp.beginPacket(Udp.remoteIP(), Udp.remotePort()); 80 | Udp.write(ReplyBuffer); 81 | Udp.endPacket(); 82 | } 83 | delay(10); 84 | } 85 | 86 | 87 | /* 88 | Processing sketch to run with this example 89 | ===================================================== 90 | 91 | // Processing UDP example to send and receive string data from Arduino 92 | // press any key to send the "Hello Arduino" message 93 | 94 | 95 | import hypermedia.net.*; 96 | 97 | UDP udp; // define the UDP object 98 | 99 | 100 | void setup() { 101 | udp = new UDP( this, 6000 ); // create a new datagram connection on port 6000 102 | //udp.log( true ); // <-- printout the connection activity 103 | udp.listen( true ); // and wait for incoming message 104 | } 105 | 106 | void draw() 107 | { 108 | } 109 | 110 | void keyPressed() { 111 | String ip = "192.168.1.177"; // the remote IP address 112 | int port = 8888; // the destination port 113 | 114 | udp.send("Hello World", ip, port ); // the message to send 115 | 116 | } 117 | 118 | void receive( byte[] data ) { // <-- default handler 119 | //void receive( byte[] data, String ip, int port ) { // <-- extended handler 120 | 121 | for(int i=0; i < data.length; i++) 122 | print(char(data[i])); 123 | println(); 124 | } 125 | */ 126 | 127 | 128 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/WebClientRepeating/WebClientRepeating.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Repeating Web client 3 | 4 | This sketch connects to a a web server and makes a request 5 | using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or 6 | the Adafruit Ethernet shield, either one will work, as long as it's got 7 | a Wiznet Ethernet module on board. 8 | 9 | This example uses DNS, by assigning the Ethernet client with a MAC address, 10 | IP address, and DNS address. 11 | 12 | Circuit: 13 | * Ethernet shield attached to pins 10, 11, 12, 13 14 | 15 | created 19 Apr 2012 16 | by Tom Igoe 17 | modified 12 Aug 2013 18 | by Soohwan Kim 19 | 20 | http://arduino.cc/en/Tutorial/WebClientRepeating 21 | This code is in the public domain. 22 | 23 | */ 24 | 25 | #include 26 | #include 27 | 28 | // assign a MAC address for the ethernet controller. 29 | // fill in your address here: 30 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 31 | ; 32 | #else 33 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 34 | #endif 35 | // fill in an available IP address on your network here, 36 | // for manual configuration: 37 | // If you don't specify the IP address, DHCP is used(only in Arduino 1.0 or later). 38 | // fill in an available IP address on your network here, 39 | IPAddress ip(1,1,1,1); 40 | IPAddress gwip(1,1,1,1); 41 | IPAddress snip(1,1,1,1); 42 | IPAddress myDns(1,1,1,1); 43 | 44 | 45 | // initialize the library instance: 46 | EthernetClient client; 47 | 48 | char server[] = "www.arduino.cc"; 49 | 50 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds 51 | boolean lastConnected = false; // state of the connection last time through the main loop 52 | const unsigned long postingInterval = 60*1000; // delay between updates, in milliseconds 53 | 54 | 55 | void setup() { 56 | // start serial port: 57 | Serial.begin(9600); 58 | // give the ethernet module time to boot up: 59 | delay(1000); 60 | // start the Ethernet connection using a fixed IP address and DNS server: 61 | #if defined(WIZ550io_WITH_MACADDRESS) 62 | Ethernet.begin(ip, myDns, gwip, snip); 63 | #else 64 | Ethernet.begin(mac, ip, myDns, gwip, snip); 65 | #endif 66 | 67 | // print the Ethernet board/shield's IP address: 68 | Serial.print("My IP address: "); 69 | Serial.println(Ethernet.localIP()); 70 | } 71 | 72 | void loop() { 73 | // if there's incoming data from the net connection. 74 | // send it out the serial port. This is for debugging 75 | // purposes only: 76 | if (client.available()) { 77 | char c = client.read(); 78 | Serial.print(c); 79 | } 80 | 81 | // if there's no net connection, but there was one last time 82 | // through the loop, then stop the client: 83 | if (!client.connected() && lastConnected) { 84 | Serial.println(); 85 | Serial.println("disconnecting."); 86 | client.stop(); 87 | } 88 | 89 | // if you're not connected, and ten seconds have passed since 90 | // your last connection, then connect again and send data: 91 | if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { 92 | httpRequest(); 93 | } 94 | // store the state of the connection for next time through 95 | // the loop: 96 | lastConnected = client.connected(); 97 | } 98 | 99 | // this method makes a HTTP connection to the server: 100 | void httpRequest() { 101 | // if there's a successful connection: 102 | if (client.connect(server, 80)) { 103 | Serial.println("connecting..."); 104 | // send the HTTP PUT request: 105 | client.println("GET /latest.txt HTTP/1.1"); 106 | client.println("Host: www.arduino.cc"); 107 | client.println("User-Agent: arduino-ethernet"); 108 | client.println("Connection: close"); 109 | client.println(); 110 | 111 | // note the time that the connection was made: 112 | lastConnectionTime = millis(); 113 | } 114 | else { 115 | // if you couldn't make a connection: 116 | Serial.println("connection failed"); 117 | Serial.println("disconnecting."); 118 | client.stop(); 119 | } 120 | } 121 | 122 | 123 | 124 | 125 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/EthernetClient.cpp: -------------------------------------------------------------------------------- 1 | #include "w5100.h" 2 | #include "socket.h" 3 | 4 | extern "C" { 5 | #include "string.h" 6 | } 7 | 8 | #include "Arduino.h" 9 | 10 | #include "Ethernet.h" 11 | #include "EthernetClient.h" 12 | #include "EthernetServer.h" 13 | #include "Dns.h" 14 | 15 | uint16_t EthernetClient::_srcport = 1024; 16 | 17 | EthernetClient::EthernetClient() : _sock(MAX_SOCK_NUM) { 18 | } 19 | 20 | EthernetClient::EthernetClient(uint8_t sock) : _sock(sock) { 21 | } 22 | 23 | int EthernetClient::connect(const char* host, uint16_t port) { 24 | // Look up the host first 25 | int ret = 0; 26 | DNSClient dns; 27 | IPAddress remote_addr; 28 | 29 | dns.begin(Ethernet.dnsServerIP()); 30 | ret = dns.getHostByName(host, remote_addr); 31 | if (ret == 1) { 32 | return connect(remote_addr, port); 33 | } else { 34 | return ret; 35 | } 36 | } 37 | 38 | int EthernetClient::connect(IPAddress ip, uint16_t port) { 39 | if (_sock != MAX_SOCK_NUM) 40 | return 0; 41 | 42 | for (int i = 0; i < MAX_SOCK_NUM; i++) { 43 | uint8_t s = W5100.readSnSR(i); 44 | if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) { 45 | _sock = i; 46 | break; 47 | } 48 | } 49 | 50 | if (_sock == MAX_SOCK_NUM) 51 | return 0; 52 | 53 | _srcport++; 54 | if (_srcport == 0) _srcport = 1024; 55 | socket(_sock, SnMR::TCP, _srcport, 0); 56 | 57 | if (!::connect(_sock, rawIPAddress(ip), port)) { 58 | _sock = MAX_SOCK_NUM; 59 | return 0; 60 | } 61 | 62 | while (status() != SnSR::ESTABLISHED) { 63 | delay(1); 64 | if (status() == SnSR::CLOSED) { 65 | _sock = MAX_SOCK_NUM; 66 | return 0; 67 | } 68 | } 69 | 70 | return 1; 71 | } 72 | 73 | size_t EthernetClient::write(uint8_t b) { 74 | return write(&b, 1); 75 | } 76 | 77 | size_t EthernetClient::write(const uint8_t *buf, size_t size) { 78 | if (_sock == MAX_SOCK_NUM) { 79 | setWriteError(); 80 | return 0; 81 | } 82 | if (!send(_sock, buf, size)) { 83 | setWriteError(); 84 | return 0; 85 | } 86 | return size; 87 | } 88 | 89 | int EthernetClient::available() { 90 | if (_sock != MAX_SOCK_NUM) 91 | return W5100.getRXReceivedSize(_sock); 92 | return 0; 93 | } 94 | 95 | int EthernetClient::read() { 96 | uint8_t b; 97 | if ( recv(_sock, &b, 1) > 0 ) 98 | { 99 | // recv worked 100 | return b; 101 | } 102 | else 103 | { 104 | // No data available 105 | return -1; 106 | } 107 | } 108 | 109 | int EthernetClient::read(uint8_t *buf, size_t size) { 110 | return recv(_sock, buf, size); 111 | } 112 | 113 | int EthernetClient::peek() { 114 | uint8_t b; 115 | // Unlike recv, peek doesn't check to see if there's any data available, so we must 116 | if (!available()) 117 | return -1; 118 | ::peek(_sock, &b); 119 | return b; 120 | } 121 | 122 | void EthernetClient::flush() { 123 | while (available()) 124 | read(); 125 | } 126 | 127 | void EthernetClient::stop() { 128 | if (_sock == MAX_SOCK_NUM) 129 | return; 130 | 131 | // attempt to close the connection gracefully (send a FIN to other side) 132 | disconnect(_sock); 133 | unsigned long start = millis(); 134 | 135 | // wait a second for the connection to close 136 | while (status() != SnSR::CLOSED && millis() - start < 1000) 137 | delay(1); 138 | 139 | // if it hasn't closed, close it forcefully 140 | if (status() != SnSR::CLOSED) 141 | close(_sock); 142 | 143 | EthernetClass::_server_port[_sock] = 0; 144 | _sock = MAX_SOCK_NUM; 145 | } 146 | 147 | uint8_t EthernetClient::connected() { 148 | if (_sock == MAX_SOCK_NUM) return 0; 149 | 150 | uint8_t s = status(); 151 | return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT || 152 | (s == SnSR::CLOSE_WAIT && !available())); 153 | } 154 | 155 | uint8_t EthernetClient::status() { 156 | if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED; 157 | return W5100.readSnSR(_sock); 158 | } 159 | 160 | // the next function allows us to use the client returned by 161 | // EthernetServer::available() as the condition in an if-statement. 162 | 163 | EthernetClient::operator bool() { 164 | return _sock != MAX_SOCK_NUM; 165 | } 166 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/utility/w5500.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by WIZnet 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | #include "w5100.h" 15 | #if defined(W5500_ETHERNET_SHIELD) 16 | 17 | // W5500 controller instance 18 | W5500Class W5100; 19 | 20 | 21 | void W5500Class::init(void) 22 | { 23 | 24 | initSS(); 25 | delay(300); 26 | SPI.begin(); 27 | 28 | for (int i=0; i> 8); 101 | SPI.transfer(_addr & 0xFF); 102 | SPI.transfer(_cb); 103 | SPI.transfer(_data); 104 | resetSS(); 105 | return 1; 106 | } 107 | 108 | uint16_t W5500Class::write(uint16_t _addr, uint8_t _cb, const uint8_t *_buf, uint16_t _len) 109 | { 110 | setSS(); 111 | SPI.transfer(_addr >> 8); 112 | SPI.transfer(_addr & 0xFF); 113 | SPI.transfer(_cb); 114 | for (uint16_t i=0; i<_len; i++){ 115 | SPI.transfer(_buf[i]); 116 | } 117 | resetSS(); 118 | return _len; 119 | } 120 | 121 | uint8_t W5500Class::read(uint16_t _addr, uint8_t _cb) 122 | { 123 | setSS(); 124 | SPI.transfer(_addr >> 8); 125 | SPI.transfer(_addr & 0xFF); 126 | SPI.transfer(_cb); 127 | uint8_t _data = SPI.transfer(0); 128 | resetSS(); 129 | return _data; 130 | } 131 | 132 | uint16_t W5500Class::read(uint16_t _addr, uint8_t _cb, uint8_t *_buf, uint16_t _len) 133 | { 134 | setSS(); 135 | SPI.transfer(_addr >> 8); 136 | SPI.transfer(_addr & 0xFF); 137 | SPI.transfer(_cb); 138 | for (uint16_t i=0; i<_len; i++){ 139 | _buf[i] = SPI.transfer(0); 140 | } 141 | resetSS(); 142 | 143 | return _len; 144 | } 145 | 146 | void W5500Class::execCmdSn(SOCKET s, SockCMD _cmd) { 147 | // Send command to socket 148 | writeSnCR(s, _cmd); 149 | // Wait for command to complete 150 | while (readSnCR(s)) 151 | ; 152 | } 153 | #endif -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/EthernetClient.cpp: -------------------------------------------------------------------------------- 1 | #include "utility/w5100.h" 2 | #include "utility/socket.h" 3 | 4 | extern "C" { 5 | #include "string.h" 6 | } 7 | 8 | #include "Arduino.h" 9 | 10 | #include "Ethernet.h" 11 | #include "EthernetClient.h" 12 | #include "EthernetServer.h" 13 | #include "Dns.h" 14 | 15 | uint16_t EthernetClient::_srcport = 1024; 16 | 17 | EthernetClient::EthernetClient() : _sock(MAX_SOCK_NUM) { 18 | } 19 | 20 | EthernetClient::EthernetClient(uint8_t sock) : _sock(sock) { 21 | } 22 | 23 | int EthernetClient::connect(const char* host, uint16_t port) { 24 | // Look up the host first 25 | int ret = 0; 26 | DNSClient dns; 27 | IPAddress remote_addr; 28 | 29 | dns.begin(Ethernet.dnsServerIP()); 30 | ret = dns.getHostByName(host, remote_addr); 31 | if (ret == 1) { 32 | return connect(remote_addr, port); 33 | } else { 34 | return ret; 35 | } 36 | } 37 | 38 | int EthernetClient::connect(IPAddress ip, uint16_t port) { 39 | if (_sock != MAX_SOCK_NUM) 40 | return 0; 41 | 42 | for (int i = 0; i < MAX_SOCK_NUM; i++) { 43 | uint8_t s = W5100.readSnSR(i); 44 | if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT || s == SnSR::CLOSE_WAIT) { 45 | _sock = i; 46 | break; 47 | } 48 | } 49 | 50 | if (_sock == MAX_SOCK_NUM) 51 | return 0; 52 | 53 | _srcport++; 54 | if (_srcport == 0) _srcport = 1024; 55 | socket(_sock, SnMR::TCP, _srcport, 0); 56 | 57 | if (!::connect(_sock, rawIPAddress(ip), port)) { 58 | _sock = MAX_SOCK_NUM; 59 | return 0; 60 | } 61 | 62 | while (status() != SnSR::ESTABLISHED) { 63 | delay(1); 64 | if (status() == SnSR::CLOSED) { 65 | _sock = MAX_SOCK_NUM; 66 | return 0; 67 | } 68 | } 69 | 70 | return 1; 71 | } 72 | 73 | size_t EthernetClient::write(uint8_t b) { 74 | return write(&b, 1); 75 | } 76 | 77 | size_t EthernetClient::write(const uint8_t *buf, size_t size) { 78 | if (_sock == MAX_SOCK_NUM) { 79 | setWriteError(); 80 | return 0; 81 | } 82 | if (!send(_sock, buf, size)) { 83 | setWriteError(); 84 | return 0; 85 | } 86 | return size; 87 | } 88 | 89 | int EthernetClient::available() { 90 | if (_sock != MAX_SOCK_NUM) 91 | return W5100.getRXReceivedSize(_sock); 92 | return 0; 93 | } 94 | 95 | int EthernetClient::read() { 96 | uint8_t b; 97 | if ( recv(_sock, &b, 1) > 0 ) 98 | { 99 | // recv worked 100 | return b; 101 | } 102 | else 103 | { 104 | // No data available 105 | return -1; 106 | } 107 | } 108 | 109 | int EthernetClient::read(uint8_t *buf, size_t size) { 110 | return recv(_sock, buf, size); 111 | } 112 | 113 | int EthernetClient::peek() { 114 | uint8_t b; 115 | // Unlike recv, peek doesn't check to see if there's any data available, so we must 116 | if (!available()) 117 | return -1; 118 | ::peek(_sock, &b); 119 | return b; 120 | } 121 | 122 | void EthernetClient::flush() { 123 | ::flush(_sock); 124 | } 125 | 126 | void EthernetClient::stop() { 127 | if (_sock == MAX_SOCK_NUM) 128 | return; 129 | 130 | // attempt to close the connection gracefully (send a FIN to other side) 131 | disconnect(_sock); 132 | unsigned long start = millis(); 133 | 134 | // wait a second for the connection to close 135 | while (status() != SnSR::CLOSED && millis() - start < 1000) 136 | delay(1); 137 | 138 | // if it hasn't closed, close it forcefully 139 | if (status() != SnSR::CLOSED) 140 | close(_sock); 141 | 142 | EthernetClass::_server_port[_sock] = 0; 143 | _sock = MAX_SOCK_NUM; 144 | } 145 | 146 | uint8_t EthernetClient::connected() { 147 | if (_sock == MAX_SOCK_NUM) return 0; 148 | 149 | uint8_t s = status(); 150 | return !(s == SnSR::LISTEN || s == SnSR::CLOSED || s == SnSR::FIN_WAIT || 151 | (s == SnSR::CLOSE_WAIT && !available())); 152 | } 153 | 154 | uint8_t EthernetClient::status() { 155 | if (_sock == MAX_SOCK_NUM) return SnSR::CLOSED; 156 | return W5100.readSnSR(_sock); 157 | } 158 | 159 | // the next function allows us to use the client returned by 160 | // EthernetServer::available() as the condition in an if-statement. 161 | 162 | EthernetClient::operator bool() { 163 | return _sock != MAX_SOCK_NUM; 164 | } 165 | 166 | bool EthernetClient::operator==(const EthernetClient& rhs) { 167 | return _sock == rhs._sock && _sock != MAX_SOCK_NUM && rhs._sock != MAX_SOCK_NUM; 168 | } 169 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/utility/w5100.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by Cristian Maglie 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | 14 | 15 | #include "w5100.h" 16 | 17 | #if defined(W5100_ETHERNET_SHIELD) 18 | 19 | // W5100 controller instance 20 | W5100Class W5100; 21 | 22 | #define TX_RX_MAX_BUF_SIZE 2048 23 | #define TX_BUF 0x1100 24 | #define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE) 25 | 26 | #define TXBUF_BASE 0x4000 27 | #define RXBUF_BASE 0x6000 28 | 29 | void W5100Class::init(void) 30 | { 31 | delay(300); 32 | 33 | SPI.begin(); 34 | initSS(); 35 | 36 | writeMR(1< SSIZE) 85 | { 86 | // Wrap around circular buffer 87 | uint16_t size = SSIZE - offset; 88 | write(dstAddr, data, size); 89 | write(SBASE[s], data + size, len - size); 90 | } 91 | else { 92 | write(dstAddr, data, len); 93 | } 94 | 95 | ptr += len; 96 | writeSnTX_WR(s, ptr); 97 | } 98 | 99 | 100 | void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek) 101 | { 102 | uint16_t ptr; 103 | ptr = readSnRX_RD(s); 104 | read_data(s, (uint8_t *)ptr, data, len); 105 | if (!peek) 106 | { 107 | ptr += len; 108 | writeSnRX_RD(s, ptr); 109 | } 110 | } 111 | 112 | void W5100Class::read_data(SOCKET s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len) 113 | { 114 | uint16_t size; 115 | uint16_t src_mask; 116 | uint16_t src_ptr; 117 | 118 | src_mask = (uint16_t)src & RMASK; 119 | src_ptr = RBASE[s] + src_mask; 120 | 121 | if( (src_mask + len) > RSIZE ) 122 | { 123 | size = RSIZE - src_mask; 124 | read(src_ptr, (uint8_t *)dst, size); 125 | dst += size; 126 | read(RBASE[s], (uint8_t *) dst, len - size); 127 | } 128 | else 129 | read(src_ptr, (uint8_t *) dst, len); 130 | } 131 | 132 | 133 | uint8_t W5100Class::write(uint16_t _addr, uint8_t _data) 134 | { 135 | setSS(); 136 | SPI.transfer(0xF0); 137 | SPI.transfer(_addr >> 8); 138 | SPI.transfer(_addr & 0xFF); 139 | SPI.transfer(_data); 140 | resetSS(); 141 | return 1; 142 | } 143 | 144 | uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len) 145 | { 146 | for (uint16_t i=0; i<_len; i++) 147 | { 148 | setSS(); 149 | SPI.transfer(0xF0); 150 | SPI.transfer(_addr >> 8); 151 | SPI.transfer(_addr & 0xFF); 152 | _addr++; 153 | SPI.transfer(_buf[i]); 154 | resetSS(); 155 | } 156 | return _len; 157 | } 158 | 159 | uint8_t W5100Class::read(uint16_t _addr) 160 | { 161 | setSS(); 162 | SPI.transfer(0x0F); 163 | SPI.transfer(_addr >> 8); 164 | SPI.transfer(_addr & 0xFF); 165 | uint8_t _data = SPI.transfer(0); 166 | resetSS(); 167 | return _data; 168 | } 169 | 170 | uint16_t W5100Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len) 171 | { 172 | for (uint16_t i=0; i<_len; i++) 173 | { 174 | setSS(); 175 | SPI.transfer(0x0F); 176 | SPI.transfer(_addr >> 8); 177 | SPI.transfer(_addr & 0xFF); 178 | _addr++; 179 | _buf[i] = SPI.transfer(0); 180 | resetSS(); 181 | } 182 | return _len; 183 | } 184 | 185 | void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) { 186 | // Send command to socket 187 | writeSnCR(s, _cmd); 188 | // Wait for command to complete 189 | while (readSnCR(s)) 190 | ; 191 | } 192 | 193 | #endif -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/Dhcp.h: -------------------------------------------------------------------------------- 1 | // DHCP Library v0.3 - April 25, 2009 2 | // Author: Jordan Terrell - blog.jordanterrell.com 3 | 4 | #ifndef Dhcp_h 5 | #define Dhcp_h 6 | 7 | #include "EthernetUdp.h" 8 | 9 | /* DHCP state machine. */ 10 | #define STATE_DHCP_START 0 11 | #define STATE_DHCP_DISCOVER 1 12 | #define STATE_DHCP_REQUEST 2 13 | #define STATE_DHCP_LEASED 3 14 | #define STATE_DHCP_REREQUEST 4 15 | #define STATE_DHCP_RELEASE 5 16 | 17 | #define DHCP_FLAGSBROADCAST 0x8000 18 | 19 | /* UDP port numbers for DHCP */ 20 | #define DHCP_SERVER_PORT 67 /* from server to client */ 21 | #define DHCP_CLIENT_PORT 68 /* from client to server */ 22 | 23 | /* DHCP message OP code */ 24 | #define DHCP_BOOTREQUEST 1 25 | #define DHCP_BOOTREPLY 2 26 | 27 | /* DHCP message type */ 28 | #define DHCP_DISCOVER 1 29 | #define DHCP_OFFER 2 30 | #define DHCP_REQUEST 3 31 | #define DHCP_DECLINE 4 32 | #define DHCP_ACK 5 33 | #define DHCP_NAK 6 34 | #define DHCP_RELEASE 7 35 | #define DHCP_INFORM 8 36 | 37 | #define DHCP_HTYPE10MB 1 38 | #define DHCP_HTYPE100MB 2 39 | 40 | #define DHCP_HLENETHERNET 6 41 | #define DHCP_HOPS 0 42 | #define DHCP_SECS 0 43 | 44 | #define MAGIC_COOKIE 0x63825363 45 | #define MAX_DHCP_OPT 16 46 | 47 | #define HOST_NAME "WIZnet" 48 | #define DEFAULT_LEASE (900) //default lease time in seconds 49 | 50 | #define DHCP_CHECK_NONE (0) 51 | #define DHCP_CHECK_RENEW_FAIL (1) 52 | #define DHCP_CHECK_RENEW_OK (2) 53 | #define DHCP_CHECK_REBIND_FAIL (3) 54 | #define DHCP_CHECK_REBIND_OK (4) 55 | 56 | enum 57 | { 58 | padOption = 0, 59 | subnetMask = 1, 60 | timerOffset = 2, 61 | routersOnSubnet = 3, 62 | /* timeServer = 4, 63 | nameServer = 5,*/ 64 | dns = 6, 65 | /*logServer = 7, 66 | cookieServer = 8, 67 | lprServer = 9, 68 | impressServer = 10, 69 | resourceLocationServer = 11,*/ 70 | hostName = 12, 71 | /*bootFileSize = 13, 72 | meritDumpFile = 14,*/ 73 | domainName = 15, 74 | /*swapServer = 16, 75 | rootPath = 17, 76 | extentionsPath = 18, 77 | IPforwarding = 19, 78 | nonLocalSourceRouting = 20, 79 | policyFilter = 21, 80 | maxDgramReasmSize = 22, 81 | defaultIPTTL = 23, 82 | pathMTUagingTimeout = 24, 83 | pathMTUplateauTable = 25, 84 | ifMTU = 26, 85 | allSubnetsLocal = 27, 86 | broadcastAddr = 28, 87 | performMaskDiscovery = 29, 88 | maskSupplier = 30, 89 | performRouterDiscovery = 31, 90 | routerSolicitationAddr = 32, 91 | staticRoute = 33, 92 | trailerEncapsulation = 34, 93 | arpCacheTimeout = 35, 94 | ethernetEncapsulation = 36, 95 | tcpDefaultTTL = 37, 96 | tcpKeepaliveInterval = 38, 97 | tcpKeepaliveGarbage = 39, 98 | nisDomainName = 40, 99 | nisServers = 41, 100 | ntpServers = 42, 101 | vendorSpecificInfo = 43, 102 | netBIOSnameServer = 44, 103 | netBIOSdgramDistServer = 45, 104 | netBIOSnodeType = 46, 105 | netBIOSscope = 47, 106 | xFontServer = 48, 107 | xDisplayManager = 49,*/ 108 | dhcpRequestedIPaddr = 50, 109 | dhcpIPaddrLeaseTime = 51, 110 | /*dhcpOptionOverload = 52,*/ 111 | dhcpMessageType = 53, 112 | dhcpServerIdentifier = 54, 113 | dhcpParamRequest = 55, 114 | /*dhcpMsg = 56, 115 | dhcpMaxMsgSize = 57,*/ 116 | dhcpT1value = 58, 117 | dhcpT2value = 59, 118 | /*dhcpClassIdentifier = 60,*/ 119 | dhcpClientIdentifier = 61, 120 | endOption = 255 121 | }; 122 | 123 | typedef struct _RIP_MSG_FIXED 124 | { 125 | uint8_t op; 126 | uint8_t htype; 127 | uint8_t hlen; 128 | uint8_t hops; 129 | uint32_t xid; 130 | uint16_t secs; 131 | uint16_t flags; 132 | uint8_t ciaddr[4]; 133 | uint8_t yiaddr[4]; 134 | uint8_t siaddr[4]; 135 | uint8_t giaddr[4]; 136 | uint8_t chaddr[6]; 137 | }RIP_MSG_FIXED; 138 | 139 | class DhcpClass { 140 | private: 141 | uint32_t _dhcpInitialTransactionId; 142 | uint32_t _dhcpTransactionId; 143 | uint8_t _dhcpMacAddr[6]; 144 | uint8_t _dhcpLocalIp[4]; 145 | uint8_t _dhcpSubnetMask[4]; 146 | uint8_t _dhcpGatewayIp[4]; 147 | uint8_t _dhcpDhcpServerIp[4]; 148 | uint8_t _dhcpDnsServerIp[4]; 149 | uint32_t _dhcpLeaseTime; 150 | uint32_t _dhcpT1, _dhcpT2; 151 | signed long _renewInSec; 152 | signed long _rebindInSec; 153 | signed long _lastCheck; 154 | unsigned long _timeout; 155 | unsigned long _responseTimeout; 156 | unsigned long _secTimeout; 157 | uint8_t _dhcp_state; 158 | EthernetUDP _dhcpUdpSocket; 159 | 160 | int request_DHCP_lease(); 161 | void reset_DHCP_lease(); 162 | void presend_DHCP(); 163 | void send_DHCP_MESSAGE(uint8_t, uint16_t); 164 | void printByte(char *, uint8_t); 165 | 166 | uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId); 167 | public: 168 | IPAddress getLocalIp(); 169 | IPAddress getSubnetMask(); 170 | IPAddress getGatewayIp(); 171 | IPAddress getDhcpServerIp(); 172 | IPAddress getDnsServerIp(); 173 | 174 | int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); 175 | int checkLease(); 176 | }; 177 | 178 | #endif 179 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/Dhcp.h: -------------------------------------------------------------------------------- 1 | // DHCP Library v0.3 - April 25, 2009 2 | // Author: Jordan Terrell - blog.jordanterrell.com 3 | 4 | #ifndef Dhcp_h 5 | #define Dhcp_h 6 | 7 | #include "EthernetUdp.h" 8 | 9 | /* DHCP state machine. */ 10 | #define STATE_DHCP_START 0 11 | #define STATE_DHCP_DISCOVER 1 12 | #define STATE_DHCP_REQUEST 2 13 | #define STATE_DHCP_LEASED 3 14 | #define STATE_DHCP_REREQUEST 4 15 | #define STATE_DHCP_RELEASE 5 16 | 17 | #define DHCP_FLAGSBROADCAST 0x8000 18 | 19 | /* UDP port numbers for DHCP */ 20 | #define DHCP_SERVER_PORT 67 /* from server to client */ 21 | #define DHCP_CLIENT_PORT 68 /* from client to server */ 22 | 23 | /* DHCP message OP code */ 24 | #define DHCP_BOOTREQUEST 1 25 | #define DHCP_BOOTREPLY 2 26 | 27 | /* DHCP message type */ 28 | #define DHCP_DISCOVER 1 29 | #define DHCP_OFFER 2 30 | #define DHCP_REQUEST 3 31 | #define DHCP_DECLINE 4 32 | #define DHCP_ACK 5 33 | #define DHCP_NAK 6 34 | #define DHCP_RELEASE 7 35 | #define DHCP_INFORM 8 36 | 37 | #define DHCP_HTYPE10MB 1 38 | #define DHCP_HTYPE100MB 2 39 | 40 | #define DHCP_HLENETHERNET 6 41 | #define DHCP_HOPS 0 42 | #define DHCP_SECS 0 43 | 44 | #define MAGIC_COOKIE 0x63825363 45 | #define MAX_DHCP_OPT 16 46 | 47 | #define HOST_NAME "WIZnet" 48 | #define DEFAULT_LEASE (900) //default lease time in seconds 49 | 50 | #define DHCP_CHECK_NONE (0) 51 | #define DHCP_CHECK_RENEW_FAIL (1) 52 | #define DHCP_CHECK_RENEW_OK (2) 53 | #define DHCP_CHECK_REBIND_FAIL (3) 54 | #define DHCP_CHECK_REBIND_OK (4) 55 | 56 | enum 57 | { 58 | padOption = 0, 59 | subnetMask = 1, 60 | timerOffset = 2, 61 | routersOnSubnet = 3, 62 | /* timeServer = 4, 63 | nameServer = 5,*/ 64 | dns = 6, 65 | /*logServer = 7, 66 | cookieServer = 8, 67 | lprServer = 9, 68 | impressServer = 10, 69 | resourceLocationServer = 11,*/ 70 | hostName = 12, 71 | /*bootFileSize = 13, 72 | meritDumpFile = 14,*/ 73 | domainName = 15, 74 | /*swapServer = 16, 75 | rootPath = 17, 76 | extentionsPath = 18, 77 | IPforwarding = 19, 78 | nonLocalSourceRouting = 20, 79 | policyFilter = 21, 80 | maxDgramReasmSize = 22, 81 | defaultIPTTL = 23, 82 | pathMTUagingTimeout = 24, 83 | pathMTUplateauTable = 25, 84 | ifMTU = 26, 85 | allSubnetsLocal = 27, 86 | broadcastAddr = 28, 87 | performMaskDiscovery = 29, 88 | maskSupplier = 30, 89 | performRouterDiscovery = 31, 90 | routerSolicitationAddr = 32, 91 | staticRoute = 33, 92 | trailerEncapsulation = 34, 93 | arpCacheTimeout = 35, 94 | ethernetEncapsulation = 36, 95 | tcpDefaultTTL = 37, 96 | tcpKeepaliveInterval = 38, 97 | tcpKeepaliveGarbage = 39, 98 | nisDomainName = 40, 99 | nisServers = 41, 100 | ntpServers = 42, 101 | vendorSpecificInfo = 43, 102 | netBIOSnameServer = 44, 103 | netBIOSdgramDistServer = 45, 104 | netBIOSnodeType = 46, 105 | netBIOSscope = 47, 106 | xFontServer = 48, 107 | xDisplayManager = 49,*/ 108 | dhcpRequestedIPaddr = 50, 109 | dhcpIPaddrLeaseTime = 51, 110 | /*dhcpOptionOverload = 52,*/ 111 | dhcpMessageType = 53, 112 | dhcpServerIdentifier = 54, 113 | dhcpParamRequest = 55, 114 | /*dhcpMsg = 56, 115 | dhcpMaxMsgSize = 57,*/ 116 | dhcpT1value = 58, 117 | dhcpT2value = 59, 118 | /*dhcpClassIdentifier = 60,*/ 119 | dhcpClientIdentifier = 61, 120 | endOption = 255 121 | }; 122 | 123 | typedef struct _RIP_MSG_FIXED 124 | { 125 | uint8_t op; 126 | uint8_t htype; 127 | uint8_t hlen; 128 | uint8_t hops; 129 | uint32_t xid; 130 | uint16_t secs; 131 | uint16_t flags; 132 | uint8_t ciaddr[4]; 133 | uint8_t yiaddr[4]; 134 | uint8_t siaddr[4]; 135 | uint8_t giaddr[4]; 136 | uint8_t chaddr[6]; 137 | }RIP_MSG_FIXED; 138 | 139 | class DhcpClass { 140 | private: 141 | uint32_t _dhcpInitialTransactionId; 142 | uint32_t _dhcpTransactionId; 143 | uint8_t _dhcpMacAddr[6]; 144 | uint8_t _dhcpLocalIp[4]; 145 | uint8_t _dhcpSubnetMask[4]; 146 | uint8_t _dhcpGatewayIp[4]; 147 | uint8_t _dhcpDhcpServerIp[4]; 148 | uint8_t _dhcpDnsServerIp[4]; 149 | uint32_t _dhcpLeaseTime; 150 | uint32_t _dhcpT1, _dhcpT2; 151 | signed long _renewInSec; 152 | signed long _rebindInSec; 153 | signed long _lastCheck; 154 | unsigned long _timeout; 155 | unsigned long _responseTimeout; 156 | unsigned long _secTimeout; 157 | uint8_t _dhcp_state; 158 | EthernetUDP _dhcpUdpSocket; 159 | 160 | int request_DHCP_lease(); 161 | void reset_DHCP_lease(); 162 | void presend_DHCP(); 163 | void send_DHCP_MESSAGE(uint8_t, uint16_t); 164 | void printByte(char *, uint8_t); 165 | 166 | uint8_t parseDHCPResponse(unsigned long responseTimeout, uint32_t& transactionId); 167 | public: 168 | IPAddress getLocalIp(); 169 | IPAddress getSubnetMask(); 170 | IPAddress getGatewayIp(); 171 | IPAddress getDhcpServerIp(); 172 | IPAddress getDnsServerIp(); 173 | 174 | int beginWithDHCP(uint8_t *, unsigned long timeout = 60000, unsigned long responseTimeout = 4000); 175 | int checkLease(); 176 | }; 177 | 178 | #endif 179 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/EthernetUdp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield. 3 | * This version only offers minimal wrapping of socket.c/socket.h 4 | * Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 5 | * 6 | * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) 7 | * 1) UDP does not guarantee the order in which assembled UDP packets are received. This 8 | * might not happen often in practice, but in larger network topologies, a UDP 9 | * packet can be received out of sequence. 10 | * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being 11 | * aware of it. Again, this may not be a concern in practice on small local networks. 12 | * For more information, see http://www.cafeaulait.org/course/week12/35.html 13 | * 14 | * MIT License: 15 | * Copyright (c) 2008 Bjoern Hartmann 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in 24 | * all copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 32 | * THE SOFTWARE. 33 | * 34 | * bjoern@cs.stanford.edu 12/30/2008 35 | */ 36 | 37 | #ifndef ethernetudp_h 38 | #define ethernetudp_h 39 | 40 | #include 41 | 42 | #define UDP_TX_PACKET_MAX_SIZE 24 43 | 44 | class EthernetUDP : public UDP { 45 | private: 46 | uint8_t _sock; // socket ID for Wiz5100 47 | uint16_t _port; // local port to listen on 48 | IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed 49 | uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed 50 | uint16_t _offset; // offset into the packet being sent 51 | uint16_t _remaining; // remaining bytes of incoming packet yet to be processed 52 | 53 | public: 54 | EthernetUDP(); // Constructor 55 | virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use 56 | virtual void stop(); // Finish with the UDP socket 57 | 58 | // Sending UDP packets 59 | 60 | // Start building up a packet to send to the remote host specific in ip and port 61 | // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port 62 | virtual int beginPacket(IPAddress ip, uint16_t port); 63 | // Start building up a packet to send to the remote host specific in host and port 64 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port 65 | virtual int beginPacket(const char *host, uint16_t port); 66 | // Finish off this packet and send it 67 | // Returns 1 if the packet was sent successfully, 0 if there was an error 68 | virtual int endPacket(); 69 | // Write a single byte into the packet 70 | virtual size_t write(uint8_t); 71 | // Write size bytes from buffer into the packet 72 | virtual size_t write(const uint8_t *buffer, size_t size); 73 | 74 | using Print::write; 75 | 76 | // Start processing the next available incoming packet 77 | // Returns the size of the packet in bytes, or 0 if no packets are available 78 | virtual int parsePacket(); 79 | // Number of bytes remaining in the current packet 80 | virtual int available(); 81 | // Read a single byte from the current packet 82 | virtual int read(); 83 | // Read up to len bytes from the current packet and place them into buffer 84 | // Returns the number of bytes read, or 0 if none are available 85 | virtual int read(unsigned char* buffer, size_t len); 86 | // Read up to len characters from the current packet and place them into buffer 87 | // Returns the number of characters read, or 0 if none are available 88 | virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); }; 89 | // Return the next byte from the current packet without moving on to the next byte 90 | virtual int peek(); 91 | virtual void flush(); // Finish reading the current packet 92 | 93 | // Return the IP address of the host who sent the current incoming packet 94 | virtual IPAddress remoteIP() { return _remoteIP; }; 95 | // Return the port of the host who sent the current incoming packet 96 | virtual uint16_t remotePort() { return _remotePort; }; 97 | }; 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/EthernetUdp.h: -------------------------------------------------------------------------------- 1 | /* 2 | * Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield. 3 | * This version only offers minimal wrapping of socket.c/socket.h 4 | * Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 5 | * 6 | * NOTE: UDP is fast, but has some important limitations (thanks to Warren Gray for mentioning these) 7 | * 1) UDP does not guarantee the order in which assembled UDP packets are received. This 8 | * might not happen often in practice, but in larger network topologies, a UDP 9 | * packet can be received out of sequence. 10 | * 2) UDP does not guard against lost packets - so packets *can* disappear without the sender being 11 | * aware of it. Again, this may not be a concern in practice on small local networks. 12 | * For more information, see http://www.cafeaulait.org/course/week12/35.html 13 | * 14 | * MIT License: 15 | * Copyright (c) 2008 Bjoern Hartmann 16 | * Permission is hereby granted, free of charge, to any person obtaining a copy 17 | * of this software and associated documentation files (the "Software"), to deal 18 | * in the Software without restriction, including without limitation the rights 19 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 20 | * copies of the Software, and to permit persons to whom the Software is 21 | * furnished to do so, subject to the following conditions: 22 | * 23 | * The above copyright notice and this permission notice shall be included in 24 | * all copies or substantial portions of the Software. 25 | * 26 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 27 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 28 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 29 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 30 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 31 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 32 | * THE SOFTWARE. 33 | * 34 | * bjoern@cs.stanford.edu 12/30/2008 35 | */ 36 | 37 | #ifndef ethernetudp_h 38 | #define ethernetudp_h 39 | 40 | #include 41 | 42 | #define UDP_TX_PACKET_MAX_SIZE 24 43 | 44 | class EthernetUDP : public UDP { 45 | private: 46 | uint8_t _sock; // socket ID for Wiz5100 47 | uint16_t _port; // local port to listen on 48 | IPAddress _remoteIP; // remote IP address for the incoming packet whilst it's being processed 49 | uint16_t _remotePort; // remote port for the incoming packet whilst it's being processed 50 | uint16_t _offset; // offset into the packet being sent 51 | uint16_t _remaining; // remaining bytes of incoming packet yet to be processed 52 | 53 | public: 54 | EthernetUDP(); // Constructor 55 | virtual uint8_t begin(uint16_t); // initialize, start listening on specified port. Returns 1 if successful, 0 if there are no sockets available to use 56 | virtual void stop(); // Finish with the UDP socket 57 | 58 | // Sending UDP packets 59 | 60 | // Start building up a packet to send to the remote host specific in ip and port 61 | // Returns 1 if successful, 0 if there was a problem with the supplied IP address or port 62 | virtual int beginPacket(IPAddress ip, uint16_t port); 63 | // Start building up a packet to send to the remote host specific in host and port 64 | // Returns 1 if successful, 0 if there was a problem resolving the hostname or port 65 | virtual int beginPacket(const char *host, uint16_t port); 66 | // Finish off this packet and send it 67 | // Returns 1 if the packet was sent successfully, 0 if there was an error 68 | virtual int endPacket(); 69 | // Write a single byte into the packet 70 | virtual size_t write(uint8_t); 71 | // Write size bytes from buffer into the packet 72 | virtual size_t write(const uint8_t *buffer, size_t size); 73 | 74 | using Print::write; 75 | 76 | // Start processing the next available incoming packet 77 | // Returns the size of the packet in bytes, or 0 if no packets are available 78 | virtual int parsePacket(); 79 | // Number of bytes remaining in the current packet 80 | virtual int available(); 81 | // Read a single byte from the current packet 82 | virtual int read(); 83 | // Read up to len bytes from the current packet and place them into buffer 84 | // Returns the number of bytes read, or 0 if none are available 85 | virtual int read(unsigned char* buffer, size_t len); 86 | // Read up to len characters from the current packet and place them into buffer 87 | // Returns the number of characters read, or 0 if none are available 88 | virtual int read(char* buffer, size_t len) { return read((unsigned char*)buffer, len); }; 89 | // Return the next byte from the current packet without moving on to the next byte 90 | virtual int peek(); 91 | virtual void flush(); // Finish reading the current packet 92 | 93 | // Return the IP address of the host who sent the current incoming packet 94 | virtual IPAddress remoteIP() { return _remoteIP; }; 95 | // Return the port of the host who sent the current incoming packet 96 | virtual uint16_t remotePort() { return _remotePort; }; 97 | }; 98 | 99 | #endif 100 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/utility/w5200.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 by WIZnet 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #include 11 | #include 12 | #include 13 | #include "w5100.h" 14 | 15 | #if defined(W5200_ETHERNET_SHIELD) 16 | // W5200 controller instance 17 | W5200Class W5100; 18 | 19 | #define TX_RX_MAX_BUF_SIZE 2048 20 | #define TX_BUF 0x1100 21 | #define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE) 22 | 23 | 24 | #define TXBUF_BASE 0x8000 25 | #define RXBUF_BASE 0xC000 26 | 27 | void W5200Class::init(void) 28 | { 29 | delay(300); 30 | 31 | SPI.begin(); 32 | initSS(); 33 | 34 | writeMR(1< SSIZE) 85 | { 86 | // Wrap around circular buffer 87 | uint16_t size = SSIZE - offset; 88 | write(dstAddr, data, size); 89 | write(SBASE[s], data + size, len - size); 90 | } 91 | else { 92 | write(dstAddr, data, len); 93 | } 94 | 95 | ptr += len; 96 | writeSnTX_WR(s, ptr); 97 | } 98 | 99 | 100 | void W5200Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek) 101 | { 102 | uint16_t ptr; 103 | ptr = readSnRX_RD(s); 104 | read_data(s, (uint8_t *)ptr, data, len); 105 | if (!peek) 106 | { 107 | ptr += len; 108 | writeSnRX_RD(s, ptr); 109 | } 110 | } 111 | 112 | void W5200Class::read_data(SOCKET s, volatile uint8_t *src, volatile uint8_t *dst, uint16_t len) 113 | { 114 | uint16_t size; 115 | uint16_t src_mask; 116 | uint16_t src_ptr; 117 | 118 | src_mask = (uint16_t)src & RMASK; 119 | src_ptr = RBASE[s] + src_mask; 120 | 121 | if( (src_mask + len) > RSIZE ) 122 | { 123 | size = RSIZE - src_mask; 124 | read(src_ptr, (uint8_t *)dst, size); 125 | dst += size; 126 | read(RBASE[s], (uint8_t *) dst, len - size); 127 | } 128 | else 129 | read(src_ptr, (uint8_t *) dst, len); 130 | } 131 | 132 | 133 | uint8_t W5200Class::write(uint16_t _addr, uint8_t _data) 134 | { 135 | setSS(); 136 | 137 | SPI.transfer(_addr >> 8); 138 | SPI.transfer(_addr & 0xFF); 139 | SPI.transfer(0x80); 140 | SPI.transfer(0x01); 141 | SPI.transfer(_data); 142 | resetSS(); 143 | return 1; 144 | } 145 | 146 | uint16_t W5200Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len) 147 | { 148 | if (_len == 0) //Fix: a write request with _len == 0 hangs the W5200 149 | return 0; 150 | 151 | setSS(); 152 | SPI.transfer(_addr >> 8); 153 | SPI.transfer(_addr & 0xFF); 154 | SPI.transfer((0x80 | ((_len & 0x7F00) >> 8))); 155 | SPI.transfer(_len & 0x00FF); 156 | 157 | for (uint16_t i=0; i<_len; i++) 158 | { 159 | SPI.transfer(_buf[i]); 160 | 161 | } 162 | resetSS(); 163 | return _len; 164 | } 165 | 166 | uint8_t W5200Class::read(uint16_t _addr) 167 | { 168 | setSS(); 169 | SPI.transfer(_addr >> 8); 170 | SPI.transfer(_addr & 0xFF); 171 | SPI.transfer(0x00); 172 | SPI.transfer(0x01); 173 | uint8_t _data = SPI.transfer(0); 174 | resetSS(); 175 | return _data; 176 | } 177 | 178 | uint16_t W5200Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len) 179 | { 180 | setSS(); 181 | SPI.transfer(_addr >> 8); 182 | SPI.transfer(_addr & 0xFF); 183 | SPI.transfer((0x00 | ((_len & 0x7F00) >> 8))); 184 | SPI.transfer(_len & 0x00FF); 185 | 186 | for (uint16_t i=0; i<_len; i++) 187 | { 188 | _buf[i] = SPI.transfer(0); 189 | 190 | } 191 | resetSS(); 192 | return _len; 193 | } 194 | 195 | void W5200Class::execCmdSn(SOCKET s, SockCMD _cmd) { 196 | // Send command to socket 197 | writeSnCR(s, _cmd); 198 | // Wait for command to complete 199 | while (readSnCR(s)) 200 | ; 201 | } 202 | #endif 203 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/UdpNtpClient/UdpNtpClient.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Udp NTP Client 4 | 5 | Get the time from a Network Time Protocol (NTP) time server 6 | Demonstrates use of UDP sendPacket and ReceivePacket 7 | For more on NTP time servers and the messages needed to communicate with them, 8 | see http://en.wikipedia.org/wiki/Network_Time_Protocol 9 | 10 | Warning: NTP Servers are subject to temporary failure or IP address change. 11 | Plese check 12 | 13 | http://tf.nist.gov/tf-cgi/servers.cgi 14 | 15 | if the time server used in the example didn't work. 16 | 17 | created 4 Sep 2010 18 | by Michael Margolis 19 | modified 9 Apr 2012 20 | by Tom Igoe 21 | modified 12 Aug 2013 22 | by Soohwan Kim 23 | 24 | This code is in the public domain. 25 | 26 | */ 27 | 28 | #include 29 | #include 30 | #include 31 | 32 | // Enter a MAC address for your controller below. 33 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 34 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 35 | ; 36 | #else 37 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 38 | #endif 39 | 40 | unsigned int localPort = 8888; // local port to listen for UDP packets 41 | 42 | IPAddress timeServer(132, 163, 4, 101); // time-a.timefreq.bldrdoc.gov NTP server 43 | // IPAddress timeServer(132, 163, 4, 102); // time-b.timefreq.bldrdoc.gov NTP server 44 | // IPAddress timeServer(132, 163, 4, 103); // time-c.timefreq.bldrdoc.gov NTP server 45 | 46 | const int NTP_PACKET_SIZE= 48; // NTP time stamp is in the first 48 bytes of the message 47 | 48 | byte packetBuffer[ NTP_PACKET_SIZE]; //buffer to hold incoming and outgoing packets 49 | 50 | // A UDP instance to let us send and receive packets over UDP 51 | EthernetUDP Udp; 52 | 53 | void setup() 54 | { 55 | // Open serial communications and wait for port to open: 56 | Serial.begin(9600); 57 | while (!Serial) { 58 | ; // wait for serial port to connect. Needed for Leonardo only 59 | } 60 | 61 | 62 | // start Ethernet and UDP 63 | #if defined(WIZ550io_WITH_MACADDRESS) 64 | if (Ethernet.begin() == 0) { 65 | #else 66 | if (Ethernet.begin(mac) == 0) { 67 | #endif 68 | Serial.println("Failed to configure Ethernet using DHCP"); 69 | // no point in carrying on, so do nothing forevermore: 70 | for(;;) 71 | ; 72 | } 73 | Udp.begin(localPort); 74 | } 75 | 76 | void loop() 77 | { 78 | sendNTPpacket(timeServer); // send an NTP packet to a time server 79 | 80 | // wait to see if a reply is available 81 | delay(1000); 82 | if ( Udp.parsePacket() ) { 83 | // We've received a packet, read the data from it 84 | Udp.read(packetBuffer,NTP_PACKET_SIZE); // read the packet into the buffer 85 | 86 | //the timestamp starts at byte 40 of the received packet and is four bytes, 87 | // or two words, long. First, esxtract the two words: 88 | 89 | unsigned long highWord = word(packetBuffer[40], packetBuffer[41]); 90 | unsigned long lowWord = word(packetBuffer[42], packetBuffer[43]); 91 | // combine the four bytes (two words) into a long integer 92 | // this is NTP time (seconds since Jan 1 1900): 93 | unsigned long secsSince1900 = highWord << 16 | lowWord; 94 | Serial.print("Seconds since Jan 1 1900 = " ); 95 | Serial.println(secsSince1900); 96 | 97 | // now convert NTP time into everyday time: 98 | Serial.print("Unix time = "); 99 | // Unix time starts on Jan 1 1970. In seconds, that's 2208988800: 100 | const unsigned long seventyYears = 2208988800UL; 101 | // subtract seventy years: 102 | unsigned long epoch = secsSince1900 - seventyYears; 103 | // print Unix time: 104 | Serial.println(epoch); 105 | 106 | 107 | // print the hour, minute and second: 108 | Serial.print("The UTC time is "); // UTC is the time at Greenwich Meridian (GMT) 109 | Serial.print((epoch % 86400L) / 3600); // print the hour (86400 equals secs per day) 110 | Serial.print(':'); 111 | if ( ((epoch % 3600) / 60) < 10 ) { 112 | // In the first 10 minutes of each hour, we'll want a leading '0' 113 | Serial.print('0'); 114 | } 115 | Serial.print((epoch % 3600) / 60); // print the minute (3600 equals secs per minute) 116 | Serial.print(':'); 117 | if ( (epoch % 60) < 10 ) { 118 | // In the first 10 seconds of each minute, we'll want a leading '0' 119 | Serial.print('0'); 120 | } 121 | Serial.println(epoch %60); // print the second 122 | } 123 | // wait ten seconds before asking for the time again 124 | delay(10000); 125 | } 126 | 127 | // send an NTP request to the time server at the given address 128 | unsigned long sendNTPpacket(IPAddress& address) 129 | { 130 | // set all bytes in the buffer to 0 131 | memset(packetBuffer, 0, NTP_PACKET_SIZE); 132 | // Initialize values needed to form NTP request 133 | // (see URL above for details on the packets) 134 | packetBuffer[0] = 0b11100011; // LI, Version, Mode 135 | packetBuffer[1] = 0; // Stratum, or type of clock 136 | packetBuffer[2] = 6; // Polling Interval 137 | packetBuffer[3] = 0xEC; // Peer Clock Precision 138 | // 8 bytes of zero for Root Delay & Root Dispersion 139 | packetBuffer[12] = 49; 140 | packetBuffer[13] = 0x4E; 141 | packetBuffer[14] = 49; 142 | packetBuffer[15] = 52; 143 | 144 | // all NTP fields have been given values, now 145 | // you can send a packet requesting a timestamp: 146 | Udp.beginPacket(address, 123); //NTP requests are to port 123 147 | Udp.write(packetBuffer,NTP_PACKET_SIZE); 148 | Udp.endPacket(); 149 | } 150 | 151 | 152 | 153 | 154 | 155 | 156 | 157 | 158 | 159 | 160 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/Ethernet.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | modified 12 Aug 2013 3 | by Soohwan Kim (suhwan@wiznet.co.kr) 4 | */ 5 | #include "Ethernet.h" 6 | #include "Dhcp.h" 7 | 8 | // XXX: don't make assumptions about the value of MAX_SOCK_NUM. 9 | uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { 0, }; 10 | uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 0, }; 11 | 12 | int EthernetClass::begin(uint8_t *mac_address) 13 | { 14 | _dhcp = new DhcpClass(); 15 | 16 | // Initialise the basic info 17 | W5100.init(); 18 | W5100.setMACAddress(mac_address); 19 | W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); 20 | 21 | // Now try to get our config info from a DHCP server 22 | int ret = _dhcp->beginWithDHCP(mac_address); 23 | if(ret == 1) 24 | { 25 | // We've successfully found a DHCP server and got our configuration info, so set things 26 | // accordingly 27 | W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); 28 | W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); 29 | W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); 30 | _dnsServerAddress = _dhcp->getDnsServerIp(); 31 | } 32 | 33 | return ret; 34 | } 35 | 36 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip) 37 | { 38 | // Assume the DNS server will be the machine on the same network as the local IP 39 | // but with last octet being '1' 40 | IPAddress dns_server = local_ip; 41 | dns_server[3] = 1; 42 | begin(mac_address, local_ip, dns_server); 43 | } 44 | 45 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server) 46 | { 47 | // Assume the gateway will be the machine on the same network as the local IP 48 | // but with last octet being '1' 49 | IPAddress gateway = local_ip; 50 | gateway[3] = 1; 51 | begin(mac_address, local_ip, dns_server, gateway); 52 | } 53 | 54 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway) 55 | { 56 | IPAddress subnet(255, 255, 255, 0); 57 | begin(mac_address, local_ip, dns_server, gateway, subnet); 58 | } 59 | 60 | void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) 61 | { 62 | W5100.init(); 63 | W5100.setMACAddress(mac); 64 | W5100.setIPAddress(local_ip._address); 65 | W5100.setGatewayIp(gateway._address); 66 | W5100.setSubnetMask(subnet._address); 67 | _dnsServerAddress = dns_server; 68 | } 69 | 70 | #if defined(WIZ550io_WITH_MACADDRESS) 71 | int EthernetClass::begin(void) 72 | { 73 | byte mac_address[6] ={0,}; 74 | _dhcp = new DhcpClass(); 75 | 76 | 77 | // Initialise the basic info 78 | W5100.init(); 79 | W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); 80 | W5100.getMACAddress(mac_address); 81 | 82 | // Now try to get our config info from a DHCP server 83 | int ret = _dhcp->beginWithDHCP(mac_address); 84 | if(ret == 1) 85 | { 86 | // We've successfully found a DHCP server and got our configuration info, so set things 87 | // accordingly 88 | W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); 89 | W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); 90 | W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); 91 | _dnsServerAddress = _dhcp->getDnsServerIp(); 92 | } 93 | 94 | return ret; 95 | } 96 | 97 | void EthernetClass::begin(IPAddress local_ip) 98 | { 99 | // Assume the DNS server will be the machine on the same network as the local IP 100 | // but with last octet being '1' 101 | IPAddress dns_server = local_ip; 102 | dns_server[3] = 1; 103 | begin(local_ip, dns_server); 104 | } 105 | 106 | void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server) 107 | { 108 | // Assume the gateway will be the machine on the same network as the local IP 109 | // but with last octet being '1' 110 | IPAddress gateway = local_ip; 111 | gateway[3] = 1; 112 | begin(local_ip, dns_server, gateway); 113 | } 114 | 115 | void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) 116 | { 117 | IPAddress subnet(255, 255, 255, 0); 118 | begin(local_ip, dns_server, gateway, subnet); 119 | } 120 | 121 | void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) 122 | { 123 | byte mac_address[6] ={0,}; 124 | W5100.init(); 125 | W5100.setIPAddress(local_ip._address); 126 | W5100.setGatewayIp(gateway._address); 127 | W5100.setSubnetMask(subnet._address); 128 | _dnsServerAddress = dns_server; 129 | } 130 | #endif 131 | 132 | int EthernetClass::maintain(){ 133 | int rc = DHCP_CHECK_NONE; 134 | if(_dhcp != NULL){ 135 | //we have a pointer to dhcp, use it 136 | rc = _dhcp->checkLease(); 137 | switch ( rc ){ 138 | case DHCP_CHECK_NONE: 139 | //nothing done 140 | break; 141 | case DHCP_CHECK_RENEW_OK: 142 | case DHCP_CHECK_REBIND_OK: 143 | //we might have got a new IP. 144 | W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); 145 | W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); 146 | W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); 147 | _dnsServerAddress = _dhcp->getDnsServerIp(); 148 | break; 149 | default: 150 | //this is actually a error, it will retry though 151 | break; 152 | } 153 | } 154 | return rc; 155 | } 156 | 157 | IPAddress EthernetClass::localIP() 158 | { 159 | IPAddress ret; 160 | W5100.getIPAddress(ret.raw_address()); 161 | return ret; 162 | } 163 | 164 | IPAddress EthernetClass::subnetMask() 165 | { 166 | IPAddress ret; 167 | W5100.getSubnetMask(ret.raw_address()); 168 | return ret; 169 | } 170 | 171 | IPAddress EthernetClass::gatewayIP() 172 | { 173 | IPAddress ret; 174 | W5100.getGatewayIp(ret.raw_address()); 175 | return ret; 176 | } 177 | 178 | IPAddress EthernetClass::dnsServerIP() 179 | { 180 | return _dnsServerAddress; 181 | } 182 | 183 | EthernetClass Ethernet; 184 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/Ethernet.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | modified 12 Aug 2013 3 | by Soohwan Kim (suhwan@wiznet.co.kr) 4 | */ 5 | #include "Ethernet.h" 6 | #include "Dhcp.h" 7 | 8 | // XXX: don't make assumptions about the value of MAX_SOCK_NUM. 9 | uint8_t EthernetClass::_state[MAX_SOCK_NUM] = { 0, }; 10 | uint16_t EthernetClass::_server_port[MAX_SOCK_NUM] = { 0, }; 11 | 12 | 13 | 14 | #if defined(WIZ550io_WITH_MACADDRESS) 15 | int EthernetClass::begin(void) 16 | { 17 | byte mac_address[6] ={0,}; 18 | _dhcp = new DhcpClass(); 19 | 20 | // Initialise the basic info 21 | W5100.init(); 22 | W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); 23 | W5100.getMACAddress(mac_address); 24 | 25 | // Now try to get our config info from a DHCP server 26 | int ret = _dhcp->beginWithDHCP(mac_address); 27 | if(ret == 1) 28 | { 29 | // We've successfully found a DHCP server and got our configuration info, so set things 30 | // accordingly 31 | W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); 32 | W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); 33 | W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); 34 | _dnsServerAddress = _dhcp->getDnsServerIp(); 35 | } 36 | 37 | return ret; 38 | } 39 | 40 | void EthernetClass::begin(IPAddress local_ip) 41 | { 42 | // Assume the DNS server will be the machine on the same network as the local IP 43 | // but with last octet being '1' 44 | IPAddress dns_server = local_ip; 45 | dns_server[3] = 1; 46 | begin(local_ip, dns_server); 47 | } 48 | 49 | void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server) 50 | { 51 | // Assume the gateway will be the machine on the same network as the local IP 52 | // but with last octet being '1' 53 | IPAddress gateway = local_ip; 54 | gateway[3] = 1; 55 | begin(local_ip, dns_server, gateway); 56 | } 57 | 58 | void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway) 59 | { 60 | IPAddress subnet(255, 255, 255, 0); 61 | begin(local_ip, dns_server, gateway, subnet); 62 | } 63 | 64 | void EthernetClass::begin(IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) 65 | { 66 | W5100.init(); 67 | W5100.setIPAddress(local_ip.raw_address()); 68 | W5100.setGatewayIp(gateway.raw_address()); 69 | W5100.setSubnetMask(subnet.raw_address()); 70 | _dnsServerAddress = dns_server; 71 | } 72 | #else 73 | int EthernetClass::begin(uint8_t *mac_address) 74 | { 75 | _dhcp = new DhcpClass(); 76 | 77 | // Initialise the basic info 78 | W5100.init(); 79 | W5100.setMACAddress(mac_address); 80 | W5100.setIPAddress(IPAddress(0,0,0,0).raw_address()); 81 | 82 | // Now try to get our config info from a DHCP server 83 | int ret = _dhcp->beginWithDHCP(mac_address); 84 | if(ret == 1) 85 | { 86 | // We've successfully found a DHCP server and got our configuration info, so set things 87 | // accordingly 88 | W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); 89 | W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); 90 | W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); 91 | _dnsServerAddress = _dhcp->getDnsServerIp(); 92 | } 93 | 94 | return ret; 95 | } 96 | 97 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip) 98 | { 99 | // Assume the DNS server will be the machine on the same network as the local IP 100 | // but with last octet being '1' 101 | IPAddress dns_server = local_ip; 102 | dns_server[3] = 1; 103 | begin(mac_address, local_ip, dns_server); 104 | } 105 | 106 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server) 107 | { 108 | // Assume the gateway will be the machine on the same network as the local IP 109 | // but with last octet being '1' 110 | IPAddress gateway = local_ip; 111 | gateway[3] = 1; 112 | begin(mac_address, local_ip, dns_server, gateway); 113 | } 114 | 115 | void EthernetClass::begin(uint8_t *mac_address, IPAddress local_ip, IPAddress dns_server, IPAddress gateway) 116 | { 117 | IPAddress subnet(255, 255, 255, 0); 118 | begin(mac_address, local_ip, dns_server, gateway, subnet); 119 | } 120 | 121 | void EthernetClass::begin(uint8_t *mac, IPAddress local_ip, IPAddress dns_server, IPAddress gateway, IPAddress subnet) 122 | { 123 | W5100.init(); 124 | W5100.setMACAddress(mac); 125 | W5100.setIPAddress(local_ip.raw_address()); 126 | W5100.setGatewayIp(gateway.raw_address()); 127 | W5100.setSubnetMask(subnet.raw_address()); 128 | _dnsServerAddress = dns_server; 129 | } 130 | 131 | #endif 132 | 133 | int EthernetClass::maintain(){ 134 | int rc = DHCP_CHECK_NONE; 135 | if(_dhcp != NULL){ 136 | //we have a pointer to dhcp, use it 137 | rc = _dhcp->checkLease(); 138 | switch ( rc ){ 139 | case DHCP_CHECK_NONE: 140 | //nothing done 141 | break; 142 | case DHCP_CHECK_RENEW_OK: 143 | case DHCP_CHECK_REBIND_OK: 144 | //we might have got a new IP. 145 | W5100.setIPAddress(_dhcp->getLocalIp().raw_address()); 146 | W5100.setGatewayIp(_dhcp->getGatewayIp().raw_address()); 147 | W5100.setSubnetMask(_dhcp->getSubnetMask().raw_address()); 148 | _dnsServerAddress = _dhcp->getDnsServerIp(); 149 | break; 150 | default: 151 | //this is actually a error, it will retry though 152 | break; 153 | } 154 | } 155 | return rc; 156 | } 157 | 158 | IPAddress EthernetClass::localIP() 159 | { 160 | IPAddress ret; 161 | W5100.getIPAddress(ret.raw_address()); 162 | return ret; 163 | } 164 | 165 | IPAddress EthernetClass::subnetMask() 166 | { 167 | IPAddress ret; 168 | W5100.getSubnetMask(ret.raw_address()); 169 | return ret; 170 | } 171 | 172 | IPAddress EthernetClass::gatewayIP() 173 | { 174 | IPAddress ret; 175 | W5100.getGatewayIp(ret.raw_address()); 176 | return ret; 177 | } 178 | 179 | IPAddress EthernetClass::dnsServerIP() 180 | { 181 | return _dnsServerAddress; 182 | } 183 | 184 | EthernetClass Ethernet; 185 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/utility/w5500.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by WIZnet 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #include "utility/w5100.h" 14 | #if defined(W5500_ETHERNET_SHIELD) 15 | 16 | // W5500 controller instance 17 | W5500Class W5100; 18 | 19 | #define SPI_CS 10 20 | 21 | void W5500Class::init(void) 22 | { 23 | delay(1000); 24 | 25 | #if defined(ARDUINO_ARCH_AVR) || defined(ESP8266) 26 | initSS(); 27 | SPI.begin(); 28 | #else 29 | SPI.begin(SPI_CS); 30 | // Set clock to 4Mhz (W5100 should support up to about 14Mhz) 31 | // SPI.setClockDivider(SPI_CS, 21); 32 | // SPI.setClockDivider(SPI_CS, 6); // 14 Mhz, ok 33 | // SPI.setClockDivider(SPI_CS, 3); // 28 Mhz, ok 34 | SPI.setClockDivider(SPI_CS, 2); // 42 Mhz, ok 35 | SPI.setDataMode(SPI_CS, SPI_MODE0); 36 | #endif 37 | for (int i=0; i> 8); 111 | SPI.transfer(_addr & 0xFF); 112 | SPI.transfer(_cb); 113 | SPI.transfer(_data); 114 | resetSS(); 115 | #else 116 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 117 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 118 | SPI.transfer(SPI_CS, _cb, SPI_CONTINUE); 119 | SPI.transfer(SPI_CS, _data); 120 | #endif 121 | return 1; 122 | } 123 | 124 | uint16_t W5500Class::write(uint16_t _addr, uint8_t _cb, const uint8_t *_buf, uint16_t _len) 125 | { 126 | #if defined(ARDUINO_ARCH_AVR) || defined(ESP8266) 127 | setSS(); 128 | SPI.transfer(_addr >> 8); 129 | SPI.transfer(_addr & 0xFF); 130 | SPI.transfer(_cb); 131 | for (uint16_t i=0; i<_len; i++){ 132 | SPI.transfer(_buf[i]); 133 | } 134 | resetSS(); 135 | #else 136 | uint16_t i; 137 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 138 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 139 | SPI.transfer(SPI_CS, _cb, SPI_CONTINUE); 140 | for (i=0; i<_len-1; i++){ 141 | SPI.transfer(SPI_CS, _buf[i], SPI_CONTINUE); 142 | } 143 | SPI.transfer(SPI_CS, _buf[i]); 144 | 145 | #endif 146 | return _len; 147 | } 148 | 149 | uint8_t W5500Class::read(uint16_t _addr, uint8_t _cb) 150 | { 151 | #if defined(ARDUINO_ARCH_AVR) || defined(ESP8266) 152 | setSS(); 153 | SPI.transfer(_addr >> 8); 154 | SPI.transfer(_addr & 0xFF); 155 | SPI.transfer(_cb); 156 | uint8_t _data = SPI.transfer(0); 157 | resetSS(); 158 | #else 159 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 160 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 161 | SPI.transfer(SPI_CS, _cb, SPI_CONTINUE); 162 | uint8_t _data = SPI.transfer(SPI_CS, 0); 163 | #endif 164 | return _data; 165 | } 166 | 167 | uint16_t W5500Class::read(uint16_t _addr, uint8_t _cb, uint8_t *_buf, uint16_t _len) 168 | { 169 | #if defined(ARDUINO_ARCH_AVR) || defined(ESP8266) 170 | setSS(); 171 | SPI.transfer(_addr >> 8); 172 | SPI.transfer(_addr & 0xFF); 173 | SPI.transfer(_cb); 174 | for (uint16_t i=0; i<_len; i++){ 175 | _buf[i] = SPI.transfer(0); 176 | } 177 | resetSS(); 178 | #else 179 | uint16_t i; 180 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 181 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 182 | SPI.transfer(SPI_CS, _cb, SPI_CONTINUE); 183 | for (i=0; i<_len-1; i++){ 184 | _buf[i] = SPI.transfer(SPI_CS, 0, SPI_CONTINUE); 185 | } 186 | _buf[_len-1] = SPI.transfer(SPI_CS, 0); 187 | 188 | 189 | #endif 190 | return _len; 191 | } 192 | 193 | void W5500Class::execCmdSn(SOCKET s, SockCMD _cmd) { 194 | // Send command to socket 195 | writeSnCR(s, _cmd); 196 | // Wait for command to complete 197 | while (readSnCR(s)) 198 | ; 199 | } 200 | #endif -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/XivelyClientString/XivelyClientString.ino: -------------------------------------------------------------------------------- 1 | /* 2 | Xively(Cosm, Pachube) sensor client 3 | 4 | This sketch connects an analog sensor to Xively (http://xively.com) 5 | using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or 6 | the Adafruit Ethernet shield, either one will work, as long as it's got 7 | a Wiznet Ethernet module on board. 8 | 9 | This example has been updated to use the Xively.com API. 10 | To make it work, create a feed with two datastreams, and give them the IDs 11 | sensor1 and sensor2. Or change the code below to match your feed. 12 | 13 | This example uses the String library, which is part of the Arduino core from 14 | version 0019. 15 | 16 | Circuit: 17 | * Analog sensor attached to analog in 0 18 | * Ethernet shield attached to pins 10, 11, 12, 13 19 | 20 | created 15 March 2010 21 | modified 9 Apr 2012 22 | by Tom Igoe with input from Usman Haque and Joe Saavedra 23 | modified 8 September 2012 24 | by Scott Fitzgerald 25 | modified 12 Aug 2013 26 | by Soohwan Kim 27 | 28 | http://arduino.cc/en/Tutorial/PachubeClientString 29 | This code is in the public domain. 30 | 31 | */ 32 | 33 | #include 34 | #include 35 | 36 | 37 | #define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here 38 | #define FEEDID 00000 // replace your feed ID 39 | #define USERAGENT "My Project" // user agent is the project name 40 | 41 | 42 | // assign a MAC address for the ethernet controller. 43 | // fill in your address here: 44 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 45 | ; 46 | #else 47 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 48 | #endif 49 | 50 | // fill in an available IP address on your network here, 51 | // for manual configuration: 52 | IPAddress ip(192,168,1,177); 53 | IPAddress gw(0,0,0,0); 54 | IPAddress snip(0,0,0,0); 55 | IPAddress dnsip(0,0,0,0); 56 | // initialize the library instance: 57 | EthernetClient client; 58 | 59 | // if you don't want to use DNS (and reduce your sketch size) 60 | // use the numeric IP instead of the name for the server: 61 | //IPAddress server(173,203,98,29); // In cases where it is not possible to use DNS, you can use the following bare-IP address alternative 62 | char server[] = "api.xively.com"; // name address for cosm API 63 | 64 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds 65 | boolean lastConnected = false; // state of the connection last time through the main loop 66 | const unsigned long postingInterval = 10*1000; //delay between updates to xively.com 67 | 68 | void setup() { 69 | // Open serial communications and wait for port to open: 70 | Serial.begin(9600); 71 | while (!Serial) { 72 | ; // wait for serial port to connect. Needed for Leonardo only 73 | } 74 | 75 | 76 | // give the ethernet module time to boot up: 77 | delay(1000); 78 | // start the Ethernet connection: 79 | #if defined(WIZ550io_WITH_MACADDRESS) 80 | if (Ethernet.begin() == 0) { 81 | #else 82 | if (Ethernet.begin(mac) == 0) { 83 | #endif 84 | Serial.println("Failed to configure Ethernet using DHCP"); 85 | // DHCP failed, so use a fixed IP address: 86 | #if defined(WIZ550io_WITH_MACADDRESS) 87 | Ethernet.begin(ip, dnsip, gw,snip); 88 | #else 89 | Ethernet.begin(mac, ip, dnsip, gw,snip); 90 | #endif 91 | } 92 | } 93 | 94 | void loop() { 95 | // read the analog sensor: 96 | int sensorReading = analogRead(A0); 97 | // convert the data to a String to send it: 98 | 99 | String dataString = "sensor1,"; 100 | dataString += sensorReading; 101 | 102 | // you can append multiple readings to this String if your 103 | // pachube feed is set up to handle multiple values: 104 | int otherSensorReading = analogRead(A1); 105 | dataString += "\nsensor2,"; 106 | dataString += otherSensorReading; 107 | 108 | // if there's incoming data from the net connection. 109 | // send it out the serial port. This is for debugging 110 | // purposes only: 111 | if (client.available()) { 112 | char c = client.read(); 113 | Serial.print(c); 114 | } 115 | 116 | // if there's no net connection, but there was one last time 117 | // through the loop, then stop the client: 118 | if (!client.connected() && lastConnected) { 119 | Serial.println(); 120 | Serial.println("disconnecting."); 121 | client.stop(); 122 | } 123 | 124 | // if you're not connected, and ten seconds have passed since 125 | // your last connection, then connect again and send data: 126 | if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { 127 | sendData(dataString); 128 | } 129 | // store the state of the connection for next time through 130 | // the loop: 131 | lastConnected = client.connected(); 132 | } 133 | 134 | // this method makes a HTTP connection to the server: 135 | void sendData(String thisData) { 136 | // if there's a successful connection: 137 | if (client.connect(server, 80)) { 138 | Serial.println("connecting..."); 139 | // send the HTTP PUT request: 140 | client.print("PUT /v2/feeds/"); 141 | client.print(FEEDID); 142 | client.println(".csv HTTP/1.1"); 143 | client.println("Host: api.xively.com"); 144 | client.print("X-ApiKey: "); 145 | client.println(APIKEY); 146 | client.print("User-Agent: "); 147 | client.println(USERAGENT); 148 | client.print("Content-Length: "); 149 | client.println(thisData.length()); 150 | 151 | // last pieces of the HTTP PUT request: 152 | client.println("Content-Type: text/csv"); 153 | client.println("Connection: close"); 154 | client.println(); 155 | 156 | // here's the actual content of the PUT request: 157 | client.println(thisData); 158 | } 159 | else { 160 | // if you couldn't make a connection: 161 | Serial.println("connection failed"); 162 | Serial.println(); 163 | Serial.println("disconnecting."); 164 | client.stop(); 165 | } 166 | // note the time that the connection was made or attempted: 167 | lastConnectionTime = millis(); 168 | } 169 | 170 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/utility/w5100.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2010 by Cristian Maglie 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #include 11 | #include 12 | 13 | #include "utility/w5100.h" 14 | 15 | #if defined(W5100_ETHERNET_SHIELD) 16 | 17 | // W5100 controller instance 18 | W5100Class W5100; 19 | 20 | #define SPI_CS 10 21 | 22 | #define TX_RX_MAX_BUF_SIZE 2048 23 | #define TX_BUF 0x1100 24 | #define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE) 25 | 26 | #define TXBUF_BASE 0x4000 27 | #define RXBUF_BASE 0x6000 28 | 29 | void W5100Class::init(void) 30 | { 31 | delay(300); 32 | 33 | #if defined(ARDUINO_ARCH_AVR) || defined(ESP8266) 34 | SPI.begin(); 35 | initSS(); 36 | #else 37 | SPI.begin(SPI_CS); 38 | // Set clock to 4Mhz (W5100 should support up to about 14Mhz) 39 | SPI.setClockDivider(SPI_CS, 21); 40 | SPI.setDataMode(SPI_CS, SPI_MODE0); 41 | #endif 42 | writeMR(1< SSIZE) 91 | { 92 | // Wrap around circular buffer 93 | uint16_t size = SSIZE - offset; 94 | write(dstAddr, data, size); 95 | write(SBASE[s], data + size, len - size); 96 | } 97 | else { 98 | write(dstAddr, data, len); 99 | } 100 | 101 | ptr += len; 102 | writeSnTX_WR(s, ptr); 103 | } 104 | 105 | 106 | void W5100Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek) 107 | { 108 | uint16_t ptr; 109 | ptr = readSnRX_RD(s); 110 | read_data(s, ptr, data, len); 111 | if (!peek) 112 | { 113 | ptr += len; 114 | writeSnRX_RD(s, ptr); 115 | } 116 | } 117 | 118 | void W5100Class::read_data(SOCKET s, volatile uint16_t src, volatile uint8_t *dst, uint16_t len) 119 | { 120 | uint16_t size; 121 | uint16_t src_mask; 122 | uint16_t src_ptr; 123 | 124 | src_mask = src & RMASK; 125 | src_ptr = RBASE[s] + src_mask; 126 | 127 | if( (src_mask + len) > RSIZE ) 128 | { 129 | size = RSIZE - src_mask; 130 | read(src_ptr, (uint8_t *)dst, size); 131 | dst += size; 132 | read(RBASE[s], (uint8_t *) dst, len - size); 133 | } 134 | else 135 | read(src_ptr, (uint8_t *) dst, len); 136 | } 137 | 138 | 139 | uint8_t W5100Class::write(uint16_t _addr, uint8_t _data) 140 | { 141 | #if defined(ARDUINO_ARCH_AVR) || defined(ESP8266) 142 | setSS(); 143 | SPI.transfer(0xF0); 144 | SPI.transfer(_addr >> 8); 145 | SPI.transfer(_addr & 0xFF); 146 | SPI.transfer(_data); 147 | resetSS(); 148 | #else 149 | SPI.transfer(SPI_CS, 0xF0, SPI_CONTINUE); 150 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 151 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 152 | SPI.transfer(SPI_CS, _data); 153 | #endif 154 | return 1; 155 | } 156 | 157 | uint16_t W5100Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len) 158 | { 159 | for (uint16_t i=0; i<_len; i++) 160 | { 161 | #if defined(ARDUINO_ARCH_AVR) || defined(ESP8266) 162 | setSS(); 163 | SPI.transfer(0xF0); 164 | SPI.transfer(_addr >> 8); 165 | SPI.transfer(_addr & 0xFF); 166 | _addr++; 167 | SPI.transfer(_buf[i]); 168 | resetSS(); 169 | #else 170 | SPI.transfer(SPI_CS, 0xF0, SPI_CONTINUE); 171 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 172 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 173 | SPI.transfer(SPI_CS, _buf[i]); 174 | _addr++; 175 | #endif 176 | } 177 | return _len; 178 | } 179 | 180 | uint8_t W5100Class::read(uint16_t _addr) 181 | { 182 | #if defined(ARDUINO_ARCH_AVR) || defined(ESP8266) 183 | setSS(); 184 | SPI.transfer(0x0F); 185 | SPI.transfer(_addr >> 8); 186 | SPI.transfer(_addr & 0xFF); 187 | uint8_t _data = SPI.transfer(0); 188 | resetSS(); 189 | #else 190 | SPI.transfer(SPI_CS, 0x0F, SPI_CONTINUE); 191 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 192 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 193 | uint8_t _data = SPI.transfer(SPI_CS, 0); 194 | #endif 195 | return _data; 196 | } 197 | 198 | uint16_t W5100Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len) 199 | { 200 | for (uint16_t i=0; i<_len; i++) 201 | { 202 | #if defined(ARDUINO_ARCH_AVR) || defined(ESP8266) 203 | setSS(); 204 | SPI.transfer(0x0F); 205 | SPI.transfer(_addr >> 8); 206 | SPI.transfer(_addr & 0xFF); 207 | _addr++; 208 | _buf[i] = SPI.transfer(0); 209 | resetSS(); 210 | #else 211 | SPI.transfer(SPI_CS, 0x0F, SPI_CONTINUE); 212 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 213 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 214 | _buf[i] = SPI.transfer(SPI_CS, 0); 215 | _addr++; 216 | #endif 217 | } 218 | return _len; 219 | } 220 | 221 | void W5100Class::execCmdSn(SOCKET s, SockCMD _cmd) { 222 | // Send command to socket 223 | writeSnCR(s, _cmd); 224 | // Wait for command to complete 225 | while (readSnCR(s)) 226 | ; 227 | } 228 | 229 | #endif 230 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/EthernetUdp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield. 3 | * This version only offers minimal wrapping of socket.c/socket.h 4 | * Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 5 | * 6 | * MIT License: 7 | * Copyright (c) 2008 Bjoern Hartmann 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | * bjoern@cs.stanford.edu 12/30/2008 27 | */ 28 | 29 | #include "w5100.h" 30 | #include "socket.h" 31 | #include "Ethernet.h" 32 | #include "Udp.h" 33 | #include "Dns.h" 34 | 35 | /* Constructor */ 36 | EthernetUDP::EthernetUDP() : _sock(MAX_SOCK_NUM) {} 37 | 38 | /* Start EthernetUDP socket, listening at local port PORT */ 39 | uint8_t EthernetUDP::begin(uint16_t port) { 40 | if (_sock != MAX_SOCK_NUM) 41 | return 0; 42 | 43 | for (int i = 0; i < MAX_SOCK_NUM; i++) { 44 | uint8_t s = W5100.readSnSR(i); 45 | if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) { 46 | _sock = i; 47 | break; 48 | } 49 | } 50 | 51 | if (_sock == MAX_SOCK_NUM) 52 | return 0; 53 | 54 | _port = port; 55 | _remaining = 0; 56 | socket(_sock, SnMR::UDP, _port, 0); 57 | 58 | return 1; 59 | } 60 | 61 | /* return number of bytes available in the current packet, 62 | will return zero if parsePacket hasn't been called yet */ 63 | int EthernetUDP::available() { 64 | return _remaining; 65 | } 66 | 67 | /* Release any resources being used by this EthernetUDP instance */ 68 | void EthernetUDP::stop() 69 | { 70 | if (_sock == MAX_SOCK_NUM) 71 | return; 72 | 73 | close(_sock); 74 | 75 | EthernetClass::_server_port[_sock] = 0; 76 | _sock = MAX_SOCK_NUM; 77 | } 78 | 79 | int EthernetUDP::beginPacket(const char *host, uint16_t port) 80 | { 81 | // Look up the host first 82 | int ret = 0; 83 | DNSClient dns; 84 | IPAddress remote_addr; 85 | 86 | dns.begin(Ethernet.dnsServerIP()); 87 | ret = dns.getHostByName(host, remote_addr); 88 | if (ret == 1) { 89 | return beginPacket(remote_addr, port); 90 | } else { 91 | return ret; 92 | } 93 | } 94 | 95 | int EthernetUDP::beginPacket(IPAddress ip, uint16_t port) 96 | { 97 | _offset = 0; 98 | return startUDP(_sock, rawIPAddress(ip), port); 99 | } 100 | 101 | int EthernetUDP::endPacket() 102 | { 103 | return sendUDP(_sock); 104 | } 105 | 106 | size_t EthernetUDP::write(uint8_t byte) 107 | { 108 | return write(&byte, 1); 109 | } 110 | 111 | size_t EthernetUDP::write(const uint8_t *buffer, size_t size) 112 | { 113 | uint16_t bytes_written = bufferData(_sock, _offset, buffer, size); 114 | _offset += bytes_written; 115 | return bytes_written; 116 | } 117 | 118 | int EthernetUDP::parsePacket() 119 | { 120 | // discard any remaining bytes in the last packet 121 | flush(); 122 | 123 | if (W5100.getRXReceivedSize(_sock) > 0) 124 | { 125 | //HACK - hand-parse the UDP packet using TCP recv method 126 | uint8_t tmpBuf[8]; 127 | int ret =0; 128 | //read 8 header bytes and get IP and port from it 129 | ret = recv(_sock,tmpBuf,8); 130 | if (ret > 0) 131 | { 132 | _remoteIP = tmpBuf; 133 | _remotePort = tmpBuf[4]; 134 | _remotePort = (_remotePort << 8) + tmpBuf[5]; 135 | _remaining = tmpBuf[6]; 136 | _remaining = (_remaining << 8) + tmpBuf[7]; 137 | 138 | // When we get here, any remaining bytes are the data 139 | ret = _remaining; 140 | } 141 | return ret; 142 | } 143 | // There aren't any packets available 144 | return 0; 145 | } 146 | 147 | int EthernetUDP::read() 148 | { 149 | uint8_t byte; 150 | 151 | if ((_remaining > 0) && (recv(_sock, &byte, 1) > 0)) 152 | { 153 | // We read things without any problems 154 | _remaining--; 155 | return byte; 156 | } 157 | 158 | // If we get here, there's no data available 159 | return -1; 160 | } 161 | 162 | int EthernetUDP::read(unsigned char* buffer, size_t len) 163 | { 164 | 165 | if (_remaining > 0) 166 | { 167 | 168 | int got; 169 | 170 | if (_remaining <= len) 171 | { 172 | // data should fit in the buffer 173 | got = recv(_sock, buffer, _remaining); 174 | } 175 | else 176 | { 177 | // too much data for the buffer, 178 | // grab as much as will fit 179 | got = recv(_sock, buffer, len); 180 | } 181 | 182 | if (got > 0) 183 | { 184 | _remaining -= got; 185 | return got; 186 | } 187 | 188 | } 189 | 190 | // If we get here, there's no data available or recv failed 191 | return -1; 192 | 193 | } 194 | 195 | int EthernetUDP::peek() 196 | { 197 | uint8_t b; 198 | // Unlike recv, peek doesn't check to see if there's any data available, so we must. 199 | // If the user hasn't called parsePacket yet then return nothing otherwise they 200 | // may get the UDP header 201 | if (!_remaining) 202 | return -1; 203 | ::peek(_sock, &b); 204 | return b; 205 | } 206 | 207 | void EthernetUDP::flush() 208 | { 209 | // could this fail (loop endlessly) if _remaining > 0 and recv in read fails? 210 | // should only occur if recv fails after telling us the data is there, lets 211 | // hope the w5100 always behaves :) 212 | 213 | while (_remaining) 214 | { 215 | read(); 216 | } 217 | } 218 | 219 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/XivelyClient/XivelyClient.ino: -------------------------------------------------------------------------------- 1 | /* 2 | 3 | Cosm sensor client 4 | 5 | This sketch connects an analog sensor to Cosm (http://xively.com) 6 | using a Wiznet Ethernet shield. You can use the Arduino Ethernet shield, or 7 | the Adafruit Ethernet shield, either one will work, as long as it's got 8 | a Wiznet Ethernet module on board. 9 | 10 | This example has been updated to use version 2.0 of the cosm.com API. 11 | To make it work, create a feed with a datastream, and give it the ID 12 | sensor1. Or change the code below to match your feed. 13 | 14 | 15 | Circuit: 16 | * Analog sensor attached to analog in 0 17 | * Ethernet shield attached to pins 10, 11, 12, 13 18 | 19 | created 27 July 2013 20 | updated 14 May 2012 21 | modified 12 Aug 2013 22 | by Soohwan Kim 23 | 24 | thank to Tom Igoe with input from Usman Haque and Joe Saavedra 25 | 26 | http://arduino.cc/en/Tutorial/CosmClient 27 | This code is in the public domain. 28 | 29 | */ 30 | 31 | #include 32 | #include 33 | 34 | #define APIKEY "YOUR API KEY GOES HERE" // replace your pachube api key here 35 | #define FEEDID 00000 // replace your feed ID 36 | #define USERAGENT "My Project" // user agent is the project name 37 | 38 | // assign a MAC address for the ethernet controller. 39 | // Newer Ethernet shields have a MAC address printed on a sticker on the shield 40 | // fill in your address here: 41 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 42 | ; 43 | #else 44 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 45 | #endif 46 | 47 | // fill in an available IP address on your network here, 48 | // for manual configuration: 49 | IPAddress ip(192,168,1,177); 50 | IPAddress gw(0,0,0,0); 51 | IPAddress snip(0,0,0,0); 52 | IPAddress dnsip(0,0,0,0); 53 | // initialize the library instance: 54 | EthernetClient client; 55 | 56 | // if you don't want to use DNS (and reduce your sketch size) 57 | // use the numeric IP instead of the name for the server: 58 | //IPAddress server(173,203,98,29); // In cases where it is not possible to use DNS, you can use the following bare-IP address alternative 59 | char server[] = "api.xively.com"; // name address for cosm API 60 | 61 | 62 | unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds 63 | boolean lastConnected = false; // state of the connection last time through the main loop 64 | const unsigned long postingInterval = 10*1000; //delay between updates to cosm.com 65 | 66 | void setup() { 67 | // start serial port: 68 | Serial.begin(9600); 69 | 70 | // start the Ethernet connection: 71 | #if defined(WIZ550io_WITH_MACADDRESS) 72 | if (Ethernet.begin() == 0) { 73 | #else 74 | if (Ethernet.begin(mac) == 0) { 75 | #endif 76 | Serial.println("Failed to configure Ethernet using DHCP"); 77 | // DHCP failed, so use a fixed IP address: 78 | #if defined(WIZ550io_WITH_MACADDRESS) 79 | Ethernet.begin(ip, dnsip, gw,snip); 80 | #else 81 | Ethernet.begin(mac, ip, dnsip, gw,snip); 82 | #endif 83 | } 84 | } 85 | 86 | void loop() { 87 | // read the analog sensor: 88 | int sensorReading = analogRead(A0); 89 | 90 | // if there's incoming data from the net connection. 91 | // send it out the serial port. This is for debugging 92 | // purposes only: 93 | if (client.available()) { 94 | char c = client.read(); 95 | Serial.print(c); 96 | } 97 | 98 | // if there's no net connection, but there was one last time 99 | // through the loop, then stop the client: 100 | if (!client.connected() && lastConnected) { 101 | Serial.println(); 102 | Serial.println("disconnecting."); 103 | client.stop(); 104 | } 105 | 106 | // if you're not connected, and ten seconds have passed since 107 | // your last connection, then connect again and send data: 108 | if(!client.connected() && (millis() - lastConnectionTime > postingInterval)) { 109 | sendData(sensorReading); 110 | } 111 | // store the state of the connection for next time through 112 | // the loop: 113 | lastConnected = client.connected(); 114 | } 115 | 116 | // this method makes a HTTP connection to the server: 117 | void sendData(int thisData) { 118 | // if there's a successful connection: 119 | if (client.connect(server, 80)) { 120 | Serial.println("connecting..."); 121 | // send the HTTP PUT request: 122 | client.print("PUT /v2/feeds/"); 123 | client.print(FEEDID); 124 | client.println(".csv HTTP/1.1"); 125 | client.println("Host: api.cosm.com"); 126 | client.print("X-ApiKey: "); 127 | client.println(APIKEY); 128 | client.print("User-Agent: "); 129 | client.println(USERAGENT); 130 | client.print("Content-Length: "); 131 | 132 | // calculate the length of the sensor reading in bytes: 133 | // 8 bytes for "sensor1," + number of digits of the data: 134 | int thisLength = 8 + getLength(thisData); 135 | client.println(thisLength); 136 | 137 | // last pieces of the HTTP PUT request: 138 | client.println("Content-Type: text/csv"); 139 | client.println("Connection: close"); 140 | client.println(); 141 | 142 | // here's the actual content of the PUT request: 143 | client.print("sensor1,"); 144 | client.println(thisData); 145 | 146 | } 147 | else { 148 | // if you couldn't make a connection: 149 | Serial.println("connection failed"); 150 | Serial.println(); 151 | Serial.println("disconnecting."); 152 | client.stop(); 153 | } 154 | // note the time that the connection was made or attempted: 155 | lastConnectionTime = millis(); 156 | } 157 | 158 | 159 | // This method calculates the number of digits in the 160 | // sensor reading. Since each digit of the ASCII decimal 161 | // representation is a byte, the number of digits equals 162 | // the number of bytes: 163 | 164 | int getLength(int someValue) { 165 | // there's at least one byte: 166 | int digits = 1; 167 | // continually divide the value by ten, 168 | // adding one to the digit count for each 169 | // time you divide, until you're at 0: 170 | int dividend = someValue /10; 171 | while (dividend > 0) { 172 | dividend = dividend /10; 173 | digits++; 174 | } 175 | // return the number of digits: 176 | return digits; 177 | } 178 | 179 | 180 | 181 | 182 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/EthernetUdp.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Udp.cpp: Library to send/receive UDP packets with the Arduino ethernet shield. 3 | * This version only offers minimal wrapping of socket.c/socket.h 4 | * Drop Udp.h/.cpp into the Ethernet library directory at hardware/libraries/Ethernet/ 5 | * 6 | * MIT License: 7 | * Copyright (c) 2008 Bjoern Hartmann 8 | * Permission is hereby granted, free of charge, to any person obtaining a copy 9 | * of this software and associated documentation files (the "Software"), to deal 10 | * in the Software without restriction, including without limitation the rights 11 | * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 12 | * copies of the Software, and to permit persons to whom the Software is 13 | * furnished to do so, subject to the following conditions: 14 | * 15 | * The above copyright notice and this permission notice shall be included in 16 | * all copies or substantial portions of the Software. 17 | * 18 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 19 | * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 20 | * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 21 | * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 22 | * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 23 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 24 | * THE SOFTWARE. 25 | * 26 | * bjoern@cs.stanford.edu 12/30/2008 27 | */ 28 | 29 | #include "utility/w5100.h" 30 | #include "utility/socket.h" 31 | #include "Ethernet.h" 32 | #include "Udp.h" 33 | #include "Dns.h" 34 | 35 | /* Constructor */ 36 | EthernetUDP::EthernetUDP() : _sock(MAX_SOCK_NUM) {} 37 | 38 | /* Start EthernetUDP socket, listening at local port PORT */ 39 | uint8_t EthernetUDP::begin(uint16_t port) { 40 | if (_sock != MAX_SOCK_NUM) 41 | return 0; 42 | 43 | for (int i = 0; i < MAX_SOCK_NUM; i++) { 44 | uint8_t s = W5100.readSnSR(i); 45 | if (s == SnSR::CLOSED || s == SnSR::FIN_WAIT) { 46 | _sock = i; 47 | break; 48 | } 49 | } 50 | 51 | if (_sock == MAX_SOCK_NUM) 52 | return 0; 53 | 54 | _port = port; 55 | _remaining = 0; 56 | socket(_sock, SnMR::UDP, _port, 0); 57 | 58 | return 1; 59 | } 60 | 61 | /* return number of bytes available in the current packet, 62 | will return zero if parsePacket hasn't been called yet */ 63 | int EthernetUDP::available() { 64 | return _remaining; 65 | } 66 | 67 | /* Release any resources being used by this EthernetUDP instance */ 68 | void EthernetUDP::stop() 69 | { 70 | if (_sock == MAX_SOCK_NUM) 71 | return; 72 | 73 | close(_sock); 74 | 75 | EthernetClass::_server_port[_sock] = 0; 76 | _sock = MAX_SOCK_NUM; 77 | } 78 | 79 | int EthernetUDP::beginPacket(const char *host, uint16_t port) 80 | { 81 | // Look up the host first 82 | int ret = 0; 83 | DNSClient dns; 84 | IPAddress remote_addr; 85 | 86 | dns.begin(Ethernet.dnsServerIP()); 87 | ret = dns.getHostByName(host, remote_addr); 88 | if (ret == 1) { 89 | return beginPacket(remote_addr, port); 90 | } else { 91 | return ret; 92 | } 93 | } 94 | 95 | int EthernetUDP::beginPacket(IPAddress ip, uint16_t port) 96 | { 97 | _offset = 0; 98 | return startUDP(_sock, rawIPAddress(ip), port); 99 | } 100 | 101 | int EthernetUDP::endPacket() 102 | { 103 | return sendUDP(_sock); 104 | } 105 | 106 | size_t EthernetUDP::write(uint8_t byte) 107 | { 108 | return write(&byte, 1); 109 | } 110 | 111 | size_t EthernetUDP::write(const uint8_t *buffer, size_t size) 112 | { 113 | uint16_t bytes_written = bufferData(_sock, _offset, buffer, size); 114 | _offset += bytes_written; 115 | return bytes_written; 116 | } 117 | 118 | int EthernetUDP::parsePacket() 119 | { 120 | // discard any remaining bytes in the last packet 121 | flush(); 122 | 123 | if (W5100.getRXReceivedSize(_sock) > 0) 124 | { 125 | //HACK - hand-parse the UDP packet using TCP recv method 126 | uint8_t tmpBuf[8]; 127 | int ret =0; 128 | //read 8 header bytes and get IP and port from it 129 | ret = recv(_sock,tmpBuf,8); 130 | if (ret > 0) 131 | { 132 | _remoteIP = tmpBuf; 133 | _remotePort = tmpBuf[4]; 134 | _remotePort = (_remotePort << 8) + tmpBuf[5]; 135 | _remaining = tmpBuf[6]; 136 | _remaining = (_remaining << 8) + tmpBuf[7]; 137 | 138 | // When we get here, any remaining bytes are the data 139 | ret = _remaining; 140 | } 141 | return ret; 142 | } 143 | // There aren't any packets available 144 | return 0; 145 | } 146 | 147 | int EthernetUDP::read() 148 | { 149 | uint8_t byte; 150 | 151 | if ((_remaining > 0) && (recv(_sock, &byte, 1) > 0)) 152 | { 153 | // We read things without any problems 154 | _remaining--; 155 | return byte; 156 | } 157 | 158 | // If we get here, there's no data available 159 | return -1; 160 | } 161 | 162 | int EthernetUDP::read(unsigned char* buffer, size_t len) 163 | { 164 | 165 | if (_remaining > 0) 166 | { 167 | 168 | int got; 169 | 170 | if (_remaining <= len) 171 | { 172 | // data should fit in the buffer 173 | got = recv(_sock, buffer, _remaining); 174 | } 175 | else 176 | { 177 | // too much data for the buffer, 178 | // grab as much as will fit 179 | got = recv(_sock, buffer, len); 180 | } 181 | 182 | if (got > 0) 183 | { 184 | _remaining -= got; 185 | return got; 186 | } 187 | 188 | } 189 | 190 | // If we get here, there's no data available or recv failed 191 | return -1; 192 | 193 | } 194 | 195 | int EthernetUDP::peek() 196 | { 197 | uint8_t b; 198 | // Unlike recv, peek doesn't check to see if there's any data available, so we must. 199 | // If the user hasn't called parsePacket yet then return nothing otherwise they 200 | // may get the UDP header 201 | if (!_remaining) 202 | return -1; 203 | ::peek(_sock, &b); 204 | return b; 205 | } 206 | 207 | void EthernetUDP::flush() 208 | { 209 | // could this fail (loop endlessly) if _remaining > 0 and recv in read fails? 210 | // should only occur if recv fails after telling us the data is there, lets 211 | // hope the w5100 always behaves :) 212 | 213 | while (_remaining) 214 | { 215 | read(); 216 | } 217 | } 218 | 219 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/utility/w5200.cpp: -------------------------------------------------------------------------------- 1 | /* 2 | * Copyright (c) 2013 by WIZnet 3 | * 4 | * This file is free software; you can redistribute it and/or modify 5 | * it under the terms of either the GNU General Public License version 2 6 | * or the GNU Lesser General Public License version 2.1, both as 7 | * published by the Free Software Foundation. 8 | */ 9 | 10 | #include 11 | #include 12 | #include "utility/w5100.h" 13 | 14 | #if defined(W5200_ETHERNET_SHIELD) 15 | // W5200 controller instance 16 | W5200Class W5100; 17 | 18 | #define SPI_CS 10 19 | 20 | #define TX_RX_MAX_BUF_SIZE 2048 21 | #define TX_BUF 0x1100 22 | #define RX_BUF (TX_BUF + TX_RX_MAX_BUF_SIZE) 23 | 24 | 25 | #define TXBUF_BASE 0x8000 26 | #define RXBUF_BASE 0xC000 27 | 28 | void W5200Class::init(void) 29 | { 30 | delay(300); 31 | 32 | #if defined(ARDUINO_ARCH_AVR) 33 | SPI.begin(); 34 | initSS(); 35 | #else 36 | SPI.begin(SPI_CS); 37 | // Set clock to 4Mhz (W5100 should support up to about 14Mhz) 38 | // SPI.setClockDivider(SPI_CS, 21); 39 | // SPI.setClockDivider(SPI_CS, 6); // 14 Mhz, ok 40 | // SPI.setClockDivider(SPI_CS, 3); // 28 Mhz, ok 41 | SPI.setClockDivider(SPI_CS, 2); // 42 Mhz, ok 42 | SPI.setDataMode(SPI_CS, SPI_MODE0); 43 | #endif 44 | 45 | writeMR(1< SSIZE) 96 | { 97 | // Wrap around circular buffer 98 | uint16_t size = SSIZE - offset; 99 | write(dstAddr, data, size); 100 | write(SBASE[s], data + size, len - size); 101 | } 102 | else { 103 | write(dstAddr, data, len); 104 | } 105 | 106 | ptr += len; 107 | writeSnTX_WR(s, ptr); 108 | } 109 | 110 | 111 | void W5200Class::recv_data_processing(SOCKET s, uint8_t *data, uint16_t len, uint8_t peek) 112 | { 113 | uint16_t ptr; 114 | ptr = readSnRX_RD(s); 115 | read_data(s, ptr, data, len); 116 | if (!peek) 117 | { 118 | ptr += len; 119 | writeSnRX_RD(s, ptr); 120 | } 121 | } 122 | 123 | void W5200Class::read_data(SOCKET s, volatile uint16_t src, volatile uint8_t *dst, uint16_t len) 124 | { 125 | uint16_t size; 126 | uint16_t src_mask; 127 | uint16_t src_ptr; 128 | 129 | src_mask = src & RMASK; 130 | src_ptr = RBASE[s] + src_mask; 131 | 132 | if( (src_mask + len) > RSIZE ) 133 | { 134 | size = RSIZE - src_mask; 135 | read(src_ptr, (uint8_t *)dst, size); 136 | dst += size; 137 | read(RBASE[s], (uint8_t *) dst, len - size); 138 | } 139 | else 140 | read(src_ptr, (uint8_t *) dst, len); 141 | } 142 | 143 | 144 | uint8_t W5200Class::write(uint16_t _addr, uint8_t _data) 145 | { 146 | #if defined(ARDUINO_ARCH_AVR) 147 | setSS(); 148 | SPI.transfer(_addr >> 8); 149 | SPI.transfer(_addr & 0xFF); 150 | SPI.transfer(0x80); 151 | SPI.transfer(0x01); 152 | SPI.transfer(_data); 153 | resetSS(); 154 | #else 155 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 156 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 157 | SPI.transfer(SPI_CS, 0x80, SPI_CONTINUE); 158 | SPI.transfer(SPI_CS, 0x01, SPI_CONTINUE); 159 | SPI.transfer(SPI_CS, _data); 160 | #endif 161 | 162 | return 1; 163 | } 164 | 165 | uint16_t W5200Class::write(uint16_t _addr, const uint8_t *_buf, uint16_t _len) 166 | { 167 | if (_len == 0) //Fix: a write request with _len == 0 hangs the W5200 168 | return 0; 169 | 170 | #if defined(ARDUINO_ARCH_AVR) 171 | setSS(); 172 | SPI.transfer(_addr >> 8); 173 | SPI.transfer(_addr & 0xFF); 174 | SPI.transfer((0x80 | ((_len & 0x7F00) >> 8))); 175 | SPI.transfer(_len & 0x00FF); 176 | 177 | for (uint16_t i=0; i<_len; i++) 178 | { 179 | SPI.transfer(_buf[i]); 180 | } 181 | resetSS(); 182 | #else 183 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 184 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 185 | SPI.transfer(SPI_CS, (0x80 | ((_len & 0x7F00) >> 8)), SPI_CONTINUE); 186 | SPI.transfer(SPI_CS, _len & 0x00FF, SPI_CONTINUE); 187 | 188 | for (uint16_t i=0; i<(_len-1); i++) 189 | { 190 | SPI.transfer(SPI_CS, _buf[i], SPI_CONTINUE); 191 | } 192 | SPI.transfer(SPI_CS, _buf[_len-1]); 193 | 194 | #endif 195 | 196 | return _len; 197 | } 198 | 199 | uint8_t W5200Class::read(uint16_t _addr) 200 | { 201 | 202 | #if defined(ARDUINO_ARCH_AVR) 203 | setSS(); 204 | SPI.transfer(_addr >> 8); 205 | SPI.transfer(_addr & 0xFF); 206 | SPI.transfer(0x00); 207 | SPI.transfer(0x01); 208 | uint8_t _data = SPI.transfer(0); 209 | resetSS(); 210 | #else 211 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 212 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 213 | SPI.transfer(SPI_CS, 0x00, SPI_CONTINUE); 214 | SPI.transfer(SPI_CS, 0x01, SPI_CONTINUE); 215 | uint8_t _data = SPI.transfer(SPI_CS, 0); 216 | #endif 217 | return _data; 218 | } 219 | 220 | uint16_t W5200Class::read(uint16_t _addr, uint8_t *_buf, uint16_t _len) 221 | { 222 | 223 | #if defined(ARDUINO_ARCH_AVR) 224 | setSS(); 225 | SPI.transfer(_addr >> 8); 226 | SPI.transfer(_addr & 0xFF); 227 | SPI.transfer((0x00 | ((_len & 0x7F00) >> 8))); 228 | SPI.transfer(_len & 0x00FF); 229 | 230 | for (uint16_t i=0; i<_len; i++) 231 | { 232 | _buf[i] = SPI.transfer(0); 233 | 234 | } 235 | resetSS(); 236 | #else 237 | SPI.transfer(SPI_CS, _addr >> 8, SPI_CONTINUE); 238 | SPI.transfer(SPI_CS, _addr & 0xFF, SPI_CONTINUE); 239 | SPI.transfer(SPI_CS, (0x00 | ((_len & 0x7F00) >> 8)), SPI_CONTINUE); 240 | SPI.transfer(SPI_CS, _len & 0x00FF, SPI_CONTINUE); 241 | for (uint16_t i=0; i<(_len-1); i++) 242 | { 243 | _buf[i] = SPI.transfer(SPI_CS, 0, SPI_CONTINUE); 244 | } 245 | _buf[_len-1] = SPI.transfer(SPI_CS, 0); 246 | 247 | #endif 248 | return _len; 249 | } 250 | 251 | void W5200Class::execCmdSn(SOCKET s, SockCMD _cmd) { 252 | // Send command to socket 253 | writeSnCR(s, _cmd); 254 | // Wait for command to complete 255 | while (readSnCR(s)) 256 | ; 257 | } 258 | #endif 259 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/examples/BarometricPressureWebServer/BarometricPressureWebServer.ino: -------------------------------------------------------------------------------- 1 | /* 2 | SCP1000 Barometric Pressure Sensor Display 3 | 4 | Serves the output of a Barometric Pressure Sensor as a web page. 5 | Uses the SPI library. For details on the sensor, see: 6 | http://www.sparkfun.com/commerce/product_info.php?products_id=8161 7 | http://www.vti.fi/en/support/obsolete_products/pressure_sensors/ 8 | 9 | This sketch adapted from Nathan Seidle's SCP1000 example for PIC: 10 | http://www.sparkfun.com/datasheets/Sensors/SCP1000-Testing.zip 11 | 12 | Circuit: 13 | SCP1000 sensor attached to pins 6,7, and 11 - 13: 14 | DRDY: pin 6 15 | CSB: pin 7 16 | MOSI: pin 11 17 | MISO: pin 12 18 | SCK: pin 13 19 | 20 | created 31 July 2010 21 | by Tom Igoe 22 | modified 12 Aug 2013 23 | by Soohwan Kim 24 | */ 25 | 26 | #include 27 | // the sensor communicates using SPI, so include the library: 28 | #include 29 | 30 | 31 | // assign a MAC address for the ethernet controller. 32 | // fill in your address here: 33 | 34 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 35 | ; 36 | #else 37 | byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED}; 38 | #endif 39 | 40 | // assign an IP address for the controller: 41 | IPAddress ip(192,168,1,20); 42 | IPAddress gateway(192,168,1,1); 43 | IPAddress subnet(255, 255, 255, 0); 44 | 45 | 46 | // Initialize the Ethernet server library 47 | // with the IP address and port you want to use 48 | // (port 80 is default for HTTP): 49 | EthernetServer server(80); 50 | 51 | 52 | //Sensor's memory register addresses: 53 | const int PRESSURE = 0x1F; //3 most significant bits of pressure 54 | const int PRESSURE_LSB = 0x20; //16 least significant bits of pressure 55 | const int TEMPERATURE = 0x21; //16 bit temperature reading 56 | 57 | // pins used for the connection with the sensor 58 | // the others you need are controlled by the SPI library): 59 | const int dataReadyPin = 6; 60 | const int chipSelectPin = 7; 61 | 62 | float temperature = 0.0; 63 | long pressure = 0; 64 | long lastReadingTime = 0; 65 | 66 | void setup() { 67 | // start the SPI library: 68 | SPI.begin(); 69 | 70 | // start the Ethernet connection and the server: 71 | #if defined(WIZ550io_WITH_MACADDRESS) // Use assigned MAC address of WIZ550io 72 | Ethernet.begin(ip); 73 | #else 74 | Ethernet.begin(mac, ip); 75 | #endif 76 | 77 | server.begin(); 78 | // initalize the data ready and chip select pins: 79 | pinMode(dataReadyPin, INPUT); 80 | pinMode(chipSelectPin, OUTPUT); 81 | 82 | Serial.begin(9600); 83 | 84 | //Configure SCP1000 for low noise configuration: 85 | writeRegister(0x02, 0x2D); 86 | writeRegister(0x01, 0x03); 87 | writeRegister(0x03, 0x02); 88 | 89 | // give the sensor and Ethernet shield time to set up: 90 | delay(1000); 91 | 92 | //Set the sensor to high resolution mode tp start readings: 93 | writeRegister(0x03, 0x0A); 94 | 95 | } 96 | 97 | void loop() { 98 | // check for a reading no more than once a second. 99 | if (millis() - lastReadingTime > 1000){ 100 | // if there's a reading ready, read it: 101 | // don't do anything until the data ready pin is high: 102 | if (digitalRead(dataReadyPin) == HIGH) { 103 | getData(); 104 | // timestamp the last time you got a reading: 105 | lastReadingTime = millis(); 106 | } 107 | } 108 | 109 | // listen for incoming Ethernet connections: 110 | listenForEthernetClients(); 111 | } 112 | 113 | 114 | void getData() { 115 | Serial.println("Getting reading"); 116 | //Read the temperature data 117 | int tempData = readRegister(0x21, 2); 118 | 119 | // convert the temperature to celsius and display it: 120 | temperature = (float)tempData / 20.0; 121 | 122 | //Read the pressure data highest 3 bits: 123 | byte pressureDataHigh = readRegister(0x1F, 1); 124 | pressureDataHigh &= 0b00000111; //you only needs bits 2 to 0 125 | 126 | //Read the pressure data lower 16 bits: 127 | unsigned int pressureDataLow = readRegister(0x20, 2); 128 | //combine the two parts into one 19-bit number: 129 | pressure = ((pressureDataHigh << 16) | pressureDataLow)/4; 130 | 131 | Serial.print("Temperature: "); 132 | Serial.print(temperature); 133 | Serial.println(" degrees C"); 134 | Serial.print("Pressure: " + String(pressure)); 135 | Serial.println(" Pa"); 136 | } 137 | 138 | void listenForEthernetClients() { 139 | // listen for incoming clients 140 | EthernetClient client = server.available(); 141 | if (client) { 142 | Serial.println("Got a client"); 143 | // an http request ends with a blank line 144 | boolean currentLineIsBlank = true; 145 | while (client.connected()) { 146 | if (client.available()) { 147 | char c = client.read(); 148 | // if you've gotten to the end of the line (received a newline 149 | // character) and the line is blank, the http request has ended, 150 | // so you can send a reply 151 | if (c == '\n' && currentLineIsBlank) { 152 | // send a standard http response header 153 | client.println("HTTP/1.1 200 OK"); 154 | client.println("Content-Type: text/html"); 155 | client.println(); 156 | // print the current readings, in HTML format: 157 | client.print("Temperature: "); 158 | client.print(temperature); 159 | client.print(" degrees C"); 160 | client.println("
"); 161 | client.print("Pressure: " + String(pressure)); 162 | client.print(" Pa"); 163 | client.println("
"); 164 | break; 165 | } 166 | if (c == '\n') { 167 | // you're starting a new line 168 | currentLineIsBlank = true; 169 | } 170 | else if (c != '\r') { 171 | // you've gotten a character on the current line 172 | currentLineIsBlank = false; 173 | } 174 | } 175 | } 176 | // give the web browser time to receive the data 177 | delay(1); 178 | // close the connection: 179 | client.stop(); 180 | } 181 | } 182 | 183 | 184 | //Send a write command to SCP1000 185 | void writeRegister(byte registerName, byte registerValue) { 186 | // SCP1000 expects the register name in the upper 6 bits 187 | // of the byte: 188 | registerName <<= 2; 189 | // command (read or write) goes in the lower two bits: 190 | registerName |= 0b00000010; //Write command 191 | 192 | // take the chip select low to select the device: 193 | digitalWrite(chipSelectPin, LOW); 194 | 195 | SPI.transfer(registerName); //Send register location 196 | SPI.transfer(registerValue); //Send value to record into register 197 | 198 | // take the chip select high to de-select: 199 | digitalWrite(chipSelectPin, HIGH); 200 | } 201 | 202 | 203 | //Read register from the SCP1000: 204 | unsigned int readRegister(byte registerName, int numBytes) { 205 | byte inByte = 0; // incoming from the SPI read 206 | unsigned int result = 0; // result to return 207 | 208 | // SCP1000 expects the register name in the upper 6 bits 209 | // of the byte: 210 | registerName <<= 2; 211 | // command (read or write) goes in the lower two bits: 212 | registerName &= 0b11111100; //Read command 213 | 214 | // take the chip select low to select the device: 215 | digitalWrite(chipSelectPin, LOW); 216 | // send the device the register you want to read: 217 | int command = SPI.transfer(registerName); 218 | // send a value of 0 to read the first byte returned: 219 | inByte = SPI.transfer(0x00); 220 | 221 | result = inByte; 222 | // if there's more than one byte returned, 223 | // shift the first byte then get the second byte: 224 | if (numBytes > 1){ 225 | result = inByte << 8; 226 | inByte = SPI.transfer(0x00); 227 | result = result |inByte; 228 | } 229 | // take the chip select high to de-select: 230 | digitalWrite(chipSelectPin, HIGH); 231 | // return the result: 232 | return(result); 233 | } 234 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/utility/socket.cpp: -------------------------------------------------------------------------------- 1 | #include "w5100.h" 2 | #include "socket.h" 3 | 4 | static uint16_t local_port; 5 | 6 | /** 7 | * @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it. 8 | * @return 1 for success else 0. 9 | */ 10 | uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag) 11 | { 12 | if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE)) 13 | { 14 | close(s); 15 | W5100.writeSnMR(s, protocol | flag); 16 | if (port != 0) { 17 | W5100.writeSnPORT(s, port); 18 | } 19 | else { 20 | local_port++; // if don't set the source port, set local_port number. 21 | W5100.writeSnPORT(s, local_port); 22 | } 23 | 24 | W5100.execCmdSn(s, Sock_OPEN); 25 | 26 | return 1; 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | 33 | /** 34 | * @brief This function close the socket and parameter is "s" which represent the socket number 35 | */ 36 | void close(SOCKET s) 37 | { 38 | W5100.execCmdSn(s, Sock_CLOSE); 39 | W5100.writeSnIR(s, 0xFF); 40 | } 41 | 42 | 43 | /** 44 | * @brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer. 45 | * @return 1 for success else 0. 46 | */ 47 | uint8_t listen(SOCKET s) 48 | { 49 | if (W5100.readSnSR(s) != SnSR::INIT) 50 | return 0; 51 | W5100.execCmdSn(s, Sock_LISTEN); 52 | return 1; 53 | } 54 | 55 | 56 | /** 57 | * @brief This function established the connection for the channel in Active (client) mode. 58 | * This function waits for the untill the connection is established. 59 | * 60 | * @return 1 for success else 0. 61 | */ 62 | uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port) 63 | { 64 | if 65 | ( 66 | ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) || 67 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || 68 | (port == 0x00) 69 | ) 70 | return 0; 71 | 72 | // set destination IP 73 | W5100.writeSnDIPR(s, addr); 74 | W5100.writeSnDPORT(s, port); 75 | W5100.execCmdSn(s, Sock_CONNECT); 76 | 77 | return 1; 78 | } 79 | 80 | 81 | 82 | /** 83 | * @brief This function used for disconnect the socket and parameter is "s" which represent the socket number 84 | * @return 1 for success else 0. 85 | */ 86 | void disconnect(SOCKET s) 87 | { 88 | W5100.execCmdSn(s, Sock_DISCON); 89 | } 90 | 91 | 92 | /** 93 | * @brief This function used to send the data in TCP mode 94 | * @return 1 for success else 0. 95 | */ 96 | uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len) 97 | { 98 | uint8_t status=0; 99 | uint16_t ret=0; 100 | uint16_t freesize=0; 101 | 102 | if (len > W5100.SSIZE) 103 | ret = W5100.SSIZE; // check size not to exceed MAX size. 104 | else 105 | ret = len; 106 | 107 | // if freebuf is available, start. 108 | do 109 | { 110 | freesize = W5100.getTXFreeSize(s); 111 | status = W5100.readSnSR(s); 112 | if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT)) 113 | { 114 | ret = 0; 115 | break; 116 | } 117 | } 118 | while (freesize < ret); 119 | 120 | // copy data 121 | W5100.send_data_processing(s, (uint8_t *)buf, ret); 122 | W5100.execCmdSn(s, Sock_SEND); 123 | 124 | /* +2008.01 bj */ 125 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 126 | { 127 | /* m2008.01 [bj] : reduce code */ 128 | if ( W5100.readSnSR(s) == SnSR::CLOSED ) 129 | { 130 | close(s); 131 | return 0; 132 | } 133 | } 134 | /* +2008.01 bj */ 135 | W5100.writeSnIR(s, SnIR::SEND_OK); 136 | return ret; 137 | } 138 | 139 | 140 | /** 141 | * @brief This function is an application I/F function which is used to receive the data in TCP mode. 142 | * It continues to wait for data as much as the application wants to receive. 143 | * 144 | * @return received data size for success else -1. 145 | */ 146 | int16_t recv(SOCKET s, uint8_t *buf, int16_t len) 147 | { 148 | // Check how much data is available 149 | int16_t ret = W5100.getRXReceivedSize(s); 150 | if ( ret == 0 ) 151 | { 152 | // No data available. 153 | uint8_t status = W5100.readSnSR(s); 154 | if ( status == SnSR::LISTEN || status == SnSR::CLOSED || status == SnSR::CLOSE_WAIT ) 155 | { 156 | // The remote end has closed its side of the connection, so this is the eof state 157 | ret = 0; 158 | } 159 | else 160 | { 161 | // The connection is still up, but there's no data waiting to be read 162 | ret = -1; 163 | } 164 | } 165 | else if (ret > len) 166 | { 167 | ret = len; 168 | } 169 | 170 | if ( ret > 0 ) 171 | { 172 | W5100.recv_data_processing(s, buf, ret); 173 | W5100.execCmdSn(s, Sock_RECV); 174 | } 175 | return ret; 176 | } 177 | 178 | 179 | /** 180 | * @brief Returns the first byte in the receive queue (no checking) 181 | * 182 | * @return 183 | */ 184 | uint16_t peek(SOCKET s, uint8_t *buf) 185 | { 186 | W5100.recv_data_processing(s, buf, 1, 1); 187 | 188 | return 1; 189 | } 190 | 191 | 192 | /** 193 | * @brief This function is an application I/F function which is used to send the data for other then TCP mode. 194 | * Unlike TCP transmission, The peer's destination address and the port is needed. 195 | * 196 | * @return This function return send data size for success else -1. 197 | */ 198 | uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t port) 199 | { 200 | uint16_t ret=0; 201 | 202 | if (len > W5100.SSIZE) ret = W5100.SSIZE; // check size not to exceed MAX size. 203 | else ret = len; 204 | 205 | if 206 | ( 207 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || 208 | ((port == 0x00)) ||(ret == 0) 209 | ) 210 | { 211 | /* +2008.01 [bj] : added return value */ 212 | ret = 0; 213 | } 214 | else 215 | { 216 | W5100.writeSnDIPR(s, addr); 217 | W5100.writeSnDPORT(s, port); 218 | 219 | // copy data 220 | W5100.send_data_processing(s, (uint8_t *)buf, ret); 221 | W5100.execCmdSn(s, Sock_SEND); 222 | 223 | /* +2008.01 bj */ 224 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 225 | { 226 | if (W5100.readSnIR(s) & SnIR::TIMEOUT) 227 | { 228 | /* +2008.01 [bj]: clear interrupt */ 229 | W5100.writeSnIR(s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */ 230 | return 0; 231 | } 232 | } 233 | 234 | /* +2008.01 bj */ 235 | W5100.writeSnIR(s, SnIR::SEND_OK); 236 | } 237 | return ret; 238 | } 239 | 240 | 241 | /** 242 | * @brief This function is an application I/F function which is used to receive the data in other then 243 | * TCP mode. This function is used to receive UDP, IP_RAW and MAC_RAW mode, and handle the header as well. 244 | * 245 | * @return This function return received data size for success else -1. 246 | */ 247 | uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t *port) 248 | { 249 | uint8_t head[8]; 250 | uint16_t data_len=0; 251 | uint16_t ptr=0; 252 | 253 | if ( len > 0 ) 254 | { 255 | ptr = W5100.readSnRX_RD(s); 256 | switch (W5100.readSnMR(s) & 0x07) 257 | { 258 | case SnMR::UDP : 259 | W5100.read_data(s, (uint8_t *)ptr, head, 0x08); 260 | ptr += 8; 261 | // read peer's IP address, port number. 262 | addr[0] = head[0]; 263 | addr[1] = head[1]; 264 | addr[2] = head[2]; 265 | addr[3] = head[3]; 266 | *port = head[4]; 267 | *port = (*port << 8) + head[5]; 268 | data_len = head[6]; 269 | data_len = (data_len << 8) + head[7]; 270 | 271 | W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy. 272 | ptr += data_len; 273 | 274 | W5100.writeSnRX_RD(s, ptr); 275 | break; 276 | 277 | case SnMR::IPRAW : 278 | W5100.read_data(s, (uint8_t *)ptr, head, 0x06); 279 | ptr += 6; 280 | 281 | addr[0] = head[0]; 282 | addr[1] = head[1]; 283 | addr[2] = head[2]; 284 | addr[3] = head[3]; 285 | data_len = head[4]; 286 | data_len = (data_len << 8) + head[5]; 287 | 288 | W5100.read_data(s, (uint8_t *)ptr, buf, data_len); // data copy. 289 | ptr += data_len; 290 | 291 | W5100.writeSnRX_RD(s, ptr); 292 | break; 293 | 294 | case SnMR::MACRAW: 295 | W5100.read_data(s,(uint8_t*)ptr,head,2); 296 | ptr+=2; 297 | data_len = head[0]; 298 | data_len = (data_len<<8) + head[1] - 2; 299 | 300 | W5100.read_data(s,(uint8_t*) ptr,buf,data_len); 301 | ptr += data_len; 302 | W5100.writeSnRX_RD(s, ptr); 303 | break; 304 | 305 | default : 306 | break; 307 | } 308 | W5100.execCmdSn(s, Sock_RECV); 309 | } 310 | return data_len; 311 | } 312 | 313 | 314 | uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len) 315 | { 316 | uint8_t status=0; 317 | uint16_t ret=0; 318 | 319 | if (len > W5100.SSIZE) 320 | ret = W5100.SSIZE; // check size not to exceed MAX size. 321 | else 322 | ret = len; 323 | 324 | if (ret == 0) 325 | return 0; 326 | 327 | W5100.send_data_processing(s, (uint8_t *)buf, ret); 328 | W5100.execCmdSn(s, Sock_SEND); 329 | 330 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 331 | { 332 | status = W5100.readSnSR(s); 333 | if (W5100.readSnIR(s) & SnIR::TIMEOUT) 334 | { 335 | /* in case of igmp, if send fails, then socket closed */ 336 | /* if you want change, remove this code. */ 337 | close(s); 338 | return 0; 339 | } 340 | } 341 | 342 | W5100.writeSnIR(s, SnIR::SEND_OK); 343 | return ret; 344 | } 345 | 346 | uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len) 347 | { 348 | uint16_t ret =0; 349 | if (len > W5100.getTXFreeSize(s)) 350 | { 351 | ret = W5100.getTXFreeSize(s); // check size not to exceed MAX size. 352 | } 353 | else 354 | { 355 | ret = len; 356 | } 357 | W5100.send_data_processing_offset(s, offset, buf, ret); 358 | return ret; 359 | } 360 | 361 | int startUDP(SOCKET s, uint8_t* addr, uint16_t port) 362 | { 363 | if 364 | ( 365 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || 366 | ((port == 0x00)) 367 | ) 368 | { 369 | return 0; 370 | } 371 | else 372 | { 373 | W5100.writeSnDIPR(s, addr); 374 | W5100.writeSnDPORT(s, port); 375 | return 1; 376 | } 377 | } 378 | 379 | int sendUDP(SOCKET s) 380 | { 381 | W5100.execCmdSn(s, Sock_SEND); 382 | 383 | /* +2008.01 bj */ 384 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 385 | { 386 | if (W5100.readSnIR(s) & SnIR::TIMEOUT) 387 | { 388 | /* +2008.01 [bj]: clear interrupt */ 389 | W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT)); 390 | return 0; 391 | } 392 | } 393 | 394 | /* +2008.01 bj */ 395 | W5100.writeSnIR(s, SnIR::SEND_OK); 396 | 397 | /* Sent ok */ 398 | return 1; 399 | } 400 | 401 | -------------------------------------------------------------------------------- /Arduino IDE 1.5.x/Ethernet/src/utility/socket.cpp: -------------------------------------------------------------------------------- 1 | #include "utility/w5100.h" 2 | #include "utility/socket.h" 3 | 4 | static uint16_t local_port; 5 | 6 | /** 7 | * @brief This Socket function initialize the channel in perticular mode, and set the port and wait for W5100 done it. 8 | * @return 1 for success else 0. 9 | */ 10 | uint8_t socket(SOCKET s, uint8_t protocol, uint16_t port, uint8_t flag) 11 | { 12 | if ((protocol == SnMR::TCP) || (protocol == SnMR::UDP) || (protocol == SnMR::IPRAW) || (protocol == SnMR::MACRAW) || (protocol == SnMR::PPPOE)) 13 | { 14 | close(s); 15 | W5100.writeSnMR(s, protocol | flag); 16 | if (port != 0) { 17 | W5100.writeSnPORT(s, port); 18 | } 19 | else { 20 | local_port++; // if don't set the source port, set local_port number. 21 | W5100.writeSnPORT(s, local_port); 22 | } 23 | 24 | W5100.execCmdSn(s, Sock_OPEN); 25 | 26 | return 1; 27 | } 28 | 29 | return 0; 30 | } 31 | 32 | 33 | /** 34 | * @brief This function close the socket and parameter is "s" which represent the socket number 35 | */ 36 | void close(SOCKET s) 37 | { 38 | W5100.execCmdSn(s, Sock_CLOSE); 39 | W5100.writeSnIR(s, 0xFF); 40 | } 41 | 42 | 43 | /** 44 | * @brief This function established the connection for the channel in passive (server) mode. This function waits for the request from the peer. 45 | * @return 1 for success else 0. 46 | */ 47 | uint8_t listen(SOCKET s) 48 | { 49 | if (W5100.readSnSR(s) != SnSR::INIT) 50 | return 0; 51 | W5100.execCmdSn(s, Sock_LISTEN); 52 | return 1; 53 | } 54 | 55 | 56 | /** 57 | * @brief This function established the connection for the channel in Active (client) mode. 58 | * This function waits for the untill the connection is established. 59 | * 60 | * @return 1 for success else 0. 61 | */ 62 | uint8_t connect(SOCKET s, uint8_t * addr, uint16_t port) 63 | { 64 | if 65 | ( 66 | ((addr[0] == 0xFF) && (addr[1] == 0xFF) && (addr[2] == 0xFF) && (addr[3] == 0xFF)) || 67 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || 68 | (port == 0x00) 69 | ) 70 | return 0; 71 | 72 | // set destination IP 73 | W5100.writeSnDIPR(s, addr); 74 | W5100.writeSnDPORT(s, port); 75 | W5100.execCmdSn(s, Sock_CONNECT); 76 | 77 | return 1; 78 | } 79 | 80 | 81 | 82 | /** 83 | * @brief This function used for disconnect the socket and parameter is "s" which represent the socket number 84 | * @return 1 for success else 0. 85 | */ 86 | void disconnect(SOCKET s) 87 | { 88 | W5100.execCmdSn(s, Sock_DISCON); 89 | } 90 | 91 | 92 | /** 93 | * @brief This function used to send the data in TCP mode 94 | * @return 1 for success else 0. 95 | */ 96 | uint16_t send(SOCKET s, const uint8_t * buf, uint16_t len) 97 | { 98 | uint8_t status=0; 99 | uint16_t ret=0; 100 | uint16_t freesize=0; 101 | 102 | if (len > W5100.SSIZE) 103 | ret = W5100.SSIZE; // check size not to exceed MAX size. 104 | else 105 | ret = len; 106 | 107 | // if freebuf is available, start. 108 | do 109 | { 110 | freesize = W5100.getTXFreeSize(s); 111 | status = W5100.readSnSR(s); 112 | if ((status != SnSR::ESTABLISHED) && (status != SnSR::CLOSE_WAIT)) 113 | { 114 | ret = 0; 115 | break; 116 | } 117 | } 118 | while (freesize < ret); 119 | 120 | // copy data 121 | W5100.send_data_processing(s, (uint8_t *)buf, ret); 122 | W5100.execCmdSn(s, Sock_SEND); 123 | 124 | /* +2008.01 bj */ 125 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 126 | { 127 | /* m2008.01 [bj] : reduce code */ 128 | if ( W5100.readSnSR(s) == SnSR::CLOSED ) 129 | { 130 | close(s); 131 | return 0; 132 | } 133 | } 134 | /* +2008.01 bj */ 135 | W5100.writeSnIR(s, SnIR::SEND_OK); 136 | return ret; 137 | } 138 | 139 | 140 | /** 141 | * @brief This function is an application I/F function which is used to receive the data in TCP mode. 142 | * It continues to wait for data as much as the application wants to receive. 143 | * 144 | * @return received data size for success else -1. 145 | */ 146 | int16_t recv(SOCKET s, uint8_t *buf, int16_t len) 147 | { 148 | // Check how much data is available 149 | int16_t ret = W5100.getRXReceivedSize(s); 150 | if ( ret == 0 ) 151 | { 152 | // No data available. 153 | uint8_t status = W5100.readSnSR(s); 154 | if ( status == SnSR::LISTEN || status == SnSR::CLOSED || status == SnSR::CLOSE_WAIT ) 155 | { 156 | // The remote end has closed its side of the connection, so this is the eof state 157 | ret = 0; 158 | } 159 | else 160 | { 161 | // The connection is still up, but there's no data waiting to be read 162 | ret = -1; 163 | } 164 | } 165 | else if (ret > len) 166 | { 167 | ret = len; 168 | } 169 | 170 | if ( ret > 0 ) 171 | { 172 | W5100.recv_data_processing(s, buf, ret); 173 | W5100.execCmdSn(s, Sock_RECV); 174 | } 175 | return ret; 176 | } 177 | 178 | 179 | /** 180 | * @brief Returns the first byte in the receive queue (no checking) 181 | * 182 | * @return 183 | */ 184 | uint16_t peek(SOCKET s, uint8_t *buf) 185 | { 186 | W5100.recv_data_processing(s, buf, 1, 1); 187 | 188 | return 1; 189 | } 190 | 191 | 192 | /** 193 | * @brief This function is an application I/F function which is used to send the data for other then TCP mode. 194 | * Unlike TCP transmission, The peer's destination address and the port is needed. 195 | * 196 | * @return This function return send data size for success else -1. 197 | */ 198 | uint16_t sendto(SOCKET s, const uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t port) 199 | { 200 | uint16_t ret=0; 201 | 202 | if (len > W5100.SSIZE) ret = W5100.SSIZE; // check size not to exceed MAX size. 203 | else ret = len; 204 | 205 | if 206 | ( 207 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || 208 | ((port == 0x00)) ||(ret == 0) 209 | ) 210 | { 211 | /* +2008.01 [bj] : added return value */ 212 | ret = 0; 213 | } 214 | else 215 | { 216 | W5100.writeSnDIPR(s, addr); 217 | W5100.writeSnDPORT(s, port); 218 | 219 | // copy data 220 | W5100.send_data_processing(s, (uint8_t *)buf, ret); 221 | W5100.execCmdSn(s, Sock_SEND); 222 | 223 | /* +2008.01 bj */ 224 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 225 | { 226 | if (W5100.readSnIR(s) & SnIR::TIMEOUT) 227 | { 228 | /* +2008.01 [bj]: clear interrupt */ 229 | W5100.writeSnIR(s, (SnIR::SEND_OK | SnIR::TIMEOUT)); /* clear SEND_OK & TIMEOUT */ 230 | return 0; 231 | } 232 | } 233 | 234 | /* +2008.01 bj */ 235 | W5100.writeSnIR(s, SnIR::SEND_OK); 236 | } 237 | return ret; 238 | } 239 | 240 | 241 | /** 242 | * @brief This function is an application I/F function which is used to receive the data in other then 243 | * TCP mode. This function is used to receive UDP, IP_RAW and MAC_RAW mode, and handle the header as well. 244 | * 245 | * @return This function return received data size for success else -1. 246 | */ 247 | uint16_t recvfrom(SOCKET s, uint8_t *buf, uint16_t len, uint8_t *addr, uint16_t *port) 248 | { 249 | uint8_t head[8]; 250 | uint16_t data_len=0; 251 | uint16_t ptr=0; 252 | 253 | if ( len > 0 ) 254 | { 255 | ptr = W5100.readSnRX_RD(s); 256 | switch (W5100.readSnMR(s) & 0x07) 257 | { 258 | case SnMR::UDP : 259 | W5100.read_data(s, ptr, head, 0x08); 260 | ptr += 8; 261 | // read peer's IP address, port number. 262 | addr[0] = head[0]; 263 | addr[1] = head[1]; 264 | addr[2] = head[2]; 265 | addr[3] = head[3]; 266 | *port = head[4]; 267 | *port = (*port << 8) + head[5]; 268 | data_len = head[6]; 269 | data_len = (data_len << 8) + head[7]; 270 | 271 | W5100.read_data(s, ptr, buf, data_len); // data copy. 272 | ptr += data_len; 273 | 274 | W5100.writeSnRX_RD(s, ptr); 275 | break; 276 | 277 | case SnMR::IPRAW : 278 | W5100.read_data(s, ptr, head, 0x06); 279 | ptr += 6; 280 | 281 | addr[0] = head[0]; 282 | addr[1] = head[1]; 283 | addr[2] = head[2]; 284 | addr[3] = head[3]; 285 | data_len = head[4]; 286 | data_len = (data_len << 8) + head[5]; 287 | 288 | W5100.read_data(s, ptr, buf, data_len); // data copy. 289 | ptr += data_len; 290 | 291 | W5100.writeSnRX_RD(s, ptr); 292 | break; 293 | 294 | case SnMR::MACRAW: 295 | W5100.read_data(s, ptr, head, 2); 296 | ptr+=2; 297 | data_len = head[0]; 298 | data_len = (data_len<<8) + head[1] - 2; 299 | 300 | W5100.read_data(s, ptr, buf, data_len); 301 | ptr += data_len; 302 | W5100.writeSnRX_RD(s, ptr); 303 | break; 304 | 305 | default : 306 | break; 307 | } 308 | W5100.execCmdSn(s, Sock_RECV); 309 | } 310 | return data_len; 311 | } 312 | 313 | /** 314 | * @brief Wait for buffered transmission to complete. 315 | */ 316 | void flush(SOCKET s) { 317 | // TODO 318 | } 319 | 320 | uint16_t igmpsend(SOCKET s, const uint8_t * buf, uint16_t len) 321 | { 322 | uint8_t status=0; 323 | uint16_t ret=0; 324 | 325 | if (len > W5100.SSIZE) 326 | ret = W5100.SSIZE; // check size not to exceed MAX size. 327 | else 328 | ret = len; 329 | 330 | if (ret == 0) 331 | return 0; 332 | 333 | W5100.send_data_processing(s, (uint8_t *)buf, ret); 334 | W5100.execCmdSn(s, Sock_SEND); 335 | 336 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 337 | { 338 | status = W5100.readSnSR(s); 339 | if (W5100.readSnIR(s) & SnIR::TIMEOUT) 340 | { 341 | /* in case of igmp, if send fails, then socket closed */ 342 | /* if you want change, remove this code. */ 343 | close(s); 344 | return 0; 345 | } 346 | } 347 | 348 | W5100.writeSnIR(s, SnIR::SEND_OK); 349 | return ret; 350 | } 351 | 352 | uint16_t bufferData(SOCKET s, uint16_t offset, const uint8_t* buf, uint16_t len) 353 | { 354 | uint16_t ret =0; 355 | if (len > W5100.getTXFreeSize(s)) 356 | { 357 | ret = W5100.getTXFreeSize(s); // check size not to exceed MAX size. 358 | } 359 | else 360 | { 361 | ret = len; 362 | } 363 | W5100.send_data_processing_offset(s, offset, buf, ret); 364 | return ret; 365 | } 366 | 367 | int startUDP(SOCKET s, uint8_t* addr, uint16_t port) 368 | { 369 | if 370 | ( 371 | ((addr[0] == 0x00) && (addr[1] == 0x00) && (addr[2] == 0x00) && (addr[3] == 0x00)) || 372 | ((port == 0x00)) 373 | ) 374 | { 375 | return 0; 376 | } 377 | else 378 | { 379 | W5100.writeSnDIPR(s, addr); 380 | W5100.writeSnDPORT(s, port); 381 | return 1; 382 | } 383 | } 384 | 385 | int sendUDP(SOCKET s) 386 | { 387 | W5100.execCmdSn(s, Sock_SEND); 388 | 389 | /* +2008.01 bj */ 390 | while ( (W5100.readSnIR(s) & SnIR::SEND_OK) != SnIR::SEND_OK ) 391 | { 392 | if (W5100.readSnIR(s) & SnIR::TIMEOUT) 393 | { 394 | /* +2008.01 [bj]: clear interrupt */ 395 | W5100.writeSnIR(s, (SnIR::SEND_OK|SnIR::TIMEOUT)); 396 | return 0; 397 | } 398 | } 399 | 400 | /* +2008.01 bj */ 401 | W5100.writeSnIR(s, SnIR::SEND_OK); 402 | 403 | /* Sent ok */ 404 | return 1; 405 | } 406 | 407 | -------------------------------------------------------------------------------- /Arduino IDE 1.0.x/Ethernet/Dns.cpp: -------------------------------------------------------------------------------- 1 | // Arduino DNS client for WizNet5100-based Ethernet shield 2 | // (c) Copyright 2009-2010 MCQN Ltd. 3 | // Released under Apache License, version 2.0 4 | 5 | #include "w5100.h" 6 | #include "EthernetUdp.h" 7 | #include "util.h" 8 | 9 | #include "Dns.h" 10 | #include 11 | //#include 12 | #include "Arduino.h" 13 | 14 | 15 | #define SOCKET_NONE 255 16 | // Various flags and header field values for a DNS message 17 | #define UDP_HEADER_SIZE 8 18 | #define DNS_HEADER_SIZE 12 19 | #define TTL_SIZE 4 20 | #define QUERY_FLAG (0) 21 | #define RESPONSE_FLAG (1<<15) 22 | #define QUERY_RESPONSE_MASK (1<<15) 23 | #define OPCODE_STANDARD_QUERY (0) 24 | #define OPCODE_INVERSE_QUERY (1<<11) 25 | #define OPCODE_STATUS_REQUEST (2<<11) 26 | #define OPCODE_MASK (15<<11) 27 | #define AUTHORITATIVE_FLAG (1<<10) 28 | #define TRUNCATION_FLAG (1<<9) 29 | #define RECURSION_DESIRED_FLAG (1<<8) 30 | #define RECURSION_AVAILABLE_FLAG (1<<7) 31 | #define RESP_NO_ERROR (0) 32 | #define RESP_FORMAT_ERROR (1) 33 | #define RESP_SERVER_FAILURE (2) 34 | #define RESP_NAME_ERROR (3) 35 | #define RESP_NOT_IMPLEMENTED (4) 36 | #define RESP_REFUSED (5) 37 | #define RESP_MASK (15) 38 | #define TYPE_A (0x0001) 39 | #define CLASS_IN (0x0001) 40 | #define LABEL_COMPRESSION_MASK (0xC0) 41 | // Port number that DNS servers listen on 42 | #define DNS_PORT 53 43 | 44 | // Possible return codes from ProcessResponse 45 | #define SUCCESS 1 46 | #define TIMED_OUT -1 47 | #define INVALID_SERVER -2 48 | #define TRUNCATED -3 49 | #define INVALID_RESPONSE -4 50 | 51 | void DNSClient::begin(const IPAddress& aDNSServer) 52 | { 53 | iDNSServer = aDNSServer; 54 | iRequestId = 0; 55 | } 56 | 57 | 58 | int DNSClient::inet_aton(const char* aIPAddrString, IPAddress& aResult) 59 | { 60 | // See if we've been given a valid IP address 61 | const char* p =aIPAddrString; 62 | while (*p && 63 | ( (*p == '.') || (*p >= '0') || (*p <= '9') )) 64 | { 65 | p++; 66 | } 67 | 68 | if (*p == '\0') 69 | { 70 | // It's looking promising, we haven't found any invalid characters 71 | p = aIPAddrString; 72 | int segment =0; 73 | int segmentValue =0; 74 | while (*p && (segment < 4)) 75 | { 76 | if (*p == '.') 77 | { 78 | // We've reached the end of a segment 79 | if (segmentValue > 255) 80 | { 81 | // You can't have IP address segments that don't fit in a byte 82 | return 0; 83 | } 84 | else 85 | { 86 | aResult[segment] = (byte)segmentValue; 87 | segment++; 88 | segmentValue = 0; 89 | } 90 | } 91 | else 92 | { 93 | // Next digit 94 | segmentValue = (segmentValue*10)+(*p - '0'); 95 | } 96 | p++; 97 | } 98 | // We've reached the end of address, but there'll still be the last 99 | // segment to deal with 100 | if ((segmentValue > 255) || (segment > 3)) 101 | { 102 | // You can't have IP address segments that don't fit in a byte, 103 | // or more than four segments 104 | return 0; 105 | } 106 | else 107 | { 108 | aResult[segment] = (byte)segmentValue; 109 | return 1; 110 | } 111 | } 112 | else 113 | { 114 | return 0; 115 | } 116 | } 117 | 118 | int DNSClient::getHostByName(const char* aHostname, IPAddress& aResult) 119 | { 120 | int ret =0; 121 | 122 | // See if it's a numeric IP address 123 | if (inet_aton(aHostname, aResult)) 124 | { 125 | // It is, our work here is done 126 | return 1; 127 | } 128 | 129 | // Check we've got a valid DNS server to use 130 | if (iDNSServer == INADDR_NONE) 131 | { 132 | return INVALID_SERVER; 133 | } 134 | 135 | // Find a socket to use 136 | if (iUdp.begin(1024+(millis() & 0xF)) == 1) 137 | { 138 | // Try up to three times 139 | int retries = 0; 140 | // while ((retries < 3) && (ret <= 0)) 141 | { 142 | // Send DNS request 143 | ret = iUdp.beginPacket(iDNSServer, DNS_PORT); 144 | if (ret != 0) 145 | { 146 | // Now output the request data 147 | ret = BuildRequest(aHostname); 148 | if (ret != 0) 149 | { 150 | // And finally send the request 151 | ret = iUdp.endPacket(); 152 | if (ret != 0) 153 | { 154 | // Now wait for a response 155 | int wait_retries = 0; 156 | ret = TIMED_OUT; 157 | while ((wait_retries < 3) && (ret == TIMED_OUT)) 158 | { 159 | ret = ProcessResponse(5000, aResult); 160 | wait_retries++; 161 | } 162 | } 163 | } 164 | } 165 | retries++; 166 | } 167 | 168 | // We're done with the socket now 169 | iUdp.stop(); 170 | } 171 | 172 | return ret; 173 | } 174 | 175 | uint16_t DNSClient::BuildRequest(const char* aName) 176 | { 177 | // Build header 178 | // 1 1 1 1 1 1 179 | // 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 180 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 181 | // | ID | 182 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 183 | // |QR| Opcode |AA|TC|RD|RA| Z | RCODE | 184 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 185 | // | QDCOUNT | 186 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 187 | // | ANCOUNT | 188 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 189 | // | NSCOUNT | 190 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 191 | // | ARCOUNT | 192 | // +--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+ 193 | // As we only support one request at a time at present, we can simplify 194 | // some of this header 195 | iRequestId = millis(); // generate a random ID 196 | uint16_t twoByteBuffer; 197 | 198 | // FIXME We should also check that there's enough space available to write to, rather 199 | // FIXME than assume there's enough space (as the code does at present) 200 | iUdp.write((uint8_t*)&iRequestId, sizeof(iRequestId)); 201 | 202 | twoByteBuffer = htons(QUERY_FLAG | OPCODE_STANDARD_QUERY | RECURSION_DESIRED_FLAG); 203 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 204 | 205 | twoByteBuffer = htons(1); // One question record 206 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 207 | 208 | twoByteBuffer = 0; // Zero answer records 209 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 210 | 211 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 212 | // and zero additional records 213 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 214 | 215 | // Build question 216 | const char* start =aName; 217 | const char* end =start; 218 | uint8_t len; 219 | // Run through the name being requested 220 | while (*end) 221 | { 222 | // Find out how long this section of the name is 223 | end = start; 224 | while (*end && (*end != '.') ) 225 | { 226 | end++; 227 | } 228 | 229 | if (end-start > 0) 230 | { 231 | // Write out the size of this section 232 | len = end-start; 233 | iUdp.write(&len, sizeof(len)); 234 | // And then write out the section 235 | iUdp.write((uint8_t*)start, end-start); 236 | } 237 | start = end+1; 238 | } 239 | 240 | // We've got to the end of the question name, so 241 | // terminate it with a zero-length section 242 | len = 0; 243 | iUdp.write(&len, sizeof(len)); 244 | // Finally the type and class of question 245 | twoByteBuffer = htons(TYPE_A); 246 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 247 | 248 | twoByteBuffer = htons(CLASS_IN); // Internet class of question 249 | iUdp.write((uint8_t*)&twoByteBuffer, sizeof(twoByteBuffer)); 250 | // Success! Everything buffered okay 251 | return 1; 252 | } 253 | 254 | 255 | uint16_t DNSClient::ProcessResponse(uint16_t aTimeout, IPAddress& aAddress) 256 | { 257 | uint32_t startTime = millis(); 258 | 259 | // Wait for a response packet 260 | while(iUdp.parsePacket() <= 0) 261 | { 262 | if((millis() - startTime) > aTimeout) 263 | return TIMED_OUT; 264 | delay(50); 265 | } 266 | 267 | // We've had a reply! 268 | // Read the UDP header 269 | uint8_t header[DNS_HEADER_SIZE]; // Enough space to reuse for the DNS header 270 | // Check that it's a response from the right server and the right port 271 | if ( (iDNSServer != iUdp.remoteIP()) || 272 | (iUdp.remotePort() != DNS_PORT) ) 273 | { 274 | // It's not from who we expected 275 | return INVALID_SERVER; 276 | } 277 | 278 | // Read through the rest of the response 279 | if (iUdp.available() < DNS_HEADER_SIZE) 280 | { 281 | return TRUNCATED; 282 | } 283 | iUdp.read(header, DNS_HEADER_SIZE); 284 | 285 | uint16_t header_flags = htons(*((uint16_t*)&header[2])); 286 | // Check that it's a response to this request 287 | if ( ( iRequestId != (*((uint16_t*)&header[0])) ) || 288 | ((header_flags & QUERY_RESPONSE_MASK) != (uint16_t)RESPONSE_FLAG) ) 289 | { 290 | // Mark the entire packet as read 291 | iUdp.flush(); 292 | return INVALID_RESPONSE; 293 | } 294 | // Check for any errors in the response (or in our request) 295 | // although we don't do anything to get round these 296 | if ( (header_flags & TRUNCATION_FLAG) || (header_flags & RESP_MASK) ) 297 | { 298 | // Mark the entire packet as read 299 | iUdp.flush(); 300 | return -5; //INVALID_RESPONSE; 301 | } 302 | 303 | // And make sure we've got (at least) one answer 304 | uint16_t answerCount = htons(*((uint16_t*)&header[6])); 305 | if (answerCount == 0 ) 306 | { 307 | // Mark the entire packet as read 308 | iUdp.flush(); 309 | return -6; //INVALID_RESPONSE; 310 | } 311 | 312 | // Skip over any questions 313 | for (uint16_t i =0; i < htons(*((uint16_t*)&header[4])); i++) 314 | { 315 | // Skip over the name 316 | uint8_t len; 317 | do 318 | { 319 | iUdp.read(&len, sizeof(len)); 320 | if (len > 0) 321 | { 322 | // Don't need to actually read the data out for the string, just 323 | // advance ptr to beyond it 324 | while(len--) 325 | { 326 | iUdp.read(); // we don't care about the returned byte 327 | } 328 | } 329 | } while (len != 0); 330 | 331 | // Now jump over the type and class 332 | for (int i =0; i < 4; i++) 333 | { 334 | iUdp.read(); // we don't care about the returned byte 335 | } 336 | } 337 | 338 | // Now we're up to the bit we're interested in, the answer 339 | // There might be more than one answer (although we'll just use the first 340 | // type A answer) and some authority and additional resource records but 341 | // we're going to ignore all of them. 342 | 343 | for (uint16_t i =0; i < answerCount; i++) 344 | { 345 | // Skip the name 346 | uint8_t len; 347 | do 348 | { 349 | iUdp.read(&len, sizeof(len)); 350 | if ((len & LABEL_COMPRESSION_MASK) == 0) 351 | { 352 | // It's just a normal label 353 | if (len > 0) 354 | { 355 | // And it's got a length 356 | // Don't need to actually read the data out for the string, 357 | // just advance ptr to beyond it 358 | while(len--) 359 | { 360 | iUdp.read(); // we don't care about the returned byte 361 | } 362 | } 363 | } 364 | else 365 | { 366 | // This is a pointer to a somewhere else in the message for the 367 | // rest of the name. We don't care about the name, and RFC1035 368 | // says that a name is either a sequence of labels ended with a 369 | // 0 length octet or a pointer or a sequence of labels ending in 370 | // a pointer. Either way, when we get here we're at the end of 371 | // the name 372 | // Skip over the pointer 373 | iUdp.read(); // we don't care about the returned byte 374 | // And set len so that we drop out of the name loop 375 | len = 0; 376 | } 377 | } while (len != 0); 378 | 379 | // Check the type and class 380 | uint16_t answerType; 381 | uint16_t answerClass; 382 | iUdp.read((uint8_t*)&answerType, sizeof(answerType)); 383 | iUdp.read((uint8_t*)&answerClass, sizeof(answerClass)); 384 | 385 | // Ignore the Time-To-Live as we don't do any caching 386 | for (int i =0; i < TTL_SIZE; i++) 387 | { 388 | iUdp.read(); // we don't care about the returned byte 389 | } 390 | 391 | // And read out the length of this answer 392 | // Don't need header_flags anymore, so we can reuse it here 393 | iUdp.read((uint8_t*)&header_flags, sizeof(header_flags)); 394 | 395 | if ( (htons(answerType) == TYPE_A) && (htons(answerClass) == CLASS_IN) ) 396 | { 397 | if (htons(header_flags) != 4) 398 | { 399 | // It's a weird size 400 | // Mark the entire packet as read 401 | iUdp.flush(); 402 | return -9;//INVALID_RESPONSE; 403 | } 404 | iUdp.read(aAddress.raw_address(), 4); 405 | return SUCCESS; 406 | } 407 | else 408 | { 409 | // This isn't an answer type we're after, move onto the next one 410 | for (uint16_t i =0; i < htons(header_flags); i++) 411 | { 412 | iUdp.read(); // we don't care about the returned byte 413 | } 414 | } 415 | } 416 | 417 | // Mark the entire packet as read 418 | iUdp.flush(); 419 | 420 | // If we get here then we haven't found an answer 421 | return -10;//INVALID_RESPONSE; 422 | } 423 | 424 | --------------------------------------------------------------------------------