The code given below is an example code I found on the internet. This is to use my nodeMCU as a server. For my application, I believe that these lines of code...:

if (request.indexOf("/LED=ON") != -1)  {
            digitalWrite(ledPin, HIGH);
            value = HIGH;
          if (request.indexOf("/LED=OFF") != -1)  {
            digitalWrite(ledPin, LOW);
            value = LOW;

... from the below program should run thereby interrupting every processes.

 #include <ESP8266WiFi.h>

    const char* ssid = "Linux"; // SSID i.e. Service Set Identifier is the name of your WIFI
    const char* password = "0123456789"; // Your Wifi password, in case you have open network comment the whole statement.

    int ledPin = 13; // GPIO13 or for NodeMCU you can directly write D7 
    WiFiServer server(80); // Creates a server that listens for incoming connections on the specified port, here in this case port is 80.

    void setup() {

      pinMode(ledPin, OUTPUT);
      digitalWrite(ledPin, LOW);

      // Connect to WiFi network
      Serial.print("Connecting to ");

      WiFi.begin(ssid, password);

      while (WiFi.status() != WL_CONNECTED) {
      Serial.println("WiFi connected");

      // Start the server
      Serial.println("Server started");

      // Print the IP address
      Serial.print("Use this URL to connect: ");
      Serial.print(WiFi.localIP()); //Gets the WiFi shield's IP address and Print the IP address of serial monitor


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

      // Wait until the client sends some data
      Serial.println("new client");

      // Read the first line of the request
      String request = client.readStringUntil('\r');

      // Match the request

      int value = LOW;
      if (request.indexOf("/LED=ON") != -1)  {
        digitalWrite(ledPin, HIGH);
        value = HIGH;
      if (request.indexOf("/LED=OFF") != -1)  {
        digitalWrite(ledPin, LOW);
        value = LOW;

    // Set ledPin according to the request
    //digitalWrite(ledPin, value);

      // Return the response
      client.println("HTTP/1.1 200 OK");
      client.println("Content-Type: text/html");
      client.println(""); //  do not forget this one
      client.println("<!DOCTYPE HTML>");

      client.print("Led pin is now: ");

      if(value == HIGH) {
      } else {
      client.println("<a href=\"/LED=ON\"\"><button>Turn On </button></a>");
      client.println("<a href=\"/LED=OFF\"\"><button>Turn Off </button></a><br />");  

      Serial.println("Client disconnected");


How can we achieve this kind of software Interrupt(s)? PS: Timer Interrupt won't work -- I believe.


That doesn't need an interrupt. Think about it, just check it right after you read it from the client. So the response will be immediate. It's not like it can suddenly become true at some other point without being read from the client. You're already using a blocking read so if the first part of the message has arrived you'll stop right there and wait for it. The loop should turn over really fast, keep it fast and non-blocking anywhere else and you can count on even if the message starts coming in immediately after your read the loop will make it back around to read again before the end of the message gets there.

  • +Delta_G I think I couldn't make my problem clear. When there are some other additional code (which are not shown here) within loop(), I think we do need interrupt. That's why we need a software interrupt. – Big Yan Oct 4 '17 at 3:27
  • Why do you think that? How will either of those conditions ever become true except in concert with a read from the client? If you think this other code causes some problem then please edit the question and post it. But I stand by this answer. The stuff coming in from the client is very very slow. You have a lot of time between the time that the message starts the arrive and the point where it ends that you could do a lot of other things in that long time. – Delta_G Oct 4 '17 at 3:44
  • As long as you get back to the point where you read at some point during your message, your code will stop there and not do the other things until the message is complete since you use readString. So the only thing that your interrupt could possibly ever interrupt is the next line after the readString command which is the section that you want to put in the interrupt. If you think about the overhead of an interrupt vs just letting the code hit that next line, the interrupt would actually make your code slower and add more lag. – Delta_G Oct 4 '17 at 3:44
  • +Delta_G Thank you that seemed to work. But I still have one more problem: Because of these lines if (!client) { return; } // Wait until the client sends some data Serial.println("new client"); while(!client.available()){ delay(1); } the rest of embedded system project has become so slow. How can I run these code alongside my actual embedded system project? – Big Yan Oct 4 '17 at 11:14
  • The while(!client.available()) line says, stop right here and do absolutely nothing until we get some message from the client. Then you use readString() which also waits until the transmission ends. That's blocking code, it stops the program to wait on something. If you don't want it to work like that you'll have to do a lot of rewriting. – Delta_G Oct 5 '17 at 2:12

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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