This is the server for the SmartTerra project. It is a RESTful API that provides access to the data stored in the database. The server is built using Node.js and Express.js.
See config
folder.
- Clone the repository.
- Run
npm install
to install the dependencies. - Run
npm start
to start the server.
The SensorData
model represents the data collected from the sensors in the garden, such as temperature, humidity, and moisture levels.
const sensorDataSchema = new mongoose.Schema({
Id: { type: Number, required: true },
temperature: { type: Number },
humidity: { type: Number },
moisture: { type: Number },
timestamp: { type: Date, default: Date.now }
});
- Id: The unique identifier for each circuit (integer).
- temperature: Temperature value from the sensor (float).
- humidity: Humidity value from the sensor (float).
- moisture: Moisture level from the sensor (float).
- timestamp: The date and time when the data was recorded (Date).
The Command
model represents the commands sent to the actuators (fan and pump).
const commandSchema = new mongoose.Schema({
Id: { type: Number, required: true },
fan: Boolean,
pump: Boolean,
duration: Number,
timestamp: { type: Date, default: Date.now }
});
- Id: The unique identifier for each circuit (integer).
- fan: A boolean indicating whether the fan should be on (
true
) or off (false
). - pump: A boolean indicating whether the pump should be on (
true
) or off (false
). - duration: The duration for which the command should be executed (in seconds).
- timestamp: The date and time when the command was issued (Date).
The KeepAlive model represents the keep-alive status of the circuits.
const keepAliveSchema = new mongoose.Schema({
Id: { type: Number, required: true },
alive: { type: Boolean, required: true },
timestamp: { type: Date, default: Date.now }
});
- Id: The unique identifier for each circuit (integer).
- alive: A boolean indicating whether the circuit is alive (
true
) or not (false
). - timestamp: The date and time when the keep-alive message was received (Date).
- Description: Retrieve all sensor data records.
- Response Example:
[
{
"Id": 1,
"temperature": 25.5,
"humidity": 60.0,
"moisture": 80.0,
"timestamp": "2024-12-03T12:00:00Z"
},
{
"Id": 2,
"temperature": 22.0,
"humidity": 55.0,
"moisture": 75.0,
"timestamp": "2024-12-03T12:05:00Z"
}
]
- Description: Submit new sensor data to the system.
- Request Example:
{
"Id": 3,
"temperature": 26.2,
"humidity": 63.0,
"moisture": 85.0
}
- Response Example:
{
"Id": 3,
"temperature": 26.2,
"humidity": 63.0,
"moisture": 85.0,
"timestamp": "2024-12-03T12:10:00Z"
}
- Description: Retrieve a specific sensor data record by its circuit
Id
. - Response Example:
{
"Id": 1,
"temperature": 25.5,
"humidity": 60.0,
"moisture": 80.0,
"timestamp": "2024-12-03T12:00:00Z"
}
- Description: Retrieve all command records.
- Response Example:
[
{
"Id": 1,
"fan": true,
"pump": false,
"duration": 120,
"timestamp": "2024-12-03T12:00:00Z"
}
]
- Description: Submit a new command to control the actuators (fan and pump).
- Request Example:
{
"Id": 2,
"fan": true,
"pump": true,
"duration": 60
}
- Response Example:
{
"Id": 2,
"fan": true,
"pump": true,
"duration": 60,
"timestamp": "2024-12-03T12:15:00Z"
}
- Description: Retrieve a specific command record by its
Id
. - Response Example:
{
"Id": 1,
"fan": true,
"pump": false,
"duration": 120,
"timestamp": "2024-12-03T12:00:00Z"
}
- Description: Update an existing command by
Id
. - Request Example:
{
"Id": 1,
"fan": false,
"pump": true,
"duration": 180
}
- Response Example:
{
"Id": 1,
"fan": false,
"pump": true,
"duration": 180,
"timestamp": "2024-12-03T12:20:00Z"
}
- Description: Delete a specific command record by
Id
. - Response Example:
{
"message": "Command with Id 1 has been deleted successfully"
}
- Description: Retrieve all keep-alive records.
- Response Example:
[
{
"Id": 1,
"alive": true,
"timestamp": "2024-12-03T12:00:00Z"
}
]
- Description: Retrieve a specific keep-alive record by its
Id
. - Response Example:
{
"Id": 1,
"alive": true,
"timestamp": "2024-12-03T12:00:00Z"
}
- Description: Submit a new keep-alive record.
- Request Example:
{
"Id": 1,
"alive": true
}
- Response Example:
[
{
"Id": 1,
"alive": true,
"timestamp": "2024-12-03T12:00:00Z"
}
]
- Sensor Data:
ict66/smarterra/sensors/
- Contains sensor readings: temperature, humidity, and moisture.
- Command Data:
ict66/smarterra/commands/
- Used to send commands to the actuators (fan, pump).
- Keep-Alive:
ict66/smarterra/keepalive/
- Used to check the circuit's status (alive or not).
git clone https://github.com/your-username/your-repository.git
cd your-repository
Make sure you have Node.js
and npm
installed. Then run:
npm install
Install the .env
file in the root directory of the project by:
npm install dotenv
npm start
This will start the server on http://localhost:3000
.
- Ensure that the MongoDB server is running and accessible from your application. Add your IP address to the allowed list in the MongoDB Atlas dashboard.
- The app subscribes to the topics
ict66/smarterra/sensors/
,ict66/smarterra/commands/
, andict66/smarterra/keepalive/
to handle sensor data, commands, and keep-alive messages. - We work with only one circuit for now, so the default
Id
is set to1
. You can modify the code to handle multiple circuits if needed.