Go Down

Topic: WiFiNINA - SSL client connect by IP Address is freezing (Read 640 times) previous topic - next topic

sorawitph

Hello everyone. I just find 2 issues about WiFiNINA library.
Does anyone try SSL client connect using this library?

Issue #1
SSL client connect by host name is too slow, around 4-6 seconds.
Code: [Select]
#include <SPI.h>
#include <WiFiNINA.h>

#define WIFI_SSID "SSID"
#define WIFI_PASSWORD "PASSWD"

int status = WL_IDLE_STATUS;
WiFiClient client;
unsigned long start_ms, stop_ms = 0;

void connect_wifi();
void post_write_data();
void post_read_data();
void printWiFiStatus();

void setup() {
  // put your setup code here, to run once:
  // 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
  }

  Serial.println("################################################################################");

  connect_wifi();
  post_write_data();
}

void loop() {
  // put your main code here, to run repeatedly:
}

void connect_wifi()
{
  Serial.println("[LOG] Connect Wi-Fi");
 
  start_ms = millis();
 
  // 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 < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  Serial.print("[LOG] Attempting to connect to SSID: ");
  Serial.println(WIFI_SSID);
  status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  stop_ms = millis();
 
  printWiFiStatus();
 
  Serial.print("[LOG] Connected to wifi using ");
  Serial.print((stop_ms - start_ms));
  Serial.println(" ms");
  Serial.println("");
}

void post_write_data()
{
  String PostData = "{\"device_id\": \"DEV0000001\", \"station_id\": \"STA0000001\", \"rfid\": \"000001\"}";
  char server_url[] = "www.WEBSITE.com";
  IPAddress server_ip;
 
  Serial.println("[LOG] DNS Lookup");

  start_ms = millis();
 
  WiFi.hostByName(server_url, server_ip);

  stop_ms = millis();
 
  Serial.print("[LOG] Get IP Address ");
  Serial.print(server_ip);
  Serial.print(" using ");
  Serial.print((stop_ms - start_ms));
  Serial.println(" ms");
  Serial.println("");
 
  Serial.println("[LOG] Starting connection to server...");

  start_ms = millis();
 
  if (client.connectSSL(server_url, 443)) {

    stop_ms = millis();
   
    Serial.print("[LOG] Connected to server using ");
    Serial.print((stop_ms - start_ms));
    Serial.println(" ms");
    Serial.println("");
   
    start_ms = millis();
   
    client.println("POST WEBSITE PATH HTTP/1.1");
    client.println("Host: www.WEBSITE.com");
    client.println("Content-Type: application/json");
    client.println("Authorization: Beare BEARE");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);
  }

  while (!client.available());

  // 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 (!client.connected()) {
    Serial.println();
    Serial.println("[LOG] Disconnecting from server.");
    client.stop();
  }

  stop_ms = millis();
   
  Serial.print("[LOG] Send data to server using ");
  Serial.print((stop_ms - start_ms));
  Serial.println(" ms");
  Serial.println("");
}

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");
}


Issue #2
SSL client connect by IP address is freezing.
Code: [Select]
#include <SPI.h>
#include <WiFiNINA.h>

#define WIFI_SSID "SSID"
#define WIFI_PASSWORD "PASSWD"

int status = WL_IDLE_STATUS;
WiFiClient client;
unsigned long start_ms, stop_ms = 0;

void connect_wifi();
void post_write_data();
void post_read_data();
void printWiFiStatus();

void setup() {
  // put your setup code here, to run once:
  // 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
  }

  Serial.println("################################################################################");

  connect_wifi();
  post_write_data();
}

void loop() {
  // put your main code here, to run repeatedly:
}

void connect_wifi()
{
  Serial.println("[LOG] Connect Wi-Fi");
 
  start_ms = millis();
 
  // 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 < WIFI_FIRMWARE_LATEST_VERSION) {
    Serial.println("Please upgrade the firmware");
  }

  // attempt to connect to WiFi network:
  Serial.print("[LOG] Attempting to connect to SSID: ");
  Serial.println(WIFI_SSID);
  status = WiFi.begin(WIFI_SSID, WIFI_PASSWORD);

  stop_ms = millis();
 
  printWiFiStatus();
 
  Serial.print("[LOG] Connected to wifi using ");
  Serial.print((stop_ms - start_ms));
  Serial.println(" ms");
  Serial.println("");
}

void post_write_data()
{
  String PostData = "{\"device_id\": \"DEV0000001\", \"station_id\": \"STA0000001\", \"rfid\": \"000001\"}";
  char server_url[] = "www.WEBSITE.com";
  IPAddress server_ip;
 
  Serial.println("[LOG] DNS Lookup");

  start_ms = millis();
 
  WiFi.hostByName(server_url, server_ip);

  stop_ms = millis();
 
  Serial.print("[LOG] Get IP Address ");
  Serial.print(server_ip);
  Serial.print(" using ");
  Serial.print((stop_ms - start_ms));
  Serial.println(" ms");
  Serial.println("");
 
  Serial.println("[LOG] Starting connection to server...");

  start_ms = millis();
 
  if (client.connectSSL(server_ip, 443)) {

    stop_ms = millis();
   
    Serial.print("[LOG] Connected to server using ");
    Serial.print((stop_ms - start_ms));
    Serial.println(" ms");
    Serial.println("");
   
    start_ms = millis();
   
    client.println("POST WEBSITE PATH HTTP/1.1");
    client.println("Host: www.WEBSITE.com");
    client.println("Content-Type: application/json");
    client.println("Authorization: Beare BEARE");
    client.println("Connection: close");
    client.print("Content-Length: ");
    client.println(PostData.length());
    client.println();
    client.println(PostData);
  }

  while (!client.available());

  // 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 (!client.connected()) {
    Serial.println();
    Serial.println("[LOG] Disconnecting from server.");
    client.stop();
  }

  stop_ms = millis();
   
  Serial.print("[LOG] Send data to server using ");
  Serial.print((stop_ms - start_ms));
  Serial.println(" ms");
  Serial.println("");
}

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");
}


My development environment
- Arduino MKR WiFi 1010
- NINA firmware version 1.3.0
- Arduino IDE version 1.8.12
- WiFiNINA library version 1.5.0

ballscrewbob

Moved your topic to it's current location as it is more suitable.

Could you take a few moments to Learn How To Use The Forum.
Other general help and troubleshooting advice can be found here.
It will help you get the best out of the forum in the future.
It may not be the answer you were looking for but its the one I am giving based on either experience, educated guess, google (who would have thunk it ! ) or the fact that you gave nothing to go with in the first place so I used my wonky crystal ball.

sorawitph


Go Up