-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
160 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters