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
}
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.
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?
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?