Serial node reading: Arduino IDE string code problem

Dear all.

I have a piece of code that is quite stubborn and I can't seem to find the problem.
I am using an emergency ESP8266 as my nano showed the white-magic-mist-of-disappointment
The code:

#include <Wire.h>
#include <Adafruit_Sensor.h>
#include "Adafruit_BMP3XX.h"
#include <ArduinoJson.h>

#define SEALEVELPRESSURE_HPA (1013.25)

Adafruit_BMP3XX bmp3xx;

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

  bmp3xx.begin_I2C(0x76);

  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() {
  StaticJsonDocument<128> doc;

  bmp3xx.performReading();

  doc["Temperature bmp3xx"] = bmp3xx.temperature;
  doc["Pressure bmp3xx"] = bmp3xx.pressure / 100.0;

  String jsonString;
  serializeJson(doc, jsonString);

  Serial.println(jsonString);
  Serial.println();

  delay(1000);
}

The problem:

Why does this happen?

Same problem with:

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

Adafruit_BME680 bme680; // Create an instance of the BME680 sensor

void setup() {
  Serial.begin(115200); // Initialize serial communication
  bme680.begin(); // Initialize the BME680 sensor

  // Configure 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);
}

void loop() {
  bme680.performReading(); // Perform sensor reading

  // Create a JSON object to hold the sensor values
  StaticJsonDocument<256> jsonDoc;

  // Create an object to hold the bme680 sensor values
  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;

  // 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
}

This is my debug:

"nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�nb�nl`�rl�l��l�n..."

I grabbed my Arduino 2560 mega pro and uploaded the code. This works like a charm:

#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
}

Could it be that my ESP8266 cannot use this code?

looks like an incorrect comms config at first glance. Are you certain you have the right settings for the comms (stop bits, baud, parity etc)

Yes! Because it works on the arduino 2560 mega pro!

Well if the code runs correctly in one device but not another then it isn't a node red problem. It must be the device.

True, but Isn’t it a common thing to use USB to send data from a microcontroller to pi?

(Except the wifi/mqtt functionality ofc)

Yes, but you said it works on the other device.

But I thought you were using serial, not usb.

I was today years old when i learned that this is different.

But yes, usb!

I 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
image

I think you mean serial via a serial to usb adaptor (which may be built into the device). Otherwise you would not be seeing the serial settings.

Again, though, if it works on one device and not on another then it must be differences in the device.

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