How do I move values from my ESP2866 to my Uno with a Serial connection?

Hello r/arduino,

I have recently discovered the wonderful world of the Arduino with modules, and I want to create a cryptocurrency ticker that retrieves prices from the Internet, onto the ESP-8266 (the one pictured here), then on the Arduino, and finally on a 20x4 LCD that I have.

So, I was trying to learn how to move a variable Y from the ESP to the Arduino, and then having the Arduino to output the code onto my LCD.

The wiring part is something I believe I have correct. I just hooked up the TX of the ESP to the RX of the Arduino, the RX of the ESP to the TX of the Arduino, the 3V3 pin of the ESP to the 3.3V pin of the Arduino, and Ground to Ground.

For programming, I just hook up my ESP to the PC using its microUSB port, since it has one:

Here is the code from the ESP-8266:

  #include <ESP8266WiFi.h>
  //ESP-8266 Test Code
  int y = 1337;
  void setup() {
    Serial.begin(115200);
  }
  void loop() {
  delay(500);
  Serial.write(y);
  }

And here is the code from the Arduino Uno:

  #include <LiquidCrystal.h>
  const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  LiquidCrystal lcd(rs, en, d4, d5, d6, d7);
  int y = 0;
  void setup() {
    lcd.begin(20, 4);
    Serial.begin(115200);
  }
  void loop() {
    if (Serial.available() > 0) {
      y = Serial.read();
      Serial.print("I received: ");
      Serial.println(y);
  
      lcd.setCursor(0, 0);
      lcd.println(y);
      Serial.print(y);
    }
    delay(500);
  }

When I do run the code, the Arduino’s LCD output is 57, while the 2 character squares after 57 on the LCD are lit up, but the even-numbered lines on them are not lit up. The serial output on the Arduino is “I received: 57” repeatedly, as you would expect.

The LCD works fine since I tried some other example code and it worked correctly.

So my question is, what have I forgotten here? What am I doing wrong? I googled for quite a few hours, and I didn’t get anywhere. The if statement was pulled directly from Arduino’s example code on the serial page they have.

If you need any other info, just reply here asking for info.

Thank you for reading until here, and you would be doing me a solid if you can help me fix my code!

Have a nice day!

    lcd.print(y);

TolpuddleSartre:

    lcd.print(y);

This did fix the box problem, but the main problem of the code displaying 57 is still there.

Any other suggestions?

Thank you for taking the time to answer!

Do you expect '9'?

 lcd.print((char)y);

Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker
Serial.print(value1);
Serial.print(','); // comma separator
Serial.print(value2);
Serial.println('>'); // end marker

Also study the difference between Serial.write() and Serial.print()

…R

Robin2:
Have a look at the examples in Serial Input Basics - simple reliable ways to receive data. The technique in the 3rd example will be the most reliable.

You can send data in a compatible format with code like this

Serial.print('<'); // start marker

Serial.print(value1);
Serial.print(’,’); // comma separator
Serial.print(value2);
Serial.println(’>’); // end marker




Also study the difference between Serial.write() and Serial.print()

...R

Okay, so it works!

I didn’t really modify your code, But when I try to print to my LCD, the program just doesn’t compile.

I imported <LiquidCrystal.h> just like the rest of my projects, but when I try to compile the program, the compiler says that 'lcd' was not declared in this scopeat every line I use the LCD command. Do you know what could be causing this?

Thank you for your very insightful guide on Serial Inputs and for helping me with this! Rock on!

Code of the ESP:

#include <ESP8266WiFi.h>
//ESP-8266 
int y = 123456789;
void setup() {
  Serial.begin(9600);
}
void loop() {
delay(500);
Serial.print('<'); // start marker
Serial.print(y);
Serial.println('>'); // end marker
}

Code of the Arduino:

#include <LiquidCrystal.h>
// Example 3 - Receive with start- and end-markers
const byte numChars = 32;
char receivedChars[numChars];

boolean newData = false;

void setup() {
    Serial.begin(9600);
    Serial.println("<Arduino is ready>");
    
   lcd.begin(20, 4);
   lcd.setCursor(10, 3);
}

void loop() {
    recvWithStartEndMarkers();
    showNewData();
}

void recvWithStartEndMarkers() {
    static boolean recvInProgress = false;
    static byte ndx = 0;
    char startMarker = '<';
    char endMarker = '>';
    char rc;
 
    while (Serial.available() > 0 && newData == false) {
        rc = Serial.read();

        if (recvInProgress == true) {
            if (rc != endMarker) {
                receivedChars[ndx] = rc;
                ndx++;
                if (ndx >= numChars) {
                    ndx = numChars - 1;
                }
            }
            
            else {
                receivedChars[ndx] = '\0'; // terminate the string
                recvInProgress = false;
                ndx = 0;
                newData = true;
            }
        }
        
        else if (rc == startMarker) {
            recvInProgress = true;
        }
    }
}

void showNewData() {
    if (newData == true) {
        Serial.print("This just in ... ");
        Serial.println(receivedChars);
        newData = false;
    }
}
const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;
  LiquidCrystal lcd(rs, en, d4, d5, d6, d7);

From your original post

TolpuddleSartre:

const int rs = 12, en = 11, d4 = 5, d5 = 4, d6 = 3, d7 = 2;

LiquidCrystal lcd(rs, en, d4, d5, d6, d7);


From your original post

This fixed it! Thank you so much!