Temperature Data Log
Temperature Data Log
The table above displays live temperature data recorded by our Wireless Agriculture Temperature Monitoring System. This log is automatically updated and stored in Google Sheets, providing real-time and past temperature records for analysis.
Explanation of the Table:
Date/Time Column: Shows the exact timestamp of each temperature reading in MM/DD/YYYY HH:MM:SS format.
Temperature Column (°C): Displays the measured temperature values recorded by the sensor.
The graph above displays real-time temperature data collected from our 433MHz Wireless Agriculture Temperature Monitoring System, designed for smart farming applications and visually generated from ThinkSpeak. This system enables remote temperature monitoring to help farmers track environmental conditions and make data-driven decisions.
Graph Explanation:
The X-axis (Time) shows the date and time of recorded temperature readings.
The Y-axis (Temperature °C) represents the measured temperature in degrees Celsius.
MATLAB Code: Real-Time Temperature Monitoring, Plotting, and Cloud Upload via Arduino Serial Data
%Matlab code :
% Define serial port and baud rate
serialPort = "COM3"; % Change this to your Arduino's port (COM3)
baudRate = 9600;
s = serialport(serialPort, baudRate);
% Set up real-time plotting
figure;
hold on;
grid on;
title('Real-Time Temperature Plot');
xlabel('Time (s)');
ylabel('Temperature (°C)');
% Initialize time and temperature data
timeData = [0]; % Start the time at 0 seconds
tempData = [20]; % Start the temperature at 20°C
% Initial plot with starting temperature
h = plot(timeData, tempData, '-o', 'LineWidth', 1.5);
% Set axis limits to show the range from 15°C to 40°C
axis([0 10 15 40]); % X axis from 0 to 10 seconds, Y axis from 15°C to 40°C
% Start timer
tic;
% ThingSpeak Channel ID and API Key
channelID = 2860803; % Your ThingSpeak Channel ID
writeAPIKey = 'ZG7WFY92KVB3CTCH'; % Your actual Write API Key
fieldID1 = 1; % Field number for temperature (Field 1)
% Google Sheets Web App URL (replace with your Web App URL)
googleSheetURL = 'https://script.google.com/macros/s/AKfycbxi5YlIfR0naZDa1iM-8WEfEkEvL2_0145iGpzPKLNRpnRBsoNL3TWnw_LpCpkFS3bpsQ/exec';
% Variable to control when to upload to ThingSpeak
lastUploadTime = 0; % Start the timer at 0 seconds
while true
try
% Read incoming serial data
rawData = readline(s);
% Clean up the raw data to remove unwanted text and special characters
rawData = strtrim(rawData); % Trim whitespace/newlines
numericData = regexprep(rawData, '[^\d\.\-]', ''); % Remove non-numeric characters (including "Data Sent: " and "←")
% Convert to numeric
temp = str2double(numericData);
% Only plot if the data is a valid number
if ~isnan(temp)
% Append time and temperature data
timeData = [timeData, toc]; % Track elapsed time
tempData = [tempData, temp]; % Append new temp value
% Display the current temperature
disp(['Temperature: ', num2str(temp), ' °C']);
% Update the plot with new data
set(h, 'XData', timeData, 'YData', tempData);
% Dynamically update y-axis limits based on the data range
ylim([15, 40]); % Fixed Y-axis range from 15°C to 40°C
% Update the plot
drawnow;
% Get the current date
currentDate = datestr(now, 'yyyy-mm-dd'); % Get the current date
% Send the data to Google Sheets via the Web App
params = ['?temp=' num2str(temp)];
url = [googleSheetURL params];
try
webread(url); % Send data to Google Sheets Web App
disp(['Temperature uploaded to Google Sheets: ', num2str(temp), ' °C']);
catch uploadError
disp(['Error uploading to Google Sheets: ', uploadError.message]);
end
% Upload the temperature data to ThingSpeak only if 15 seconds have passed
if toc - lastUploadTime >= 15 % Upload data every 15 seconds
try
% Upload temperature to Field 1
thingSpeakWrite(channelID, temp, 'WriteKey', writeAPIKey, 'Field', fieldID1); % Upload temperature to Field 1
disp(['Temperature uploaded to ThingSpeak: ', num2str(temp), ' °C']);
lastUploadTime = toc; % Update last upload time
catch uploadError
disp(['Error uploading to ThingSpeak: ', uploadError.message]);
end
end
else
disp('Invalid data received, skipping update.');
end
% Pause to avoid overwhelming MATLAB with too many updates
pause(0.1); % Adjust based on data refresh rate
catch
disp('An error occurred, stopping the serial communication.');
break;
end
end
% Close serial connection when finished
clear s; % Close the serial port
This MATLAB code reads real-time temperature data from an Arduino via serial communication, plots the data live on a graph, and uploads it to both ThingSpeak and Google Sheets for cloud storage and visualization. It initializes a serial port connection, continuously reads and cleans incoming temperature data, and dynamically updates a temperature vs. time plot. Every 15 seconds, it sends the latest temperature reading to a ThingSpeak channel, while also uploading each reading to a Google Sheets web app.
Cloud-Based Data Logging of Real-Time Temperature Measurements Using Google Sheets Web App
function doGet(e) {
var temp = e.parameter.temp; // Retrieve the 'temp' parameter sent by MATLAB
var date = e.parameter.date; // Retrieve the 'date' parameter
var time = e.parameter.time; // Retrieve the 'time' parameter
// Combine date and time in the format you want (e.g., 'yyyy-mm-dd HH:MM:SS')
var dateTime = date + ' ' + time;
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName("Sheet1");
var lastRow = sheet.getLastRow() + 1;
// Set the dateTime, temp, and time in the appropriate columns
sheet.getRange(lastRow, 1).setValue(dateTime); // Add combined date and time
sheet.getRange(lastRow, 2).setValue(temp); // Add temperature
// Format the first column (Date/Time) explicitly
var dateTimeCell = sheet.getRange(lastRow, 1);
dateTimeCell.setNumberFormat('yyyy-mm-dd HH:mm:ss'); // Set date-time format
return ContentService.createTextOutput("Success");
}
This Google Apps Script receives temperature data, date, and time from MATLAB via a GET request and logs it into a Google Sheet. It combines the date and time into a single timestamp, inserts it in the first column, stores the temperature in the second column, and formats the timestamp in a standard yyyy-mm-dd HH:mm:ss format. This allows seamless cloud-based logging of real-time sensor data from an Arduino into Google Sheets .