Arduino mega pro vs ESP8266: Code for serial communication problems

I was using an Arduino Nano for my project. This was working fine (code also worked like a charm) but I made a mistake and saw the Magic-Smoke-Of-Disappointment coming out of my Nano.

So I searched for a new board, my ESP8266, and finished the final piece of code.

When I tried the code I got strange errors. I thought it was my code (in the final piece I wrote). But to ensure that that was cause I uploaded the code to an Arduino mega 2560 pro. (this is actually in use for other projects so I cant permanently use it for this situation - I just borrowed it)

The code seemed to work!

But when I switched back to the ESP8266 it failed again.

Could someone help me to explain why?

The code:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME680.h>
#include <Adafruit_BMP3XX.h>
#include <Adafruit_MPRLS.h>
#include <ArduinoJson.h>


Adafruit_BME680 bme680; // Create an instance of the BME680 sensor
Adafruit_BMP3XX bmp3xx;
Adafruit_MPRLS mpr;

void setup() {
  Serial.begin(115200); // Initialize serial communication

  // Initialize the sensors
  bme680.begin();
  bmp3xx.begin_I2C(0x76);
  mpr.begin();

  // Configure BME680 sensor settings
  bme680.setTemperatureOversampling(BME680_OS_4X);
  bme680.setHumidityOversampling(BME680_OS_4X);
  bme680.setPressureOversampling(BME680_OS_4X);
  bme680.setIIRFilterSize(BME680_FILTER_SIZE_3);
  bme680.setGasHeater(320, 150);

  // Configure BMP3XX sensor settings
  bmp3xx.setTemperatureOversampling(BMP3_OVERSAMPLING_4X);
  bmp3xx.setPressureOversampling(BMP3_OVERSAMPLING_4X);
  bmp3xx.setIIRFilterCoeff(BMP3_IIR_FILTER_COEFF_3);
  bmp3xx.setOutputDataRate(BMP3_ODR_50_HZ);
}

void loop() {
  // Create a JSON object to hold the sensor values
  StaticJsonDocument<512> jsonDoc;

  // Read BME680 sensor values
  bme680.performReading();
  JsonObject bme680Obj = jsonDoc.createNestedObject("bme680");
  bme680Obj["temperature"] = bme680.temperature;
  bme680Obj["pressure"] = bme680.pressure / 100.0;
  bme680Obj["humidity"] = bme680.humidity;
  bme680Obj["gas"] = bme680.gas_resistance / 1000.0;

  // Read BMP3XX sensor values
  bmp3xx.performReading();
  JsonObject bmp3xxObj = jsonDoc.createNestedObject("bmp3xx");
  bmp3xxObj["temperature"] = bmp3xx.temperature;
  bmp3xxObj["pressure"] = bmp3xx.pressure / 100.0;

  // Read MPRLS sensor values
  float pressure_hPa = mpr.readPressure();
  JsonObject mprlsObj = jsonDoc.createNestedObject("mprls");
  mprlsObj["pressure_hPa"] = pressure_hPa;
  mprlsObj["pressure_PSI"] = pressure_hPa / 68.947572932;

  // Serialize the JSON object to a string
  String jsonString;
  serializeJson(jsonDoc, jsonString);

  // Send the JSON string through serial
  Serial.println(jsonString);

  delay(1000); // Delay for 1 second before the next reading
}

The error when reading serial data using ESP8266:

The same code but Arduino mega pro:

Hello wbr

Check the logic voltage levels used for the system.

Have a nice day and enjoy coding in C++.

Everything is running with 3.3V
Both boards were powered through USB...

Do you spot a problem here? :thinking:

Maybe just print out your sensor readings directly to Serial to debug rather than going to all the trouble to put them in JSON format. Do the readings come out correct? if so, then maybe the issue is with creating the JSON. If not, there is an issue with the sensors and your ESP2866.

The format is correct and functional. Works when I use a different microcontroller (the Arduino mega 2560)

The sensors are functional

The sensors also show correct values and behaviour when using with the ESP8266 and the serial monitor from Arduino IDE

But when I disconnect the USB cable from my PC and insert it to my Raspberry Pi then the serial acts strange

When I disconnect the ESP8266 and connect it to the Arduino 2560, the serial behaves correct.

Same happens with my other ESP8266 boards so this is not a faulty one....

That does not prove it will work on the ESP8266. The code looks the same, but compiles to very different code for different controllers.

so...
ESP8266 to PC = works
ESP8266 to RPi = not working
ESP8266 to Arduino 2560 = works - how do you know this? Does the Arduino 2560 forward the info on to the Serial Monitor? (The Arduino 2560 is connected to your PC)

The rPi operates at 3.3V but the Arduino operates at 5V. Are you voltage shifting when mixing/matching these boards?

1 Like

Not 100% (maybe I was a bit vague)

With one and the same code:
ESP8266 to PC = works
Arduino mega to pc = works
ESP8266 to RPi = not working
Arduino to RPi = works
ESP8266 to Arduino 2560 = never tested this.

Both boards are powered by usb from my pc
Or the board is powered by usb from pi

All the sensors are connected to 3.3v. This is the case for arduino 2560 and esp8266
The SDA and SCL are correctly wired in every scenario

How are you powering all this? When you have ESP8266 to RPi, or Arduino to RPi, do you also have it connected to your PC? I'm just wondering if your PC USB port can supply enough power but the RPi can not?

Nope. Both situations the usb provides enough power :smiley:

Found the problem…
I used a different ESP8266 board library in Arduino IDE: The "Generic ESP8266 module" (wrong)

Turns out NodeMCU 1.0 is the correct version

I feel quite stupid

Thx for help all! It did help me look in the right direction!

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