Your new topic does not fit any of the above??? Check first. Then post here. Thanks.

Moderator: igrr

User avatar
By planetleak
#21901 Hello,

uploaded basic webserver to the ESP01, and uploaded a simple serial read program to an arduino nano.
Connected the ESP to the arduino with Tx/Rx, but when send something from the ESP, that received three times at the arduino serial monitor.

I mean ESP side once do Serial.println("on"), that appear in the arduino nano serial monitor as:
on

on

on

I got this problem too, when using lua example at the ESP side.
Any idea?
Or what the best way to send data from ESP to Arduino with Serial?

Thank you.
User avatar
By brutzler
#21909 Can you post your code?
Really hard with such low information to help.

Perhaps you are sending three times?
What do you get in the serial monitor of the arduino IDE, when you are connected to the ESP?
If there is only one "on", than you have to look at arduino side?
User avatar
By planetleak
#21910 Im write only once "on" with ESP8266, but arduino serial monitor write three time the "on".

I get the same problem when use nodeMCU lua webserver example too.
Thank you.

ESP8266-01 code:

Code: Select all#include <ESP8266WiFi.h>

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

// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);

void setup() {
  Serial.begin(115200);
  delay(10);

  // prepare GPIO2
  pinMode(2, OUTPUT);
  digitalWrite(2, 0);
 
  // Connect to WiFi network
  Serial.println();
  Serial.println();
  Serial.print("Connecting to ");
  Serial.println(ssid);
 
  WiFi.begin(ssid, password);
 
  while (WiFi.status() != WL_CONNECTED) {
    delay(500);
    Serial.print(".");
  }
  Serial.println("");
  Serial.println("WiFi connected");
 
  // Start the server
  server.begin();
  Serial.println("Server started");

  // Print the IP address
  Serial.println(WiFi.localIP());
}

void loop() {
  // Check if a client has connected
  WiFiClient client = server.available();
  if (!client) {
    return;
  }
  //Serial.println("new client");
  while(!client.available()){
    delay(1);
  }
  String req = client.readStringUntil('\r');
  //Serial.println(req);
  client.flush();
 
  // Match the request
  int val;
  if (req.indexOf("/gpio/0") != -1){
    val = 0;
    Serial.println("on");
}
  else if (req.indexOf("/gpio/1") != -1){
    val = 1;
    Serial.println("off");
  }
  else {
    //Serial.println("invalid request");
    client.stop();
    return;
  }
  // Set GPIO2 according to the request
  digitalWrite(2, val);
  client.flush();
  // Prepare the response
  String s = "HTTP/1.1 200 OK\r\nContent-Type: text/html\r\n\r\n<!DOCTYPE HTML>\r\n<html>\r\nGPIO is now ";
  s += (val)?"high":"low";
  s += "</html>\n";
  // Send the response to the client
  //client.print(s);
  //delay(1);
  //Serial.println("Client disonnected");
}


Arduino code:

Code: Select allint ledPin = 13;
String readString;

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

void loop() {
  while (Serial.available()) {
    delay(3);
    char c = Serial.read();
    readString += c;
  }
  if (readString.length() >0) {
    Serial.println(readString);
    if (readString == "on")     
    {
      digitalWrite(ledPin, HIGH);
    }
    if (readString == "off")
    {
      digitalWrite(ledPin, LOW);
    }
    readString="";
  }
}

User avatar
By brutzler
#21928 Hi,

ok, tested your ESP8266 code on my ESP-01. Without connection to an arduino, there is only one "on" in my serial-monitor. This looks fine
BTW:
- "/gpio/0" --> on
- "/gpio/1" --> off
this I expected vice versa...... the response to the client (browser) is the right one.

To your arduino: You are making two things on the serial-interface at the same time:
- communicating with the ESP
- communicating with the serial monitor
you are sure, there is no loopback?