IoT - Modular - BMP180.cpp

From The TinkerNet Wiki
Revision as of 12:38, 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-BMP180.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-BMP180.h"
5 
6 extern int Altitude;
7 struct BMP180_Readings BMP180_Datum;

BMP180_main()

 1 void BMP180_main()
 2 {
 3 
 4     for (int Count = 0; Count < BMP180_Count; Count++)
 5     {
 6         BMP180_getReadings(0);
 7     }
 8 
 9     if (BMP180_Datum.Status == 0)
10     {
11         BMP180_Display();
12 
13         SSD1306_Indicate_Action();
14 
15         BMP180_SendData();
16 
17         BMP180_debug();
18     }
19     else
20     {
21         debug_Trouble("BMP180 Status Bad");
22         BMP180_init();
23     }
24 }

BMP180_init()

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

BMP180_getReadings()

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

Climate_Pressure_Correction()

 1 float Climate_Pressure_Correction(float Pressure_Actual)
 2 {
 3     BMP180_getReadings(0);
 4 float inHg = (BMP180_Datum.AbsPressure / 33.863886666667);
 5 // // float inHg = 0;
 6 Pressure_Correction = Pressure_Actual - inHg;
 7             char FooBar[42];
 8             sprintf(FooBar, "actual: %f, reading: %f", Pressure_Actual, inHg);
 9             debug_Success(FooBar);
10             sprintf(FooBar, "Correction: %f", Pressure_Correction);
11             debug_Success(FooBar);
12 return (Pressure_Correction);
13 // return(0);
14 }

BMP180_SendData()

 1 void BMP180_SendData()
 2 {
 3     char BMP180_JSON[100];
 4 
 5     sprintf(BMP180_JSON,
 6             "{\"Type\":\"BMP180\", \"Temperature\":%5.1f, \"AbsPressure\":%5.2f, \"SeaPressure\":%5.2f, \"inHg\":%5.2f}",
 7             BMP180_Datum.Temperature,
 8             BMP180_Datum.AbsPressure,
 9             BMP180_Datum.SeaPressure,
10             (BMP180_Datum.AbsPressure / 33.863886666667) + Pressure_Correction);
11 
12     MQTT_SendTELE("SENSOR", BMP180_JSON);
13 }

BMP180_Display()

 1 void BMP180_Display()
 2 {
 3     float inHg = (BMP180_Datum.AbsPressure / 33.863886666667) + Pressure_Correction;
 4     float Water = (49.161 * log(inHg) + 44.932);
 5     SSD1306_Display_Sensor_Data2(
 6         "BMP180",
 7         "Temp:",
 8         BMP180_Datum.Temperature,
 9         "inHg",
10         inHg,
11         " H2O",
12         Water);
13 
14     char FooBar[42];
15     sprintf(FooBar,
16             "inHg: %f - Corrected: %f",
17             BMP180_Datum.AbsPressure / 33.863886666667,
18             BMP180_Datum.AbsPressure / 33.863886666667 + Pressure_Correction);
19     debug_Success(FooBar);
20                 sprintf(FooBar, "Correction: %f", Pressure_Correction);
21             debug_Success(FooBar);
22 }

BMP180_debug()

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