|
| 1 | +/* |
| 2 | + Repeating TLS/SSL Ethernet Web client. |
| 3 | +
|
| 4 | + Remeber to update the CA certificates using WiFiFirmwareUpdater sketch |
| 5 | + before using this sketch. |
| 6 | +
|
| 7 | + */ |
| 8 | + |
| 9 | +#include <Ethernet.h> |
| 10 | +#include <EthernetSSLClient.h> |
| 11 | +#include <PortentaEthernet.h> |
| 12 | + |
| 13 | +// initialize the library instance: |
| 14 | +EthernetSSLClient client; |
| 15 | + |
| 16 | +char server[] = "www.arduino.cc"; |
| 17 | +int port = 443; |
| 18 | +// IPAddress server(64,131,82,241); |
| 19 | + |
| 20 | +unsigned long lastConnectionTime = 0; // last time you connected to the server, in milliseconds |
| 21 | +const unsigned long postingInterval = 10 * 1000; // delay between updates, in milliseconds |
| 22 | + |
| 23 | +void setup() |
| 24 | +{ |
| 25 | + |
| 26 | + // start serial port: |
| 27 | + Serial.begin(9600); |
| 28 | + while (!Serial) { |
| 29 | + ; // wait for serial port to connect. Needed for native USB port only |
| 30 | + } |
| 31 | + |
| 32 | + // start the Ethernet connection: |
| 33 | + Serial.println("Initialize Ethernet with DHCP:"); |
| 34 | + if (Ethernet.begin() == 0) { |
| 35 | + Serial.println("Failed to configure Ethernet using DHCP"); |
| 36 | + // Check for Ethernet hardware present |
| 37 | + if (Ethernet.hardwareStatus() == EthernetNoHardware) { |
| 38 | + Serial.println("Ethernet shield was not found. Sorry, can't run without hardware. :("); |
| 39 | + while (true) { |
| 40 | + delay(1); // do nothing, no point running without Ethernet hardware |
| 41 | + } |
| 42 | + } |
| 43 | + if (Ethernet.linkStatus() == LinkOFF) { |
| 44 | + Serial.println("Ethernet cable is not connected."); |
| 45 | + } |
| 46 | + // try to congifure using IP address instead of DHCP: |
| 47 | + Ethernet.begin(ip, myDns); |
| 48 | + Serial.print("My IP address: "); |
| 49 | + Serial.println(Ethernet.localIP()); |
| 50 | + } else { |
| 51 | + Serial.print(" DHCP assigned IP "); |
| 52 | + Serial.println(Ethernet.localIP()); |
| 53 | + } |
| 54 | + // give the Ethernet shield a second to initialize: |
| 55 | + delay(1000); |
| 56 | +} |
| 57 | + |
| 58 | +void loop() |
| 59 | +{ |
| 60 | + // if there's incoming data from the net connection. |
| 61 | + // send it out the serial port. This is for debugging |
| 62 | + // purposes only: |
| 63 | + if (client.available()) { |
| 64 | + char c = client.read(); |
| 65 | + Serial.write(c); |
| 66 | + } |
| 67 | + |
| 68 | + // if ten seconds have passed since your last connection, |
| 69 | + // then connect again and send data: |
| 70 | + if (millis() - lastConnectionTime > postingInterval) { |
| 71 | + httpRequest(); |
| 72 | + } |
| 73 | +} |
| 74 | + |
| 75 | +// this method makes a HTTP connection to the server: |
| 76 | +void httpRequest() |
| 77 | +{ |
| 78 | + // close any connection before send a new request. |
| 79 | + // This will free the socket on the WiFi shield |
| 80 | + client.stop(); |
| 81 | + |
| 82 | + // if there's a successful connection: |
| 83 | + if (client.connect(server, port)) { |
| 84 | + Serial.println("connecting..."); |
| 85 | + // send the HTTP GET request: |
| 86 | + client.println("GET /latest.txt HTTP/1.1"); |
| 87 | + client.print("Host: "); |
| 88 | + client.println(server); |
| 89 | + client.println("User-Agent: arduino-ethernet"); |
| 90 | + client.println("Accept: *"); |
| 91 | + client.println("Connection: close"); |
| 92 | + client.println(); |
| 93 | + |
| 94 | + // note the time that the connection was made: |
| 95 | + lastConnectionTime = millis(); |
| 96 | + } else { |
| 97 | + // if you couldn't make a connection: |
| 98 | + Serial.println("connection failed"); |
| 99 | + } |
| 100 | +} |
0 commit comments