Skip to content

Commit

Permalink
Sensing part and notification done
Browse files Browse the repository at this point in the history
  • Loading branch information
Kalvinci committed Apr 25, 2022
1 parent f4c7285 commit 532122d
Show file tree
Hide file tree
Showing 6 changed files with 160 additions and 16 deletions.
46 changes: 46 additions & 0 deletions arduino.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#include <ArduinoJson.h>
StaticJsonDocument<200> doc;

void setup()
{
Serial.begin(9600);
}

void loop()
{
if (Serial.available() > 0)
{
String receivedString = (String)Serial.readStringUntil('\n');
DeserializationError error = deserializeJson(doc, receivedString);
if (error)
{
return;
}
const char *action = doc["action"];

if (strcmp(action, "LIGHT") == 0)
{
const char *state = doc["state"];
if (strcmp(state, "ON") == 0)
{
digitalWrite(4, HIGH);
}
else
{
digitalWrite(4, LOW);
}
}
else if (strcmp(action, "WATER") == 0)
{
digitalWrite(5, HIGH);
}
else if (strcmp(action, "GET_INFO") == 0)
{
digitalWrite(6, HIGH);
}
else if (strcmp(action, "SET_PLANT") == 0)
{
digitalWrite(7, HIGH);
}
}
}
8 changes: 7 additions & 1 deletion arduinoService.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,13 @@ arduinoSerialPort.on("open", () => {
const parser = new ReadlineParser();
arduinoSerialPort.pipe(parser);
parser.on("data", (message) => {
sendNotification(message);
try {
console.log(message);
const jsonMessage = JSON.parse(message);
if (jsonMessage.action === "NOTIFY") {
sendNotification(jsonMessage.type, jsonMessage.data);
}
} catch {}
});

function writeToSerialPort(jsonData) {
Expand Down
84 changes: 80 additions & 4 deletions notifier.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,89 @@
const mongoose = require("mongoose");
const axios = require("axios");
require("dotenv").config();

async function sendNotification(message) {
mongoose
.connect(process.env.CONNECTION_URL)
.catch((error) => console.log("mongodb connection error", error));

async function sendNotification(type, data) {
try {
await axios.post(process.env.WEB_HOOK_URL, {
content: message,
});
const postData = await getPostData(type, data);
await axios.post(process.env.DISCORD_WEBHOOK_URL, postData);
} catch (error) {
console.log(error);
throw new Error("Error sending message");
}
}

async function getPostData(type, data) {
let postData;
if (type === "WATER_ALERT") {
postData = {
content:
"Your plants are running out of water :disappointed_relieved:",
embeds: [
{
image: {
url: "https://c.tenor.com/5J2A7MTVr-IAAAAC/summer-water-spongebob-squarepants.gif",
},
},
],
};
} else if (type === "PLANT_INFO") {
const plantDBData = await getPlantDBData(data.rackId);
postData = {
content: `I have got you the info of plant in rack ${data.rackId} :smiley:`,
embeds: [
{
color: parseInt("0x12e385", 16),
fields: [
{
name: "Rack",
value: plantDBData.rackId.toString(),
inline: true,
},
{
name: "Name",
value: plantDBData.name,
inline: true,
},
{
name: "Temperature",
value: `${data.temperature} / ${plantDBData.temperature}`,
inline: true,
},
{
name: "Humidity",
value: `${data.humidity} / ${plantDBData.humidity}`,
inline: true,
},
{
name: "Moisture",
value: `${data.moisture} / ${plantDBData.moisture}`,
inline: true,
},
{
name: "Light",
value: `${data.light} / ${plantDBData.light}`,
inline: true,
},
],
image: {
url: "https://www.thespruce.com/thmb/_6OfTexQcyd-3aW8Z1O2y78sc-Q=/2048x1545/filters:fill(auto,1)/snake-plant-care-overview-1902772-04-d3990a1d0e1d4202a824e929abb12fc1-349b52d646f04f31962707a703b94298.jpeg",
},
timestamp: new Date(),
},
],
};
}
return postData;
}

async function getPlantDBData(rackId) {
const Plants = new mongoose.model(process.env.COLLECTION_NAME);
let plant = await Plants.findOne({ rackId }, { _id: false, __v: false });
return plant;
}

exports.sendNotification = sendNotification;
35 changes: 24 additions & 11 deletions plantService.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,15 @@ const plantSchema = new mongoose.Schema({
required: true,
},
temperature: {
type: Number,
type: String,
required: true,
},
humidity: {
type: Number,
type: String,
required: true,
},
water: {
type: Number,
moisture: {
type: String,
required: true,
},
light: {
Expand All @@ -51,13 +51,12 @@ async function listPlants() {

async function getPlantInfo(rackId) {
try {
let plant = await Plants.findOne(
{ rackId },
{ _id: false, __v: false }
);
let plant = await getPlantDBData(rackId);
if (!plant) throw new Error(`No plant exists in rack ${rackId}`);
console.log("Plant info retrieved successfully!");
return plant;
const data = JSON.stringify({ action: "PLANT_INFO", rackId });
writeToSerialPort(data);
console.log("Retrieving plant info!");
return "Hang tight! I am getting you the plant info :)";
} catch (error) {
console.log("Error while retrieving plant", error);
throw error;
Expand All @@ -69,6 +68,8 @@ async function setPlant(plantDetails) {
let plant = new Plants(plantDetails);
plant = await plant.save();
console.log("Plant set successfully!");
const data = JSON.stringify({ action: "SET_PLANT", ...plantDetails });
writeToSerialPort(data);
return plant;
} catch (error) {
console.log("Error while setting plant", error);
Expand All @@ -85,6 +86,8 @@ async function editPlant(plantDetails) {
plantDetails.updateProperty
);
if (!result) throw new Error(`No plant exists in rack ${rackId}`);
const data = JSON.stringify({ action: "EDIT_PLANT", ...plantDetails });
writeToSerialPort(data);
console.log("Plant updated successfully!");
return "Plant updated successfully!";
} catch (error) {
Expand Down Expand Up @@ -112,14 +115,24 @@ function waterPlant(rackId) {
}

function lightPlant(rackId, state) {
const data = JSON.stringify({ action: "LIGHT", state, rackId });
const data = JSON.stringify({
action: "LIGHT",
state,
rackId,
});
writeToSerialPort(data);
}

async function getPlantDBData(rackId) {
let plant = await Plants.findOne({ rackId }, { _id: false, __v: false });
return plant;
}

exports.listPlants = listPlants;
exports.getPlantInfo = getPlantInfo;
exports.setPlant = setPlant;
exports.editPlant = editPlant;
exports.removePlant = removePlant;
exports.waterPlant = waterPlant;
exports.lightPlant = lightPlant;
exports.getPlantDBData = getPlantDBData;
Binary file added public/images/twitter-banner.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
3 changes: 3 additions & 0 deletions server.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const express = require("express");
const path = require("path");
const {
listPlants,
getPlantInfo,
Expand All @@ -14,6 +15,8 @@ const port = process.env.PORT || 3000;
const app = express();
app.use(express.json());

app.use(express.static(path.join(__dirname, "public")));

app.get("/", (req, res) => {
res.send("Smart Garden Server up & running :)");
});
Expand Down

0 comments on commit 532122d

Please sign in to comment.