I am using a D1 mini (clone) in STA mode, connected to my home wifi. I have successfully got an Android APP connected through the wifi network to the D1 and can send HEX commands based on button presses.
I have previously used Blynk but when I found this App https://play.google.com/store/apps/details?id=udpcontroller.nomal&hl=en I thought I'd give it a go.
All works well and I am able to 'get' the incoming data byte by byte in the main 'Loop' adding them together to give the unique 'button command' then calling a new function when all bytes are received (buffer empty).
void loop() {
WiFiClient client = server.available();
while (client.connected()) {
while (client.available()) {
byte thischar = client.read();
allchars = ++ thischar;
Serial.print(thischar, HEX);
}
if (allchars != 0) {
newcommand();
}
allchars = 0;
}
}
and then use a case statement in the new command function to carry out actions depending on the button pressed.
void newcommand() {
Serial.println("-----");
Serial.print(allchars);
Serial.println("-----");
switch (allchars) {
case 19 : newloop(); break;
case 20 : break;
case 21 : break;
}
}
My question is this..
When I call the newloop() function I want that function to run until new data is received,
I tried
void newloop() {
while (1) {
if (client.available()) return;
}
}
But this gives client undeclared, which makes sense as it is previously declared inside the main loop and not globally.
However I have tried numerous combinations, adding declarations in various places, including in newloop(), I have also tried declaring a new client1 and disconnecting and reconnecting the TCP (Application), but no matter what I try I can't seem to come up with a way to 'break out' of the loop when I try to send a new command from the Android app.
For context I am trying to control an RGB strip and can use the App to change colour, turn them on and off etc. But the idea was to call a function which would run a fade or flashing/transition pattern until the next command is received fro the App.
Please note the code above is just sample code I put together by way of an explanation and not from the actual RGB.
Thanks in advance and apologies if I have posted in the wrong place or if the links/code snippets are not formatted as expected.