Advertisement
gyhn

Untitled

Nov 22nd, 2019
740
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 13.97 KB | None | 0 0
  1.  
  2. // source : https://github.com/BoschSensortec/BSEC-Arduino-library/blob/master/examples/basic/basic.ino
  3.  
  4. // didn't add the remotedebug nor the condition print with
  5. //  if (debugremote)  and         if (debugserial)
  6.  
  7. // # ============================================================== ¤ value for fake clock
  8. int number_loop = 1;
  9. long time_trigger = 0;
  10.  
  11. // # ============================================================== ¤ Header
  12. //# ------------------------------------------------------ ¤¤ connection wifi
  13. #include <ESP8266WiFi.h>
  14.  
  15. const char *ssid = "...";
  16. const char *password = "...";
  17. WiFiClient wifiClient; // needed to call  WiFi.begin, WiFi.status, WL_CONNECTED...
  18.  
  19. // # ------------------------------------------------------ ¤¤  SETTINGS FOR opensensemap
  20. // upload to openSenseMap https://electronza.com/h2s-esp8266-opensensemap/3/
  21.  
  22. #define upload_to_osm true
  23. char osmserver[] = "ingress.opensensemap.org";
  24. // ignore "Deprecation-Warning: If your client supports TLS, please use https://ingress.opensensemap.org"
  25. // https://ingress.opensensemap.org  : need a secure connection
  26. //  api.opensensemap.org  : the connection is succesfull but the measurement is not save in the sensebox
  27.  
  28. //senseBox ID
  29. #define SENSEBOX_ID "5dd0db84fb57c1001a104f0f"
  30.  
  31. //Sensor IDs
  32. // Temperature
  33. #define SENSOR1_ID "5dd12dd1fb57c1001a280b59"
  34. // Humidity
  35. #define SENSOR2_ID "5dd12dd1fb57c1001a280b5a"
  36. // Pressure
  37. #define SENSOR3_ID "5dd130ddfb57c1001a28eb29"
  38. // Altitude
  39. #define SENSOR4_ID "5dd130ddfb57c1001a28eb2a"
  40. // gas
  41. #define SENSOR5_ID "5dd130ddfb57c1001a28eb2b"
  42. // raw IAQ  (Indoor Air Quality)
  43. #define SENSOR6_ID "5dd130ddfb57c1001a28eb2c"
  44. // IAQ accuracy
  45. #define SENSOR7_ID "5dd130ddfb57c1001a28eb2d"
  46. // IAQ Static
  47. #define SENSOR8_ID "5dd130ddfb57c1001a28eb2e"
  48. // CO2 estimated
  49. #define SENSOR9_ID "5dd130ddfb57c1001a28eb2f"
  50. // VOC estimated
  51. #define SENSOR10_ID "5dd130ddfb57c1001a28eb30"
  52.  
  53. WiFiClient osmclient;
  54.  
  55. // # ------------------------------------------------------ ¤¤ set up for sensor BME860
  56.  
  57. #include "bsec.h"
  58.  
  59. // Helper functions declarations
  60. void checkIaqSensorStatus(void);
  61. void errLeds(void);
  62.  
  63. // Create an object of the class Bsec
  64. Bsec iaqSensor;
  65.  
  66. String output;
  67.  
  68. // Entry point for the example
  69. void setup(void)
  70. {
  71.  
  72.     //# ------------------------------------------------------ ¤¤ initialization serial monitor
  73.     Serial.begin(115200);
  74.     Serial.println("");
  75.     Serial.println("BME680_bsec_basic__opensensemap.ino  ");
  76.     Serial.println("with delay 3000 + generic_33v_3s_4d");
  77.     Serial.println("commented many print");
  78.     Wire.begin();
  79.  
  80.     // # ------------------------------------------------------ ¤¤ initialize bme sensor
  81.  
  82.     iaqSensor.begin(BME680_I2C_ADDR_PRIMARY, Wire);
  83.     output = "\nBSEC library version " + String(iaqSensor.version.major) + "." + String(iaqSensor.version.minor) + "." + String(iaqSensor.version.major_bugfix) + "." + String(iaqSensor.version.minor_bugfix);
  84.     Serial.println(output);
  85.     checkIaqSensorStatus();
  86.  
  87.     bsec_virtual_sensor_t sensorList[10] = {
  88.         BSEC_OUTPUT_RAW_TEMPERATURE,
  89.         BSEC_OUTPUT_RAW_PRESSURE,
  90.         BSEC_OUTPUT_RAW_HUMIDITY,
  91.         BSEC_OUTPUT_RAW_GAS,
  92.         BSEC_OUTPUT_IAQ,
  93.         BSEC_OUTPUT_STATIC_IAQ,
  94.         BSEC_OUTPUT_CO2_EQUIVALENT,
  95.         BSEC_OUTPUT_BREATH_VOC_EQUIVALENT,
  96.         BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_TEMPERATURE,
  97.         BSEC_OUTPUT_SENSOR_HEAT_COMPENSATED_HUMIDITY,
  98.     };
  99.  
  100.     iaqSensor.updateSubscription(sensorList, 10, BSEC_SAMPLE_RATE_LP);
  101.     checkIaqSensorStatus();
  102.  
  103.     // Print the header
  104.     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";
  105.     Serial.println(output);
  106. }
  107.  
  108. // Function that is looped forever
  109. void loop(void)
  110. {
  111.  
  112.     unsigned long time_trigger_mill = millis();
  113.     Serial.println(time_trigger_mill);
  114.     time_trigger = 3000 * number_loop;
  115.     Serial.println(time_trigger);
  116.  
  117.     //iaqSensor.run()  :  https://github.com/BoschSensortec/BSEC-Arduino-library/blob/cc1493804f69b2c094ef56ab5700e141d07c8295/src/bsec.cpp  line 60
  118.     // Check if the time has arrived to call do_steps()
  119.     // then call readProcessData  (line 243)
  120.  
  121.     if (iaqSensor.run(time_trigger))
  122.     { // If new data is available
  123.         output = String(time_trigger);
  124.         output += ", " + String(iaqSensor.rawTemperature);
  125.         output += ", " + String(iaqSensor.pressure);
  126.         output += ", " + String(iaqSensor.rawHumidity);
  127.         output += ", " + String(iaqSensor.gasResistance);
  128.         output += ", " + String(iaqSensor.iaq);
  129.         output += ", " + String(iaqSensor.iaqAccuracy);
  130.         output += ", " + String(iaqSensor.temperature);
  131.         output += ", " + String(iaqSensor.humidity);
  132.         output += ", " + String(iaqSensor.staticIaq);
  133.         output += ", " + String(iaqSensor.co2Equivalent);
  134.         output += ", " + String(iaqSensor.breathVocEquivalent);
  135.         Serial.println(output);
  136.  
  137.         // # ------------------------------------------------------ ¤¤ upload to openSenseMap
  138.         if (upload_to_osm)
  139.         {
  140.  
  141.             // Read the sensors  and upload them via postFloatValue
  142.             // replace with variable : ??
  143.             // float temperature = sensor.readTemperature();
  144.             // postFloatValue(temperature, 1, temperatureSensorID);
  145.             //2nd argument : seems to be decimal : 0 = int / 3 = float with 3 decimal
  146.             //  missingn  IAQ Static SENSOR8_ID     CO2 estimated   SENSOR9_ID    VOC estimated  SENSOR10_ID
  147.  
  148.             // // post data after data but sometimes "too mnay request"
  149.             // postFloatValue(iaqSensor.temperature, 3, SENSOR1_ID);
  150.             // postFloatValue(iaqSensor.humidity, 3, SENSOR2_ID);
  151.             // postFloatValue(iaqSensor.pressure, 3, SENSOR3_ID);
  152.             // postFloatValue(iaqSensor.gasResistance, 3, SENSOR5_ID);
  153.             // postFloatValue(iaqSensor.iaq, 3, SENSOR6_ID);
  154.             // postFloatValue(iaqSensor.iaqAccuracy, 3, SENSOR7_ID);
  155.  
  156.             // https://docs.opensensemap.org/#api-Measurements-postNewMeasurements : format = JSON-Array
  157.             // ingress.opensensemap.org/boxes/:senseBoxId/data
  158.             // content-type: application/json
  159.             // [
  160.             //   {"sensor":"sensorID", "value":"value"},
  161.             //   {"sensor":"anotherSensorId", "value":"value", "createdAt": "RFC 3339-timestamp", "location": [lng,lat,height]}
  162.             //   ...
  163.             // ]
  164.             int digits = 3;
  165.             char obstemperature[10];
  166.             dtostrf(iaqSensor.temperature, 5, digits, obstemperature);
  167.             char obshumidity[10];
  168.             dtostrf(iaqSensor.humidity, 5, digits, obshumidity);
  169.             char obspressure[10];
  170.             dtostrf(iaqSensor.pressure, 5, digits, obspressure);
  171.             char obstgasResistance[10];
  172.             dtostrf(iaqSensor.gasResistance, 5, digits, obstgasResistance);
  173.             char obsiaq[10];
  174.             dtostrf(iaqSensor.iaq, 5, digits, obsiaq);
  175.             char obsiaqAccuracy[10];
  176.             dtostrf(iaqSensor.iaqAccuracy, 5, digits, obsiaqAccuracy);
  177.  
  178.             // Create Json
  179.  
  180.             // // Create Json
  181.             // String jsonValue = "{\"value\":";
  182.             // jsonValue += obs;
  183.             // jsonValue += "}";
  184.  
  185.             String jsonValue = "[{\"sensor\":\"";
  186.             jsonValue += SENSOR1_ID;
  187.             jsonValue += "\",\"value\":\"";
  188.             jsonValue += obstemperature;
  189.             jsonValue += "\"},";
  190.  
  191.             jsonValue += "{\"sensor\":\"";
  192.             jsonValue += SENSOR2_ID;
  193.             jsonValue += "\",\"value\":\"";
  194.             jsonValue += obshumidity;
  195.             jsonValue += "\"},";
  196.  
  197.             jsonValue += "{\"sensor\":\"";
  198.             jsonValue += SENSOR3_ID;
  199.             jsonValue += "\",\"value\":\"";
  200.             jsonValue += obspressure;
  201.             jsonValue += "\"},";
  202.  
  203.             jsonValue += "{\"sensor\":\"";
  204.             jsonValue += SENSOR5_ID;
  205.             jsonValue += "\",\"value\":\"";
  206.             jsonValue += obstgasResistance;
  207.             jsonValue += "\"},";
  208.  
  209.             jsonValue += "{\"sensor\":\"";
  210.             jsonValue += SENSOR6_ID;
  211.             jsonValue += "\",\"value\":\"";
  212.             jsonValue += obsiaq;
  213.             jsonValue += "\"},";
  214.  
  215.             jsonValue += "{\"sensor\":\"";
  216.             jsonValue += SENSOR7_ID;
  217.             jsonValue += "\",\"value\":\"";
  218.             jsonValue += obsiaqAccuracy;
  219.             jsonValue += "\"}]";
  220.  
  221.             Serial.println("Connecting to OSeM Server...");
  222.  
  223.             if (osmclient.connect(osmserver, 80))
  224.             {
  225.  
  226.                 Serial.println("connected!");
  227.  
  228.                 // Build HTTP headers
  229.                 osmclient.print("POST /boxes/");
  230.                 osmclient.print(SENSEBOX_ID);
  231.                 osmclient.print("/data");
  232.                 // osmclient.print(sensorId);
  233.                 osmclient.println(" HTTP/1.1");
  234.                 osmclient.print("Host:");
  235.                 osmclient.println(osmserver);
  236.                 osmclient.println("Content-Type: application/json");
  237.                 osmclient.println("Connection: close");
  238.                 osmclient.print("Content-Length: ");
  239.                 osmclient.println(jsonValue.length());
  240.                 osmclient.println();
  241.                 //Daten senden
  242.                 osmclient.println(jsonValue);
  243.             }
  244.             else
  245.             {
  246.                 Serial.println("failed!");
  247.             }
  248.             // Show answer from server in the serial monitor
  249.             waitForServerResponse();
  250.         }
  251.         //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
  252.         number_loop += 1;
  253.     }
  254.     else
  255.     {
  256.         checkIaqSensorStatus();
  257.     }
  258. }
  259.  
  260. // # ============================================================== ¤  Helper function definitions
  261.  
  262. // # ------------------------------------------------------ ¤¤  checkIaqSensorStatus
  263. void checkIaqSensorStatus(void)
  264. {
  265.     if (iaqSensor.status != BSEC_OK)
  266.     {
  267.         if (iaqSensor.status < BSEC_OK)
  268.         {
  269.             output = "BSEC error code : " + String(iaqSensor.status);
  270.             Serial.println(output);
  271.             for (;;)
  272.                 errLeds(); /* Halt in case of failure */
  273.         }
  274.         else
  275.         {
  276.             output = "BSEC warning code : " + String(iaqSensor.status);
  277.             Serial.println(output);
  278.         }
  279.     }
  280.  
  281.     if (iaqSensor.bme680Status != BME680_OK)
  282.     {
  283.         if (iaqSensor.bme680Status < BME680_OK)
  284.         {
  285.             output = "BME680 error code : " + String(iaqSensor.bme680Status);
  286.             Serial.println(output);
  287.             for (;;)
  288.                 errLeds(); /* Halt in case of failure */
  289.         }
  290.         else
  291.         {
  292.             output = "BME680 warning code : " + String(iaqSensor.bme680Status);
  293.             Serial.println(output);
  294.         }
  295.     }
  296. }
  297.  
  298. // # ------------------------------------------------------ ¤¤ errLeds
  299. void errLeds(void)
  300. {
  301.     pinMode(LED_BUILTIN, OUTPUT);
  302.     digitalWrite(LED_BUILTIN, HIGH);
  303.     delay(100);
  304.     digitalWrite(LED_BUILTIN, LOW);
  305.     delay(100);
  306. }
  307.  
  308. // # ============================================================== ¤  opensensemap upload function
  309. // (given by opensensemap when you create a new sensebox in your account)
  310.  
  311. // # ------------------------------------------------------ ¤¤ send the data to opensensemap as json
  312. void postFloatValue(float measurement, int digits, String sensorId)
  313. {
  314.     // Convert Float to String
  315.     char obs[10];
  316.     dtostrf(measurement, 5, digits, obs);
  317.     // Create Json
  318.     String jsonValue = "{\"value\":";
  319.     jsonValue += obs;
  320.     jsonValue += "}";
  321.     // Connect to OSeM Server and perform POST operation
  322.  
  323.     Serial.println("Connecting to OSeM Server...");
  324.  
  325.     if (osmclient.connect(osmserver, 80))
  326.     {
  327.  
  328.         Serial.println("connected!");
  329.  
  330.         // Build HTTP headers
  331.         osmclient.print("POST /boxes/");
  332.         osmclient.print(SENSEBOX_ID);
  333.         osmclient.print("/");
  334.         osmclient.print(sensorId);
  335.         osmclient.println(" HTTP/1.1");
  336.         osmclient.print("Host:");
  337.         osmclient.println(osmserver);
  338.         osmclient.println("Content-Type: application/json");
  339.         osmclient.println("Connection: close");
  340.         osmclient.print("Content-Length: ");
  341.         osmclient.println(jsonValue.length());
  342.         osmclient.println();
  343.         //Daten senden
  344.         osmclient.println(jsonValue);
  345.     }
  346.     else
  347.     {
  348.  
  349.         Serial.println("failed!");
  350.     }
  351.     // Show answer from server in the serial monitor
  352.     waitForServerResponse();
  353. }
  354.  
  355. // # ------------------------------------------------------ ¤¤ waitForServerResponse
  356. void waitForServerResponse()
  357. {
  358.     // Output incoming bytes
  359.     boolean repeat = true;
  360.     do
  361.     {
  362.         if (osmclient.available())
  363.         {
  364.             char c = osmclient.read();
  365.  
  366.             Serial.print(c);
  367.         }
  368.         // End connection
  369.         if (!osmclient.connected())
  370.         {
  371.  
  372.             Serial.println("Disconnecting.");
  373.  
  374.             osmclient.stop();
  375.             repeat = false;
  376.         }
  377.     } while (repeat);
  378. }
  379.  
  380. //# ============================================================== ¤ wifi connection
  381.  
  382. void setup_wifi()
  383. {
  384.     delay(10);
  385.     // info display in debug
  386.  
  387.     Serial.println();
  388.     Serial.print("Connecting to ");
  389.     Serial.println(ssid);
  390.  
  391.     // connect to wifi define above
  392.     WiFi.begin(ssid, password);
  393.  
  394.     // keep trying to connect while not connected
  395.     while (WiFi.status() != WL_CONNECTED)
  396.     {
  397.         delay(500);
  398.  
  399.         Serial.print(".");
  400.     }
  401.     // print that connection is succesful
  402.  
  403.     Serial.println("");
  404.     Serial.println("WiFi connexion OK ");
  405.     Serial.print("=> Addresse IP : ");
  406.     Serial.print(WiFi.localIP());
  407.  
  408.     WiFi.setAutoReconnect(true);
  409. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement