Go Down

Topic: WifiNINA Library (Read 423 times) previous topic - next topic

nwoollcombe

Apr 07, 2019, 05:34 am Last Edit: Apr 07, 2019, 08:05 pm by nwoollcombe Reason: Suggestion
Hi,

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):

Code: [Select]


/*
  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:

Code: [Select]
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: Sun, 07 Apr 2019 15:02:16 GMT
Transfer-Encoding: chunked
Location: https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&outputsize=full&datatype=csv&apikey=demo
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.
Code: [Select]
{
    "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"
        }
    }
}


Thank you for your help!

pert

Please post your full sketch.

If possible, you should always post code directly in the forum thread as text using code tags:
  • Do an Auto Format (Tools > Auto Format in the Arduino IDE or Ctrl + B in the Arduino Web Editor) on your code. This will make it easier for you to spot bugs and make it easier for us to read.
  • In the Arduino IDE or Arduino Web Editor, click on the window that contains your sketch code.
  • Press "Ctrl + A". This will select all the text.
  • Press "Ctrl + C". This will copy the selected text to the clipboard.
  • In a forum reply here, click on the reply field.
  • Click the </> button on the forum toolbar. This will add the forum's code tags markup to your reply.
  • Press "Ctrl + V". This will paste the sketch between the code tags.
  • Move the cursor outside of the code tags before you add any additional text to your reply.
  • Repeat the above process if your sketch has multiple tabs.


This will make it easy for anyone to look at it, which will increase the likelihood of you getting help.

If the sketch is longer than the 9000 characters maximum allowed by the forum, then it's OK to add it as an attachment. After clicking the "Reply" button, you will see an "Attachments and other settings" link.

When your code requires a library that's not included with the Arduino IDE please post a link (using the chain links icon on the forum toolbar to make it clickable) to where you downloaded that library from or if you installed it using Library Manger (Sketch > Include Library > Manage Libraries in the Arduino IDE or Libraries > Library Manager in the Arduino Web Editor) then say so and state the full name of the library.

nwoollcombe

Hi,

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):

Code: [Select]


/*
  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:

Code: [Select]
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: Sun, 07 Apr 2019 15:02:16 GMT
Transfer-Encoding: chunked
Location: https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&outputsize=full&datatype=csv&apikey=demo
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.
Code: [Select]
{
    "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"
        }
    }
}


Thank you for your help!

nwoollcombe

Thanks for the suggestion. I just edited the question.

arduino_new

Try changing:
Code: [Select]

client.println("GET /query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&apikey=demo HTTP/1.1");


to this:
Code: [Select]

client.println("GET /query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&outputsize=full&datatype=csv&apikey=demo HTTP/1.1");

nwoollcombe

Thank you arduino_new for the suggestion.

I just tried making the change, and I received a similar error which I copied below.


Code: [Select]
Attempting to connect to SSID: BLOCKED
Connected to wifi
SSID: BLOCKED
IP Address: BLOCKED
signal strength (RSSI):-48 dBm

Starting connection to server...
connected to server
HTTP/1.1 301 Moved Permanently
Connection: close
Server: gunicorn/19.7.0
Date: Sun, 07 Apr 2019 19:19:20 GMT
Transfer-Encoding: chunked
Location: https://www.alphavantage.co/query?function=TIME_SERIES_INTRADAY&symbol=MSFT&interval=5min&outputsize=full&datatype=csv&apikey=demo
Content-Type: text/html; charset=utf-8
Via: 1.1 vegur

0


disconnecting from server.

Juraj

answer https://arduino.stackexchange.com/questions/63265/arduino-wifi-rev2-json-string
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

pert

I think this is the fourth time you've posted this now. Please stop doing that! We're already trying to help you in your original thread:
http://forum.arduino.cc/index.php?topic=608492
as well as on your Stack Exchange post:
https://arduino.stackexchange.com/questions/63265

Duplicate posts can waste the time of the people trying to help. Someone might spend 15 minutes writing a detailed answer on this thread, without knowing that someone else already did the same in the other thread.

In the future, take some time to pick the forum section that best suits the topic of your question and then only post once to that forum section. This is basic forum etiquette, as explained in the sticky "How to use this forum - please read." post you will find at the top of every forum section. It contains a lot of other useful information. Please read it.

Coding Badly


@nwoollcombe, stop cross-posting.  Threads merged.


kenharpster

#9
Apr 08, 2019, 06:25 pm Last Edit: Apr 08, 2019, 06:27 pm by kenharpster
3 things:

1. You are connecting to an SSL site, you will need to use: WiFiSSLClient for the client and port 443 to connect
2. The server and host name should be: www.alphavantage.co

LASTLY and very importantly

3. The SSL certificate (alphavantage.co:443) for the site you are trying to hit must be on the Arduino (it's not).

Unfortunately you cannot add/download the the SSL certificate to the Arduino using the existing feature in the Firmware update tool. It returns "Remote host closed connection during handshake".  I am guessing that The SSL certificate is probably stored on a third party server and cannot be retrieved by the existing Arduino tool set.

I created a program to write JSON data to my Firebase server and therefore learned the necessary steps. In your case unless someone knows how to get the SSL certificate on to the Arduino, you will not be able GET or PUT to the SSL site...

Juraj

#10
Apr 08, 2019, 07:10 pm Last Edit: Apr 08, 2019, 07:11 pm by Juraj
3 things:

1. You are connecting to an SSL site, you will need to use: WiFiSSLClient for the client and port 443 to connect
2. The server and host name should be: www.alphavantage.co

LASTLY and very importantly

3. The SSL certificate (alphavantage.co:443) for the site you are trying to hit must be on the Arduino (it's not).
1. and 2. are in the Stack Exchange answer (by me)

3. the certificate must not be "on the arduino", but it must have a certificate path to a CA certificate stored in Nina firmware

the CA public certificates store of the Nina firmware is here https://github.com/arduino/nina-fw/blob/master/data/roots.pem
You can't write an Arduino sketch if you didn't learn programming. Not the language, but the concepts of programming - algorithms and data types.

kenharpster

#11
Apr 08, 2019, 07:22 pm Last Edit: Apr 08, 2019, 10:47 pm by kenharpster
1. and 2. are in the Stack Exchange answer (by me)

3. the certificate must not be "on the arduino", but it must have a certificate path to a CA certificate stored in Nina firmware

the CA public certificates store of the Nina firmware is here https://github.com/arduino/nina-fw/blob/master/data/roots.pem
I have been playing with this code all morning and quickly figured out 1 and 2 (the hint was the HTTPS address returned in the 301 error). But, with regard to number 3:

The completion window for the "Upload Certificates to WiFi module" button states "The certificates have been uploaded" if it was successful.

That contradicts what you are saying and instead verifies that the certificate for the site that I am adding with the "Add domain" button does indeed get uploaded to the WiFi module.  This process does not work for alphavantage.co and I have verified that there is no certificate available at the domain address ( openssl s_client -showcerts -connect www.alphavantage.co:443 ) which is what the problem eventually comes down to.

The documented procedure I have used to upload a certificate to the WiFi module can be found here (under the heading "Certificate uploading"): https://www.arduino.cc/en/Tutorial/FirmwareUpdater
and although it does not identify the Arduino Uno WiFi Rev 2 as "Hardware required" in the document, it works.

And as an FYI, I had to add the domain www.firebaseio.com using the firmware update tool and upload the certificate to the module before I could do a PUT to my Firebase database which is accessed via SSL (port 443). 

Go Up