How to POST JSON data from NANO 33 IOT?

Hello all,

I'm trying to POST JSON data from the NANO 33 IoT board every 2-3 min. I only can find examples for ESP32 or ESP8266 boards but the NANO 33 IoT has a WiFiNINA library that might require different protocol. I'm wondering if any forerunners have walked this path before, which is to POST JSON data from NANO 33 IoT or similar boards, and would like to share some examples or thoughts?

Thanks in advance!

Could you please provide a link to an example for the ESP? POST JSON data could mean all kind of things. JSON is just a data notation.

Thank you for your reply! Here is an example I found on ArduinoJson's website (the third example is about POST JSON). I'm not sure if I should use HTTPClient or not, as I'm using an URL to POST data. I'm totally new to JSON and POST...

I just tried HTTPClient, which seems to be a ESP8266 library class and cannot be used for my NANO 33 IOT. I'm experimenting with ArduinoHttpClient library now. And welcome any thoughts or examples. I will also post my code if it works. Many thanks!

I have a URL that can send JSON data to but have no idea how I should implement the code...

I had a look at the library. As far as I understand the library just creates and parses JSON data. It does not seem to be specific to ESP.

I found an example for POST using the WiFiNINA library with very positive feedback from the creator of the post. It is in reply #8.

https://forum.arduino.cc/t/using-post-and-wifinina-on-uno-wifi-rev2/596583/8

Can you provide some more details of what you would like to achieve and what the specific issue is? Do you want to send JSON data to somewhere or do you need to parse JSON data that you receive from somewhere? Maybe both?

Can you ask an admin to move the post to the Arduino Nano 33 IoT sub forum? That will allow other Nano 33 IoT user to find this thread.

Thanks for the link!

I want to POST measurement data to a government website - the website provides me a URL and a token that I need for the POST. I don't need to receive data.

Sure I can ask the admin to move the post to the Arduino NANO 33 IOT sub form. Thanks a lot!

Here is the example modified to post JSON and to wait for the Serial Monitor to connect to the board. Tested on Arduino Nano 33 IoT.

/*
  Simple POST client for ArduinoHttpClient library
  Connects to server once every 30 seconds, sends a POST request
  and a request body

  created 14 Feb 2016
  modified 22 Jan 2019
  by Tom Igoe
  modfied 28 July 2021 sending JSON

  this example is in the public domain  
*/

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

///////please enter your sensitive data in the Secret tab/arduino_secrets.h
/////// Wifi Settings ///////
char ssid[] = SECRET_SSID;
char pass[] = SECRET_PASS;

const char serverName[] = "httpbin.org";  // server name
int port = 80;

WiFiClient wifi;
HttpClient client = HttpClient( wifi, serverName, port );
int status = WL_IDLE_STATUS;

void setup()
{
  Serial.begin( 9600 );
  while ( !Serial );

  while ( status != WL_CONNECTED )
  {
    Serial.print( "Attempting to connect to Network named: " );
    Serial.println( ssid );
    status = WiFi.begin( ssid, pass );
  }

  Serial.print( "SSID: " );
  Serial.println( WiFi.SSID() );

  IPAddress ip = WiFi.localIP();
  Serial.print( "IP Address: " );
  Serial.println( ip );
}

void loop()
{
  String contentType = "application/json";
  String postData = "{\"Id\": 42,\"sensor\": \"temperature\",\"value\": 21.5,\"unit\": \"C\"}";

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

  // read the status code and body of the response
  int statusCode = client.responseStatusCode();
  Serial.print( "Status code: " );
  Serial.println( statusCode );
  String response = client.responseBody();
  Serial.print( "Response: " );
  Serial.println( response );

  Serial.println( "Wait 30 seconds" );
  delay( 30000 );
}

Thank you so much! I modified the code and the website is responding. However, there's one issue that the government website also requires a token to be sent with the JSON data.

I tried modifying my code based on the following example by adding headers but it still doesn't work:

Does this website provide any information for users? They should have some documentation that will help you.