0

I’m trying to send data from a NodeMCU to an Arduino nano.

If I try to send data with one second delay - all working fine.

But if I try to send without delay or with a delay less than one second, then arduino sees data but nothing not does.

Arduino nano sketch:

#include <Servo.h> 

Servo servo1; 

int servo = 6; 
int engine = 5; 

void setup() { 
Serial.begin(9600); 
analogWrite(servo, 67); 
servo1.attach(8); 
} 

void loop() { 
if (Serial.available()) { 
String model = Serial.readStringUntil(':'); 
i 

f (model.equals("engine")){ 
String value = Serial.readStringUntil('c'); 
int power = value.toInt(); 
analogWrite(engine, power); 
} 
if (model.equals("servo")){ 
String value_servo = Serial.readStringUntil('c'); 
int power_servo = value_servo.toInt(); 
servo1.write(power_servo); 
} 
} 
}

NodeMCU sketch:

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

const char* ssid = "my_wifi_ssid"; 
const char* password = "my_wifi_password"; 

ESP8266WebServer server(80); 


void get_vars(){ 
if (server.argName(0) == "engine"){ 
server.send(200, "text/plain", "Reading engine. Value: "+server.arg(0)); 
Serial.println("engine:"+server.arg(0)+"c"); 
} 
if (server.argName(0) == "servo"){ 
server.send(200, "text/plain", "Reading engine. Value: "+server.arg(0)); 
Serial.println("servo:"+server.arg(0)+"c"); 
} 
} 

void setup(void){ 
main_stuff(); 
} 

void loop(void){ 
server.handleClient(); 
} 


void main_stuff(){ 
Serial.begin(9600); 
WiFi.mode(WIFI_STA); 
WiFi.begin(ssid, password); 
Serial.println(""); 

// Wait for connection 
while (WiFi.status() != WL_CONNECTED) { 
delay(100); 
Serial.print("."); 
} 
Serial.println(""); 
Serial.print("Connected to "); 
Serial.println(ssid); 
Serial.print("IP address: "); 
Serial.println(WiFi.localIP()); 

if (MDNS.begin("esp8266")) { 
Serial.println("MDNS responder started"); 
} 

server.onNotFound(get_vars); 

server.begin(); 
Serial.println("HTTP server started"); 

}
4
  • In the Arduino IDE, press "Ctrl" + "T" that will make your code easier to read. Commented Jun 8, 2018 at 14:59
  • What does this mean "But if i try to send without delay or delay < one second, then arduino sees data but nothing not does.", could you elaborate? Commented Jun 8, 2018 at 15:00
  • @MatsK, when the NodeMcu sends something to the Arduino - the built-in LED on it blinks Commented Jun 8, 2018 at 15:22
  • nothing not does is a double negative ... it means something does .... it is unclear what you mean by that statement Commented Jun 8, 2018 at 17:32

1 Answer 1

1

the newline characters from println (\r\n) stay in the serial input buffer and you read them with Serial.readStringUntil(':');. then the string is not "servo" but "\r\nservo"

if you wait one second with sending, the the \r\n are discarded because the readStringUntil times out one second after reading them

2
  • And how to fix this? Commented Jun 8, 2018 at 14:51
  • Thank you very much! All work fine. I'am just change "Serial.println" to "Serial.print" Commented Jun 8, 2018 at 15:32

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.