Using Tx/Rx pins of UNO for sending values to ESP8266

Hi,
I'm using the following 2 boards in my projects - Arduino - UNO and Node MCU ESP8266.
I'm having these conditions :

  1. I can't use ESP8266 for my circuit, because I've interfaced mostly pins to 3.5" TFT display.

  2. all other pins are filled because of the display, luckily I've managed to pull out 3 analog and 1 digital pin for my other work (for sending pulses and recieving analog data and one for ECG signal)

  3. I've only Tx and Rx pin left and I want to send three values ( float)to IOT cloud. For that I'm using ESP8266.

I've connected the following connection :

Arduino <------------------------ > ESP8266
Tx --------------------------------------- Rx
Rx --------------------------------------- Tx
GND (connected from bread board)------------------------------------ GND
ESP is being powered by the USB cable for the time beingm, later after verification I'll connect it to 3.3 V pin (from bread board).

I've been using a dummy code for testing purpose, later I'll use that code for sending real values.
Arduino side code :

#include <SoftwareSerial.h>
SoftwareSerial espSerial(1, 0);
float sp = 0;
float heart = 0;

void setup() {
  Serial.begin(115200);
  espSerial.begin(115200);
  delay(2000);
}
void loop()
{
 
    espSerial.write(sp);
    sp = 5 + random(5);
  delay(3000);
  
    heart = 100 + random(40);
    espSerial.write(heart);
  
  delay(3000);
}

ESP8266 side code :

double s;
float h;
void setup() {
// Open serial communications and wait for port to open:
Serial.begin(115200);
while (!Serial) {
; // wait for serial port to connect. Needed for native USB port only
}
}

void loop() { // run over and over
  
if (Serial.available()) {
 h = Serial.read();
}
Serial.print("h =  ");
Serial.print(h);
Serial.print("    ");
delay(1000);
if (Serial.available()) {
 s = Serial.read();
}
Serial.print("s =  ");
Serial.println(s);
delay(1000);
}

My doubts are :

  1. Am i declaring the Tx and Rx in arduino code correctly?
  2. I've used it without the resistor divider and with divider also.
  3. I've used different pins on ESP8266 for Tx/ Rx as D2 and D4 with voltage divider and wothout also.
    (adding the following line in the ESP side code)
#include <SoftwareSerial.h>
SoftwareSerial espSerial(D2, D4);

I've all pins left in ESP8266, I can use any pin in that, unfortunately I've only Tx and Rx left in Arduino, preciously I was determing pin 5 and 6 in the arduino for sending data to arduino and that was working fine then I've to use the pins for the display :frowning_face: .

Thanks,
Aniket

Typical stuff to know. Softwareserial.h does not play nice with an ESP. Look for the software serial library that works with an ESP.

On an Arduino Uno using pins 0 and 1 for serial can result in issues.

An Uno is a 5V device. An ESP is a 3.3V device. ESP's are not 5V tolerant. Which level shifters are you using? The ESP may be damaged with 5V on its GPIO pins.

Software Serial on a Uno does not work at 115200 Baud, get it working at 9600 Baud then increase it later if you need to. I'm not sure what the limit is.

You are trying to use software serial on the Uno on the pins that already have hardware serial on them, you can't use the same pins for 2 completely difference serial ports, you need to choose different pins.

You can use 0 and 1 as hardware serial but be aware that it is the same serial port as used by the USB so you might have a conflict in doing so, you have to decide if the conflict is a problem or not.

You can switch the serial port on an ESP8266 away from the USB connection to GPIO13 Rx and GPIO 15 Tx by using:
Serial.swap();

2 Likes

In my opinion, if all you need is more digital IO pins, adding an Uno is not the way to go. Adding digital GPIO pins with I2C expanders (MCP23008, MXCP23017, PCF8574) or with the SPI versions of the MCP parts or with serial to parallel shift registers (74HC595) is easier.

1 Like

Always the first consideration.

Actually, I thought TFT displays operated at 3.3 v, so are better interfaced to an ESP. The port expander you need to interface it will operate at either. The only problem would be getting a library which works through the port expander.

1 Like

What are you not telling us?


https://simple-circuit.com/esp8266-nodemcu-ili9341-tft-display/

image

I forgot to mention that I'm using a display shield, of this type.

I did reached to a solution from last night :smile: with some help.
Connections :
Arduino <----------------------------------> ESP8266
Tx <--------------------------------------------> Rx
Rx <--------------------------------------------> Tx
GND (from bread board which is of Arduino) <-------> GND

The arduino side of code is :

float sp = 0;
float heart = 0;

void setup() {
  Serial.begin(115200);
}
void loop()
{
  sp = 5 + random(5);
  heart = 100 + random(40);
  Serial.print(sp);
  Serial.print(",");
  Serial.println(heart);
  delay(1000);
}

and the ESP side of code :

