0
#include <SPI.h>
#include <Ethernet.h>

// Enter a MAC address for your controller below.
// Newer Ethernet shields have a MAC address printed on a sticker on the shield
byte mac[] = {my mac };
char serverName[] = "api.weather.gov";

// 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;

void setup() {
 // Open serial communications and wait for port to open:
  Serial.begin(9600);
   while (!Serial) {
    ; // wait for serial port to connect. Needed for Leonardo only
  }


  // start the Ethernet connection:
  if (Ethernet.begin(mac) == 0) {
    Serial.println("Failed to configure Ethernet using DHCP");
    // no point in carrying on, so do nothing forevermore:
    while(true);
  }
  // give the Ethernet shield a second to initialize:
  delay(1000);
  Serial.println("connecting...");

  // if you get a connection, report back via serial:
  int ret = client.connect(serverName,443);
  if (ret==1) {
    Serial.println("connected");
    // Make a HTTP request:
    client.println("GET /stations/KGSO/observations/latest");
    client.println("accept: application/geo+json,*/*");
    client.println("user-agent: arduino, mymail@asdf.com");
    client.println("host: api.weather.gov");
    client.println();
  }
  else {
    // kf you didn't get a connection to the server:
    Serial.println("connection failed");
    Serial.println(ret);
    Serial.println(client.status());
  }
}

void loop()
{
  // if there are incoming bytes available
  // from the server, read them and print them:

  if (client.available()) {
    Serial.print("\ngotsome\n");
    char c = client.read();
    Serial.print(c);
  }

  // if the server's disconnected, stop the client:
  if (!client.connected()) {
    Serial.println();
    Serial.println("disconnecting.");
    client.stop();

    // do nothing forevermore:
    while(true);
  }
}

When I run this I get:

connecting... connected

disconnecting.

Why don't I get data? I thought it was about the header info. But I looked at https://www.weather.gov/documentation/services-web-api#/ and it doesn't say it needs much.

  • what happens if you remove the last if block? – jsotola Jan 19 at 8:07
  • connecting... connected – Jason William Stone Jan 19 at 13:11
  • Yes I think Juraj has the answer (thanks): Can't get there from here. Maybe that's an excuse to get a raspberry pi. – Jason William Stone Jan 19 at 14:44
0

The Ethernet library doesn't support secure connection (so called SSL, but TLS now). To connect to a secure HTTP transport HTTPS the secure connection is necessary. The AVR MCU don't have the computing power to compute the security keys. And for less popular 32bit MCU nobody created a 'SSL' implementation for Ethernet library yet.

esp8266, esp32, MKR1010, Uno WiFi, Nano 33 IoT can do HTTPS, but all only over WiFi as far as I know.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.