Real time sending data from argon to matlab

Hello to everyone. Where is the best method to send in real time sensors value from argon to matlab?

@m.romeo12, @ScruffR made a comment on this in another of your posts. What exactly do you define as “real time”. Is data to be sent every 100 milliseconds, every second, every minute? And how much data? The ability to properly describe your problem demonstrates that you have a clear understanding of your problem.

3 Likes

I can tell you the easiest way will be using Thingspeak, which can directly import your data to MATLAB. There are two ways to do that, one with a library on your device talking directly to Thingspeak servers, and the other using a web hook to let the Particle cloud do the intermediate transfer. Using the Particle cloud is very simple here, just using a publish on the device.

As @peekay123 and others have asked, you need to define what “real time” means to you in this case.

3 Likes

+1 on the ThingSpeak recommendation.
I’d also add MQTT to the available methods for ThingSpeak.

Prior to Particle moving to Data Operations, I tested all 3 methods.
Publish/Webhook used less cellular data than MQTT or the ThingSpeak Library (for me), and is extremely flexible.
However, I don’t know which would be best now with the “new” Data Operations metric.

All 3 methods are easy to implement.
Someone may shed some light on which would be the most beneficial in terms of Data Operations.

I was able to use thingspeak both with the library and with the webhook to also view the graphs of my variables

In matlab in matlab i used this function bu I need to see every second, the sensors valu, in matlab, second for second.

uhttps://it.mathworks.com/help/thingspeak/thingspeakread.html

Typically, You need to purchase the Standard License ($660/year) in order to update a channel every second. I believe the other license types are limited to updates every 15 seconds per channel.

2 Likes

@Rftop is correct! Here is the page for Thingspeak licensing:

2 Likes

how did you use mqtt in matlab?

So where is a FREE method to update my values on MATLAB every second?

You can follow their Particle Tutorial here:

As previously mentioned, the free ThingSpeak license is 1 update per 15 seconds.

1 Like

Hi,
I don’t have a clue about matlab but…
I guess that you can use matlab functionality to achieve your goal namely webread()

all of my particle dev are offline right now so I can’t really test this but is working good for open iota chrysalis node API but should work with Particle API as well

please note that I just modify an official example mathworks.com
Results from matlab webread for chrysalis:

results with formatted Url for particle (it’s gonna errored out of course in this example :slight_smile: )

and here potential code for your Argon for testing:

#include "Particle.h"

SYSTEM_THREAD(ENABLED);


int loop_interval = 1000;
float pressure_hPa = 0.0;
float temperature_c= 0.0;
float altitude_ft = 0.0;
float altitude_m = 0.0;
float MQ2 = 0.0;
float humidity = 0.0;
unsigned long interval = 0;

float X1 = 17.874;
float X2 = 49.314;
float X3 = 22.459;
float X4 = 12.345;
float X5 = 67.891;
float X6 = 2.987;

char status_template[] = "{\"MQ2\":%.02f,\"humidity\":%.02f,\"pressure_hPa\":%.02f,\"temperature_c\":%.02f,\"altitude_ft\":%.02f,\"altitude_m\":%.02f}";
char msg[sizeof(status_template) + 32];

void setup(){

    Particle.variable("status", msg); 
    
    
    pressure_hPa = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X1);
    temperature_c = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X2);
    altitude_ft = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X3);
    altitude_m = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X4);
    MQ2 = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X5);
    humidity = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X6);
   
    
    snprintf(msg, sizeof(msg), status_template, MQ2, humidity, pressure_hPa, temperature_c, altitude_ft, altitude_m);  
}

void loop(){

 if (millis() - interval > loop_interval) {
     interval = millis();
         
    pressure_hPa = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X1);
    temperature_c = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X2);
    altitude_ft = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X3);
    altitude_m = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X4);
    MQ2 = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X5);
    humidity = static_cast <float> (rand() ) / static_cast <float> (RAND_MAX/X6);
    
    
    snprintf(msg, sizeof(msg), status_template, MQ2, humidity, pressure_hPa, temperature_c, altitude_ft, altitude_m);
     
 }
 

}

The Particle variable name in this example is “status”
I hope this will help somehow

Best

UPDATE
Is working like a harm :grin::+1:

1 Like

thanks a lot!!
Have you ever use TCP protocol??

with the particle

What exactly do you mean ?
What you are trying to achieve ?
More details easier to help :slight_smile:
By the way I was able to get all of the rand() data (from previous example code) every 1s. and plot over 3D area it's preaty funky ! and now I know that Matlab is a monster !!! what is really nice that after you collect some data over time (in this example is set to 240 semples every 1s.) you can analyse the entire data after stop the script !

here is a Matlab script:

x = 1;
sensors = [1, 1, 1, 1, 1, 1];

httpsUrl = "https://api.particle.io/v1/devices/YOUR_DEV_ID/status?access_token=YOUR_ACCESS_TOKEN";
D = parallel.pool.DataQueue;
afterEach(D, @all);

while true
res =  webread(httpsUrl);
data = jsondecode(res.result);
sensors(x,1) = data.MQ2;
sensors(x,2) = data.pressure_hPa;
sensors(x,3) = data.altitude_ft;
sensors(x,4) = data.altitude_m;
sensors(x,5) = data.humidity;
sensors(x,6) = data.temperature_c;
send(D, sensors);

if x == 240
    x = x - 1; 
    sensors(1,:) = [];
end
x = x+1;
pause(1);
end
function all(d)
    figure(7);
    %plot(d);
    %area(d,'DisplayName','sensors')
    %hold on;
    surf(d);
    title('random data')
    ylabel('time[240s]')
    xlabel('MQ2,T,H,P,Alti_m,Alti_ft')
    zlabel('Amplitudes')
    disp(d);
end

You can play arround mark surface unmark plot etc.
here some resultss :

I know that professional Matlab users gonna kill me one day but this is my 3th day with Matlab so after a little learning will be better :stuck_out_tongue_winking_eye:

I hope that help
Best,
Arek

I have this problem with your code… why?

So I have to send my sensors values from argon to Matlab, one value for second. It’s for my thesis

I don’t know it’s working out of the box even after just editing example in mathworks website.

could you mark all plots,area,etc and also data = jsondecode(res.result);
and all sensors and just use display(res) after initial webread
like this:

httpsUrl = "https://api.particle.io/v1/devices/YOUR_DEV_ID/status?access_token=YOUR_ACCESS_TOKEN";

while true
res =  webread(httpsUrl);
display(res)
pause(1);
end

and share results here

thanks a lot, now i’ts working <3

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.