Skip to content

Commit 540e0ab

Browse files
committed
Add Static IP support
1 parent 05a1b80 commit 540e0ab

File tree

3 files changed

+141
-10
lines changed

3 files changed

+141
-10
lines changed
Lines changed: 126 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,126 @@
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 21 Jan 2014
18+
by Federico Vanzati
19+
20+
http://www.arduino.cc/en/Tutorial/WebClientRepeating
21+
This code is in the public domain.
22+
23+
*/
24+
25+
#include <SPI.h>
26+
#include <PortentaEthernet.h>
27+
#include <Ethernet.h>
28+
29+
// Set the static IP address
30+
IPAddress ip(192, 168, 0, 177);
31+
IPAddress myDns(192, 168, 0, 1);
32+
33+
// initialize the library instance:
34+
EthernetClient client;
35+
36+
char server[] = "www.arduino.cc"; // also change the Host line in httpRequest()
37+
//IPAddress server(64,131,82,241);
38+
39+
unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds
40+
const unsigned long postingInterval = 10*1000; // delay between updates, in milliseconds
41+
42+
void setup() {
43+
// You can use Ethernet.init(pin) to configure the CS pin
44+
//Ethernet.init(10); // Most Arduino shields
45+
//Ethernet.init(5); // MKR ETH shield
46+
//Ethernet.init(0); // Teensy 2.0
47+
//Ethernet.init(20); // Teensy++ 2.0
48+
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
49+
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
50+
51+
// start serial port:
52+
Serial.begin(9600);
53+
while (!Serial) {
54+
; // wait for serial port to connect. Needed for native USB port only
55+
}
56+
57+
// start the Ethernet connection:
58+
Serial.println("Initialize Ethernet with DHCP:");
59+
if (Ethernet.begin(mac) == 0) {
60+
Serial.println("Failed to configure Ethernet using DHCP");
61+
// Check for Ethernet hardware present
62+
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
63+
Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :(");
64+
while (true) {
65+
delay(1); // do nothing, no point running without Ethernet hardware
66+
}
67+
}
68+
if (Ethernet.linkStatus() == LinkOFF) {
69+
Serial.println("Ethernet cable is not connected.");
70+
}
71+
// try to congifure using IP address instead of DHCP:
72+
Ethernet.begin(mac, ip, myDns);
73+
Serial.print("My IP address: ");
74+
Serial.println(Ethernet.localIP());
75+
} else {
76+
Serial.print(" DHCP assigned IP ");
77+
Serial.println(Ethernet.localIP());
78+
}
79+
// give the Ethernet shield a second to initialize:
80+
delay(1000);
81+
}
82+
83+
void loop() {
84+
// if there's incoming data from the net connection.
85+
// send it out the serial port. This is for debugging
86+
// purposes only:
87+
if (client.available()) {
88+
char c = client.read();
89+
Serial.write(c);
90+
}
91+
92+
// if ten seconds have passed since your last connection,
93+
// then connect again and send data:
94+
if (millis() - lastConnectionTime > postingInterval) {
95+
httpRequest();
96+
}
97+
98+
}
99+
100+
// this method makes a HTTP connection to the server:
101+
void httpRequest() {
102+
// close any connection before send a new request.
103+
// This will free the socket on the WiFi shield
104+
client.stop();
105+
106+
// if there's a successful connection:
107+
if (client.connect(server, 80)) {
108+
Serial.println("connecting...");
109+
// send the HTTP GET request:
110+
client.println("GET /latest.txt HTTP/1.1");
111+
client.println("Host: www.arduino.cc");
112+
client.println("User-Agent: arduino-ethernet");
113+
client.println("Connection: close");
114+
client.println();
115+
116+
// note the time that the connection was made:
117+
lastConnectionTime = millis();
118+
} else {
119+
// if you couldn't make a connection:
120+
Serial.println("connection failed");
121+
}
122+
}
123+
124+
125+
126+

libraries/Ethernet/src/Ethernet.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,11 @@ int arduino::EthernetClass::begin(uint8_t *mac, unsigned long timeout, unsigned
3030
return (linkStatus() == LinkON ? 1 : 0);
3131
}
3232

33+
int arduino::EthernetClass::begin() {
34+
eth_if->set_network(_ip, _netmask, _gateway);
35+
eth_if->connect();
36+
}
37+
3338
void arduino::EthernetClass::end() {
3439
disconnect();
3540
}

libraries/Ethernet/src/Ethernet.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -57,21 +57,21 @@ class EthernetClass {
5757

5858
EthernetClass(voidPrtFuncPtr _cb) : _initializerCallback(_cb) {};
5959

60-
int begin(uint8_t *mac = nullptr , unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
61-
// int begin(unsigned long timeout = 60000, unsigned long responseTimeout = 4000) { return begin(nullptr, timeout, responseTimeout); };
60+
int begin(uint8_t *mac, unsigned long timeout = 60000, unsigned long responseTimeout = 4000);
61+
int begin();
6262
int maintain();
6363
EthernetLinkStatus linkStatus();
6464
EthernetHardwareStatus hardwareStatus();
6565

6666
// Manaul configuration
67-
void begin(uint8_t *mac, IPAddress ip) {}
68-
void begin(uint8_t *mac, IPAddress ip, IPAddress dns) {}
69-
void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) {}
70-
void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {}
71-
void begin(IPAddress ip) {}
72-
void begin(IPAddress ip, IPAddress dns) {}
73-
void begin(IPAddress ip, IPAddress dns, IPAddress gateway) {}
74-
void begin(IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {}
67+
int begin(uint8_t *mac, IPAddress ip) {}
68+
int begin(uint8_t *mac, IPAddress ip, IPAddress dns) {}
69+
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) {}
70+
int begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {}
71+
// void begin(uint8_t *mac, IPAddress ip) {}
72+
// void begin(uint8_t *mac, IPAddress ip, IPAddress dns) {}
73+
// void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway) {}
74+
// void begin(uint8_t *mac, IPAddress ip, IPAddress dns, IPAddress gateway, IPAddress subnet) {}
7575
void init(uint8_t sspin = 10);
7676

7777
void MACAddress(uint8_t *mac_address);

0 commit comments

Comments
 (0)