Arduino Uno R4 HttpClient Posting to Azure Web Function

Hi,

I’m a relative newbie on the IOT front, and am trying to build a project that logs data from a sensor. I’ve purchased an Arduino Un R4 Wifi board, ands have managed to connect an ultrasonic distance sensor that seems to be logging data correctly to the Serial Monitor.

The problem I am having is sending my sensor data to an Azure Web Function via a Https Pos method.

My sketch:

  • Connects to my local wifi
  • Logs data for 15 seconds, and then creates a JSON object
  • Serialises the Json object to a string
  • When I try and ‘Post’ via an HttpClient, the wheels fall off.

I have created an Azure Web Function that accepts a Post request. Testing this with PostMan works fine.

It looks like it might be the wifi client not being able to create a secure https connection, but I’m just not sure. I can’t seem to find any examples on-line and am wondering if this is an unusual path?

Does anyone have any suggestions of guidance? Below is the code I am using …

#include <ArduinoHttpClient.h>
#include <ArduinoJson.h>
#include <WiFiS3.h>

// WiFi credentials
char ssid[] = "ssid";
char pass[] = ""; // Add your WiFi password if required

// Web API server details
char serverAddress[] = "https://website.azurewebsites.net"; // Azure Function Web Site Address
int port = 443; // HTTPS port
String apiUrl = "/api/fnLevelReading?code=random_auth_code";

WiFiClient wifi; // Secure WiFi client
HttpClient client = HttpClient(wifi, serverAddress, port);
int status = WL_IDLE_STATUS;
etc.

And the code to make the post is:

void pushToWebAPI(float avgdistance) {
  // Create JSON object
  StaticJsonDocument<200> doc;
  doc["LevelReading"] = avgdistance;
  doc["LevelIdentifier"] = "Sensor2"; // Change as necessary

  // Serialize JSON object to string
  String output;
  serializeJson(doc, output);

  // Post JSON data to server
  Serial.println("making POST request");
  String contentType = "application/json";
  Serial.println(output);
  Serial.println(serverAddress);

  client.beginRequest();
  client.post(apiUrl);
  client.sendHeader("Content-Type", contentType);
  client.sendHeader("Content-Length", output.length());
  client.beginBody();
  client.print(output);
  client.endRequest();

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

Hi David.

Welome. Good to have you with us.

Code looks good on first glance.
Is this where the error occurs?
Do you get an error message at runtime at all?

1 Like

Hey David,

I have done some light research on this issue and it seems like the ESP8266 chip on this board works fine with HTTP but can struggle with the more complex nature of HTTPS which may be related to your issue.

By the look of it this Arduino WiFi library gets around this issue pretty well. It may be worthwhile looking into this method if you can’t find a decent solution to work with your current libraries.

Hopefully this is an easy fix that someone can get sorted for you but this alternate method should be easier.

Hope this helps!
Sam

2 Likes

Thanks Sam. I hope so!

In the meantime, I’ve managed to enrol my Arduino Uno R4 up to the Arduino Cloud, and data is being saved. I’ve managed to write a REST API to interact with the Arduino Cloud data - it seems a very round-about way to interact with the IOT, but I’m still investigating.

Outlook-syn2vklc.png

2 Likes

Very cool to see the progress, Arduino Cloud is a great tool.

Keep us posted!
Sam

1 Like

This might be the answer suited to your needs. GitHub - piscodev/r4httpclient: A wrapper for Arduino UNO R4 WiFi microcontroller WiFiS3 HttpClient with functionality of GET & POST.

2 Likes

Hi @Slender280542, Welcome to the Forums!!!

Thanks for sharing.

Thanks for sharing the library. That looks perfect! I’ll definitely give this a go.

For the problem above, we ended up using Arduino Cloud which has been a pretty good experience to date.

2 Likes