2020-02-07 01:15:34 +01:00
|
|
|
/*
|
|
|
|
Web client
|
|
|
|
|
|
|
|
This sketch connects to a website (http://www.arduino.cc/asciilogo.txt)
|
2020-03-09 18:12:01 +01:00
|
|
|
using an Arduino Wiznet Ethernet shield or STM32 built-in Ethernet.
|
|
|
|
Tested on ST Micro Nucleo-F767ZI.
|
2020-02-07 01:15:34 +01:00
|
|
|
|
|
|
|
Circuit:
|
|
|
|
* Ethernet shield attached to pins 10, 11, 12, 13
|
|
|
|
|
|
|
|
created 18 Dec 2009
|
|
|
|
by David A. Mellis
|
|
|
|
modified 9 Apr 2012
|
|
|
|
by Noah Koontz, based on work by Adrian McEwen and Tom Igoe
|
|
|
|
|
2020-03-09 18:12:01 +01:00
|
|
|
Modified 16 Oct 2019 by gdsports625@gmail.com for STM32duino_STM32Ethernet
|
2020-02-07 01:15:34 +01:00
|
|
|
*/
|
|
|
|
|
2020-03-09 18:12:01 +01:00
|
|
|
#include <LwIP.h>
|
|
|
|
#include <STM32Ethernet.h>
|
2020-02-07 01:15:34 +01:00
|
|
|
#include <SSLClient.h>
|
|
|
|
#include "trust_anchors.h"
|
|
|
|
|
|
|
|
// if you don't want to use DNS (and reduce your sketch size)
|
|
|
|
// use the numeric IP instead of the name for the server:
|
|
|
|
//IPAddress server(54,85,55,79); // numeric IP for Google (no DNS)
|
|
|
|
const char server[] = "www.arduino.cc"; // name address for Arduino (using DNS)
|
|
|
|
const char server_host[] = "www.arduino.cc"; // leave this alone, change only above two
|
|
|
|
|
|
|
|
// Set the static IP address to use if the DHCP fails to assign
|
2020-03-09 18:12:01 +01:00
|
|
|
IPAddress ip(192, 168, 0, 177);
|
2020-02-07 01:15:34 +01:00
|
|
|
IPAddress myDns(8, 8, 8, 8);
|
|
|
|
|
|
|
|
// Choose the analog pin to get semi-random data from for SSL
|
|
|
|
// Pick a pin that's not connected or attached to a randomish voltage source
|
|
|
|
const int rand_pin = A5;
|
|
|
|
|
|
|
|
// Initialize the SSL client library
|
|
|
|
// We input an EthernetClient, our trust anchors, and the analog pin
|
|
|
|
EthernetClient base_client;
|
|
|
|
SSLClient client(base_client, TAs, (size_t)TAs_NUM, rand_pin);
|
|
|
|
// 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() {
|
|
|
|
// 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:
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.println("Initialize Ethernet with DHCP:");
|
|
|
|
// STM32 built-in Ethernet has a factory installed MAC address.
|
|
|
|
if (Ethernet.begin() == 0) {
|
|
|
|
Serial.println("Failed to configure Ethernet using DHCP");
|
|
|
|
while (1) delay(1);
|
2020-02-07 01:15:34 +01:00
|
|
|
} else {
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.print(" DHCP assigned IP ");
|
2020-02-07 01:15:34 +01:00
|
|
|
Serial.println(Ethernet.localIP());
|
|
|
|
}
|
|
|
|
// give the Ethernet shield a second to initialize:
|
|
|
|
delay(2000);
|
|
|
|
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.print("connecting to ");
|
2020-02-07 01:15:34 +01:00
|
|
|
Serial.print(server);
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.println("...");
|
2020-02-07 01:15:34 +01:00
|
|
|
|
|
|
|
// if you get a connection, report back via serial:
|
|
|
|
auto start = millis();
|
|
|
|
// specify the server and port, 443 is the standard port for HTTPS
|
|
|
|
if (client.connect(server, 443)) {
|
|
|
|
auto time = millis() - start;
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.print("connected to ");
|
|
|
|
Serial.println(base_client.remoteIP());
|
|
|
|
Serial.print("Took: ");
|
2020-02-07 01:15:34 +01:00
|
|
|
Serial.println(time);
|
|
|
|
// Make a HTTP request:
|
2020-03-09 18:12:01 +01:00
|
|
|
client.println("GET /asciilogo.txt HTTP/1.1");
|
|
|
|
client.println("User-Agent: SSLClientOverEthernet");
|
|
|
|
client.print("Host: ");
|
2020-02-07 01:15:34 +01:00
|
|
|
client.println(server_host);
|
2020-03-09 18:12:01 +01:00
|
|
|
client.println("Connection: close");
|
2020-02-07 01:15:34 +01:00
|
|
|
client.println();
|
|
|
|
} else {
|
|
|
|
// if you didn't get a connection to the server:
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.println("connection failed");
|
2020-02-07 01:15:34 +01:00
|
|
|
}
|
|
|
|
beginMicros = micros();
|
|
|
|
}
|
|
|
|
|
|
|
|
void loop() {
|
|
|
|
// if there are incoming bytes available
|
|
|
|
// from the server, read them and print them:
|
|
|
|
int len = client.available();
|
2020-03-09 18:12:01 +01:00
|
|
|
if (len > 0) {
|
|
|
|
byte buffer[80];
|
|
|
|
if (len > 80) len = 80;
|
2020-02-07 01:15:34 +01:00
|
|
|
client.read(buffer, len);
|
|
|
|
if (printWebData) {
|
|
|
|
Serial.write(buffer, len); // show in the serial monitor (slows some boards)
|
|
|
|
}
|
|
|
|
byteCount = byteCount + len;
|
|
|
|
}
|
|
|
|
|
2020-03-09 18:12:01 +01:00
|
|
|
// if the server's disconnected, stop the client:
|
2020-02-07 01:15:34 +01:00
|
|
|
if (!client.connected()) {
|
|
|
|
endMicros = micros();
|
|
|
|
Serial.println();
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.println("disconnecting.");
|
2020-02-07 01:15:34 +01:00
|
|
|
client.stop();
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.print("Received ");
|
2020-02-07 01:15:34 +01:00
|
|
|
Serial.print(byteCount);
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.print(" bytes in ");
|
2020-02-07 01:15:34 +01:00
|
|
|
float seconds = (float)(endMicros - beginMicros) / 1000000.0;
|
|
|
|
Serial.print(seconds, 4);
|
|
|
|
float rate = (float)byteCount / seconds / 1000.0;
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.print(", rate = ");
|
2020-02-07 01:15:34 +01:00
|
|
|
Serial.print(rate);
|
2020-03-09 18:12:01 +01:00
|
|
|
Serial.print(" kbytes/second");
|
2020-02-07 01:15:34 +01:00
|
|
|
Serial.println();
|
|
|
|
|
|
|
|
// do nothing forevermore:
|
|
|
|
while (true) {
|
|
|
|
delay(1);
|
|
|
|
}
|
|
|
|
}
|
2020-03-09 18:12:01 +01:00
|
|
|
}
|