Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // source : https://github.com/BoschSensortec/BSEC-Arduino-library/blob/master/examples/basic/basic.ino
- // didn't add the remotedebug nor the condition print with
- // if (debugremote) and if (debugserial)
- // # ============================================================== ¤ value for fake clock
- int number_loop = 1;
- long time_trigger = 0;
- // # ============================================================== ¤ Header
- //# ------------------------------------------------------ ¤¤ connection wifi
- #include <ESP8266WiFi.h>
- const char *ssid = "...";
- const char *password = "...";
- WiFiClient wifiClient; // needed to call WiFi.begin, WiFi.status, WL_CONNECTED...
- // # ------------------------------------------------------ ¤¤ SETTINGS FOR opensensemap
- // upload to openSenseMap https://electronza.com/h2s-esp8266-opensensemap/3/
- #define upload_to_osm true
- char osmserver[] = "ingress.opensensemap.org";
- // ignore "Deprecation-Warning: If your client supports TLS, please use https://ingress.opensensemap.org"
- // https://ingress.opensensemap.org : need a secure connection
- // api.opensensemap.org : the connection is succesfull but the measurement is not save in the sensebox
- //senseBox ID
- #define SENSEBOX_ID "5dd0db84fb57c1001a104f0f"
- //Sensor IDs
- // Temperature
- #define SENSOR1_ID "5dd12dd1fb57c1001a280b59"
- // Humidity
- #define SENSOR2_ID "5dd12dd1fb57c1001a280b5a"
- // Pressure
- #define SENSOR3_ID "5dd130ddfb57c1001a28eb29"
- // Altitude
- #define SENSOR4_ID "5dd130ddfb57c1001a28eb2a"
- // gas
- #define SENSOR5_ID "5dd130ddfb57c1001a28eb2b"
- // raw IAQ (Indoor Air Quality)
- #define SENSOR6_ID "5dd130ddfb57c1001a28eb2c"
- // IAQ accuracy
- #define SENSOR7_ID "5dd130ddfb57c1001a28eb2d"
- // IAQ Static
- #define SENSOR8_ID "5dd130ddfb57c1001a28eb2e"
- // CO2 estimated
- #define SENSOR9_ID "5dd130ddfb57c1001a28eb2f"
- // VOC estimated
- #define SENSOR10_ID "5dd130ddfb57c1001a28eb30"
- WiFiClient osmclient;
- // # ------------------------------------------------------ ¤¤ set up for sensor BME860
- #include "bsec.h"
- // Helper functions declarations
- void checkIaqSensorStatus(void);
- void errLeds(void);
- // Create an object of the class Bsec
- Bsec iaqSensor;
- String output;
- // Entry point for the example
- void setup(void)
- {
- //# ------------------------------------------------------ ¤¤ initialization serial monitor
- Serial.begin(115200);
- Serial.println("");
- Serial.println("BME680_bsec_basic__opensensemap.ino ");
- Serial.println("with delay 3000 + generic_33v_3s_4d");
- Serial.println("commented many print");
- Wire.begin();
- // # ------------------------------------------------------ ¤¤ initialize bme sensor
- iaqSensor.begin(BME680_I2C_ADDR_PRIMARY, Wire);
- output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
- Serial.println(output);
- checkIaqSensorStatus();
- bsec_virtual_sensor_t sensorList[10] = {
- BSEC_OUTPUT_RAW_TEMPERATURE,
- BSEC_OUTPUT_RAW_PRESSURE,
- BSEC_OUTPUT_RAW_HUMIDITY,
- BSEC_OUTPUT_RAW_GAS,
- BSEC_OUTPUT_IAQ,
- BSEC_OUTPUT_STATIC_IAQ,
- BSEC_OUTPUT_CO2_EQUIVALENT,
- BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
- BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
- BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
- };
- iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
- checkIaqSensorStatus();
- // Print the header
- output = "Timestamp [ms], raw temperature [°C], pressure [hPa], raw relative humidity [%], gas [Ohm], IAQ, IAQ accuracy, temperature [°C], relative humidity [%], Static IAQ, CO2 equivalent, breath VOC equivalent";
- Serial.println(output);
- }
- // Function that is looped forever
- void loop(void)
- {
- unsigned long time_trigger_mill = millis();
- Serial.println(time_trigger_mill);
- time_trigger = 3000 * number_loop;
- Serial.println(time_trigger);
- //iaqSensor.run() : https://github.com/BoschSensortec/BSEC-Arduino-library/blob/cc1493804f69b2c094ef56ab5700e141d07c8295/src/bsec.cpp line 60
- // Check if the time has arrived to call do_steps()
- // then call readProcessData (line 243)
- if (iaqSensor.run(time_trigger))
- { // If new data is available
- output = String(time_trigger);
- output += ", " + String(iaqSensor.rawTemperature);
- output += ", " + String(iaqSensor.pressure);
- output += ", " + String(iaqSensor.rawHumidity);
- output += ", " + String(iaqSensor.gasResistance);
- output += ", " + String(iaqSensor.iaq);
- output += ", " + String(iaqSensor.iaqAccuracy);
- output += ", " + String(iaqSensor.temperature);
- output += ", " + String(iaqSensor.humidity);
- output += ", " + String(iaqSensor.staticIaq);
- output += ", " + String(iaqSensor.co2Equivalent);
- output += ", " + String(iaqSensor.breathVocEquivalent);
- Serial.println(output);
- // # ------------------------------------------------------ ¤¤ upload to openSenseMap
- if (upload_to_osm)
- {
- // Read the sensors and upload them via postFloatValue
- // replace with variable : ??
- // float temperature = sensor.readTemperature();
- // postFloatValue(temperature, 1, temperatureSensorID);
- //2nd argument : seems to be decimal : 0 = int / 3 = float with 3 decimal
- // missingn IAQ Static SENSOR8_ID CO2 estimated SENSOR9_ID VOC estimated SENSOR10_ID
- // // post data after data but sometimes "too mnay request"
- // postFloatValue(iaqSensor.temperature, 3, SENSOR1_ID);
- // postFloatValue(iaqSensor.humidity, 3, SENSOR2_ID);
- // postFloatValue(iaqSensor.pressure, 3, SENSOR3_ID);
- // postFloatValue(iaqSensor.gasResistance, 3, SENSOR5_ID);
- // postFloatValue(iaqSensor.iaq, 3, SENSOR6_ID);
- // postFloatValue(iaqSensor.iaqAccuracy, 3, SENSOR7_ID);
- // https://docs.opensensemap.org/#api-Measurements-postNewMeasurements : format = JSON-Array
- // ingress.opensensemap.org/boxes/:senseBoxId/data
- // content-type: application/json
- // [
- // {"sensor":"sensorID", "value":"value"},
- // {"sensor":"anotherSensorId", "value":"value", "createdAt": "RFC 3339-timestamp", "location": [lng,lat,height]}
- // ...
- // ]
- int digits = 3;
- char obstemperature[10];
- dtostrf(iaqSensor.temperature, 5, digits, obstemperature);
- char obshumidity[10];
- dtostrf(iaqSensor.humidity, 5, digits, obshumidity);
- char obspressure[10];
- dtostrf(iaqSensor.pressure, 5, digits, obspressure);
- char obstgasResistance[10];
- dtostrf(iaqSensor.gasResistance, 5, digits, obstgasResistance);
- char obsiaq[10];
- dtostrf(iaqSensor.iaq, 5, digits, obsiaq);
- char obsiaqAccuracy[10];
- dtostrf(iaqSensor.iaqAccuracy, 5, digits, obsiaqAccuracy);
- // Create Json
- // // Create Json
- // String jsonValue = "{\"value\":";
- // jsonValue += obs;
- // jsonValue += "}";
- String jsonValue = "[{\"sensor\":\"";
- jsonValue += SENSOR1_ID;
- jsonValue += "\",\"value\":\"";
- jsonValue += obstemperature;
- jsonValue += "\"},";
- jsonValue += "{\"sensor\":\"";
- jsonValue += SENSOR2_ID;
- jsonValue += "\",\"value\":\"";
- jsonValue += obshumidity;
- jsonValue += "\"},";
- jsonValue += "{\"sensor\":\"";
- jsonValue += SENSOR3_ID;
- jsonValue += "\",\"value\":\"";
- jsonValue += obspressure;
- jsonValue += "\"},";
- jsonValue += "{\"sensor\":\"";
- jsonValue += SENSOR5_ID;
- jsonValue += "\",\"value\":\"";
- jsonValue += obstgasResistance;
- jsonValue += "\"},";
- jsonValue += "{\"sensor\":\"";
- jsonValue += SENSOR6_ID;
- jsonValue += "\",\"value\":\"";
- jsonValue += obsiaq;
- jsonValue += "\"},";
- jsonValue += "{\"sensor\":\"";
- jsonValue += SENSOR7_ID;
- jsonValue += "\",\"value\":\"";
- jsonValue += obsiaqAccuracy;
- jsonValue += "\"}]";
- Serial.println("Connecting to OSeM Server...");
- if (osmclient.connect(osmserver, 80))
- {
- Serial.println("connected!");
- // Build HTTP headers
- osmclient.print("POST /boxes/");
- osmclient.print(SENSEBOX_ID);
- osmclient.print("/data");
- // osmclient.print(sensorId);
- osmclient.println(" HTTP/1.1");
- osmclient.print("Host:");
- osmclient.println(osmserver);
- osmclient.println("Content-Type: application/json");
- osmclient.println("Connection: close");
- osmclient.print("Content-Length: ");
- osmclient.println(jsonValue.length());
- osmclient.println();
- //Daten senden
- osmclient.println(jsonValue);
- }
- else
- {
- Serial.println("failed!");
- }
- // Show answer from server in the serial monitor
- waitForServerResponse();
- }
- //delay(3000); // added https://community.bosch-sensortec.com/t5/MEMS-sensors-forum/BME680-warning-code-2-and-BME680-error-code-2-on-NodeMcu-esp8266/m-p/10524/highlight/true#M1869
- number_loop += 1;
- }
- else
- {
- checkIaqSensorStatus();
- }
- }
- // # ============================================================== ¤ Helper function definitions
- // # ------------------------------------------------------ ¤¤ checkIaqSensorStatus
- void checkIaqSensorStatus(void)
- {
- if (iaqSensor.status != BSEC_OK)
- {
- if (iaqSensor.status < BSEC_OK)
- {
- output = "BSEC error code : " + String(iaqSensor.status);
- Serial.println(output);
- for (;;)
- errLeds(); /* Halt in case of failure */
- }
- else
- {
- output = "BSEC warning code : " + String(iaqSensor.status);
- Serial.println(output);
- }
- }
- if (iaqSensor.bme680Status != BME680_OK)
- {
- if (iaqSensor.bme680Status < BME680_OK)
- {
- output = "BME680 error code : " + String(iaqSensor.bme680Status);
- Serial.println(output);
- for (;;)
- errLeds(); /* Halt in case of failure */
- }
- else
- {
- output = "BME680 warning code : " + String(iaqSensor.bme680Status);
- Serial.println(output);
- }
- }
- }
- // # ------------------------------------------------------ ¤¤ errLeds
- void errLeds(void)
- {
- pinMode(LED_BUILTIN, OUTPUT);
- digitalWrite(LED_BUILTIN, HIGH);
- delay(100);
- digitalWrite(LED_BUILTIN, LOW);
- delay(100);
- }
- // # ============================================================== ¤ opensensemap upload function
- // (given by opensensemap when you create a new sensebox in your account)
- // # ------------------------------------------------------ ¤¤ send the data to opensensemap as json
- void postFloatValue(float measurement, int digits, String sensorId)
- {
- // Convert Float to String
- char obs[10];
- dtostrf(measurement, 5, digits, obs);
- // Create Json
- String jsonValue = "{\"value\":";
- jsonValue += obs;
- jsonValue += "}";
- // Connect to OSeM Server and perform POST operation
- Serial.println("Connecting to OSeM Server...");
- if (osmclient.connect(osmserver, 80))
- {
- Serial.println("connected!");
- // Build HTTP headers
- osmclient.print("POST /boxes/");
- osmclient.print(SENSEBOX_ID);
- osmclient.print("/");
- osmclient.print(sensorId);
- osmclient.println(" HTTP/1.1");
- osmclient.print("Host:");
- osmclient.println(osmserver);
- osmclient.println("Content-Type: application/json");
- osmclient.println("Connection: close");
- osmclient.print("Content-Length: ");
- osmclient.println(jsonValue.length());
- osmclient.println();
- //Daten senden
- osmclient.println(jsonValue);
- }
- else
- {
- Serial.println("failed!");
- }
- // Show answer from server in the serial monitor
- waitForServerResponse();
- }
- // # ------------------------------------------------------ ¤¤ waitForServerResponse
- void waitForServerResponse()
- {
- // Output incoming bytes
- boolean repeat = true;
- do
- {
- if (osmclient.available())
- {
- char c = osmclient.read();
- Serial.print(c);
- }
- // End connection
- if (!osmclient.connected())
- {
- Serial.println("Disconnecting.");
- osmclient.stop();
- repeat = false;
- }
- } while (repeat);
- }
- //# ============================================================== ¤ wifi connection
- void setup_wifi()
- {
- delay(10);
- // info display in debug
- Serial.println();
- Serial.print("Connecting to ");
- Serial.println(ssid);
- // connect to wifi define above
- WiFi.begin(ssid, password);
- // keep trying to connect while not connected
- while (WiFi.status() != WL_CONNECTED)
- {
- delay(500);
- Serial.print(".");
- }
- // print that connection is succesful
- Serial.println("");
- Serial.println("WiFi connexion OK ");
- Serial.print("=> Addresse IP : ");
- Serial.print(WiFi.localIP());
- WiFi.setAutoReconnect(true);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement