Advertisement
safwan092

Untitled

Aug 10th, 2022
29
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.10 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 "aaaa"
  19. #define WIFI_PASSWORD "1234501234"
  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. if (GyX < -10000) {
  127. Firebase.setString(fbdo, "/State", "Problem");
  128. }
  129. else{
  130. Firebase.setString(fbdo, "/State", "Normal");
  131. }
  132. //Firebase.setIntAsync(fbdo, "/Distance", count);
  133. Serial.println("ok");
  134. }
  135. if ( GyY > -500 && GyY < 500 ) {
  136. ShowOLED();
  137. }
  138. else {
  139. display.clear();
  140. display.display();
  141. }
  142.  
  143. resetData();
  144. }
  145. }
  146.  
  147. void parseData() {
  148. indexOfA = dataIn.indexOf("A");
  149. indexOfB = dataIn.indexOf("B");
  150. data1 = dataIn.substring(0, indexOfA);
  151. data2 = dataIn.substring(indexOfA + 1, indexOfB);
  152. }
  153.  
  154. void ShowRxData() {
  155. Serial.println("HR: " + data1);
  156. Serial.println("O2: " + data2);
  157. Serial.println("####################");
  158. }
  159.  
  160. void resetData() {
  161. c = 0;
  162. dataIn = "";
  163. }
  164.  
  165. void ShowOLED() {
  166. display.clear();
  167. display.setFont(ArialMT_Plain_10);//Size: 10 / 16 / 24
  168. display.setTextAlignment(TEXT_ALIGN_CENTER);
  169. display.drawString(64, 0, String("HR: " + data1));
  170. display.drawString(64, 13, String("O2: " + data2 + "%"));
  171. display.drawString(64, 26, String(timee));
  172. display.drawString(64, 38, String(dayyy));
  173. display.drawString(64, 50, String(tempC));
  174. display.display();
  175. delay(10);
  176. }
  177.  
  178. void Gyroscope() {
  179. Wire.beginTransmission(MPU);
  180. Wire.write(0x3B);
  181. Wire.endTransmission(false);
  182. Wire.requestFrom(MPU, 12, true);
  183. GyX = Wire.read() << 8 | Wire.read();
  184. GyY = Wire.read() << 8 | Wire.read();
  185. GyZ = Wire.read() << 8 | Wire.read();
  186. Serial.print("Gyroscope: ");
  187. Serial.print("X = "); Serial.print(GyX);
  188. Serial.print(" | Y = "); Serial.print(GyY);
  189. Serial.print(" | Z = "); Serial.println(GyZ);
  190. Serial.println(" ");
  191. delay(333);
  192. }
  193.  
  194.  
  195.  
  196.  
  197. void initOLED() {
  198. display.init();
  199. display.flipScreenVertically();
  200. display.setFont(ArialMT_Plain_10);
  201. }
  202.  
  203. void initRTC() {
  204. if (! rtc.begin()) {
  205. Serial.println("Couldn't find RTC");
  206. Serial.flush();
  207. while (1) delay(10);
  208. }
  209.  
  210. if (rtc.lostPower()) {
  211. Serial.println("RTC lost power, let's set the time!");
  212. rtc.adjust(DateTime(F(__DATE__), F(__TIME__)));
  213. //rtc.adjust(DateTime(2022, 8, 6, 18, 58, 0));
  214. }
  215. }
  216.  
  217. void initGyro() {
  218. Wire.begin();
  219. Wire.beginTransmission(MPU);
  220. Wire.write(0x6B);
  221. Wire.write(0);
  222. Wire.endTransmission(true);
  223.  
  224. }
  225.  
  226. void getTimeAndDate() {
  227. DateTime now = rtc.now();
  228. timee = "";
  229. timee += now.hour();
  230. timee += ':';
  231. timee += now.minute();
  232. timee += ':';
  233. timee += now.second();
  234. dayyy = "";
  235. dayyy += (now.year());
  236. dayyy += '/';
  237. dayyy += (now.month());
  238. dayyy += '/';
  239. dayyy += (now.day());
  240. }
  241.  
  242.  
  243.  
  244. void printTemperature(DeviceAddress deviceAddress)
  245. {
  246.  
  247. tempC = sensors.getTempC(deviceAddress);
  248.  
  249. if (tempC == -127.00)
  250. {
  251. Serial.print ("Error getting temperature");
  252. }
  253. else
  254. {
  255. Serial.print ("C: ");
  256. Serial.println (tempC);
  257. }
  258. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement