This repository has been archived by the owner on Feb 4, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathWebClient_ESP_SPI2.ino
143 lines (116 loc) · 4.05 KB
/
WebClient_ESP_SPI2.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/****************************************************************************************************************************
WebClient_ESP_SPI2.ino
Ethernet_Generic is a library for the W5x00 Ethernet shields trying to merge the good features of
previous Ethernet libraries
Built by Khoi Hoang https://github.com/khoih-prog/Ethernet_Generic
*****************************************************************************************************************************/
// Important: For ESP32, check https://github.com/khoih-prog/EthernetWebServer#7-for-fixing-esp32-compile-error
// and replace cores/esp32/Server.h to avoid compile error
#include "defines.h"
char server[] = "arduino.tips";
// Initialize the Ethernet client object
EthernetClient client;
void setup()
{
Serial.begin(115200);
while (!Serial && millis() < 5000);
delay(200);
Serial.print("\nStarting WebClient_ESP_SPI2 on ");
Serial.print(ARDUINO_BOARD);
Serial.print(" with ");
Serial.println(SHIELD_TYPE);
Serial.println(ETHERNET_GENERIC_VERSION);
// You can use Ethernet.init(pin) to configure the CS pin
//Ethernet.init(10); // Most Arduino shields
//Ethernet.init(5); // MKR ETH shield
//Ethernet.init(0); // Teensy 2.0
//Ethernet.init(20); // Teensy++ 2.0
//Ethernet.init(15); // ESP8266 with Adafruit Featherwing Ethernet
//Ethernet.init(33); // ESP32 with Adafruit Featherwing Ethernet
#ifndef USE_THIS_SS_PIN
#define USE_THIS_SS_PIN 5 //22 // For ESP32
#endif
ETG_LOGWARN1(F("ESP32 setCsPin:"), USE_THIS_SS_PIN);
// Must use library patch for Ethernet, EthernetLarge libraries
// ESP32 => GPIO2,4,5,13,15,21,22 OK with Ethernet, Ethernet2, EthernetLarge
// ESP32 => GPIO2,4,5,15,21,22 OK with Ethernet3
//Ethernet.setCsPin (USE_THIS_SS_PIN);
Ethernet.init (USE_THIS_SS_PIN);
// start the ethernet connection and the server:
// Use DHCP dynamic IP and random mac
uint16_t index = millis() % NUMBER_OF_MAC;
// Use Static IP
//Ethernet.begin(mac[index], ip);
Ethernet.begin(mac[index]);
#if defined( ESP32 )
// Just info to know how to connect correctly
// To change for other SPI
Serial.println("=========================");
Serial.println("Currently Used SPI pinout:");
Serial.print("MOSI:");
Serial.println(PIN_MOSI);
Serial.print("MISO:");
Serial.println(PIN_MISO);
Serial.print("SCK:");
Serial.println(PIN_SCK);
Serial.print("SS:");
Serial.println(USE_THIS_SS_PIN);
Serial.println(F("========================="));
#endif
Serial.print(F("Using mac index = "));
Serial.println(index);
Serial.print(F("Connected! IP address: "));
Serial.println(Ethernet.localIP());
if ( (Ethernet.getChip() == w5500) || (Ethernet.getChip() == w6100) || (Ethernet.getAltChip() == w5100s) )
{
if (Ethernet.getChip() == w6100)
Serial.print(F("W6100 => "));
else if (Ethernet.getChip() == w5500)
Serial.print(F("W6100 => "));
else
Serial.print(F("W5100S => "));
Serial.print(F("Speed: "));
Serial.print(Ethernet.speedReport());
Serial.print(F(", Duplex: "));
Serial.print(Ethernet.duplexReport());
Serial.print(F(", Link status: "));
Serial.println(Ethernet.linkReport());
}
Serial.println();
Serial.println(F("Starting connection to server..."));
// if you get a connection, report back via serial
if (client.connect(server, 80))
{
Serial.println(F("Connected to server"));
// Make a HTTP request
client.println(F("GET /asciilogo.txt HTTP/1.1"));
client.println(F("Host: arduino.tips"));
client.println(F("Connection: close"));
client.println();
}
}
void printoutData()
{
// if there are incoming bytes available
// from the server, read them and print them
while (client.available())
{
char c = client.read();
Serial.write(c);
Serial.flush();
}
}
void loop()
{
printoutData();
// if the server's disconnected, stop the client
if (!client.connected())
{
Serial.println();
Serial.println(F("Disconnecting from server..."));
client.stop();
// do nothing forevermore
while (true)
yield();
}
}