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

Don Warr
Posts: 11
Joined: Fri Oct 19, 2018 10:01 am

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

Postby Don Warr » Wed Nov 06, 2019 2:28 pm

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...
}

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

mikemoy
Posts: 627
Joined: Fri Jan 12, 2018 9:10 pm

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

Postby mikemoy » Sun Nov 10, 2019 5:27 pm

It does not work because you declared
WiFiClient client = server.available(); // listen for incoming clients
inside loop()

If you want it to work the way you suggested your going to make "WiFiClient client" a global variable. Or pass it to the function your calling.

Who is online

Users browsing this forum: No registered users and 61 guests