DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Experiment 2
Name:
Branch: BE-CSE Sec/Grp: 22BCS_
th
Semester: 6 Date of Performance: 23/1/25
Subject Name: Foundation of Cloud IoT Edge Subject Code: 22CSP-367
ML Lab
1. Aim: Simulate a cloud scenario using Matlab and run an algorithm for
temperature variations.
2. Objective: To simulate a cloud computing scenario using MATLAB and
implement an algorithm to monitor and analyze temperature variations.
3. Implementation/Code:
MATLAB
Define parameters such as time, base temperature, amplitude, and noise factor.
Simulate temperature variations based on time and environmental factors.
Plot the temperature variations against time using MATLAB’s plotting
functions.
Apply an algorithm to detect significant temperature spikes or calculate
average temperatures.
Display the detected spikes or other relevant data in the MATLAB console.
ThingSpeak
Create a ThingSpeak account and set up a new channel for storing temperature
data.
Obtain the Channel ID and API Write Key for your ThingSpeak channel.
Install the ThingSpeak support package in MATLAB to send data to
ThingSpeak.
Set up a loop in MATLAB to periodically send simulated
temperature data to ThingSpeak.
Monitor the live data on the ThingSpeak platform, and use its tools to analyze
and visualize the data in real-time.
Soyam(22BCS10325)
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
MATLAB Code for temperature analysis
% Parameters for Temperature Simulation
time = 0:0.1:24; % Time in hours (0 to 24, 0.1-hour intervals)
baseTemp = 20; % Base temperature in degrees Celsius
amplitude = 10; % Temperature fluctuation amplitude
noiseFactor = 2; % Random noise amplitude
% Simulate Temperature Variations
temperature = baseTemp + amplitude * sin((pi/12) * time) + noiseFactor * randn(size(time));
% Plot Simulated Temperature Variations
figure;
plot(time, temperature, 'b', 'LineWidth', 1.5);
xlabel('Time (hours)');
ylabel('Temperature (°C)');
title('Simulated Cloud Temperature Variations');
grid on;
% Spike Detection Algorithm
threshold = 5; % Change threshold for spikes
tempDiff = diff(temperature); % Calculate differences
spikeIndices = find(abs(tempDiff) > threshold); % Find spike indices
% Mark Spikes on the Plot
hold on;
plot(time(spikeIndices), temperature(spikeIndices), 'ro', 'MarkerSize', 8, 'LineWidth', 1.5);
legend('Temperature', 'Detected Spikes');
% Display Detected Spikes
disp('Detected spikes at the following times (hours) and temperatures (°C):');
disp([time(spikeIndices)', temperature(spikeIndices)']);
% ThingSpeak Parameters
channelID = 2817534; % Replace with your Channel ID
writeAPIKey = 'GPO5B51E1PJMFH86'; % Replace with your Write API Key
% Loop to Send Data to ThingSpeak
for i = 1:length(time)
% Get the current temperature value
data = temperature(i);
% Send the temperature value to ThingSpeak
response = thingSpeakWrite(channelID, data, 'WriteKey', writeAPIKey);
% Display status in the MATLAB console
fprintf('Time: %.1f hours, Temperature: %.2f°C - Sent to ThingSpeak\n', time(i), data);
% Pause to simulate real-time data upload
Soyam(22BCS10325)
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
pause(15); % Wait 5 seconds before sending the next data point end
disp('All data successfully sent to ThingSpeak.');
MATLAB Code for humidity analysis
% Parameters for Humidity Simulation
time = 0:0.1:24; % Time in hours (0 to 24, 0.1-hour intervals)
baseHumidity = 50; % Base humidity in percentage
amplitude = 20; % Humidity fluctuation amplitude
noiseFactor = 5; % Random noise amplitude
% Simulate Humidity Variations
humidity = baseHumidity + amplitude * sin((pi/12) * time) + noiseFactor * randn(size(time));
% Plot Simulated Humidity Variations
figure;
plot(time, humidity, 'g', 'LineWidth', 1.5);
xlabel('Time (hours)');
ylabel('Humidity (%)');
title('Simulated Cloud Humidity Variations');
grid on;
% Spike Detection Algorithm for Humidity
threshold = 10; % Change threshold for spikes
humidityDiff = diff(humidity); % Calculate differences
spikeIndices = find(abs(humidityDiff) > threshold); % Find spike indices
% Mark Spikes on the Plot
hold on;
plot(time(spikeIndices), humidity(spikeIndices), 'ro', 'MarkerSize', 8, 'LineWidth', 1.5);
legend('Humidity', 'Detected Spikes');
% Display Detected Spikes
disp('Detected spikes at the following times (hours) and humidity (%):');
disp([time(spikeIndices)', humidity(spikeIndices)']);
% ThingSpeak Parameters
channelID = 2817534; % Replace with your Channel ID
writeAPIKey = 'GPO5B51E1PJMFH86'; % Replace with your Write API Key
% Loop to Send Humidity Data to ThingSpeak
for i = 1:length(time)
% Get the current humidity value
data = humidity(i);
% Send the humidity value to ThingSpeak
response = thingSpeakWrite(channelID, data, 'WriteKey', writeAPIKey, 'Fields', 2); % Use Field 2 for
humidity
% Display status in the MATLAB console
fprintf('Time: %.1f hours, Humidity: %.2f%% - Sent to ThingSpeak\n', time(i), data);
% Pause to simulate real-time data upload
pause(15); % Wait 15 seconds before sending the next data point
end
disp('All humidity data successfully sent to ThingSpeak.');
Soyam(22BCS10325)
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
4. Output:
Fig(i). Temperature graph
Fig(ii). Humidity graph
Soyam(22BCS10325)
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Soyam(22BCS10325)
DEPARTMENT OF
COMPUTER SCIENCE &
ENGINEERING
Fig(iii). Temperature and Humidity graph with live data sending from matlab
Fig(iv). Average of the live data collected from matlab for Temperature and Humidity shown using widget
5. Learning Outcome
Learned to simulate temperature variations over time based on
environmental factors such as daily fluctuations and noise.
Implemented an algorithm to detect significant temperature changes, which can
be extended to monitor real-world anomalies in IoT systems.
Explored how to use ThingSpeak to aggregate, visualize, and analyze live IoT data
streams in a cloud environment.
Understand the integration of physical principles (e.g., thermodynamics and fluid
dynamics) into computational simulations.
Gained insights into the complexities of climate modeling and atmospheric studies.
Soyam(22BCS10325)