This project demonstrates a complete, end-to-end Internet of Things (IoT) data pipeline. An ESP32 microcontroller (the client) sends time data (or any sensor data) via HTTP POST requests to a Node.js server (the API). The server then securely logs this data into a MongoDB Atlas cloud database (the backend).
This setup is ideal for logging sensor readings, environmental data, or simple time stamps from a remote device to a centralized cloud database for analysis and visualization.
To get this project running, you will need to set up three main components: the MongoDB Atlas database, the Node.js server, and the ESP32 client.
Before starting, ensure you have the following installed:
- Node.js & npm (Node Package Manager)
- Arduino IDE (with ESP32 board support)
- Git (for version control)
You will need to install the necessary Node.js packages. Run the following commands in your server directory:
npm init -y
npm install express body-parser mongodbFollow these steps precisely to get your entire data pipeline running.
Steps:
- Create MongoDB account
- Create a cluster and db user pw
- Copy username password and connection string (This will be placed in your Node.js server code).
The server listens for incoming HTTP POST requests from the ESP32 and performs the database write operation.
- Create your server files (
server.js,package.json, etc.). - Replace your connection string in
server.jswith the full URI copied from MongoDB Atlas. - Set the
hostnameto'0.0.0.0'to allow connections from your ESP32 device. - Run the server:
node server.js
The ESP32 needs to be configured with the network details and the specific IP address of your running server.
- Write ESP32 code, replace with your ssid, pw and local ip address using ipconfig
- Upload the code to your ESP32.
Your Windows machine's firewall and network settings must be configured to allow external connections from the ESP32. These steps often fix the common "Connection Refused" error.
-
Make the Wifi network private in settings (Settings
$\rightarrow$ Network & Internet$\rightarrow$ Properties$\rightarrow$ Network profile type: Private). - Make a new inbound rule in windows defender firewall to accept from port you want to use for POST request (Port 3000, or whichever port you are using).
Once everything is set up, you can verify the data flow.
- Upload your code and see in serial monito if it works! Look for HTTP response code
200to confirm a successful POST. - Open MongoDB Database Explorer to find your newly created database with some entries that ESP32 sent! Confirm that the data is arriving in your collection.
DO NOT COMMIT SECRETS TO GIT.
Your MongoDB Atlas connection URI contains your username and password. This URI should NEVER be committed to your repository. Use Environment Variables and a .gitignore file to exclude the file containing the URI (e.g., a .env file).
If you leaked your URI, immediately rotate your database password in MongoDB Atlas!