why my project can't work? I'm using arduino and sim800l, the matlab gui can't send output to my phone, is there something wrong with my code?
2 次查看(过去 30 天)
显示 更早的评论
function varargout = Program_Gui_Project(varargin)
% PROGRAM_GUI_PROJECT MATLAB code for Program_Gui_Project.fig
% PROGRAM_GUI_PROJECT, by itself, creates a new PROGRAM_GUI_PROJECT or raises the existing
% singleton*.
%
% H = PROGRAM_GUI_PROJECT returns the handle to a new PROGRAM_GUI_PROJECT or the handle to
% the existing singleton*.
%
% PROGRAM_GUI_PROJECT('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PROGRAM_GUI_PROJECT.M with the given input arguments.
%
% PROGRAM_GUI_PROJECT('Property','Value',...) creates a new PROGRAM_GUI_PROJECT or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before Program_Gui_Project_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to Program_Gui_Project_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help Program_Gui_Project
% Last Modified by GUIDE v2.5 18-Oct-2022 11:00:27
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @Program_Gui_Project_OpeningFcn, ...
'gui_OutputFcn', @Program_Gui_Project_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
% End initialization code - DO NOT EDIT
% --- Executes just before Program_Gui_Project is made visible.
function Program_Gui_Project_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to Program_Gui_Project (see VARARGIN)
% Choose default command line output for Program_Gui_Project
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
movegui(hObject,'center');
% UIWAIT makes Program_Gui_Project wait for user response (see UIRESUME)
% uiwait(handles.figure1);
clear all;
global a;
a = arduino('COM4','Uno');
configurePin(a,'D7','DigitalOutput');
configurePin(a,'D8','DigitalOutput');
% --- Outputs from this function are returned to the command line.
function varargout = Program_Gui_Project_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in pushbutton1.
function pushbutton1_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% memanggil menu "browse file"
[nama_file, nama_folder] = uigetfile('*.jpg');
%jika ada nama file yang dipilih maka akan mengeksekusi perintah di bawah
%ini
if ~isequal(nama_file,0)
% membaca file citra rgb
Img = imread(fullfile(nama_folder,nama_file));
% menampilkan citra rgb pada axes
axes(handles.axes1)
imshow(Img)
title('Citra RGB')
%menampilkan nama file pada edit text
set(handles.edit1,'String',nama_file)
%menyimpan variabel Img pada lokasi handles agar dapat dipanggil oleh
%pushbutton yang lain
handles.Img = Img;
guidata(hObject, handles)
else
% jika tidak ada nama file yang dipilih maka akan kembali
return
end
% --- Executes on button press in pushbutton2.
function pushbutton2_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% memanggil variabel Img yg ada dilokasi handles
Img = handles.Img;
% melakukan konversi citra rgb menjadi citra grayscale
Img_gray = rgb2gray(Img);
%figure, imshow(Img_gray)
% melakukan konversi citra grayscale menjadi citra biner
bw = imbinarize(Img_gray);
% figure, imshow(bw)
% melakukan operasi komplemen
bw = imcomplement(bw);
% figure, imshow(bw)
% melakukan operasi morfologi filling holes
bw = imfill(bw,'holes');
%menampilkan citra biner pada axes
axes(handles.axes2)
imshow(bw)
title('Citra Biner')
%menyimpan variabel bw pada lokasi handles agar dapat dipanggil oleh
%pushbutton yang lain
handles.bw = bw;
guidata(hObject, handles)
% --- Executes on button press in pushbutton3.
function pushbutton3_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton3 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% memanggil variabel Img dan bw yang ada di lokasi handles
Img = handles.Img;
bw = handles.bw;
% ekstraksi ciri
% melakukan konversi citra rgb menjadi citra hsv
HSV = rgb2hsv(Img);
% figure, imshow(HSV)
% mengekstrak komponen h, s, dan v pada citra hsv
H = HSV(:,:,1); %hue
S = HSV(:,:,2); % Saturation
V = HSV(:,:,3); % Value
% menambah nilai pixel background menjadi nol
H(~bw) = 0;
S(~bw) = 0;
% menghitung nilai rat2 h,s, danv
Hue = sum(sum(H))/sum(sum(bw));
Saturation = sum(sum(S))/sum(sum(bw));
Value = sum(sum(V))/sum(sum(bw));
% menghitung luas objek
Luas = sum(sum(bw));
% mengisi variabel ciri_latih dengan ciri hasil ekstraksi
ciri_uji(1,1) = Hue;
ciri_uji(1,2) = Saturation;
ciri_uji(1,3) = Value;
ciri_uji(1,4) = Luas;
% menampilkan ciri hasil ekstraksi pada tabel
ciri_tabel = cell(4,2);
ciri_tabel{1,1} = 'Hue';
ciri_tabel{2,1} = 'Saturation';
ciri_tabel{3,1} = 'Value';
ciri_tabel{4,1} = 'Luas';
ciri_tabel{1,2} = num2str(Hue);
ciri_tabel{2,2} = num2str(Saturation);
ciri_tabel{3,2} = num2str(Value);
ciri_tabel{4,2} = num2str(Luas);
set(handles.uitable1,'Data',ciri_tabel,'RowName',1:4)
%menyimpan variabel ciri_uji pada lokasi handles agar dapat dipanggil oleh
%pushbutton yang lain
handles.ciri_uji = ciri_uji;
guidata(hObject, handles)
% --- Executes on button press in pushbutton4.
function pushbutton4_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton4 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% memanggil ciri_uji yang ada di lokasi handles
ciri_uji = handles.ciri_uji;
% memanggil model naive bayes hasil pelatihan
load Mdl
% membaca kelas keluaran hasil pengujian
hasil_uji = predict(Mdl,ciri_uji);
% menampilkan kelas keluaran pengujian
set(handles.edit2,'String',hasil_uji{1})
function edit1_Callback(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit1 as text
% str2double(get(hObject,'String')) returns contents of edit1 as a double
% --- Executes during object creation, after setting all properties.
function edit1_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit1 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
function edit2_Callback(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of edit2 as text
% str2double(get(hObject,'String')) returns contents of edit2 as a double
% --- Executes during object creation, after setting all properties.
function edit2_CreateFcn(hObject, eventdata, handles)
% hObject handle to edit2 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
% --- Executes on button press in pushbutton5.
function pushbutton5_Callback(hObject, eventdata, handles)
% hObject handle to pushbutton5 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% mereset tampilan GUI
set(handles.edit1,'String',[])
set(handles.edit2,'String',[])
axes(handles.axes1)
cla reset
set(gca,'XTick',[])
set(gca,'YTick',[])
axes(handles.axes2)
cla reset
set(gca,'XTick',[])
set(gca,'YTick',[])
set(handles.uitable1,'Data',[],'RowName',{'' '' ''})
% --- Executes on button press in pushbutton6.
function pushbutton6_Callback(hObject, eventdata, handles)
global a;
writeDigitalPin(a, 'D7', 1);
writeDigitalPin(a, 'D8', 1);
% hObject handle to pushbutton6 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --- Executes on button press in pushbutton7.
function pushbutton7_Callback(hObject, eventdata, handles)
global a;
writeDigitalPin(a, 'D7', 0);
writeDigitalPin(a, 'D8', 0);
% hObject handle to pushbutton7 (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
0 个评论
采纳的回答
Walter Roberson
2022-10-24
clear all;
That command is like asking to blow up the bridge you are standing on. You should almost never "clear all" inside a program.
matlab gui can't send output to my phone
I do not see any commands to communicate with your phone.
It is not possible to deploy a GUI to your phone, so you cannot be talking about the GUI as a whole ?
14 个评论
Walter Roberson
2022-11-2
I want the GUI from Matlab to be able to communicate with sim800l so that the results from sim800l can be sent in the form of sms text
In order to do that, you would have to figure out the device address of the sim800l on the arduino, and you would have to have the MATLAB host use appropriate commands, probably using https://www.mathworks.com/help/supportpkg/arduinoio/ref/matlabshared.serial.device.html device() and https://www.mathworks.com/help/supportpkg/arduinoio/ref/matlabshared.serial.device.write.html write() .
In that configuration, you would not run a sketch on the arduino: you would use the default device monitor installed by MATLAB, and your MATLAB code would tell the arduino what to do -- "tell the arduino to tell the sim800l to output AT+CMGF=1 " kind of thing.
A non-trivial difficulty with that kind of configuration is that you have very weak control over the delays, so on configurations where delays are significant, the communications might not work properly. And there is no way, in such a configuration, to say "tell the arduino to tell the sim800l to output AT+CMGF=1 and then wait 1 microsecond and then output AT+CMGS=\"08*********\" " -- you can only send one command at a time.
It can be done, except for the problem of getting the timing right. It just is not what would be recommended in such a situation.
What would be recommended in this kind of situation would be to run a sketch fairly similar to what you have now, but have the sketch listen for input from the MATLAB host -- possibly one that contains the text to be transmitted. Then have the arduino sketch talk to the sim800l and then have the arduino send the host an "Okay, did that" kind of response. In this kind of situation you would probably open the arduino with a serial() call rather than an arduino() call.
更多回答(1 个)
farhan
2022-11-4
14 个评论
Walter Roberson
2022-11-10
fprintf(s,'%s', tx);
delay(1000);
fprintf(s,'%s', tx1);
delay(1000);
function delay(milliseconds)
pause(milliseconds/1000);
end
社区
更多回答在 ThingSpeak Community
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Debugging and Analysis 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!