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

34 Ansichten (letzte 30 Tage)
Andhika Nagami
Andhika Nagami am 13 Jul. 2020
Bearbeitet: Andrei am 5 Nov. 2025
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 Kommentar
franco pizzi
franco pizzi am 27 Sep. 2020
Hello, I have the same problem and I couldn't solve it. Did you sort out the problem?

Melden Sie sich an, um zu kommentieren.

Antworten (3)

Laura Gautschi
Laura Gautschi am 18 Jan. 2021
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 am 25 Feb. 2022
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 am 5 Nov. 2025
Bearbeitet: Andrei am 5 Nov. 2025
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);

Kategorien

Mehr zu MATLAB Support Package for Arduino Hardware finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by