0

I want to get JSON string from a URL which goes like this

https://maps.googleapis.com/maps/api/directions/json?origin=7649+Paseo+Santa+Cruz&destination=SJSU&key=MY_API_KEY

and here is my code:

#include <Bridge.h>

//#include <b64.h>
#include <HttpClient.h>

//#include <ArduinoHttpClient.h>

#include <Arduino_JSON.h>
#include <SPI.h>
#include <WiFiNINA.h>

//#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "my_wifi_ssid";        // your network SSID (name)
char pass[] = "my_wifi_password";    // your network password (use for WPA, or use as key for WEP)
int status = WL_IDLE_STATUS;     // the Wifi radio's status

WiFiSSLClient wifi;
char serverAddress[] = "maps.googleapis.com";

void setup() {
  //Bridge.begin();
  Serial.begin(9600);
  //listNetworks();
  //Initialize serial and wait for port to open:

  while (!Serial) {
    ; // wait for serial port to connect. Needed for native USB port only
  }

  // check for the WiFi module:
  if (WiFi.status() == WL_NO_MODULE) {
    Serial.println("Communication with WiFi module failed!");
    // don't continue
    while (true);
  }

  String fv = WiFi.firmwareVersion();
  if (fv < "1.0.0") {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to Wifi network:
  while (status != WL_CONNECTED) {
    Serial.print("Attempting to connect to WPA SSID: ");
    Serial.println(ssid);
    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);

    // wait 10 seconds for connection:
    delay(10000);
  }

  // you're connected now, so print out the data:
  Serial.print("You're connected to the network");
  printCurrentNet();
  printWifiData();
  Serial.println("\nStarting connection to server...");
  // if you get a connection, report back via serial:
  if (wifi.connect(serverAddress, 80)) {
    Serial.println("connected to server");
    // Make a HTTP request:
    wifi.println("GET /maps/api/directions/json?origin=7649+Paseo+Santa+Cruz&destination=SJSU&key=MY_API_KEY");
    wifi.println("Host: maps.googleapis.com");
    wifi.println();
  }
}

void loop() {
  while (wifi.available()) {
    char c = wifi.read();
    Serial.write(c);
  }

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

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

  // check the network connection once every 10 seconds:
  delay(10000);
  }

It only printed in Serial Monitor:

Starting connection to server...

disconnecting from server.
  • Bridge.h is for Yun. remove it. and change the unsuitable variable name wifi to client – Juraj May 26 at 9:59
  • you connect for https to port 80? default https port is 443. use wifi.connect(serverAddress, 443) – Juraj May 26 at 15:23
  • Better use <WiFiClientSecure.h> example – Memba Aug 11 at 2:53

Your Answer

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

Browse other questions tagged or ask your own question.