How can I receive sensor data from Arduino/ESP32 to MATLAB?

조회 수: 34 (최근 30일)
Andhika Nagami
Andhika Nagami 2020년 7월 13일
편집: Andrei 2025년 11월 5일
I'm doing a final project for my school using two sensor that connected to ESP32 as a microcontroller and access point. I want to send the sensor reading from ESP32 through TCP/IP Protocol and receive it in MATLAB. The problem is I don't know how, like I don't even have the code that I've been working on, and I can't find any reference that help me through this problem. When I click run on my matlab code, the ESP32 detected a client, but the readings don't show up. I'm using a randomSeed as the sensor reading for now. Anyone can help me?
This is the Arduino code:
#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>
const char *ssid = "ESP32 AP";
const char *password = "";
WiFiServer server(80);
//float emg1 = 34
//float emg2 = 35
unsigned long to=0;
void setup() {
Serial.begin(115200);
Serial.println();
Serial.println("Configuring access point...");
// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid);
Serial.print("AP IP address: ");
Serial.println(WiFi.softAPIP());
server.begin();
Serial.println("Server started");
randomSeed(analogRead(34));
}
void loop() {
WiFiClient client = server.available();
if (client) {
Serial.println("New Client!");
//String currentLine = "";
while (client.connected()) {
if (client.available()) {
//char c=client.read();
//if(c != 'stop'){
//while(micros()>to+1000){
//to=micros();
int val1 = random(0,50);
int val2 = random(51,100);
Serial.print("val1 = ");
Serial.print(val1);
client.print(val1);
Serial.print(" val2 = ");
Serial.println(val2);
//}
//}
}
}
}
}
This is the MATLAB code:
s=tcpip('192.168.4.1',80,'NetworkRole','client')
fopen(s);
data=fread(s);
  댓글 수: 1
franco pizzi
franco pizzi 2020년 9월 27일
Hello, I have the same problem and I couldn't solve it. Did you sort out the problem?

댓글을 달려면 로그인하십시오.

답변 (3개)

Laura Gautschi
Laura Gautschi 2021년 1월 18일
Are you working with thingspeak? I guess the best solution is to create a channel on thingspeak.com and then send the data to the channel. You can do this by implicating your channelnumber and Number of the Field where you want the data to appear. Let me know if you are intrested in the full programm?

Krisada Sangpetchsong
Krisada Sangpetchsong 2022년 2월 25일
You can try Waijung 2 for ESP32. This allows you to connect ESP32 with Simulink and run in External Mode via Wifi, making it possible to tune Simulink model parameters and monitor signal in real-time.
See this video for the concept https://youtu.be/apSs1dOlFF0

Andrei
Andrei 2025년 11월 5일
편집: Andrei 2025년 11월 5일
In case someone finds this old question:
Your device code is sending ASCII encoded numeric values, but it's not sending an end-of-line character, whereas your MATLAB code is trying to read binary data.
A possible solution would be:
  • Add client.print('\n'); to your device code after client.print(val1);
  • In MATLAB, I would suggest using this code (customize per your device's IP address and port number)
t = tcpclient("<address>",<port>);
textData = readline(t);
data = str2double(textData);

카테고리

Help CenterFile Exchange에서 MATLAB Support Package for Arduino Hardware에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by