void setup()
{
Serial.begin(115200);
}

void loop() {   
if (Serial.available() > 0) 
{
  String data = Serial.readStringUntil('\n');
  Serial.println(data);
}
}

now from this code I'm getting the values in the string data in the ESP in the form of a string, for example:
9,135
7,118
8,129
.
.
.
.
like this, anyone help my to get these values from the string to the two respective variables in the ESP side.

The person who helped me said now you can saparate these two with the help of that , that is between the values.

@mrburnette I'm using the sheild, that i forgot to mention :slight_smile:

addtional doubt is :
I'm having 3 float values in the order - {(integervalue 1. decimalvalue1), (integervalue 2. decimalvalue2),(integervalue 3. decimalvalue2) }. I want to detect these two commas becaue the second or third value can vary between two digit and three digits.
for example :
97.88,75.56,123.45
102.38,112.89,87.93
.
.
.
.
so its not fixed.

or else educate me with a method so that I can convert 90.46 into 090.46 . So that every two digit value will be exaclty 6 charactars long.
[third_digit][second_digit][first_digit][.][first_decimal][second_decimal]
I dont know after doing this also it will be benificial or not. :slight_smile:

so, focus on the first doubt and in case you have a better suggestion, feel free to help me.

Thanks. :smiley:
Aniket

accidently I solved the mystrey.
instead of sending a,b,c
I sent a,b:c;
so that I can detect the positions of , : and ;
code :

float sp, hr, hr_ecg;
int firstComma = 0, secondComma = 0, thirdComma = 0;
String spo, heart_rate, heart_rate_ecg;
String data;
void setup()
{
  // Open serial communications and wait for port to open:
  Serial.begin(115200);
}

void loop() { // run over and over

  if (Serial.available() > 0)
  {
    data = Serial.readStringUntil('\n');
    Serial.println(data);
  }
  //  firstComma = data.indexOf(',');
  firstComma = data.indexOf(',');
  secondComma = data.indexOf(':');
  thirdComma = data.indexOf(';');
  Serial.println(firstComma);
  Serial.println(secondComma);
  Serial.println(thirdComma);
  spo = data.substring(0, firstComma);
  heart_rate = data.substring((firstComma + 1), (secondComma));
  heart_rate_ecg = data.substring((secondComma + 1), thirdComma);
  Serial.println("The first reading of " + data + " is " + spo);
  Serial.println("The second reading of " + data + " is " + heart_rate);
  Serial.println("The third reading of " + data + " is " + heart_rate_ecg);
  char bufsp[spo.length()];
  char bufhr[heart_rate.length()];
  char bufhr_ecg[heart_rate_ecg.length()];
  spo.toCharArray(bufsp, spo.length() + 1);
  heart_rate.toCharArray(bufhr, heart_rate.length() + 1);
  heart_rate_ecg.toCharArray(bufhr_ecg, heart_rate_ecg.length() + 1);
  sp = atof(bufsp);
  hr = atof(bufhr);
  hr_ecg = atof(bufhr_ecg);
  Serial.println(sp);
  Serial.println(hr);
  Serial.println(hr_ecg);
  delay(1000);
}

Thanks for your replies :slight_smile:
Aniket

The usual way is to parse through commas using the function strchr:

Partial from Adafruit GPS

/**************************************************************************/
bool Adafruit_GPS::parse(char *nmea) {
  if (!check(nmea))
    return false;
  // passed the check, so there's a valid source in thisSource and a valid
  // sentence in thisSentence
  char *p = nmea; // Pointer to move through the sentence -- good parsers are
                  // non-destructive
  p = strchr(p, ',') + 1; // Skip to char after the next comma, then check.

  // This may look inefficient, but an M0 will get down the list in about 1 us /
  // strcmp()! Put the GPS sentences from Adafruit_GPS at the top to make
  // pruning excess code easier. Otherwise, keep them alphabetical for ease of
  // reading.
  if (!strcmp(thisSentence, "GGA")) { //************************************GGA
    // Adafruit from Actisense NGW-1 from SH CP150C
    parseTime(p);
    p = strchr(p, ',') + 1; // parse time with specialized function
    // parse out both latitude and direction, then go to next field, or fail
    if (parseCoord(p, &latitudeDegrees, &latitude, &latitude_fixed, &lat))
      newDataValue(NMEA_LAT, latitudeDegrees);
    p = strchr(p, ',') + 1;
    p = strchr(p, ',') + 1;
    // parse out both longitude and direction, then go to next field, or fail
    if (parseCoord(p, &longitudeDegrees, &longitude, &longitude_fixed, &lon))
      newDataValue(NMEA_LON, longitudeDegrees);
    p = strchr(p, ',') + 1;
    p = strchr(p, ',') + 1;
    if (!isEmpty(p)) { // if it's a , (or a * at end of sentence) the value is
                       // not included
1 Like

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.