Arduino UNO WiFi Rev 2 not posting to SSL connections

523 Views Asked by At

I've been having an issue trying to get my UNO WiFi Rev 2 to make a POST request to my https azure server. I've been able to successfully POST to this server using a MKR1000 using the WiFi101 library, so it's weird that I can't get the UNO to POST using the WiFiNINA library. I've tried two approaches to this POST request, if anybody could offer a solution to either of them, I would appreciate it.

My first attempt was based off the WiFININA libary's example for HTTPS POST:

This example creates a client object that connects and transfers
data using always SSL.

It is compatible with the methods normally related to plain
connections, like client.connect(host, port).

Written by Arturo Guadalupi
last revision November 2015

*/

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

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
char ssid[] = "secret";        // your network SSID (name)
char pass[] = "secret";    // 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[] = "secret";    // 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):
int HTTP_PORT = 443;
String HTTP_METHOD = "POST";
char HOST_NAME[] = "secret";
//IPAddress server(secret); 
String PATH_NAME = "/wtid/logCovid";
WiFiSSLClient client;

String queryString = "uuid=5555&manufacturerID=6666";

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 < WIFI_FIRMWARE_LATEST_VERSION) {
    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, HTTP_PORT)) {
    Serial.println("connected to server");
    // Make a HTTP request:

    // MUST HAVE THESE HEADERS AND IN THIS ORDER!!!
    client.println(HTTP_METHOD + " " + PATH_NAME + " HTTP/1.1");
    client.println("Host: " + String(HOST_NAME));
    client.println("User-Agent: Arduino/1.0");
    client.println("Connection: close");
    client.println("Content-Type: application/x-www-form-urlencoded");
    client.print("Content-Length: ");
    client.println(queryString.length());
    client.println();
    // Body AKA the data we are sending to the server
    client.print(queryString);
  }
}

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

Here is the Serial monitor response whenever I upload that script:

Starting connection to server...

disconnecting from server.

My second approach was using the ArduinoHttpClient with a WiFiSSLClient, but again no luck:

#include <ArduinoHttpClient.h>
#include <WiFiNINA.h>

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// WiFi Settings ///////
char ssid[] = "secret";        // your network SSID (name)
char pass[] = "secret";    // your network password (use for WPA, or use as key for WEP)

char serverAddress[] = "secret";  // server address
int port = 443;

WiFiSSLClient wifi;
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;

void setup() {
  Serial.begin(9600);
  while ( status != WL_CONNECTED) {
    Serial.print("Attempting to connect to Network named: ");
    Serial.println(ssid);                   // print the network name (SSID);

    // Connect to WPA/WPA2 network:
    status = WiFi.begin(ssid, pass);
  }

  // print the SSID of the network you're attached to:
  Serial.print("SSID: ");
  Serial.println(WiFi.SSID());

  // print your WiFi shield's IP address:
  IPAddress ip = WiFi.localIP();
  Serial.print("IP Address: ");
  Serial.println(ip);
}

void loop() {
  Serial.println("making POST request");
  String contentType = "application/x-www-form-urlencoded";
  String postData = "uuid=4444&manufacturerID=5555";

  client.post("/wtid/logCovid", contentType, postData);

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  String response = client.responseBody();

  Serial.print("Status code: ");
  Serial.println(statusCode);
  Serial.print("Response: ");
  Serial.println(response);

  Serial.println("Wait five seconds");
  delay(5000);
}

Here is the Serial monitor response whenever I upload that script:

making POST request
Status code: -2
Response: 
Wait five seconds

I've updated the WiFiNINA firmware and uploaded the SSL certificates via the arduino IDE to the board as well, and I am able to make SSL GET requests to sites like google.com without issue. I do get Serial responses ensuring that I connected to the wifi, just didn't want to put that part of the Serial monitor response.

Thank you to anyone who can help!

:)

0

There are 0 best solutions below