Search code examples
matlabplotarduinomatlab-deployment

How to plot real time data from an AppDesigner application to a Matlab Web App Server?


I have an appdesigner application where the data coming from two sensors is graphed in real time through Arduino. Those sensors are: ultrasonic sensor and light sensor. The application in appdesigner reads the data through the Arduino Uno and plots the data into a UIAxes component.

Private properties of the app:

properties (Access = private)
    a;
    h;
    h2;
    h3;
    btnSt = 0;     
    initialTime = 2;
end

Code to configure the Arduino when the app is executed:

function startupFcn(app)
        movegui(app.UIFigure, "center")

        app.a = arduino;
        app.a.configurePin('A0', 'AnalogInput');
        app.a.configurePin('A1', 'AnalogInput');
end

Code that read and plot the data in the UIAxes when a button is pressed:

function StartPushed(app, event)
        cla(app.UIAxes, "reset");
        cla(app.UIAxes2, "reset");
        
        app.h = animatedline(app.UIAxes, "Color", "Black");
        
        app.h2 = animatedline(app.UIAxes2, "Color", "Black");
        
        start = 1;
        
        % stops the app when the stop button is pressed
        while app.btnSt == 0
            
            fotoRes = readVoltage(app.a, 'A0');
            
            distance = readVoltage(app.a, 'A1');
            
            distCm = (3027.4/distance)^1.2134;
            
            tp = app.initialTime - start; 
            
            % plot the data of the light sensor
            addpoints(app.h, tp, fotoRes);
            
            % plot the data of the ultrasonic sensor
            addpoints(app.h2, tp, distCm);
            
            drawnow
            
            app.initialTime = app.initialTime + 1;
        end
    end

So in conclusion, is there a way to do the same but from a Matlab web application server? Offcourse, with the condition of take the data from real time. Any help is appreciated.


Solution

  • What you need to make is a web service, but im not really sure if Matlab could be used to code a Web Service, however Matlab can read data from a web service using an http request... let me explain...

    Here is what i what your local setup looks like:

    Sensor->(reads voltages)->Arduino A0 Pin ->(usb serial communication)->Your local computer with Matlab

    and that works totally fine, but when you host your matlab app lives in a web server elsewhere the flow of the data would be as follows:

    Sensor->Arduino-[something to connect the arduino to the internet]->Web Service->WebApp

    So there are two additional steps to consider:

    1. How are you going to connect the arduino to the internet? there could be two possible solutions: Connecting directly your arduino to the internet using a esp8266 and make the arduino directly make the HTTP POST to the web service, OR connecting the arduino to your computer and then using your computer to make the HTTP POST.
    2. How are you going to make the web service? in this context the web service is the thing that recieves the POSTS with your arduino's info, and then your WebApp could request the info by an HTTP request, (here is some documentation on how to read data from a WebService using Matlab). Maybe it's is possible to write some code on matlab to make your app directly recieve the HTTP POSTS and process them which would look something like this:

    Sensor->Arduino-[something to connect the arduino to the internet]->[Web Service living within your app->WebApp]

    but i have not found any info on how to do that... the alternative would be to use an existing service like Arduino Cloud IoT or aREST my recomended architecture would look like this:

    Sensor->Arduino with esp8266-> Arduino IoT API ->WebApp

    Hope this helps, and goodluck!