Wifi, Sending out a string to a client...

Hi All, any suggestions would be appreciated…
I have a small problem with the ESP32 wifi coding, using arduino… The design goal is to first use a esp32 server to connect to a pc, then, after bugs in design have been worked out connect esp32 to esp32… Now using BlueTooth clasic is simple and small code, BUT there is no pairing between ESP32’s… BTE would work, but the coding is pages and pages long, just to send a “Hello World”… So we are looking at wifi … We would like to send a STRING of text BACK and FORTH between a ESP32 and PC, later to 2 ESP32’s… If you look at my Program it WORKS great, but only in the main loop not in the sub… I have put //**** in the spot to focus on… To keep it simple for this post, I am using Strings instead of Char Array, and the “Protocol” we don’t care if we have to hack it to work with the sub…

// * This is the Server that brodcasts "DonsandDarcys" to the internet.
//
// * Will work Connecting to google on PC if DonsandDarcys is the network selected...
// Access point = 192.168.4.1 ---- port = 1234 ---- No password....
// using ScriptCom 5.10
// 1) Make sure pc network selected is DonsandDarcys..
// 2) Use ScriptCom 5.10.. Select: Type = "TCP client", destination port = 1234, destination ip = 192.168.4.1,
// 3) current interface = socket....

// This "Version" uses a new loop coppyed from the BTclassic to read data in using less code...

#include <WiFi.h>
#include <WiFiClient.h>
#include <WiFiAP.h>

// Set these to your desired credentials.
const char *ssid = "DonsandDarcys";
const char *password = ""; // No Password
WiFiServer server(1234);

int LEDB = 2; // GPIO2 onboard

char inputBuffer[50]; // fifty character input buffer to store data of input
int bufPos = 0; // current position in buffer
String returnstring;


void setup()
{

Serial.begin(115200);
delay(5000); // time for me to open monitor..

pinMode(LEDB, OUTPUT);

//WIFI
Serial.println();
Serial.println("Configuring access point...");

// You can remove the password parameter if you want the AP to be open.
WiFi.softAP(ssid, password);
IPAddress myIP = WiFi.softAPIP();
Serial.print("AP IP address: ");
Serial.println(myIP);

Serial.println("Port: 1234");

server.begin();

Serial.println("Server started");
Serial.println("");
digitalWrite(LEDB, HIGH);
}

void loop()
{
WiFiClient client = server.available(); // listen for incoming clients

if (client)
{ // if you get a client,
Serial.println(""); // print a message out the serial port
Serial.println("New Client.");

while (client.connected()) // loop while the client's connected
{
bufPos = 0; // current position in buffer
memset(inputBuffer, 0, sizeof(inputBuffer)); // clear buffer

while(client.available())
{
inputBuffer[bufPos++] = client.read(); //Read next character from BlueTooth
}

if (bufPos > 0)
{
returnstring = (char*)inputBuffer;
Serial.println(returnstring);
memset(inputBuffer, 0, sizeof(inputBuffer)); // clear buffer
bufPos = 0;

//**************************************************************************
client.println(returnstring); // This works great, I tested it in a loop changing the "send string" each
// time , 50 times with no missed packets unlike UDP...
//--------------------------------------
DoStuff(); // This does not work,, and I want it too....
//**************************************************************************
}
}
// tried the sub here also.. not sending from sub...
}
}


void DoStuff()
{
client.println("This Does Not"); // Does not send this... And I need it to still print in this sub...
}

//----------------------------------------------------------------------

In your loop function you declare

void loop
{

  • WiFiClient client = server.available(); // create a WiFiClient object and name it client*
    }

This means you create an instance of WiFiClient and call it client.

Because the declaration is inside the {} of the loop function, you cannot use it in the subroutine.

void DoStuff()
{

  • client.println("This Does Not"); // client is not know here*
    }

Simple fix is to move the declaration outside loop and therefore make it global, meaning all functions in your ino file can use it.

A more common method is to pass the reference of "client" to the subfunction

void loop()
{
   ...
   ...
          DoStuff(client);
}

//no const because client may cache data before sending.
void DoStuff(WifiClient &client)
{
   client.print();

}

Well,, Thank you for the response, We will start looking in that area... The problem is that the web is full of one way communication.. (turning on a led) but not BOTH ways (full duplex).. Sometimes one gets frustrated and needs a nudge in the right direction...

The problem is that the web is full of one way communication.. (turning on a led)

in those examples after turning on the led, there tends to be a confirmation to the browser showing "LED is ON" or something.
That these examples generally have one node function as a client and the other as a server is true, but it is possible to have an ESP function as both at the same time without issue. The client makes a request, (therefore initiates the communication, just like a browser does) the server simply responds to the request, have a look at ESPwebserver examples.