Get in computer sensor readings through ESP8266 RX/TX

Good afternoon,

I am trying to read and show in Serial Monitor the CO2 measurements of a sensor MH-Z19C in a custom made PCB with chip ESP8266.

The RX/TX pins of the sensor are connected to the RX/TX pins of the microchip ESP8266, which makes it difficult to get the readings in the computer through the FTDI.

Some assumptions:
We cannot change the connection pins between sensor and the ESP8266
The board was working previously and the measurements were correctly read in the computer through Serial Monitor. However, the working code was lost...

Non-working Code used:

#include <Arduino.h>
#include <ESP8266WiFi.h>
#include <Wire.h>
#include <SoftwareSerial.h>                               

#define RX_PIN D3                                        
#define TX_PIN D1                                       
#define BAUDRATE 9600 
SoftwareSerial mySerial(RX_PIN, TX_PIN);                   
int CO2;
int tVOC;
int incomingByte = 0;

unsigned long getDataTimer = 0;
unsigned long starttime;
unsigned long sampletime_ms = 600000;
unsigned long add_base_time_portal = 300000;

//LED WIFI
#include <Ticker.h>
Ticker ticker;
#ifndef LED_BUILTIN
#define LED_BUILTIN 13 
#endif

int LED = LED_BUILTIN;

void tick()
{
  //toggle state
  digitalWrite(LED, !digitalRead(LED));
}


void setup(void) {
  Serial.begin(115200);  
  setup_mh_z19();
  delay(4000);
}
  
void loop(void) {
    run_mh_z19();
}

void setup_mh_z19()
{
  mySerial.begin(9600,SWSERIAL_8N1,D3, D1, false,64);                               
}

void run_mh_z19()
{
  starttime = millis();
  if (millis() - getDataTimer >= 2000)
    {
    incomingByte = Serial.read();

    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);

    getDataTimer = millis();
    }

}

Schematic of the involved part of the project
image

image

Your code does not compile :
Error msg: 'MHZ19' does not name a type

What is " MHZ19 myMHZ19;" ?

To facilitate our help, post a schematic of your project.

Hello @ruilviana, thank you for you message.

The code is updated and compiling now.

I updated the original post with the Schematic of the involved part of the project

In your code you define the softserial pins as D3(RX) (GPIO 0) and D1 (TX)(GPIO5).
#define RX_PIN D3
#define TX_PIN D1

Then read the sensor data through the native serial:
" incomingByte = Serial.read(); "
and prints by native serial.

The FTDI must be connected to RX (D9)(GPIO 3) and TX (D10)(GPIO 1).

The sensor must be connected to D1(GPIO 5) and D3(GPIO 0),
and this line of code: "incomingByte = Serial.read(); "
should be: " incomingByte = mySerial.read();"

Make the changes and then tell us the result.

Thank you @ruilviana. Unluckily and as stated in original post, the sensor cannot be moved from where it is. Any other solution?

Where is the FTDI connected?

Original post updated with FTDI

I didn't understand .
Which ESP8266 GPIO are connected to FTDI's RXI and TXO?

FTDI RXI and TXO are directly connected to the ESP8266 TXD and RXD. Also sensor RX and TX are connected to the same RXD and TXD. This is why I am having those issues to receive data through Serial Monitor, but I know it is possible because I have seen it working before with no modifications on the wiring

So why are you using softserial?

PS:
My suggestion to use both devices on the same serial,
is you make sure that one of them stopped using the serial and then use the serial with the other.

@ruilviana I am very new to Arduino and I was trying to use software serial as a solution for baudrates difference, as the Serial baudrate should be 115200 and the sensor baudarate 9600. But I do not know how to have different baudrates in one single Serial

Making it simple and taking one only baudrate of 9600, the following code gives a constant -1:

int incomingByte = 0;

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


void loop()
{
  incomingByte = Serial.read();

    Serial.print("I received: ");
    Serial.println(incomingByte, DEC);

  delay(1000);
}

EDIT: I finally made it work with the above code. One of the pins was not making proper contact.... Now the question is: is there a way to use the tow different baudrates (115200 and 9600), one for the normal serial communication and one for the sensor?

Thank you!

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