Repeating a portion of a loop until other directions are given?

Hey everyone!
I'm currently working on a project controlling a relay via wifi. The first portion I was able to get to work just fine, but decided to add an additional feature. Essentially, when i type the address of my esp8266 and the appropriate command, it will turn a relay on or off as wanted. But I want to add the feature of typing "IPaddress/random" and it randomly turning on and off different relays until given further instruction.

As the code is currently written, it begins to do this, but only for a second. I'm assuming it has to do with finishing the loop without receiving any further command from the web browser? Any help is greatly appreciated! I'll only post relevant portions of the code to save space and searching.

int vol1 = -1;
int vol2 = -1;
int vol3 = -1;
int vol4 = -1;
int randomInt = 0;

if (req.indexOf("/relay1/0") != -1)
vol1 = 0;
else if (req.indexOf("/relay1/1") != -1)
vol1 = 1;

//Set GPIOS according to the req.
if (vol1 >= 0)
digitalWrite(relay1, vol1);

client.flush();

if (req.indexOf("/relay2/0") != -1)
vol2 = 0;
else if (req.indexOf("/relay2/1") != -1)
vol2 = 1;

if (vol2 >= 0)
digitalWrite(relay2, vol2);

client.flush();

if (req.indexOf("/relay3/0") != -1)
vol3 = 0;
else if (req.indexOf("/relay3/1") != -1)
vol3 = 1;

if (vol3 >= 0)
digitalWrite(relay3, vol3);

client.flush();

if (req.indexOf("/relay4/0") != -1)
vol4 = 0;
else if (req.indexOf("/relay4/1") != -1)
vol4 = 1;

if (vol4 >= 0)
digitalWrite(relay4, vol4);

client.flush();

if (req.indexOf("/random") != -1)
Serial.begin(9600);
randomInt = random(1, 4);
delay(random(100, 500));

if (randomInt == 1) {
digitalWrite(relay1, 0);}
else {
digitalWrite(relay1, 1); }

if (randomInt == 2) {
digitalWrite(relay2, 0);}
else {
digitalWrite(relay2, 1); }

if (randomInt == 3) {
digitalWrite(relay3, 0);}
else {
digitalWrite(relay3, 1); }

if (randomInt == 4) {
digitalWrite(relay4, 0);}
else {
digitalWrite(relay4, 1); }

madscientist_jm:
Hey everyone!
I’m currently working on a project controlling a relay via wifi. The first portion I was able to get to work just fine, but decided to add an additional feature. Essentially, when i type the address of my esp8266 and the appropriate command, it will turn a relay on or off as wanted. But I want to add the feature of typing “IPaddress/random” and it randomly turning on and off different relays until given further instruction.

I’m not quite sure that I understand what you’re asking, but if you want a loop to run until you tell it otherwise, maybe this will do it for you:

int ctrl; // loop control

ctrl = 1; // enable loop

while (ctrl)
{
    // do something
    ctrl = // read some external information
}

The loop will run as long as “ctrl” is non-zero (i.e. “true”). Setting “ctrl” to zero stops the loop.

Reading “something external” is reading data from the 8266 or parsing a character in a string received by the 8266.

Make sense?

And, apologies if I’m off in left field… I’m not sure I get what you’re asking for.

Oh, and lastly it’s a lot easier to read code if you use the “</>” (code tags) feature (upper left corner of the editor window).

Instead of this:

if (randomInt == 4) {
digitalWrite(relay4, 0);}
else {
digitalWrite(relay4, 1); }

You get this:

if (randomInt == 4) { 
    digitalWrite(relay4, 0);
} else {
    digitalWrite(relay4, 1);
}

Do you know what client.flush() does? If not, you really need to learn. If you, you really need to explain why you call it more than once. Frankly, you need to explain why you call it at all. It is rarely a good idea to throw away random amounts of unread data, when the data COULD be useful.

Thanks for the feedback. Krupski, I’m not entirely sure we’re thinking the same thing. Essentially, each relay will be connected to a light. I want the option of making the lights flash randomly between intervals of .1-.5 seconds. Currently I can type “IP/relay1/0” or “IP/relay1/1” (and so on) into the browser to turn each individual light on or off, but the “IP/random” command will not work. the way I’m hoping it would.

PaulS, honestly, I don’t really know. I am obviously pretty new to all of this and I simply modified a code I found to make serve my purposes. I will have to do more reading on client.flush(). Thank you.

Here is the whole code, in case it helps at all.

#include <ESP8266WiFi.h>
#include <ESP8266mDNS.h>

//WiFi
const char wifissid[] = "G72ML";
const char wifipsk[] = "RD4JXYCCNJG9HM75";

//LED Definitions
const int relay1 = 13;
const int relay2 = 12; 
const int relay3 = 14;
const int relay4 = 16;//The one onboard

WiFiServer server(80);


