From 83b9bbd51b21cf0a3abc7501307f9c68090217a3 Mon Sep 17 00:00:00 2001 From: Mikhail Grushinskiy Date: Tue, 9 Apr 2024 13:09:40 -0400 Subject: [PATCH] Create bbn_m5atomS3_lite_eth_https.ino --- .../bbn_m5atomS3_lite_eth_https.ino | 138 ++++++++++++++++++ 1 file changed, 138 insertions(+) create mode 100644 bbn_m5atomS3_lite_eth_https/bbn_m5atomS3_lite_eth_https.ino diff --git a/bbn_m5atomS3_lite_eth_https/bbn_m5atomS3_lite_eth_https.ino b/bbn_m5atomS3_lite_eth_https/bbn_m5atomS3_lite_eth_https.ino new file mode 100644 index 0000000..c84ff97 --- /dev/null +++ b/bbn_m5atomS3_lite_eth_https/bbn_m5atomS3_lite_eth_https.ino @@ -0,0 +1,138 @@ +/* + + based on: + + Web SSL client + + This sketch connects to a test website (httpbin.org) + and try to do a secure GET request on port 443, + to do the SSL request we use SSLClient with the + site Trust Anchor + the output is printed on Serial + + by Renzo Mischianti + + https://www.mischianti.org + + */ + +#include +#include +#include +#include +#include +#include "trust_anchors.h" + +#define SERVER "httpbin.org" + +#define SCK 22 +#define MISO 23 +#define MOSI 33 +#define CS 19 + +byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0x99}; + +// 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 = 34; + +// 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() { + Serial.begin(115200); + + Serial.println("Initializing SPI..."); + SPI.begin(SCK, MISO, MOSI, -1); + Serial.println("Initializing ethernet..."); + Ethernet.init(CS); + + while (Ethernet.begin(mac) != 1) { + Serial.println("Error getting IP address via DHCP, trying again..."); + delay(2000); + } + + // Check for Ethernet hardware present + if (Ethernet.hardwareStatus() == EthernetNoHardware) { + Serial.println( + "Ethernet shield was not found. Sorry, can't run without " + "hardware. :("); + while (true) { + delay(1); // do nothing, no point running without Ethernet hardware + } + } + if (Ethernet.linkStatus() == LinkOFF) { + Serial.println("Ethernet cable is not connected."); + } + Serial.print("Local IP : "); + Serial.println(Ethernet.localIP()); + Serial.print("Subnet Mask : "); + Serial.println(Ethernet.subnetMask()); + Serial.print("Gateway IP : "); + Serial.println(Ethernet.gatewayIP()); + Serial.print("DNS Server : "); + Serial.println(Ethernet.dnsServerIP()); + + Serial.println("Ethernet Successfully Initialized"); + // if you get a connection, report back via serial: + if (client.connect(SERVER, 443)) { + Serial.print("connected to "); + // Make a HTTP request: + client.println("GET /get HTTP/1.1"); + client.println("Host: httpbin.org"); + 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() { + + //Serial.println("making GET request"); + + // 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); + } + } +}