How can i upload data from arduino uno ethernet shield to mongodb Atlas

i need help to upload data from sensor to mongodb atlas using arduino uno ethernet shield i was using this code and it cann’t to connect to mongodb atlas
i was testing code with static values in currentReading & voltageReading and i’m sure make network access to 0.0.0.0/0
(databasename : HEMS
collectionname: HEMS1)


#include <SPI.h>
#include <Ethernet.h>
#include <ArduinoJson.h>

// Define your sensor pins
const int currentSensorPin = A0;
const int voltageSensorPin = A1;

// Initialize Ethernet
byte mac[] = {0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED};
EthernetClient client;

// MongoDB cluster hostname
char hostname[] = "mongodb+srv://mongo:XXXXXXXXXX@cluster0.vkdjf70.mongodb.net"; // Change this to your cluster hostname

void setup() {
    Ethernet.begin(mac);
    Serial.begin(9600);
    // Initialize other setup routines
}

void loop() {
    // Read sensor values
    int currentReading = 21;
    int voltageReading = 220;

    // Calculate watt
    float watt = currentReading * voltageReading / 1000.0;

    // Create a JSON object to hold the data
    StaticJsonDocument<128> doc;
    doc["Timestamp"] = millis();
    doc["Current"] = currentReading;
    doc["Voltage"] = voltageReading;
    doc["Watt"] = watt;

    // Convert JSON to string
    String jsonString;
    serializeJson(doc, jsonString);

    // Send data to MongoDB 
    if (client.connect(hostname, 27017)) {
        client.println("POST /HEMS.HEMS1 HTTP/1.1");
        client.println("Host: " + String(hostname));
        client.println("Content-Type: application/json");
        client.print("Content-Length: ");
        client.println(jsonString.length());
        client.println();
        client.println(jsonString);
        client.flush(); // Flush the data
        // Read the response from the server
        while (client.connected()) {
            if (client.available()) {
                char c = client.read();
                Serial.print(c); // Print the response
            }
        }
        client.stop(); // Close the connection
        Serial.println("Data sent successfully!"); // Print success message
    } else {
        Serial.println("Error connecting to MongoDB!"); // Print error message
    }

    delay(1000);
}

Hi @Omar_Fekry

It looks like you are trying to use the Data Api to insert data however hostname string is the form of a connection URI that would be used with a client driver.

If you wan to use the Data API set up the Data Api on your Atlas project.

1 Like

i tried it but still the same problem i think problem is in code he can’t find url of “mongodb+srv”

If you are using the Data Api there will be an https endpoint created. As per this screenshot:

If the URI starts with mongodb:// or mongodb+srv:// is for use with the mongodb client drivers. If you want to use the Data Api the end point will need to be created and the provided URL will be what is needed in the code.

1 Like

i’m sorry but still not working maybe i’m the problem :cry: