I understand this is a Teensy forum so to be clear these will be connected to a Teensy 4.1 through serial and ONLY use the ESP32-C3 for the WIFI.
The basic project at this time is to have a clock (7” display run by a Teensy 4.1) that shows time, date, temperatures, and turns on and off some lights. The temperatures and light control will be for 2 different buildings 100’ and 200’ away and will send the readings to the display in the house. I have WIFI router and extenders already in place.
Currently I have the ESP on bread boards and they are powered by the USB port
The Problems…. I don’t understand the WIFI send and receive process.
With minimal code and the WIFI library installed it takes up 73% of the program space and 10% of dynamic memory.
Some of the code I got from the internet and modified it somewhat.
Client Code
I have tried server.write and server.print but it wont compile… also not sure what to do on the client to receive the data.
Any help would be appreciated… DJETH

The basic project at this time is to have a clock (7” display run by a Teensy 4.1) that shows time, date, temperatures, and turns on and off some lights. The temperatures and light control will be for 2 different buildings 100’ and 200’ away and will send the readings to the display in the house. I have WIFI router and extenders already in place.
Currently I have the ESP on bread boards and they are powered by the USB port
The Problems…. I don’t understand the WIFI send and receive process.
- Main problem - I can get the client to send to the server but I CANNOT get the server to send a message to the client.
- Secondary - The data sent from the client and received at server seems very SLOW at times. The client sends two different codes to the server. The server turns on and off an LED depending on the code received. I am running a millis timer that tracks the LED on to off and off to on times… so basically time from the end of one packet to the end of the next packet. Sometimes it runs below 50ms but other times around 200ms – 400ms. When it runs slow the times are very consistent like there is part of the WIFI driver waiting for something to clear or respond… buffer… ack…
- I cycle the power on the ESP and it might run below 50ms till the next time I turn it off. Then turn it back on later and it will run slow. I did try two different wireless networks and no change in the pattern. I tried another pair of ESP32’s and the same. Not sure if is the client or the server causing the slowdowns
With minimal code and the WIFI library installed it takes up 73% of the program space and 10% of dynamic memory.
Some of the code I got from the internet and modified it somewhat.
Code:
///Server Code
/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/communication-between-two-esp32
*/
// ESP32 #2: TCP SERVER + AN LED
#include <WiFi.h>
#define LED_PIN 5 // ESP32 pin GPIO15 connected to LED Was 5 for C3 - GPIO21 for C6
#define SERVER_PORT 4080
const char* ssid = "XXXXXXXXXXXX"; // CHANGE TO YOUR WIFI SSID
const char* password = "XXXXXXXXXX"; // CHANGE TO YOUR WIFI PASSWORD
IPAddress local_IP(192, 168, 1, 35);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
//WiFiServer server(80);
WiFiServer TCPserver(SERVER_PORT);
int SwitchCase = 0;
int PacketCount = -1;
uint32_t MillisTimer1 = 0;
uint32_t MillisTimer2 = 0;
uint32_t MillisToChangeLED = 0;
void setup() {
Serial.begin(115200);
pinMode(LED_PIN, OUTPUT);
Serial.println("ESP32 #2: TCP SERVER + AN LED");
WiFi.config(local_IP, gateway, subnet);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// Print your local IP address:
Serial.print("ESP32 #2: TCP Server IP address: ");
Serial.println(WiFi.localIP());
Serial.println("ESP32 #2: -> Please update the serverAddress in ESP32 #1 code");
// Start listening for a TCP client (from ESP32 #1)
TCPserver.begin();
}
void loop() {
switch (SwitchCase) {
case 0: //Basically Case 0 only runs once... then goes to case 1
{
WiFiClient client = TCPserver.accept();
if (client) {
// Read the command from the TCP client:
String command = client.readStringUntil('~');
PacketCount = command.toInt();
Serial.print(PacketCount);
Serial.print(" ");
Serial.print(command);
Serial.print(" ");
String command1 = client.readStringUntil('\n');
// Serial.print("ESP32 #2: - Received command: ");
Serial.print(command1);
TCPserver.
if (command1 == "12345678901234567890") {
digitalWrite(LED_PIN, HIGH); // Turn LED on
MillisTimer1 = millis();
} else if (command1 == "98765432109876543210") {
digitalWrite(LED_PIN, LOW); // Turn LED off
MillisTimer2 = millis();
} // end else if
}
client.stop();
SwitchCase = 1;
PacketCount++;
break;
}
case 1: // Runs continously in the void loop
{
WiFiClient client = TCPserver.accept();
if (client) {
// Read the command from the TCP client:
String command = client.readStringUntil('~');
Serial.print(PacketCount);
Serial.print(" ");
Serial.print(command);
Serial.print(" ");
String command1 = client.readStringUntil('\n');
// Serial.print("ESP32 #2: - Received command: ");
Serial.print(command1);
if (command1 == "12345678901234567890") {
digitalWrite(LED_PIN, HIGH); // Turn LED on
//Timer to measure LED Off to On or On to Off
MillisToChangeLED = millis() - MillisTimer2;
Serial.print(" ");
Serial.print(MillisToChangeLED);
Serial.println(" msec");
MillisTimer1 = millis();
} else if (command1 == "98765432109876543210") {
digitalWrite(LED_PIN, LOW); // Turn LED off
//Timer to measure LED Off to On or On to Off
MillisToChangeLED = millis() - MillisTimer1;
Serial.print(" ");
Serial.print(MillisToChangeLED);
Serial.println(" msec");
MillisTimer2 = millis();
} // end else if
client.stop();
PacketCount++;
}
break;
} // End Switch
/*
// I CANT GET THIS TO WORK..................... send message to client from server
WiFiClient client = TCPserver.accept();
if (client) {
if (client.connected()) {
client.println("Hello\n");
delay(10);
}
}
client.stop();
*/
} // End Switch case
} // End Void Loop
Client Code
Code:
//Client Code
/*
* This ESP32 code is created by esp32io.com
*
* This ESP32 code is released in the public domain
*
* For more detail (instruction and wiring diagram), visit https://esp32io.com/tutorials/communication-between-two-esp32
*/
// ESP32: TCP CLIENT + A BUTTON/SWITCH
#include <WiFi.h>
const char* ssid = "XXXXXXXXXX"; // CHANGE TO YOUR WIFI SSID
const char* password = "XXXXXXXX"; // CHANGE TO YOUR WIFI PASSWORD
const char* serverAddress = "192.168.1.35"; // CHANGE TO ESP32#2'S IP ADDRESS
const int serverPort = 4080;
IPAddress local_IP(192, 168, 1, 36);
IPAddress gateway(192, 168, 1, 1);
IPAddress subnet(255, 255, 255, 0);
WiFiClient TCPclient;
int PacketCount = 1;
int PacketstoSendBeforeNextBreak = 0;
int PacketsBeforeBreak = 30;
int TimeBeforePacketSendResumes_Millis = 1000;
uint32_t PacketSendResumeTimer = 0;
void setup() {
Serial.begin(115200);
Serial.println("ESP32: TCP CLIENT + A BUTTON/SWITCH");
WiFi.config(local_IP, gateway, subnet);
// Connect to Wi-Fi
WiFi.begin(ssid, password);
while (WiFi.status() != WL_CONNECTED) {
delay(1000);
Serial.println("Connecting to WiFi...");
}
Serial.println("Connected to WiFi");
// connect to TCP server (Arduino #2)
if (TCPclient.connect(serverAddress, serverPort)) {
Serial.println("Connected to TCP server");
} else {
Serial.println("Failed to connect to TCP server");
} // End else
PacketSendResumeTimer = millis() + TimeBeforePacketSendResumes_Millis;
} // End Void Setup
void loop() {
// The two if's below send X number of packets then waits Y time befoer sending the x number of packes again
if(PacketSendResumeTimer <= millis()) {
if(PacketCount > PacketstoSendBeforeNextBreak) {
PacketstoSendBeforeNextBreak = PacketCount + PacketsBeforeBreak;
PacketSendResumeTimer = millis() + TimeBeforePacketSendResumes_Millis;
} else { // Sends data to server to turn ON LED - also keeps wu wit packet count
TCPclient.connect(serverAddress, serverPort);
TCPclient.print(PacketCount);
TCPclient.write('~');
TCPclient.write("12345678901234567890\n");
TCPclient.clear();
TCPclient.stop();
Serial.print(PacketCount);
Serial.println(" 12345"); // shortened to free up the serial sooner
PacketCount++;
// Sends data to server to turn OFF LED - also keeps wu wit packet count
TCPclient.connect(serverAddress, serverPort);
TCPclient.print(PacketCount);
TCPclient.write('~');
TCPclient.write("98765432109876543210\n");
TCPclient.clear();
TCPclient.stop();
Serial.print(PacketCount);
Serial.println(" 98765"); // shortened to free up the serial sooner
PacketCount++;
}// End else
} // End if(PacketSendResumeTimer <= millis())
/* I CANT GET THIS TO WORK..................... recieve message from server
// get message form TCP server:
TCPserver.begin();
WiFiClient client = TCPserver.accept();
// WiFiServer server = client.accept();
if (client) {
// Read the command from the TCP client:
String command3 = client.readStringUntil('\n');
Serial.print(command3);
} // End if (client)
*/
} // End void loop
I have tried server.write and server.print but it wont compile… also not sure what to do on the client to receive the data.
Any help would be appreciated… DJETH


