0

So I am running my project on a NodeMCU, but I don't know how to let it add a yield in front of an else if. It will give me an error like this:

Arduino:1.8.12 (Windows 10), 開發板:"NodeMCU 1.0 (ESP-12E Module), 80 MHz, Flash, Legacy (new can return nullptr), All SSL ciphers (most compatible), 4MB (FS:2MB OTA:~1019KB), 2, v2 Lower Memory, Disabled, None, Sketch + WiFi Settings, 115200"

G:\資優班\科展\程式碼\Front_End\Main_Code_V5\Main_Server\Main_Server.ino: In function 'void loop()':

Main_Server:84:5: error: 'else' without a previous 'if'

exit status 1 'else' without a previous 'if'

This report would have more information with "Show verbose output during compilation" option enabled in File -> Preferences.

Here's the code:

#include <SPI.h>
#include <ESP8266WiFi.h>

#define MAX_SRV_CLIENTS 2
#define led LED_BUILTIN

WiFiServer server(80);
WiFiClient serverClients[MAX_SRV_CLIENTS];
IPAddress ip(10, 241, 241, 27);
IPAddress mask(255, 255, 255, 0);
IPAddress gateway(10, 241, 241, 254);
char ssid[] = "AP";
char pass[] = "Science_Fair";

void setup() {
  Serial.begin(9600);
  WiFi.config(ip, gateway, mask);
  WiFi.begin(ssid, pass);
  while (WiFi.status() != WL_CONNECTED) {
    yield();
  }
  server.begin();
  pinMode(led, OUTPUT);
  digitalWrite(led, LOW);
}

void loop() {
  uint8_t i;
  Serial.setTimeout(10);
  for (i = 0; i < MAX_SRV_CLIENTS; i++) {
    Serial.println("Pre 1");
    if (server.hasClient()) {
      Serial.println("Pre 2");
      if (!serverClients[i] || !serverClients[i].connected()) {
        Serial.println("Pre 3");
        if (serverClients[i]) {
          Serial.println("Pre 4");
          serverClients[i].stop();
          Serial.println("Pre 5");
        }
        Serial.println("Pre 6");
        serverClients[i] = server.available();
        Serial.println("Pre 7");
        serverClients[i].setTimeout(10);
        Serial.println("Pre 8");
        Serial.print("New client: ");
        Serial.println("Pre 9");
        Serial.println(i);
        Serial.println("Pre 10");
        break;
      }
    }
    yield();
  }
  for (i = 0; i < MAX_SRV_CLIENTS; i++) {
    String fromClient = serverClients[i].readStringUntil('e');
    Serial.println("P1");
    yield();
    if (fromClient == "LR") {
      Serial.println("LR");
      String reply = Serial.readStringUntil('e');
      Serial.println("P2");
      yield();
      if (reply == "Warn_B") {
        yield();
        Serial.println("Confirm");
        Serial.println("P3");
        for (i = 0; i < MAX_SRV_CLIENTS; i++) {
          serverClients[i].print("Warn_B");
          Serial.println("P4");
          yield();
        }
      }
    }
    yield(); // this is the yield I need to add
    else if (fromClient == "RR") {
      Serial.println("RR");
      Serial.println("P5");
      String reply = Serial.readStringUntil('e');
      Serial.println("P6");
      yield();
      if (reply == "Warn_A") {
        Serial.println("P7");
        Serial.println("Confirm");
      }
      for (i = 0; i < MAX_SRV_CLIENTS; i++) {
        serverClients[i].print("Warn_A");
        Serial.println("P8");
        yield();
      }
    }
    yield();
  }
}

I need the yield() there but I don't know how to let it still do the else if. Please help me guys, thanks!

4
  • 3
    Why do you think you need a yield there?
    – Majenko
    Commented Apr 6, 2020 at 10:03
  • @Majenko Uh cuz I am using my NodeMCU and I have a watchdog timer, I think there is the problem cuz it is always giving me a bunch of errors like feefeffe
    – Ted Lu
    Commented Apr 6, 2020 at 10:08
  • 1
    Yes, but why there? Why between two pieces of code that are "either / or"? If the first ran then the second won't. If the second ran then the first won't have. There's no "between" in that scenario.
    – Majenko
    Commented Apr 6, 2020 at 10:11
  • the yield() before the if is the yield() before the else
    – Juraj
    Commented Apr 6, 2020 at 13:23

1 Answer 1

1

First of all do yourself a favour and listen to your IDE

This report would have more information with
 "Show verbose output during compilation"
option enabled in File -> Preferences.

and in an if else there is no in between so either let it run as a state machine especially inyour case A != B != C != other:

void subfunction(){
  if(A){
  // code to run
  return; 
  }
 yield();  
 if(B){
  // code to run
  return; 
  }
 yield();
 if(C){
  // code to run
  return; 
  }
 else{}  // to catch if not A or B or C
 yield();
}

or implement switch case (Pseudo code)

 switch(fromClient){
    case "LR":
      //code to run
      yield();
      break;
    case "RR":
      //code to run
      yield();
      break;
    default:
      //code to run
      yield();
      break;
  }

Id else chains are prone to errors as in your case its A != B != C != other

3
  • Thank you! But again I am at home, I'll test it tmr in my lab, thank you again!
    – Ted Lu
    Commented Apr 6, 2020 at 11:23
  • why not yield() after the switch?
    – Juraj
    Commented Apr 6, 2020 at 13:20
  • I just copied his"use case" but you are right he could even do it before the cases and in the case (depends on his WD timer Commented Apr 6, 2020 at 13:38

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.