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

110 views (last 30 days)
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 Comment
franco pizzi
franco pizzi on 27 Sep 2020
Hello, I have the same problem and I couldn't solve it. Did you sort out the problem?

Sign in to comment.

Answers (2)

Laura Gautschi
Laura Gautschi on 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 on 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

Community Treasure Hunt

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

Start Hunting!