0% found this document useful (0 votes)
10 views

IOT & Applications Assisgnment-2

Uploaded by

bsaroja.ella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

IOT & Applications Assisgnment-2

Uploaded by

bsaroja.ella
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

UGC-sponsored Two-Week Online Refresher Course on

“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;

Output: The modified msg object is passed to the next node.


Null
Returning null stops the message from being sent and effectively halts the flow.

return null;
Example:
if (msg. payload < 10) {

return null;//No output


}
return msg;
Array of Messages (Multiple Outputs)
To send messages to different outputs, return an array where each element
corresponds to an output.
return [msg1,msg2, ...];
Example
if (msg. payload > 50)
{
return [msg, null]; // Send to the first output
}

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"};

let msg2={payload: "Message2"};


return [[msg1,msg2],null]; // Two messages to the first output, none to the
second.
Dynamic Output Generation
You can dynamically create the messages and outputs based on logic.
 Example
let outputs = [];
for (let i=0; i < 3; i++){
outputs. Push ({payload: `Message${i}` });
}
return outputs;
Object with Custom Properties
While uncommon, you can return an object containing additional data.
return {msg: msg, custom Data: "Some data" };
Using node. Send() for Asynchronous Messages
Instead of returning directly, you can use node. Send() for emitting messages
asynchronously.
 Example:
set Timeout (()=>
{
msg. payload= "Delayed message";
node. Send (msg);
},
1000);
return null; // Function node will complete immediately.

Return Type Description


msg A single message object for one output.
null Suppresses any output from the node.
[msg1,msg2] Sends different messages to multiple outputs.
Sends msg1to the first output and suppresses the second
[msg1,null]
output.
[[msg1,msg2],
Sends multiple messages to specific outputs.
[msg3]]
Sends messages asynchronously (used for delayed or
node. Send()
repeated emissions).

Each return type allows flexibility in how the Function node interacts with downstream
nodes.

You might also like