IoT - Modular - BME280.cpp

From The TinkerNet Wiki
Revision as of 12:30, 30 April 2021 by Tinker (talk | contribs) (Created page with "==#includes, Defines, etc...== <syntaxhighlight lang="cpp" line="1"> #include "libraries.h" #include "functions.h" #include "externs.h" #include "s-BME280.h" extern int Alti...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

#includes, Defines, etc...

1 #include "libraries.h"
2 #include "functions.h"
3 #include "externs.h"
4 #include "s-BME280.h"
5 
6 extern int Altitude;
7 struct BME280_Readings BME280_Datum;

BME280_main()

 1 void BME280_main()
 2 {
 3 
 4     for (int Count = 0; Count < BME280_Count; Count++)
 5     {
 6         // BME280_Datum = BME280_getReadings(0);
 7         BME280_getReadings(0);
 8     }
 9 
10     if (BME280_Datum.Status == 0)
11     {
12         // SSD1306_Display_BME280_Temps(BBB);
13         // SSD1306_Display_BME280_Temps();
14         BME280_Display();
15 
16         SSD1306_Indicate_Action();
17 
18         BME280_SendData();
19 
20         BME280_debug();
21     }
22     else
23     {
24         debug_Trouble("BME280 Status Bad");
25         BME280_init();
26     }
27 }

BME280_init()

 1 int BME280_init()
 2 {
 3     bool status;
 4     char debugTEXT[46];
 5 
 6     status = bme.begin(0x76);
 7     if (!status)
 8     {
 9         debug_LineOut("No BME280(s)");
10         return (0);
11     }
12     else
13     {
14         sprintf(debugTEXT, "%2d BME280(s) initialised", 1);
15         debug_LineOut(debugTEXT);
16         return (1); // This needs to change
17     }
18 
19     // Currently only capable of using one...
20 }

BME280_getReadings()

 1 void BME280_getReadings(int deviceNumber)
 2 {
 3     BME280_Datum.Temperature = bme.readTemperature();
 4     BME280_Datum.Humidity = bme.readHumidity();
 5     BME280_Datum.AbsPressure = bme.readPressure() / 100.0F;
 6 
 7     if (
 8         (BME280_Datum.Temperature != BME280_Datum.Temperature) ||
 9         (BME280_Datum.Humidity != BME280_Datum.Humidity) ||
10         (BME280_Datum.AbsPressure != BME280_Datum.AbsPressure))
11     {
12         BME280_Datum.Status = -1;
13     }
14     else
15         BME280_Datum.Status = 0;
16 
17     // Attempting to derive SeaPressure based on formula found at:
18     // https://keisan.casio.com/exec/system/1224575267
19     BME280_Datum.SeaPressure = BME280_Datum.AbsPressure * pow((1 - ((0.0065 * Altitude) / (BME280_Datum.Temperature + (0.0065 * Altitude) + 273.15))), -5.257);
20 
21     // return (AAA);
22 }

BME280_SendData()

 1 void BME280_SendData()
 2 {
 3     char BME280_JSON[100];
 4 
 5     sprintf(BME280_JSON,
 6             "{\"Type\":\"BME280\", \"Temperature\":%5.1f, \"Humidity\":%5.1f, \"AbsPressure\":%5.2f, \"SeaPressure\":%5.2f}",
 7             BME280_Datum.Temperature,
 8             BME280_Datum.Humidity,
 9             BME280_Datum.AbsPressure,
10             BME280_Datum.SeaPressure);
11 
12     MQTT_SendTELE("SENSOR", BME280_JSON);
13 }

BME280_Display()

 1 void BME280_Display()
 2 {
 3 
 4     // SSD1306_Display_Sensor_Data(
 5     //     "BME280!",
 6     //     "Temp:",
 7     //     BME280_Datum.Temperature,
 8     //     "Humi:",
 9     //     BME280_Datum.Humidity,
10     //     "Baro",
11     //     BME280_Datum.AbsPressure);
12 
13     float inHg = (BME280_Datum.AbsPressure / 33.863886666667) + Pressure_Correction;
14     float Water = (49.161 * log(inHg) + 44.932);
15     SSD1306_Display_Sensor_Data2(
16         "BME280",
17         "Temp:",
18         BME280_Datum.Temperature,
19         "inHg",
20         inHg,
21         " H2O",
22         Water);
23 
24 }

BME280_debug()

 1 void BME280_debug()
 2 {
 3 #ifdef DEBUG2
 4     char debugTEXT[46];
 5 
 6     debug_SectionTitle("BME280:");
 7     sprintf(debugTEXT, "Temperature:    %5.1f C", BME280_Datum.Temperature);
 8     debug_LineOut(debugTEXT);
 9     sprintf(debugTEXT, "Temperature:    %5.1f F", 1.8 * BME280_Datum.Temperature + 32);
10     debug_LineOut(debugTEXT);
11     sprintf(debugTEXT, "Humidity:       %5.1f %%", BME280_Datum.Humidity);
12     debug_LineOut(debugTEXT);
13     sprintf(debugTEXT, "AbsPressure:   %5.2f hPa", BME280_Datum.AbsPressure);
14     debug_LineOut(debugTEXT);
15     sprintf(debugTEXT, "SeaPressure:  %5.2f hPa", BME280_Datum.SeaPressure);
16     debug_LineOut(debugTEXT);
17 #endif
18 }