void setup() {
// put your setup code here, to run once:
initHardWare();
connectWiFi();
server.begin();
setupMDNS();
}

void loop() {
// Check if connected
WiFiClient client = server.available();
if (!client) {
  return;
}

//Read the first line of the req.
String req = client.readStringUntil('\r');
Serial.println(req);
client.flush();

//Match the request
int vol1 = -1;
int vol2 = -1;
int vol3 = -1;
int vol4 = -1;


if (req.indexOf("/relay1/0") != -1)
   vol1 = 0;
else if (req.indexOf("/relay1/1") != -1)
   vol1 = 1;

//Set GPIOS according to the req.
if (vol1 >= 0)
   digitalWrite(relay1, vol1);

 client.flush();

 if (req.indexOf("/relay2/0") != -1)
   vol2 = 0;
else if (req.indexOf("/relay2/1") != -1)
   vol2 = 1;


//Set GPIOS according to the req.
if (vol2 >= 0)
   digitalWrite(relay2, vol2);

 client.flush();

 if (req.indexOf("/relay3/0") != -1)
   vol3 = 0;
else if (req.indexOf("/relay3/1") != -1)
   vol3 = 1;


//Set GPIOS according to the req.
if (vol3 >= 0)
   digitalWrite(relay3, vol3);

 client.flush();

    if (req.indexOf("/relay4/0") != -1)
   vol4 = 0;
else if (req.indexOf("/relay4/1") != -1)
   vol4 = 1;


//Set GPIOS according to the req.
if (vol4 >= 0)
   digitalWrite(relay4, vol4);

 client.flush();

if (req.indexOf("/random") != -1)
  Serial.begin(9600);
  randomInt = random(1, 4);
   delay(random(100, 500));
    
if (randomInt == 1) { 
  digitalWrite(relay1, 0);}
  else {
   digitalWrite(relay1, 1); }
   
if (randomInt == 2) { 
 digitalWrite(relay2, 0);}
  else {
   digitalWrite(relay2, 1); }
   
if (randomInt == 3) { 
  digitalWrite(relay3, 0);}
  else {
   digitalWrite(relay3, 1); }
   
if (randomInt == 4) { 
  digitalWrite(relay4, 0);}
  else {
   digitalWrite(relay4, 1); }
  
 //Prepare html response

 String s = "HTTP/1.1 200 OK\r\n";
 s += "Content Type: text/html\r\n\r\n";
 s += "<!DOCTYPE HTML>\r\n<html>\r\n";

 //Print a message
 if (vol1 >= 0) {
  s += "Relay1 is now ";
  s += (vol1)?"OFF":"ON";
 }
 else if (vol2 >= 0) {
  s += "Relay2 is now ";
  s += (vol2)?"OFF":"ON";
 }
 else if (vol3 >= 0) {
  s += "Relay3 is now ";
  s += (vol3)?"OFF":"ON";
 }
 else if (vol4 >= 0) {
  s += "Relay4 is now ";
  s += (vol4)?"OFF":"ON";
 }

 else {
  s += "Invalid Request. 
 Try /led/1, /led/0 or /read";
 }
 s += "</html>\n";

 //send response to the client
 client.print(s);
 delay(1);
 Serial.println("Client disconnected");

}

void connectWiFi() {
byte ledStatus = LOW;
Serial.println();
Serial.println("Connecting to: " + String(wifissid));
WiFi.mode(WIFI_STA);

WiFi.begin(wifissid,wifipsk);

while (WiFi.status() != WL_CONNECTED) {
  //Blink the LED
  digitalWrite(relay1, ledStatus);
  ledStatus = (ledStatus == LOW) ? LOW : HIGH;

  delay(100);
}
Serial.println("WiFi Connected");
Serial.println("IP address: ");
Serial.println(WiFi.localIP());
digitalWrite(16, LOW);

}

void setupMDNS()
{
//Call mdns to setup mdns to pint to domain.local
if (!MDNS.begin("thing"))
{
  Serial.println("Error setting up MDNS responder!");
  while(1) {
    delay(1000);
  }
}
Serial.println("mDNS responder started");
}

void initHardWare()
{
Serial.begin(115200);
pinMode(relay1, OUTPUT);
pinMode(relay2, OUTPUT);
pinMode(relay3, OUTPUT);
pinMode(relay4, OUTPUT);
digitalWrite(relay1, HIGH);
digitalWrite(relay2, HIGH);
digitalWrite(relay3, HIGH);
digitalWrite(relay4, HIGH);
}

If you break a program into several single purpose functions it makes it much easier to manage when different pieces work.

Have a look at Planning and Implementing a Program

...R

but the "IP/random" command will not work. the way I'm hoping it would.

It appears to be setting all of the relay pins to one state, except for one randomly selected from 1, 2, or 3. The value 4 is never returned for the range of values you supplied.

What your hopes are is not at all clear.