Page 1 of 1

TCP between ESP32 and Megunolink

Posted: Sun Dec 19, 2021 11:43 am
by Tenere700
Dear All,
My name is Tom and I work with Arduino and ESP32 for data acquisition.
Normally I use a USB communication, but now I am trying to use wifi...
I use Megunolink to plot data from my ESP32 (M5Stack).
I am here plotting using a TCP connection.
Here below the code that I use

Code: Select all

#include "WiFi.h"
#include "MegunoLink.h"
#include <M5Core2.h>

const char *SSID = "XXX";
const char *WiFiPassword = "YYY";


const char *Host = "192.168.1.20";
const unsigned Port = 11000;

WiFiClient Client;
float Timestamps[1];
float Measurements[1];

void ConnectToWiFi()
{
 
  WiFi.mode(WIFI_STA);
  WiFi.begin(SSID, WiFiPassword);
  Serial.print("Connecting to "); Serial.println(SSID);
 
  uint8_t i = 0;
  while (WiFi.status() != WL_CONNECTED)
  {
    Serial.print('.');
    delay(500);
 
    if ((++i % 16) == 0)
    {
      Serial.println(F(" still trying to connect"));
    }
  }
 
  Serial.print(F("Connected. My IP address is: "));
  Serial.println(WiFi.localIP());
}

WiFiClient *GetClient()
{
  if (!Client.connected() && Client.connect(Host, Port))
  {
    return &Client;
  }

  return Client.connected() ? &Client : NULL;
}


void setup() 
{
  Serial.begin(9600);

  ConnectToWiFi();

  //WiFiClient *pClient = GetClient();
  //TimePlot Plot("", *pClient);
}

void loop() 
{
for(int i = 0; i < 1; ++i)
{
  analogReadResolution(12);
  Timestamps[i] = millis();
  float Value = analogRead(36);
  //float filteredval = Value;
  Measurements[i] = Value;
  delay(1);
}  
 
WiFiClient *pClient = GetClient();

  if (pClient != NULL)
  {
    
    XYPlot MyPlot("", *pClient);
    //TimePlot Plot("", *pClient);
    MyPlot.SendData("Analog Measurements", Timestamps, Measurements, 1, 2);
    //Plot.SendData("ADC Value", Value);
  }
  else
  {
    Serial.println("TCP Connection failed");
  }
}
Everything goes well, but it seems that the transmission requires about 10ms.
I would like to transmit data at 1kHz.
Is this a limit of my network or there is something in my code that can be optimized?
Thank you for your help!

Tom