Advertisement
safwan092

Project_9886_ESP32_Code

Sep 10th, 2022 (edited)
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. #include <Wire.h>
  2. #include "MAX30100_PulseOximeter.h"
  3. #include <TinyGPS++.h>
  4.  
  5. #define REPORTING_PERIOD_MS 1000
  6. #define RXD2 16
  7. #define TXD2 17
  8. #define RXD1 4
  9. #define TXD1 2
  10. #define Btn 12
  11. #define LED 13
  12.  
  13. PulseOximeter pox;
  14. TinyGPSPlus gps;
  15.  
  16. char c;
  17. String dataIn;
  18. int8_t indexOfA, indexOfB;
  19. String data1, data2;
  20. String lonGPS = "";
  21. String latGPS = "";
  22. float oTemp;
  23. float BPM, SpO2;
  24. uint32_t tsLastReport = 0;
  25. const int MPU6050_addr = 0x68;
  26. int16_t AccX, AccY, AccZ, Temp, GyroX, GyroY, GyroZ;
  27.  
  28.  
  29. void setup()
  30. {
  31. Serial.begin(115200);
  32. Serial2.begin(9600, SERIAL_8N1, RXD2, TXD2);
  33. Serial1.begin(9600, SERIAL_8N1, RXD1, TXD1);
  34. initHR_O2();
  35. initMPU6050();
  36. pinMode(Btn,INPUT);
  37. pinMode(LED,OUTPUT);
  38. digitalWrite(LED,LOW);
  39. }
  40.  
  41. void loop() {
  42.  
  43. pox.update();
  44.  
  45. if(digitalRead(Btn)){
  46. digitalWrite(LED,HIGH);
  47. delay(500);
  48. digitalWrite(LED,LOW);
  49. }
  50. if (millis() - tsLastReport > REPORTING_PERIOD_MS)
  51. {
  52. //********************************************************************
  53. //------------------------Get and Show Temprature Data
  54. readNano_Data();
  55. //------------------------Get and Show GPS Data
  56. readGPS_Data();
  57. //------------------------Get and Show Motion Data
  58. readMPU6050();
  59. //------------------------Get and Show Heart and Oxygen Data
  60. readHR_O2();
  61. Serial.println("-------------------------------------");
  62. //********************************************************************
  63. tsLastReport = millis();
  64. }
  65.  
  66. }//end of LOOP
  67.  
  68. void initMPU6050() {
  69. Wire.begin();
  70. Wire.beginTransmission(MPU6050_addr);
  71. Wire.write(0x6B);
  72. Wire.write(0);
  73. Wire.endTransmission(true);
  74. }
  75.  
  76. void initHR_O2() {
  77. Serial.print("Initializing Pulse Oximeter..");
  78. if (!pox.begin())
  79. {
  80. Serial.println("FAILED");
  81. for (;;);
  82. }
  83. else
  84. {
  85. Serial.println("SUCCESS");
  86. }
  87.  
  88. // The default current for the IR LED is 50mA and it could be changed by uncommenting the following line.
  89. pox.setIRLedCurrent(MAX30100_LED_CURR_7_6MA);
  90. }
  91.  
  92. void readHR_O2() {
  93. BPM = pox.getHeartRate();
  94. SpO2 = pox.getSpO2();
  95. Serial.print("Heart rate:");
  96. Serial.print(BPM);
  97. Serial.print(" bpm / SpO2:");
  98. Serial.print(SpO2);
  99. Serial.println(" %");
  100. }
  101.  
  102. void readMPU6050() {
  103. Wire.beginTransmission(MPU6050_addr);
  104. Wire.write(0x3B);
  105. Wire.endTransmission(false);
  106. Wire.requestFrom(MPU6050_addr, 14, true);
  107. AccX = Wire.read() << 8 | Wire.read();
  108. AccY = Wire.read() << 8 | Wire.read();
  109. AccZ = Wire.read() << 8 | Wire.read();
  110. Temp = Wire.read() << 8 | Wire.read();
  111. GyroX = Wire.read() << 8 | Wire.read();
  112. GyroY = Wire.read() << 8 | Wire.read();
  113. GyroZ = Wire.read() << 8 | Wire.read();
  114.  
  115. Serial.print(AccX);
  116. Serial.print("\t");
  117. Serial.print(AccY);
  118. Serial.print("\t");
  119. Serial.println(AccZ);
  120. Serial.print(GyroX);
  121. Serial.print("\t");
  122. Serial.print(GyroY);
  123. Serial.print("\t");
  124. Serial.println(GyroZ);
  125. }
  126.  
  127. void displayGPSInfo()
  128. {
  129.  
  130. Serial.print(F("Location: "));
  131. if (gps.location.isValid())
  132. {
  133. lonGPS = String(gps.location.lng(), 6);
  134. latGPS = String(gps.location.lat(), 6);
  135. Serial.print(latGPS);
  136. Serial.print(F(","));
  137. Serial.print(lonGPS);
  138. }
  139. else
  140. {
  141. Serial.print(F("INVALID"));
  142. }
  143.  
  144. Serial.print(F(" Date/Time: "));
  145. if (gps.date.isValid())
  146. {
  147. Serial.print(gps.date.month());
  148. Serial.print(F("/"));
  149. Serial.print(gps.date.day());
  150. Serial.print(F("/"));
  151. Serial.print(gps.date.year());
  152. }
  153. else
  154. {
  155. Serial.print(F("INVALID"));
  156. }
  157.  
  158. Serial.print(F(" "));
  159. if (gps.time.isValid())
  160. {
  161. if (gps.time.hour()+3 < 10) Serial.print(F("0"));
  162. Serial.print(gps.time.hour()+3);
  163. Serial.print(F(":"));
  164. if (gps.time.minute() < 10) Serial.print(F("0"));
  165. Serial.print(gps.time.minute());
  166. Serial.print(F(":"));
  167. if (gps.time.second() < 10) Serial.print(F("0"));
  168. Serial.print(gps.time.second());
  169. Serial.print(F("."));
  170. if (gps.time.centisecond() < 10) Serial.print(F("0"));
  171. Serial.print(gps.time.centisecond());
  172.  
  173. }
  174. else
  175. {
  176. Serial.print(F("INVALID"));
  177. }
  178.  
  179. Serial.println();
  180.  
  181. }
  182.  
  183. void parseData() {
  184. indexOfA = dataIn.indexOf("A");
  185. indexOfB = dataIn.indexOf("B");
  186. data1 = dataIn.substring(0, indexOfA);
  187. data2 = dataIn.substring(indexOfA + 1, indexOfB);
  188. }
  189.  
  190. void ShowRxData() {
  191. Serial.println("Object Temp: " + data1);
  192. Serial.println("Ambiant Temp: " + data2);
  193. }
  194.  
  195. void resetData() {
  196. c = 0;
  197. dataIn = "";
  198. }
  199.  
  200. void readGPS_Data() {
  201. // This sketch displays information every time a new sentence is correctly encoded.
  202. while (Serial2.available() > 0)
  203. if (gps.encode(Serial2.read()))
  204. displayGPSInfo();
  205. }
  206.  
  207. void readNano_Data() {
  208. while (Serial1.available() > 0) {
  209. c = Serial1.read();
  210. if (c == '\n') {
  211. break;
  212. }
  213. else {
  214. dataIn += c;
  215. }
  216. }
  217. if (c == '\n') {
  218. parseData();
  219. ShowRxData();
  220. resetData();
  221. }
  222. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement