Jump to content

A few questions regarding this piece of Arduino code.

LebowskiBuschemi
Quote

 

void POSTData()
{
      
      if(WiFi.status()== WL_CONNECTED){
      HTTPClient http;

      http.begin([webhook link]);
      http.addHeader("Content-Type", "application/json");

      String json;
      serializeJson(doc, json);

      Serial.println(json);
      int httpResponseCode = http.POST(json);
      Serial.println(httpResponseCode);
      }

 

Hi, I was hoping if someone could explain to me what exactly is going on in this function? Basically, this function is what posts to a database on MongoDB using a webhook link and I'm having trouble understanding what exactly is going on in the function itself. If someone could please explain to me what each line of the code is doing I'd appreciate it. Also, I wanted to ask what exactly the serializeJson() function is doing because it's also being used in the void loop() function, but with a different second arguement:

 

Quote

void loop()
{
  serializeJsonPretty(doc, Serial);
}

 

Link to comment
Share on other sites

Link to post
Share on other sites

Which HTTP lib is this? I found a few but non of them has a begin() function.

ಠ_ಠ

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/16/2021 at 12:32 PM, LebowskiBuschemi said:

Hi, I was hoping if someone could explain to me what exactly is going on in this function? Basically, this function is what posts to a database on MongoDB using a webhook link and I'm having trouble understanding what exactly is going on in the function itself. If someone could please explain to me what each line of the code is doing I'd appreciate it. Also, I wanted to ask what exactly the serializeJson() function is doing because it's also being used in the void loop() function, but with a different second arguement:

 

 

Im not sure but heres my theory :old-smile::

 

First it checks if wifi is connected, then it fetches some kind of API, Then prints the response from the api into a serial, with 

Serial.printLn()

 

Then gets a http response from HTTP POST sending that json it got from the API with the request POST request.

Then printing the response of that POST request to serial using

Serial.printLn()

 

If you google what a POST request does, You may find more answers on why its sending that response with

http.POST()

 

Link to comment
Share on other sites

Link to post
Share on other sites

On 10/17/2021 at 1:05 AM, shadow_ray said:

Which HTTP lib is this? I found a few but non of them has a begin() function.

Apologies for the late reply. The library that I'm using is called HTTPClient.h and I'm also using a json library called ArduinoJson.h.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×