Sending Data from Photon to MATLAB using TCPClient/Server to communicate

Hello!
I am very new to the Particle community and have a seemingly easy question about using TCP to communicate between my Photon and MATLAB. I was hoping someone could help me figure out what I’m doing that’s making both codes “run”, but MATLAB just sits there waiting for something without actually reading any data from the Photon. Please help this newby :slight_smile:

This is the MATLAB code:

clear all
close all
clc

%% Connection to the photon via TCP/IP connection
t = tcpclient('**PHTN MAC ADRS',80);
disp('Waiting for connection')
fopen(t); 
disp('Connection OK!');

%% Reading information from the Photon
bit = 12; res = 2^bit; V = 3.3; %Volts
ltime = 60*.5; %test 30 sec time limit in seconds
itime = datestr(clock); %start time in military time - for reference
data_count = ltime*1000; %reading data every 1ms

%softpot sensor data
L_active = 50.00; L_part = 95.86; %mm
SP_start = (L_part-L_active)/2; %start of active length in mm
SP_end = SP_start+L_active; %end of active length in mm
m_conv = (L_active/res); %slope of mm/bits

A=zeros(data_count,1); ii=1;
while ii <= data_count
    bit_value = read(t);
    [excess, str_trunc] = strtok(bit_value,'=');
    A(ii) = str2double(str_trunc(2:end))*m_conv;
    disp(A(ii))
    ii=ii+1;
end

clear(t);

This is the code on the Particle Photon

TCPServer server = TCPServer(80);
TCPClient client;
int potenSensor = A0; //potentiometer connected to pin analog pin A0
int sensorLevel = 0; //for storing the sensor values
int blinkLED = D7; //make sure the code is running

void setup() {
  Wifi.on();
  WiFi.setCredentials("**************", "************");
  WiFi.connect();
  Serial.begin(9600);
  server.begin();

  pinMode(potenSensor, INPUT); //input for the potentiometer
}

void loop() {
  // listen for connection with computer 
  if (client) {
    Serial.println("Connected to Computer");
    boolean currentLineIsBlank = true;
    while (client.connected()) {
        if (client.available()) {
            //actually measuring pistoning effect
            sensorLevel = analogRead(potenSensor);
            client.print("SensorValue = ");
            client.print(sensorLevel);
            client.println();
            delay(1); //delay data reading by 1 ms
        }
    }
    delay(100);
    Serial.println("client disconnected");
  }
}

You may want to have another look at the example provided by the docs about TCPServer.

You never set your client variable to actually become the object your server wants to communicate with.

BTW, your outer if (client) is superfluous since it will most likely never be false since your TCPClient client definition already creates a client object.

Thanks for the fast response! So did you mean change to something like this:

void loop() {
  // listen for connection with computer 
  if (client.connect(**computer IP address**,**port number**)) {
    Serial.println("Connected to Computer");
    while (client.available()) {
        //actually measuring pistoning effect
        sensorLevel = analogRead(potenSensor);
        client.print("SensorValue = ");
        client.print(sensorLevel);
        client.println();
        delay(1); //delay data reading by 1 ms
    }
    delay(100);
    Serial.println("client disconnected");
  } else {
      client = server.available();
  }
}

You don’t actively connect the client. The client (i.e. MATHLAB) will connect to your server and hence the server will hold the connection in its available field.

So I rather meant something like this

void loop() {
  if (client.connected()) {
    Serial.println("Client connected");
    server.printlnf("SensorValue=%d", analogRead(potenSensor));
  }
  else {
    Serial.println("No client connected - try again");
    client = server.available();
  }
}

Since your client isn’t really sending data to the server thre is no need for client.read() but otherwise this is pretty much the same as the sample code above shows about how it should be done.

3 Likes

Hello again,

I’m now having issues sending the data in the format that I want… I’m trying to plot the change in mm while a magnet is moving up and down the potentiometer, but I’m getting really strange results. Am I reading the data incorrectly from MATLAB or is my particle code still incorrect?

MATLAB

clear all
close all
clc

%% Connection to the photon via TCP/IP connection
photonIP = '192.************';
t = tcpip(photonIP, 23);

%% Reading information from the Photon
%photon data
bit = 12; res = 2^bit; V = 3.3; %Volts
ltime = 1; %test 1 sec time limit in seconds
itime = datestr(clock); %start time in military time - for reference
data_count = ltime*1000; %reading data every 1ms

%softpot sensor data
L_active = 50.00; L_part = 95.86; %mm
SP_start = (L_part-L_active)/2; %start of active length in mm
SP_end = SP_start+L_active; %end of active length in mm
m_conv = (L_active/res); %slope of mm/bits

A=zeros(data_count,1); ii=1;
while ii <= data_count
    fopen(t);
    bit_value = fscanf(t);
    fclose(t);
    [excess, str_trunc] = strtok(bit_value,'=');
    A(ii) = str2double(str_trunc(2:end))*m_conv; %reading mm change from center
    disp(A(ii))
    ii=ii+1;
end

Photon

uint8_t ip[] = {192********};
TCPServer server = TCPServer(23);
TCPClient client;
int potenSensor = A0; //potentiometer connected to pin analog pin A0
int sensorLevel = 0; //for storing the sensor values
int blinkLED = D7; //make sure the code is running

void setup() {
  // start listening for clients
  Serial.begin(9600);
  delay(1000);
  server.begin();
  delay(3000);

  pinMode(potenSensor, INPUT); //input for the potentiometer
  pinMode(blinkLED, OUTPUT); //for the board LED

}

void loop() {
  // listen for connection with computer 
   client.connect(ip, 23);
   delay(1000);

      while (server.available()) {
            sensorLevel = analogRead(potenSensor);
            server.print("SensorValue = ");
            server.print(sensorLevel);
            server.println();
            delay(5); //delay data reading by 5 ms
      }
}

If you have the Photon print out the data it's going to sending out and you check that there and then, you should be able to find out whether or not the Photon is doing it right.
If it's not, you need to understand what (expectation vs. reality) is wrong in order to lean why and find where the error gets introduced.
Once the transmitter side is known to work, you'd move on to the receiving end.
Print out in MATLAB and check whether or not the data received is correct.
Learn what is wrong, then why, then where.

Gradually homing in on the source of error is a fundamental debugging approach.

BTW, your Photon loop() looks very little like the code I posted above.

1 Like