Reading serial data from Arduino to ESP8266

Im attempting to read digital data from my sensor and print to serial then at the ESP8266 it will read the serial to get the data. But it seen like not working. Isit still have any way to do it?

For the wiring :

Arduino >> ESP8266
Rx >> Rx
Tx >> Tx
Gnd >> Gnd
3.3v >> Vcc
3.3v >> CH_PD

Here is my code for Arduino

int SensorLvG_L=22;


void setup() {
// put your setup code here, to run once:
Serial.begin (115200);
pinMode(SensorLvG_L, INPUT);
}

void loop() {
// put your main code here, to run repeatedly:
 bool A=digitalRead(SensorLvG_L);

if ( A == HIGH )
{
Serial.println(A);
}
}

While this is for my ESP8266

#include <ESP8266WiFi.h>

const char* ssid = "tanws1991";
const char* password = "09pbd02024";
int condition =0;
// 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);
}
// Start the server
server.begin();
  Serial.print(WiFi.localIP());
// Print the IP address
}
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 req = client.readStringUntil('\r');
client.flush();

// Match the request
int val;
if (req.indexOf("/gpio/0") != -1)
  val = 0;
else if (req.indexOf("/gpio/1") != -1)
  val = 1;
else {
  client.stop();
  return;
}
// Set GPIO2 according to the request
digitalWrite(2, val);

client.flush();

// Prepare the response
  if (Serial.available() > 0)
{
  condition = Serial.read();
}
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 ";
if ( condition == 1)
{

s += (val)?"|high|":"|low|";
}
s += "</html>\n";

// Send the response to the client
client.print(s);
delay(1);

// The client will actually be disconnected 
// when the function returns and 'client' object is detroyed
}

Please modify your post and use the code button </> so your code looks like this and is easy to copy to a text editor. See How to use the Forum

Have you two separate devices or is the ESP8266 connected to your Arduino - if so please post a photo of a drawing showing how everything is connected. (Please don't use Fritzing).

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data.

...R

On the ESP side, are you using Serial to talk to the Arduino or the PC? Both is the WRONG answer.

On the Arduino side, you are sending 0 or 3 characters. Why? The ESP side expects to read 1 character EVERY time.

PaulS:
On the ESP side, are you using Serial to talk to the Arduino or the PC? Both is the WRONG answer.

On the Arduino side, you are sending 0 or 3 characters. Why? The ESP side expects to read 1 character EVERY time.

As for the ESP I want to use the Serial to talk with the Arduino.. Isit the wrong way?

What you mean by sending 0 or 3 characters ?

What you mean by sending 0 or 3 characters ?

How many characters do you send if the pin is HIGH?
How many characters do you send if the pin is LOW?

It is NOT the same number in each case. It is NOT 1 in either case.

im interested too, what is the right way to "read" or talk with the PC (on the ESP side) through the UART?
im havin troubles when i try to read serial data.

marcelinolg:
im interested too, what is the right way to "read" or talk with the PC (on the ESP side) through the UART?
im havin troubles when i try to read serial data.

Are you referring to a UART attached to your PC? - If so that would not seem to be an Arduino problem.

If it is an Arduino problem post your program if you want help. That way we can see what you can see.

And when posting code please use the code button </>so your code looks like thisand is easy to copy to a text editor See How to use the Forum

...R

I think Rx to Rx , Tx to Tx connection is done when you want we want to communicate with Arduino IDE's serial monitor.
Here we are communicating within Arduino and ESP8266 so we have to connect Arduino RX to ESP Tx and Arduino Tx to ESP Rx.
While doing above mentioned connection take care of voltage level because Arduino operates at 5V and ESP at 3.3 V.