sending data from raspberry pi (python) to matlab or simulink over TCP/IP
Show older comments
Hello, I am trying to send random-data from raspberry pi (python script) to Matlab or simulink. The objective is to monitor the real time data from raspberry pi in simulink or matlab.
1) I have used !ping in matlab to connect raspberry pi
2) I wanted to use TCP/IP connection but i can't receive the data this is my code in raspberry pi :
import socket
import numpy as np
import encodings
from time import sleep
import random
#import json
from random import uniform
import datetime
connflag = True
HOST = '192.168.0.128' # Standard loopback interface address (localhost)
PORT = 46384 # Port to listen on (non-privileged ports are > 1023)
def my_server():
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
print("Server Started waiting for client to connect ")
s.bind((HOST, PORT))
s.listen(5)
conn, addr = s.accept()
with conn:
print('Connected by', addr)
while True:
sleep(5)
if connflag == True:
timestamp = datetime.datetime.now()
tempreading = uniform(20.0,25.0) # Generating Temperature Readings
message = '{"timestamp":'+'"'+str(timestamp)+'",'+'"temperature":'+str(tempreading)+'}'
print("msg sent: temperature " + "%.2f" % tempreading ) # Print sent temperature msg on console
try:
s.sendall(message.encode())
except BrokenPipeError:
print("Error: Connection to the remote end has been lost") # Handle the error as needed
else:
print("waiting for connection...")
if __name__ == '__main__':
while 1:
my_server()
3) For matlab to receive the data, i used this code but it seems that it doesn't work well.
% Define the host and port information
HOST = '192.168.0.128';
PORT = 46384;
% Define the process_data_from_server function
function [x_temperature, y_humidity] = process_data_from_server(data)
parts = strsplit(data, ",");
x_temperature = parts{1};
y_humidity = parts{2};
end
% Define the my_client function
function my_client()
t = timer('TimerFcn', 'my_client', 'Period', 11, 'ExecutionMode', 'fixedRate');
start(t);
% Create a TCP socket
s = tcpip(HOST, PORT, 'NetworkRole', 'Client');
% Open the socket connection
fopen(s);
% Send the input command to the server
my = input("Enter command ");
fwrite(s, my, 'char');
% Receive the data from the server
data = fread(s, s.BytesAvailable, 'char');
data = char(data');
% Process the data and print the results
[x_temperature, y_humidity] = process_data_from_server(data);
fprintf("Temperature %s\n", x_temperature);
fprintf("Humidity %s\n", y_humidity);
% Close the socket connection
fclose(s);
stop(t);
delete(t);
pause(5);
end
% Call the my_client function in a loop
while 1
my_client();
end
I think there is a problem with matlab code because i'm not good in matlab i don't know how to change it to be able to get the data from raspberry pi.
Thank you for your help ;)
Accepted Answer
More Answers (0)
Categories
Find more on MATLAB Support Package for Raspberry Pi Hardware in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!