Advertisement
safwan092

Untitled

Aug 10th, 2022
36
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.94 KB | None | 0 0
  1. #include <Wire.h>
  2. #include <RTClib.h>
  3. #include "SSD1306Wire.h"
  4. #include <ESP8266WiFi.h>
  5. #include <SoftwareSerial.h>
  6. #include <FirebaseESP8266.h>
  7. #include <addons/RTDBHelper.h>
  8. #include <addons/TokenHelper.h>
  9.  
  10. #include <OneWire.h>
  11. #include <DallasTemperature.h>
  12. #define onewirepin D6
  13.  
  14. OneWire oneWire(onewirepin);
  15. DallasTemperature sensors(&oneWire);
  16. DeviceAddress Probe = { 0x28, 0x2F, 0xE1, 0x49, 0xF6, 0x1F, 0x3C, 0x5D };
  17. //////////////////////////////////////////////////////////////////////
  18. #define WIFI_SSID "network"
  19. #define WIFI_PASSWORD "123456789"
  20. #define API_KEY "AIzaSyCz8k4yj9UiMvfkO6sP66cEIKdyWJLBbJQ"
  21. #define DATABASE_URL "smartwatch-37f35-default-rtdb.firebaseio.com"
  22. #define USER_EMAIL "project9686firebase@gmail.com"
  23. #define USER_PASSWORD "123456789"
  24. /////////////////////////////////////////////////////////////////////
  25.  
  26. FirebaseData fbdo;
  27. FirebaseAuth auth;
  28. FirebaseConfig config;
  29. unsigned long sendDataPrevMillis = 0;
  30.  
  31.  
  32. #define RX D1
  33. #define TX D3
  34. RTC_DS3231 rtc;
  35. SoftwareSerial ss(RX, TX);
  36. SSD1306Wire display(0x3C, D2, D5);
  37. String timee;
  38. String dayyy;
  39. const int MPU = 0x69;//0x69 [with jumper]
  40. int16_t Tmp, GyX, GyY, GyZ;
  41. char c;
  42. String dataIn;
  43. int8_t indexOfA, indexOfB;
  44. String data1, data2;
  45. float tempC;
  46.  
  47. void setup() {
  48. Serial.begin(9600);
  49. ss.begin(9600);
  50. sensors.begin (); // Initialize the sensor and set resolution level
  51. sensors.setResolution(Probe, 10);
  52.  
  53. initOLED();
  54. initRTC();
  55. initGyro();
  56.  
  57. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  58. Serial.print("Connecting to Wi-Fi");
  59. while (WiFi.status() != WL_CONNECTED)
  60. {
  61. Serial.print(".");
  62. delay(300);
  63. }
  64. Serial.println();
  65. Serial.print("Connected with IP: ");
  66. Serial.println(WiFi.localIP());
  67. Serial.println();
  68.  
  69. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  70.  
  71. /* Assign the api key (required) */
  72. config.api_key = API_KEY;
  73.  
  74. /* Assign the user sign in credentials */
  75. auth.user.email = USER_EMAIL;
  76. auth.user.password = USER_PASSWORD;
  77.  
  78. /* Assign the RTDB URL (required) */
  79. config.database_url = DATABASE_URL;
  80.  
  81. /* Assign the callback function for the long running token generation task */
  82. config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
  83.  
  84. //Or use legacy authenticate method
  85. //config.database_url = DATABASE_URL;
  86. //config.signer.tokens.legacy_token = "<database secret>";
  87.  
  88. Firebase.begin(&config, &auth);
  89.  
  90. Firebase.reconnectWiFi(true);
  91. #if defined(ESP8266)
  92. fbdo.setBSSLBufferSize(512, 2048);
  93. #endif
  94. }
  95.  
  96. void loop() {
  97. getTimeAndDate();
  98. Gyroscope();
  99.  
  100. sensors.requestTemperatures(); // Command all devices on bus to read temperature
  101.  
  102. Serial.print("Temperature is: ");
  103. printTemperature(Probe);
  104. Serial.println();
  105.  
  106. while (ss.available() > 0) {
  107. c = ss.read();
  108. if (c == '\n') {
  109. break;
  110. }
  111. else {
  112. dataIn += c;
  113. }
  114. }
  115. if (c == '\n') {
  116. parseData();
  117. ShowRxData();
  118. if (Firebase.ready() && (millis() - sendDataPrevMillis > 1000 || sendDataPrevMillis == 0))
  119. {
  120. sendDataPrevMillis = millis();
  121.  
  122. Serial.print("Set String... ");
  123. Firebase.setString(fbdo, "/HR", data1);
  124. Firebase.setString(fbdo, "/O2", data2);
  125. Firebase.setString(fbdo, "/Temp", String(tempC));
  126. //Firebase.setIntAsync(fbdo, "/Distance", count);
  127. Serial.println("ok");
  128. }
  129. if ( GyY > -500 && GyY < 500 ) {
  130. ShowOLED();
  131. }
  132. else {
  133. display.clear();
  134. display.display();
  135. }
  136.  
  137. resetData();
  138. }
  139. }
  140.  
  141. void parseData() {
  142. indexOfA = dataIn.indexOf("A");
  143. indexOfB = dataIn.indexOf("B");
  144. data1 = dataIn.substring(0, indexOfA);
  145. data2 = dataIn.substring(indexOfA + 1, indexOfB);
  146. }
  147.  
  148. void ShowRxData() {
  149. Serial.println("HR: " + data1);
  150. Serial.println("O2: " + data2);
  151. Serial.println("####################");
  152. }
  153.  
  154. void resetData() {
  155. c = 0;
  156. dataIn = "";
  157. }
  158.  
  159. void ShowOLED() {
  160. display.clear();
  161. display.setFont(ArialMT_Plain_10);//Size: 10 / 16 / 24
  162. display.setTextAlignment(TEXT_ALIGN_CENTER);
  163. display.drawString(64, 0, String("HR: " + data1));
  164. display.drawString(64, 13, String("O2: " + data2 + "%"));
  165. display.drawString(64, 26, String(timee));
  166. display.drawString(64, 38, String(dayyy));
  167. display.drawString(64, 50, String(tempC));
  168. display.display();
  169. delay(10);
  170. }
  171.  
  172. void Gyroscope() {
  173. Wire.beginTransmission(MPU);
  174. Wire.write(0x3B);
  175. Wire.endTransmission(false);
  176. Wire.requestFrom(MPU, 12, true);
  177. GyX = Wire.read() << 8 | Wire.read();
  178. GyY = Wire.read() << 8 | Wire.read();
  179. GyZ = Wire.read() << 8 | Wire.read();
  180. Serial.print("Gyroscope: ");
  181. Serial.print("X = "); Serial.print(GyX);
  182. Serial.print(" | Y = "); Serial.print(GyY);
  183. Serial.print(" | Z = "); Serial.println(GyZ);
  184. Serial.println(" ");
  185. delay(333);
  186. }
  187.  
  188.  
  189.  
  190.  
  191. void initOLED() {
  192. display.init();
  193. display.flipScreenVertically();
  194. display.setFont(ArialMT_Plain_10);
  195. }
  196.  
  197. void initRTC() {
  198. if (! rtc.begin()) {
  199. Serial.println("Couldn't find RTC");
  200. Serial.flush();
  201. while (1) delay(10);
  202. }
  203.  
  204. if (rtc.lostPower()) {
  205. Serial.println("RTC lost power, let's set the time!");
  206. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  207. //rtc.adjust(DateTime(2022, 8, 6, 18, 58, 0));
  208. }
  209. }
  210.  
  211. void initGyro() {
  212. Wire.begin();
  213. Wire.beginTransmission(MPU);
  214. Wire.write(0x6B);
  215. Wire.write(0);
  216. Wire.endTransmission(true);
  217.  
  218. }
  219.  
  220. void getTimeAndDate() {
  221. DateTime now = rtc.now();
  222. timee = "";
  223. timee += now.hour();
  224. timee += ':';
  225. timee += now.minute();
  226. timee += ':';
  227. timee += now.second();
  228. dayyy = "";
  229. dayyy += (now.year());
  230. dayyy += '/';
  231. dayyy += (now.month());
  232. dayyy += '/';
  233. dayyy += (now.day());
  234. }
  235.  
  236.  
  237.  
  238. void printTemperature(DeviceAddress deviceAddress)
  239. {
  240.  
  241. tempC = sensors.getTempC(deviceAddress);
  242.  
  243. if (tempC == -127.00)
  244. {
  245. Serial.print ("Error getting temperature");
  246. }
  247. else
  248. {
  249. Serial.print ("C: ");
  250. Serial.println (tempC);
  251. }
  252. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement