Ok. I’ve got it working. The code need’s improvement but it works and I’ve saved a backup. Actually two backups.
I’m going to change the doubles to floats, and replace that delay() with a software timer or millis() like they use in the Particle reference. A lot of it is taken from sample code (credit: Adafruit) but I analyzed it as I went so I can more easily use this micro-controller, sensor, I2C, etc in the future.
/*
* Project IOT_RemoteSensor_00
* Description:
* Author: garthenar
* Date: 04/2021
*/
#include "Particle.h"
#include <Ubidots.h>
#include "Adafruit_BME680.h"
//For BME680 ---------------
#define SEALEVELPRESSURE_HPA (1013.25)
Adafruit_BME680 bme; // I2C
//--------------------------
int boardLED = D7;
int interuptPin = D5;
int ledInteruptTriggered = 0;
int floodSensor = D3;
int itsFlooding = 0;
//int floodCountInterval = 100;
//For BME680 ---------------
double temperatureInC = 0;
double relativeHumidity = 0;
double pressureHpa = 0;
double gasResistanceKOhms = 0;
double approxAltitudeInM = 0;
//---------------------------
void setup() {
pinMode(boardLED, OUTPUT);
pinMode(interuptPin, INPUT);
attachInterrupt(interuptPin,ledInterupt,RISING);
pinMode(floodSensor, INPUT);
attachInterrupt(floodSensor,floodInterrupt, FALLING);
//For BME680 ---------------
if (!bme.begin()) {
Particle.publish("Log", "Could not find a valid BME680 sensor, check wiring!");
} else {
Particle.publish("Log", "bme.begin() success =)");
// Set up oversampling and filter initialization
bme.setTemperatureOversampling(BME680_OS_8X);
bme.setHumidityOversampling(BME680_OS_2X);
bme.setPressureOversampling(BME680_OS_4X);
bme.setIIRFilterSize(BME680_FILTER_SIZE_3);
bme.setGasHeater(320, 150); // 320*C for 150 ms
Particle.variable("temperature", &temperatureInC, DOUBLE);
Particle.variable("humidity", &relativeHumidity, DOUBLE);
Particle.variable("pressure", &pressureHpa, DOUBLE);
Particle.variable("gas", &gasResistanceKOhms, DOUBLE);
Particle.variable("altitude", &approxAltitudeInM, DOUBLE);
}
//---------------------------
}
void loop() {
if (ledInteruptTriggered == 1){
if(Particle.publish("Button_Pushed", PRIVATE) == true){
ledInteruptTriggered = 0;
blink_boardLED(2,0.25);
}
else{
Particle.publish("Shit went Wrong");
ledInteruptTriggered = 0;
}
}
if (itsFlooding == 1) {
if(Particle.publish("It's_Flooding", PRIVATE) == true){
itsFlooding = 0;
//floodCountInterval = 100;
}
else{
Particle.publish("Flood Sensor Error");
itsFlooding = 0;
}
}
//floodCountInterval -= 1; //Work on this. (making sure it's not pinging every second it's flooding)
//For BME680 ---------------
if (! bme.performReading()) {
Particle.publish("Log", "Failed to perform reading :(");
} else {
temperatureInC = bme.temperature;
relativeHumidity = bme.humidity;
pressureHpa = bme.pressure / 100.0;
gasResistanceKOhms = bme.gas_resistance / 1000.0;
approxAltitudeInM = bme.readAltitude(SEALEVELPRESSURE_HPA);
String data = String::format(
"{"
"\"temperatureInC\":%.2f,"
"\"humidityPercentage\":%.2f,"
"\"pressureHpa\":%.2f,"
"\"gasResistanceKOhms\":%.2f"
"\"approxAltitudeInM\":%.2f"
"}",
temperatureInC,
relativeHumidity,
pressureHpa,
gasResistanceKOhms,
approxAltitudeInM);
Particle.publish("Sensor", data, 60, PRIVATE);
}
delay(10 * 1000);
//---------------------------
}
int blink_boardLED(int times, float seconds) {
for(int i = 0; i <times; i++) {
digitalWrite(boardLED, HIGH);
delay(seconds*1000.0);
digitalWrite(boardLED,LOW);
delay(seconds*1000.0);
}
return 0;
}
void ledInterupt(void) {
ledInteruptTriggered = 1;
}
void floodInterrupt(void) {
itsFlooding = 1;
}