Alright, here is the Arduino code, still a bit messy, sorry:
//Includes and defines for BME280
#include <Wire.h>
#include <SPI.h>
#include <Adafruit_Sensor.h>
#include <Adafruit_BME280.h>
#define BME_SCK 13
#define BME_MISO 12
#define BME_MOSI 11
#define BME_CS 10
#define SEALEVELPRESSURE_HPA (1013.25)
//Includes and define for MQ135
#define anInput A0 //analog feed from MQ135
#define digTrigger 2 //digital feed from MQ135
#define co2Zero 0 //calibrated CO2 0 level
Adafruit_BME280 bme; // I2C
//Adafruit_BME280 bme(BME_CS); // hardware SPI
//Adafruit_BME280 bme(BME_CS, BME_MOSI, BME_MISO, BME_SCK); // software SPI
unsigned long delayTime;
String postmessage = "";
float value01 = 0;
float value02 = 0;
float value03 = 0;
float value04 = 0;
float value05 = 0;
void setup() {
Serial.begin(115200);
pinMode(anInput,INPUT); //MQ135 analog feed set for input
pinMode(digTrigger,INPUT); //MQ135 digital feed set for input
while(!Serial); // time to get serial running
Serial.println(F("BME280 test"));
unsigned status;
// default settings
status = bme.begin(0x76);
// You can also pass in a Wire library object like &Wire2
// status = bme.begin(0x76, &Wire2)
if (!status) {
Serial.println("Could not find a valid BME280 sensor, check wiring, address, sensor ID!");
Serial.print("SensorID was: 0x"); Serial.println(bme.sensorID(),16);
Serial.print(" ID of 0xFF probably means a bad address, a BMP 180 or BMP 085\n");
Serial.print(" ID of 0x56-0x58 represents a BMP 280,\n");
Serial.print(" ID of 0x60 represents a BME 280.\n");
Serial.print(" ID of 0x61 represents a BME 680.\n");
while (1) delay(10);
}
Serial.println("-- Default Test --");
delayTime = 10000;
Serial.println();
}
void loop() {
int co2now[10]; //int array for co2 readings
int co2raw = 0; //int for raw value of co2
int co2comp = 0; //int for compensated co2
int co2ppm = 0; //int for calculated ppm
int zzz = 0; //int for averaging
for (int x = 0;x<10;x++){ //samplpe co2 10x over 2 seconds
co2now[x]=analogRead(A0);
delay(200);
}
for (int x = 0;x<10;x++){ //add samples together
zzz=zzz + co2now[x];
}
co2raw = zzz/10; //divide samples by 10
co2comp = co2raw - co2Zero; //get compensated value
value01 = 0;
value02 = 0;
value03 = 0;
value04 = 0;
value05 = 0;
value01 = value01 + bme.readTemperature();
value02 = value02 + (bme.readPressure() / 100.0F);
value03 = value03 + (bme.readAltitude(SEALEVELPRESSURE_HPA));
value04 = value04 + (bme.readHumidity());
value05 = value05 + co2comp;
/// Serial & PHP Communication ///
postmessage = "value01=";
//postmessage += bme.readTemperature();
postmessage += value01;
postmessage += "&value02=";
//postmessage += "AA";
postmessage += value02;
// postmessage += bme.readPressure() / 100.0F;
postmessage += "&value03=";
//postmessage += bme.readAltitude(SEALEVELPRESSURE_HPA);
postmessage += value03;
postmessage += "&value04=";
//postmessage += bme.readHumidity();
postmessage += value04;
postmessage += "&value05=";
//postmessage += co2comp;
postmessage += value05;
// printValues();
// Serial.print("CO2 = ");
// Serial.print(co2comp);
// Serial.println(" PPM");
// Serial.println();
Serial.println(postmessage);
// Serial.println();
delay(10000);
}
As described before, it captures sensor data and creates a string "postmessage", this string goes on the serial and I get the following at BAUD 115200 every 10 seconds:
BME280 test
-- Default Test --
value01=22.99&value02=984.58&value03=241.50&value04=51.90&value05=263.00
value01=22.99&value02=984.53&value03=241.89&value04=52.07&value05=261.00
value01=23.05&value02=984.53&value03=241.90&value04=51.88&value05=258.00
value01=23.03&value02=984.53&value03=241.91&value04=51.73&value05=256.00
value01=23.08&value02=984.53&value03=241.91&value04=51.84&value05=256.00
value01=23.06&value02=984.52&value03=242.03&value04=51.70&value05=256.00
value01=23.10&value02=984.56&value03=241.65&value04=52.01&value05=257.00
This string is supposed to be added to the code of the ESP for php and sent per POST to a webserver.
As above looks great, I need to read it with the ESP, the used code is (just the important reading part of the code):
void loop() {
if (Serial.available() > 0) {
// postmessage = Serial.read(); // read the incoming byte:
postmessage = Serial.readString();
// postmessage = "value01=10&value02=20&value03=30&value04=40&value05=50";
}
A simple Serial.readString(); I use this in a different project with a Mega+WiFi board and it is working. But in this project I get for some reason no good reading.
Reading Serial at BAUD 115200 for the ESP, I get:
httpRequestData: api_key=ARAYA&⸮⸮
HTTP Response code: 200
httpRequestData: api_key=ARAYA&⸮⸮
HTTP Response code: 200
The wrong-sided question marks are supposed to be the Arduino serial "postmessage", as if the BAUD is wrong but it is set everywhere to 115200.
Appreciate any feedback.