Merge pull request #3056 from adafruit/wiz5500
ethernet breakout examples
This commit is contained in:
commit
41de6c47e0
3 changed files with 176 additions and 0 deletions
|
|
@ -0,0 +1,135 @@
|
|||
// SPDX-FileCopyrightText: 2025 Liz Clark for Adafruit Industries
|
||||
//
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
/*
|
||||
Web client
|
||||
|
||||
This sketch connects to a website (http://wifitest.adafruit.com)
|
||||
using an Adafruit Wiz5500 Ethernet Breakout
|
||||
|
||||
Circuit:
|
||||
* Ethernet breakout attached to pins 10, 11, 12, 13
|
||||
|
||||
created 18 Dec 2009
|
||||
by David A. Mellis
|
||||
modified 9 Apr 2012
|
||||
by Tom Igoe, based on work by Adrian McEwen
|
||||
modified 10 June 2025
|
||||
by Liz Clark
|
||||
|
||||
*/
|
||||
|
||||
#include <SPI.h>
|
||||
#include <Ethernet.h>
|
||||
|
||||
// Enter a MAC address for your controller below.
|
||||
byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
|
||||
|
||||
char server[] = "wifitest.adafruit.com";
|
||||
|
||||
// Set the static IP address to use if the DHCP fails to assign
|
||||
IPAddress ip(192, 168, 0, 177);
|
||||
IPAddress myDns(192, 168, 0, 1);
|
||||
|
||||
// Initialize the Ethernet client library
|
||||
// with the IP address and port of the server
|
||||
// that you want to connect to (port 80 is default for HTTP):
|
||||
EthernetClient client;
|
||||
|
||||
// Variables to measure the speed
|
||||
unsigned long beginMicros, endMicros;
|
||||
unsigned long byteCount = 0;
|
||||
bool printWebData = true; // set to false for better speed measurement
|
||||
|
||||
void setup() {
|
||||
// You can use Ethernet.init(pin) to configure the CS pin
|
||||
// Here you're using pin 10 for CS
|
||||
// SCK: 13, MISO: 12, MOSI: 11
|
||||
Ethernet.init(10); // Most Arduino shields
|
||||
|
||||
// Open serial communications and wait for port to open:
|
||||
Serial.begin(115200);
|
||||
while (!Serial) {
|
||||
; // wait for serial port to connect. Needed for native USB port only
|
||||
}
|
||||
|
||||
// start the Ethernet connection:
|
||||
Serial.println("Initialize Ethernet with DHCP:");
|
||||
if (Ethernet.begin(mac) == 0) {
|
||||
Serial.println("Failed to configure Ethernet using DHCP");
|
||||
// Check for Ethernet hardware present
|
||||
if (Ethernet.hardwareStatus() == EthernetNoHardware) {
|
||||
Serial.println("Ethernet breakout was not found. Check your wiring.");
|
||||
while (true) {
|
||||
delay(1); // do nothing, no point running without Ethernet hardware
|
||||
}
|
||||
}
|
||||
if (Ethernet.linkStatus() == LinkOFF) {
|
||||
Serial.println("Ethernet cable is not connected.");
|
||||
}
|
||||
// try to configure using IP address instead of DHCP:
|
||||
Ethernet.begin(mac, ip, myDns);
|
||||
} else {
|
||||
Serial.print(" DHCP assigned IP ");
|
||||
Serial.println(Ethernet.localIP());
|
||||
}
|
||||
// give the Ethernet a second to initialize:
|
||||
delay(1000);
|
||||
Serial.print("connecting to ");
|
||||
Serial.print(server);
|
||||
Serial.println("...");
|
||||
|
||||
// if you get a connection, report back via serial:
|
||||
if (client.connect(server, 80)) {
|
||||
Serial.print("connected to ");
|
||||
Serial.println(client.remoteIP());
|
||||
// Make a HTTP request:
|
||||
client.println("GET /testwifi/index.html HTTP/1.1");
|
||||
client.println("Host: wifitest.adafruit.com");
|
||||
client.println("Connection: close");
|
||||
client.println();
|
||||
} else {
|
||||
// if you didn't get a connection to the server:
|
||||
Serial.println("connection failed");
|
||||
}
|
||||
beginMicros = micros();
|
||||
}
|
||||
|
||||
void loop() {
|
||||
// if there are incoming bytes available
|
||||
// from the server, read them and print them:
|
||||
int len = client.available();
|
||||
if (len > 0) {
|
||||
byte buffer[80];
|
||||
if (len > 80) len = 80;
|
||||
client.read(buffer, len);
|
||||
if (printWebData) {
|
||||
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
|
||||
}
|
||||
byteCount = byteCount + len;
|
||||
}
|
||||
|
||||
// if the server's disconnected, stop the client:
|
||||
if (!client.connected()) {
|
||||
endMicros = micros();
|
||||
Serial.println();
|
||||
Serial.println("disconnecting.");
|
||||
client.stop();
|
||||
Serial.print("Received ");
|
||||
Serial.print(byteCount);
|
||||
Serial.print(" bytes in ");
|
||||
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
|
||||
Serial.print(seconds, 4);
|
||||
float rate = (float)byteCount / seconds / 1000.0;
|
||||
Serial.print(", rate = ");
|
||||
Serial.print(rate);
|
||||
Serial.print(" kbytes/second");
|
||||
Serial.println();
|
||||
|
||||
// do nothing forevermore:
|
||||
while (true) {
|
||||
delay(1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -0,0 +1,41 @@
|
|||
# SPDX-FileCopyrightText: 2021 ladyada for Adafruit Industries
|
||||
# SPDX-License-Identifier: MIT
|
||||
|
||||
import adafruit_connection_manager
|
||||
import adafruit_requests
|
||||
import board
|
||||
import busio
|
||||
import digitalio
|
||||
|
||||
from adafruit_wiznet5k.adafruit_wiznet5k import WIZNET5K
|
||||
|
||||
print("Wiz5100 Breakout WebClient Test")
|
||||
|
||||
TEXT_URL = "http://wifitest.adafruit.com/testwifi/index.html"
|
||||
|
||||
cs = digitalio.DigitalInOut(board.D10)
|
||||
spi_bus = busio.SPI(board.SCK, MOSI=board.MOSI, MISO=board.MISO)
|
||||
|
||||
# Initialize ethernet interface with DHCP
|
||||
eth = WIZNET5K(spi_bus, cs)
|
||||
|
||||
# Initialize a requests session
|
||||
pool = adafruit_connection_manager.get_radio_socketpool(eth)
|
||||
ssl_context = adafruit_connection_manager.get_radio_ssl_context(eth)
|
||||
requests = adafruit_requests.Session(pool, ssl_context)
|
||||
|
||||
print("Chip Version:", eth.chip)
|
||||
print("MAC Address:", [hex(i) for i in eth.mac_address])
|
||||
print("My IP address is:", eth.pretty_ip(eth.ip_address))
|
||||
print("IP lookup adafruit.com: %s" % eth.pretty_ip(eth.get_host_by_name("adafruit.com")))
|
||||
|
||||
|
||||
# eth._debug = True
|
||||
print("Fetching text from", TEXT_URL)
|
||||
r = requests.get(TEXT_URL)
|
||||
print("-" * 40)
|
||||
print(r.text)
|
||||
print("-" * 40)
|
||||
r.close()
|
||||
|
||||
print("Done!")
|
||||
Loading…
Reference in a new issue