Mongodb arduinohttpclient

Hello Experts , i want to communicate to mongodb atlas via 4g module ,help me
this is my code resolve it if there is any error

#define TINY_GSM_MODEM_SIM7600
#define SerialMon Serial
#define SerialAT Serial1
#define TINY_GSM_DEBUG SerialMon

#define TINY_GSM_TEST_GPRS true
#define TINY_GSM_TEST_TCP true

// Your GPRS credentials, if any
const char apn[] = "jionet"; // Your operator APN
const char gprsUser[] = "azhar";
const char gprsPass[] = "azhar";

//Endpoint = "https://ap-south-1.aws.data.mongodb-api.com/app/data-zbema/endpoint/data/v1/action/insertOne";
const char* AtlasAPIEndpoint = "ap-south-1.aws.data.mongodb-api.com";
const char* AtlasAPIKey = "o2z2gUwIyqqrwT4KY933lfcGJzlGhOEQzeK77GskfiCKK2yH0AhFYVzbO3kVA840";
const int SSL_PORT = 443;

#include <ArduinoJson.h>
#include <TinyGsmClient.h>
#include <ArduinoHttpClient.h>
#include "SSLClient.h"
#include "utilities.h"
#include "certs.h"

#ifdef DUMP_AT_COMMANDS
#include <StreamDebugger.h>
StreamDebugger debugger(SerialAT, SerialMon);
TinyGsm modem(debugger);
#else
TinyGsm modem(SerialAT);
#endif

// HTTPS Transport
TinyGsmClient base_client(modem, 0);
SSLClient secure_layer(&base_client);
HttpClient client = HttpClient(secure_layer, AtlasAPIEndpoint, SSL_PORT);

void setup()
{
// Set console baud rate
SerialMon.begin(19200);
delay(10);

// Set GSM module baud rate
SerialAT.begin(UART_BAUD, SERIAL_8N1, MODEM_RX, MODEM_TX);


pinMode(MODEM_PWRKEY, OUTPUT);
digitalWrite(MODEM_PWRKEY, HIGH);
delay(300); //Need delay
digitalWrite(MODEM_PWRKEY, LOW);

pinMode(MODEM_FLIGHT, OUTPUT);
digitalWrite(MODEM_FLIGHT, HIGH);    

//Add CA Certificate

// secure_layer.setCACert(root_ca);
bool res ;
DBG("Initializing modem...");
if (!modem.init()) {
}

#if TINY_GSM_TEST_GPRS
String ret;
do {
ret = modem.setNetworkMode(54);
} while (!ret);
DBG("Waiting for network...");
if (modem.isNetworkConnected()) {
DBG("Network connected");
}
#endif
#if TINY_GSM_TEST_GPRS
DBG("Connecting to", apn);
if (!modem.gprsConnect(apn, gprsUser, gprsPass)) {
return;
}

res = modem.isGprsConnected();
DBG("GPRS status:", res ? "connected" : "not connected");

#endif
}

void light_sleep(uint32_t sec )
{
esp_sleep_enable_timer_wakeup(sec * 1000000ULL);
esp_light_sleep_start();
}

void loop()
{

   // String url = "https://ap-south-1.aws.data.mongodb-api.com/app/data-zbema/endpoint/data/v1/action/insertOne";


      // Create the JSON payload
DynamicJsonDocument payload(1024);
payload["dataSource"] = "Cluster0";
payload["database"] = "College";
payload["collection"] = "student";

DynamicJsonDocument document(256);
document["city"] = "delhi";
payload["document"] = document;

// Serialize the JSON payload to a string
String JSONText;
serializeJson(payload, JSONText);

Serial.println("making POST request");
client.beginRequest();
client.post("/app/data-zbema/endpoint/data/v1/action/insertOne");
client.sendHeader("Content-Type", "application/json");
client.sendHeader("Content-Length", JSONText.length());
client.sendHeader("api-key", AtlasAPIKey);
client.beginBody();
client.print(JSONText);
client.endRequest();

int statusCode = client.responseStatusCode();
String response = client.responseBody();

Serial.print("Status code: ");
Serial.println(statusCode);
Serial.print("Response: ");
Serial.println(response);
esp_sleep_enable_timer_wakeup(0);
esp_deep_sleep_start();
}

Please follow the advice given in the link below when posting code, in particular the section entitled 'Posting code and common code problems'

Use code tags (the < CODE/ > icon above the compose window) to make it easier to read and copy for examination

https://forum.arduino.cc/t/how-to-get-the-best-out-of-this-forum

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.