0

I am trying to send some messages from nodeMCU to Arduino using Serial communication (aka UART). Following is the code on nodeMCU side:

#include <ESP8266WiFi.h>

const char* ssid = "my-ssid"; // SSID i.e. Service Set Identifier is the name of your WIFI
const char* password = "my-pasword"; // 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() {
          Serial.begin(115200);
          delay(10);

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

          // Connect to WiFi network
          WiFi.begin(ssid, password);
          //Trying to connect to wifi
          while (WiFi.status() != WL_CONNECTED) {
            delay(500);
          }
          Serial.println("$Wifi Connected#");
          server.begin();
        }

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

          // Wait until the client sends some data
          while(!client.available()){
            delay(1);
          }
          // Read the first line of the request
          String request = client.readStringUntil('\r');
          client.flush();

          int value = LOW;
          if (request.indexOf("/LED=ON") != -1)  {
            digitalWrite(ledPin, HIGH);
            Serial.println("$Led High#");
            value = HIGH;
          }
          if (request.indexOf("/LED=OFF") != -1)  {
            digitalWrite(ledPin, LOW);
            Serial.println("$Led Low#");
            value = LOW;
          }

          // 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.println("<html>");

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

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

Following is the code on Arduino side:

String msgFromNodeMCU;
void setup() {
  pinMode(13,OUTPUT);
  Serial.begin(115200);
}

void loop() {
  msgFromNodeMCU=Serial.readString();
  Serial.println(msgFromNodeMCU);
}

which was working fine until I modified that code to:

String msgFromNodeMCU;

void setup() {
  pinMode(13,OUTPUT);
  Serial.begin(115200);
}

void loop() {
  msgFromNodeMCU=Serial.readString();
  delay(10);
  String wifi_connected="$Wifi Connected#";
  String led_High="$Led High#";
  String led_Low="$Led Low#";
    if (msgFromNodeMCU.equalsIgnoreCase(wifi_connected)) {
      Serial.println("Wifi Connected");
      delay(10);
    }
    if (msgFromNodeMCU.equalsIgnoreCase(led_High)) {
      digitalWrite(13,HIGH);
    }
    if (msgFromNodeMCU.equalsIgnoreCase(led_Low)) {
      digitalWrite(13,LOW);
    }
}

With this code, I see no "Wifi Connected" message on my Serial monitor and there is no response on pin 13. This implies msgFromNodeMCU is not heard. What can I do for msgFromNodeMCU to be heard? Any sort of help is highly appreciated. :D

  • You have two delay(), 10 seconds each. While interrupts still works when you delay(), communications input buffer are limited in size. If you don't collect the information timely, buffers will be overwriten, information will be lost. Replace delay with millis() as shown in the BlinkWithoutDelay sketch that comes with your IDE. – user31481 Oct 20 '17 at 15:55
  • +Look Alterno Removing delay() or adding millis() was not helpful. It would be nice if as soon as any message comes to the Serial buffer, it interrupts the target device (arduino in this case). Is there a way to create such interrupt? – Bigyan Chapagain Oct 20 '17 at 16:16
  • Serial will not interrupt you. You have to poll Serial frequently. – user31481 Oct 20 '17 at 16:20
  • Serial is ESP or USB? – Juraj Oct 20 '17 at 18:42
  • @Juraj Serial is ESP. Not USB – Bigyan Chapagain Oct 20 '17 at 21:33

Your Answer

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

Browse other questions tagged or ask your own question.