IOT & Applications Assisgnment-2
IOT & Applications Assisgnment-2
“IOT Applications”
(09-12-2024to23-12-2024)
ASSIGNMENT-2
1Q: Design a Node-RED flow to alert you when you are not exercising and
the weather is good?
Answer: Here's a step-by-step design for a Node-RED flow that alerts you
when you're not exercising, and the weather is good. The flow uses a weather
API and exercise tracker API (like Fitbit or similar) to make decisions:
1. Weather API Integration
Use anode like http request or a custom weather node to fetch weather
data from a service like OpenWeatherMap.
Extract relevant data such as temperature, weather condition (e.g.,
clear or cloudy), and precipitation.
2. Exercise Data Integration
Use an API or web hook provided by your fitness tracker (e.g., Fit bit,
Google Fit, or Apple Health) to retrieve your exercise data.
Alternatively, use manual input if no API is available.
3. Define Good Weather Conditions
Use a switch node to set thresholds for good weather:
Temperature range (e.g.,15°C to30°C).
No rain or minimal precipitation.
Clear skies or light clouds.
4. Determine Exercise Status
Use the fitness tracker data to check if you've exercised today:
Time spent exercising.
Steps walked.
Calories burned.
5. Create an Alert
If the weather is good and you haven’t exercised, send an alert using:
Email node.
Notification node (e.g., push notification to your phone).
Debug node for testing purposes.
Example Node-RED Flow Overview
[Weather Data API]---> [Check Weather Conditions]
|
[Exercise Data API]--->[Check Exercise Status]
|
[Send Alert]
Node-RED Flow in Detail
Nodes Required:
1. Inject Node: Triggers the flow at regular intervals(e.g.,every1 hour).
2. HTTP Request Node: Fetches weather data from a weather API.
3. Switch Node: Filters weather conditions based on predefined good-
weather criteria.
4. HTTP Request/API Node: Fetches exercise data.
5. Switch Node: Checks if you've exercised.
6. Notification Node: Sends the alert.
Example JSON Flow
[
{
"id": "1",
"type": "inject",
"name": "Start Flow",
"props":[{"p": "payload"}],
"repeat": "3600",
"topic": "",
"payload": "",
"payload Type": "date",
"x": 150,
"y":100,
"wires": [["2"]]
},
{
"id": "2",
"type": "http request",
"name": "Fetch Weather Data",
"method": "GET",
"url":
"http://api.openweathermap.org/data/2.5/weather?q=YourCity&appid=YourAPI
Key",
"x": 350,
"y": 100,
"wires": [["3"]]
},
{
"id": "3",
"type": "switch",
"name": "Check Weather Conditions",
"property": "payload. Weather [0].main",
"rules":[{"t":"eq","v":"Clear","vt":"str"},{"t":"eq","v":"Clouds",
"vt":"str"}],
"x":550,
"y":100,
"wires": [["4"],["4"]]
},
{
"id":"4",
"type": "http request",
"name": "Fetch Exercise Data",
"method": "GET",
"url":"https://api.fitbit.com/1/user/-/activities/date/today.Json",
"x": 750,
"y": 100,
"wires": [["5"]]
},
{
"id": "5",
"type": "switch",
"name": "Check Exercise Status",
"property":"payload. summary. Steps",
"rules":[{"t": "lt", "v":"5000","vt":"num"}],
"x": 950,
"y":100,
"wires": [["6"]]
},
{
"id":"6",
"type": "notification",
"name": "Send Alert",
"message": "Good weather! Time to exercise.",
"x": 1150,
"y":100,
"wires": []
}]
2Q: What are the different returning messages in Function nodes?
Answer: In Node-RED, the Function node allows you to write custom
JavaScript logic, and the messages it can return depend on the structure of your
code. Here's an overview of the different types of return values:
Single Message Object
A Function node can return a single msg object to be sent to the next node.
return msg;
Example:
msg. payload="Hello, Node-RED!";
return msg;
return null;
Example:
if (msg. payload < 10) {
else
{
return [null, msg]; // Send to the second output
}
Output:
msg1 goes to the first output.
msg2 (or null) goes to the second output.
Null in Arrays
To suppress a message on a specific output, use null in the corresponding
position.
return [msg, null]; // Sends msg only to the first output
Array of Arrays (Multiple Messages per Output)
To send multiple messages to the same output, return an array of arrays.
Each sub-array corresponds to a single output and can contain multiple
messages.
return [[msg1,msg2],[msg3, msg4]];
Example:
let msg1={payload: "Message1"};
Each return type allows flexibility in how the Function node interacts with downstream
nodes.