I have been trying to use the WifiNINA library to access the JSON output from the following API using the WifiNINA library and the Arduino Wifi Rev2.
This is the code that I have been using (it is an edited example form the library):
/*
Web client
This sketch connects to a website (https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=demo)
using the WiFi module.
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
This example is written for a network using WPA encryption. For
WEP or WPA, change the Wifi.begin() call accordingly.
Circuit:
* Board with NINA module (Arduino MKR WiFi 1010, MKR VIDOR 4000 and UNO WiFi Rev.2)
created 13 July 2010
by dlf (Metodo2 srl)
modified 31 May 2012
by Tom Igoe
*/
#include <SPI.h>
#include <WiFiNINA.h>
#include "arduino_secrets.h"
///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = SECRET_SSID; // your network SSID (name)
char pass[] = SECRET_PASS; // your network password (use for WPA, or use as key for WEP)
int keyIndex = 0; // your network key Index number (needed only for WEP)
int status = WL_IDLE_STATUS;
// 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(74,125,232,128); // numeric IP for Google (no DNS)
char server[] = "www.alphavantage.co"; // name address for Google (using DNS)
// 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):
WiFiClient client;
void setup() {
//Initialize serial and wait for port to open:
Serial.begin(9600);
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 SSID: ");
Serial.println(ssid);
// Connect to WPA/WPA2 network. Change this line if using open or WEP network:
status = WiFi.begin(ssid, pass);
// wait 10 seconds for connection:
delay(10000);
}
Serial.println("Connected to wifi");
printWifiStatus();
Serial.println("\nStarting connection to server...");
// if you get a connection, report back via serial:
if (client.connect(server, 80)) {
Serial.println("connected to server");
// Make a HTTP request:
client.println("GET /query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=demo HTTP/1.1");
client.println("Host: https://www.alphavantage.co");
client.println("Connection: close");
client.println();
}
}
void loop() {
// if there are incoming bytes available
// from the server, read them and print them:
while (client.available()) {
char c = client.read();
Serial.write(c);
}
// if the server's disconnected, stop the client:
if (!client.connected()) {
Serial.println();
Serial.println("disconnecting from server.");
client.stop();
// do nothing forevermore:
while (true);
}
}
void printWifiStatus() {
// print the SSID of the network you're attached to:
Serial.print("SSID: ");
Serial.println(WiFi.SSID());
// print your board's IP address:
IPAddress ip = WiFi.localIP();
Serial.print("IP Address: ");
Serial.println(ip);
// print the received signal strength:
long rssi = WiFi.RSSI();
Serial.print("signal strength (RSSI):");
Serial.print(rssi);
Serial.println(" dBm");
}
However, when I run the library, all that the serial monitor returns is:
Attempting to connect to SSID: HIDDEN
Connected to wifi
SSID: HIDDEN
IP Address: HIDDEN
signal strength (RSSI):-40 dBm
Starting connection to server...
connected to server
HTTP/1.1 301 Moved Permanently
Connection: close
Server: gunicorn/19.7.0
Date: 2019年4月07日 15:02:16 GMT
Transfer-Encoding: chunked
Location: https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&outputsize=full&datatype=json&apikey=8UV48KKJJM1JW700
Content-Type: text/html; charset=utf-8
Via: 1.1 vegur
0
disconnecting from server.
I would like the code to return the string from the link. The output should like this (instead of just the 0). I had to shorten the example output below because of size restraints, but it should be the full webpage.
{
"Meta Data": {
"1. Information": "Intraday (5min) open, high, low, close prices and volume",
"2. Symbol": "MSFT",
"3. Last Refreshed": "2019-04-05 16:00:00",
"4. Interval": "5min",
"5. Output Size": "Compact",
"6. Time Zone": "US/Eastern"
},
"Time Series (5min)": {
"2019-04-05 16:00:00": {
"1. open": "119.8100",
"2. high": "119.9100",
"3. low": "119.7350",
"4. close": "119.8800",
"5. volume": "728814"
},
"2019-04-05 15:55:00": {
"1. open": "119.9200",
"2. high": "119.9200",
"3. low": "119.7800",
"4. close": "119.8000",
"5. volume": "307126"
},
"2019-04-05 15:50:00": {
"1. open": "119.9300",
"2. high": "119.9300",
"3. low": "119.8800",
"4. close": "119.9200",
"5. volume": "235238"
}
}
}
2 Answers 2
It's not that the API is incompatible with the Arduino, it's that the Arduino Firmware update tool used to add the required SSL certificate to the wifi module fails to do so because the SSL certificate for www.alphavantage.co does not exist at their domain.
This can be verified by:
openssl s_client -showcerts -connect www.alphavantage.co:443
-
I don't understand your answer. Web browsers don't have a security problem with this site. and your command returns public certificates of the site2019年04月09日 08:18:10 +00:00Commented Apr 9, 2019 at 8:18
The target site uses HTTPS. If you connect to port 80, it redirects to HTTPS on port 443.
Use the WiFiSSLClient
of the library. See the WiFiSSLClient example. Basically it is same as the example you used. There are two differences. It uses WiFiSSLClient
instead of WiFiClient
and connects to standard https port 443.
-
Thanks for the suggestion. I just tried making the edit, and this is what came up in the serial monitor: Attempting to connect to SSID: BLOCKED Connected to wifi SSID: BLOCKED IP Address: BLOCKED signal strength (RSSI):-46 dBm Starting connection to server... disconnecting from server.Jackson V– Jackson V2019年04月07日 19:36:14 +00:00Commented Apr 7, 2019 at 19:36
-
not a problem until it doesn't connect, but it should be
client.println("Host: www.alphavantage.co");
without https://2019年04月07日 20:34:30 +00:00Commented Apr 7, 2019 at 20:34 -
I just made that change too, and it still does not work.Jackson V– Jackson V2019年04月07日 20:49:17 +00:00Commented Apr 7, 2019 at 20:49
-
do you have the latest version of the firmware and library?2019年04月08日 07:42:25 +00:00Commented Apr 8, 2019 at 7:42
-
Thank you for the help, but after further investigation, I have discovered that this specific API is not compatible with Arduino.Jackson V– Jackson V2019年04月08日 23:49:14 +00:00Commented Apr 8, 2019 at 23:49
Explore related questions
See similar questions with these tags.
Location:
header and do a GET of that new URL. Basically it's filling in the missing default parameters for you and handing you a full URL to use. If you just use that new URL instead of your current one (i.e., add all the missing parameters) then it should work.