How to run simulink model in ThingSpeak?

6 vues (au cours des 30 derniers jours)
Tanusree
Tanusree le 16 Jan 2024
Modifié(e) : Hassaan le 16 Jan 2024
I have a simulink model saved on my desktop MATLAB. Now I want to run this model through ThingSpeak insted of running from desktop MATLAB. Is it possible?
Also how can I upload the simulink model on ThingSpeak cloud?

Réponse acceptée

Hassaan
Hassaan le 16 Jan 2024
Modifié(e) : Hassaan le 16 Jan 2024
ThingSpeak primarily focuses on IoT analytics, which involves aggregating, analyzing, and acting on the data from IoT devices. It can handle MATLAB code for tasks like data preprocessing, visualizations, and analytics.
However, there are a few approaches you might consider to work with Simulink and ThingSpeak together:
Run Simulink Model on Desktop and Send Data to ThingSpeak:
  • Use your desktop MATLAB to run the Simulink model.
  • Within the Simulink model, use MATLAB Function blocks or S-Function blocks to send data to ThingSpeak using the ThingSpeak Write API.
  • You can automate this process to periodically run the Simulink model and send updated data to ThingSpeak.
Simulink to MATLAB Code Conversion:
  • Convert parts of the Simulink model that can be represented as MATLAB code using the MATLAB Coder or Simulink Coder.
  • Upload the generated MATLAB code to ThingSpeak and run it as part of a ThingSpeak MATLAB Analysis app.
External Deployment:
  • Deploy the Simulink model on external hardware that can run the model in real-time (for example, using Simulink Real-Time or deploying on an Arduino or Raspberry Pi).
  • The hardware can then send the data to ThingSpeak.
Pre-Process Data with Simulink, Then Upload:
  • Use Simulink to process your data as needed, save the results as a .mat file or another data format.
  • Write a MATLAB script that uploads this data file to ThingSpeak, either directly through the ThingSpeak API or by processing the data within MATLAB and then sending it to ThingSpeak.
For uploading a Simulink model or its results to ThingSpeak, you would typically:
  1. Save the simulation results from the Simulink model to a file or workspace variable.
  2. Use MATLAB code to process these results and upload them to ThingSpeak channels using the HTTP API provided by ThingSpeak.
% Assuming you have simulation results in variables `time` and `data`
% Convert your data into a timetable or table
tData = timetable(seconds(time), data, 'VariableNames', {'Data'});
% Preprocess data if needed (e.g., aggregate, average, etc.)
% Now, upload the preprocessed data to ThingSpeak
writeAPIKey = 'YOUR_THINGSPEAK_WRITE_API_KEY'; % replace with your actual API key
channelID = 'YOUR_THINGSPEAK_CHANNEL_ID'; % replace with your actual channel ID
% ThingSpeak Write API URL
url = 'https://api.thingspeak.com/update';
% Loop through the data and upload it
for i = 1:height(tData)
% Prepare the HTTP request
dataField = sprintf('field1=%f', tData.Data(i));
timeStamp = posixtime(tData.Time(i));
timeStampField = sprintf('created_at=%s', datestr(datetime(timeStamp, 'ConvertFrom', 'posixtime'), 'yyyy-mm-ddTHH:MM:SSZ'));
% Send the HTTP POST request
webwrite(url, 'api_key', writeAPIKey, dataField, timeStampField);
pause(15); % ThingSpeak free account has an update rate limit of every 15 seconds
end
This script sends the data to a ThingSpeak channel. You would need to replace 'YOUR_THINGSPEAK_WRITE_API_KEY' and 'YOUR_THINGSPEAK_CHANNEL_ID' with your actual ThingSpeak channel's write API key and channel ID. The webwrite function is used to make an HTTP POST request to ThingSpeak's API endpoint with the data.
Note that ThingSpeak has rate limits for how often you can send data, so ensure your data upload frequency complies with those limits (as in the pause(15); line in the code).
While you cannot run a Simulink model directly on ThingSpeak, you can process and send data from a Simulink model to ThingSpeak using MATLAB code. If you need to perform real-time analytics on the data, you could use a MATLAB Analysis app on ThingSpeak to periodically analyze and visualize your data. For detailed instructions and requirements, check the ThingSpeak documentation and MATLAB support for the latest capabilities and integrations.
Reference
------------------------------------------------------------------------------------------------------------------------------------------------
If you find the solution helpful and it resolves your issue, it would be greatly appreciated if you could accept the answer. Also, leaving an upvote and a comment are also wonderful ways to provide feedback.
Professional Interests
  • Technical Services and Consulting
  • Embedded Systems | Firmware Developement | Simulations
  • Electrical and Electronics Engineering
Feel free to contact me.

Plus de réponses (1)

AMIT SURYAVANSHI
AMIT SURYAVANSHI le 16 Jan 2024

Communautés

Plus de réponses dans  ThingSpeak Community

Catégories

En savoir plus sur ThingSpeak dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by