safwan092

Untitled

Oct 14th, 2023
18
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.82 KB | None | 0 0
  1. #include <WiFi.h>
  2. #include <FirebaseESP32.h>
  3. #include <addons/TokenHelper.h>
  4. #include <addons/RTDBHelper.h>
  5. #include <Wire.h>
  6. #include <MPU6050.h>
  7.  
  8. #define flexPin 34
  9. unsigned long dataMillisInterval = 1000; // Send Data Every [1000 ms] = [1 Second]
  10.  
  11. // Define the I2C addresses of the MPU6050 sensors
  12. const uint8_t MPU1_ADDRESS = 0x68;
  13. const uint8_t MPU2_ADDRESS = 0x69;
  14.  
  15.  
  16. #define WIFI_SSID "network"
  17. #define WIFI_PASSWORD "123456789"
  18. #define API_KEY "AIzaSyCcrTq6x7TGHXQL4qQ3E48zDsqkaXBJQtw"
  19. #define USER_EMAIL "project12338@gmail.com"
  20. #define USER_PASSWORD "123456789"
  21. #define DATABASE_URL "kneetracking-default-rtdb.europe-west1.firebasedatabase.app/"
  22. #define DATABASE_SECRET "AaATdr2hwU6v5KUAITOHyG7KGl3BfhdQMEC2XFUQ"
  23.  
  24. FirebaseData fbdo;
  25. FirebaseAuth auth;
  26. FirebaseConfig config;
  27. unsigned long dataMillis = 0;
  28. MPU6050 mpu1(MPU1_ADDRESS);
  29. MPU6050 mpu2(MPU2_ADDRESS);
  30. double x_1, y_1, z_1;
  31. double x2, y2, z2;
  32. int analogValue;
  33.  
  34. void setup()
  35. {
  36. Serial.begin(115200);
  37. setup_Sensors();
  38. connect_To_WiFi_and_Firebase();
  39. }
  40.  
  41. void loop() {
  42.  
  43. read_Flex_Sensor();
  44. read_MPU6050_Sensors();
  45. send_Data_To_Firebase();
  46.  
  47. }
  48. //----------------------------------------------------------------
  49. void send_Data_To_Firebase() {
  50. if (Firebase.ready() && (millis() - dataMillis > dataMillisInterval))
  51. {
  52. dataMillis = millis();
  53. Firebase.setIntAsync(fbdo, "/test/RAW_flex" , analogValue );
  54. Firebase.setIntAsync(fbdo, "/test/RAW_x_1" , x_1 );
  55. Firebase.setIntAsync(fbdo, "/test/RAW_y_1" , y_1 );
  56. Firebase.setIntAsync(fbdo, "/test/RAW_z_1" , z_1 );
  57. Firebase.setIntAsync(fbdo, "/test/RAW_x2" , x2 );
  58. Firebase.setIntAsync(fbdo, "/test/RAW_y2" , y2 );
  59. Firebase.setIntAsync(fbdo, "/test/RAW_z2" , z2 );
  60. }
  61. }
  62. //----------------------------------------------------------------
  63. void read_Flex_Sensor() {
  64. analogValue = analogRead(flexPin);
  65. Serial.printf("ADC analog value = %d\n", analogValue);
  66. }
  67. //----------------------------------------------------------------
  68. void read_MPU6050_Sensors() {
  69. // Read accelerometer and gyroscope data from MPU1
  70. int16_t ax1, ay1, az1, gx1, gy1, gz1;
  71. mpu1.getMotion6(&ax1, &ay1, &az1, &gx1, &gy1, &gz1);
  72.  
  73. // Read accelerometer and gyroscope data from MPU2
  74. int16_t ax2, ay2, az2, gx2, gy2, gz2;
  75. mpu2.getMotion6(&ax2, &ay2, &az2, &gx2, &gy2, &gz2);
  76.  
  77. // Print the data
  78. Serial.print("MPU1: ");
  79. // Serial.print("Accel(XYZ): ");
  80. // Serial.print(ax1); Serial.print(", ");
  81. // Serial.print(ay1); Serial.print(", ");
  82. // Serial.print(az1); Serial.print(" | ");
  83. //Serial.print("Gyro(XYZ): ");
  84. x_1 = gx1 + 171;
  85. y_1 = gy1 - 179;
  86. z_1 = gz1 + 132;
  87. // Serial.print(gx1); Serial.print(", ");
  88. // Serial.print(gy1); Serial.print(", ");
  89. // Serial.println(gz1);
  90. if (x_1 > -100 && x_1 < 100) {
  91. x_1 = 0;
  92. }
  93. if (y_1 > -100 && y_1 < 100) {
  94. y_1 = 0;
  95. }
  96. if (z_1 > -100 && z_1 < 100) {
  97. z_1 = 0;
  98. }
  99. Serial.print(x_1); Serial.print(", ");
  100. Serial.print(y_1); Serial.print(", ");
  101. Serial.println(z_1);
  102.  
  103. Serial.print("MPU2: ");
  104. // Serial.print("Accel(XYZ): ");
  105. // Serial.print(ax2); Serial.print(", ");
  106. // Serial.print(ay2); Serial.print(", ");
  107. // Serial.print(az2); Serial.print(" | ");
  108. x2 = gx2 + 390;
  109. y2 = gy2 + 80;
  110. z2 = gz2 + 60;
  111. // Serial.print(gx2); Serial.print(", ");
  112. // Serial.print(gy2); Serial.print(", ");
  113. // Serial.println(gz2);
  114. if (x2 > -100 && x2 < 100) {
  115. x2 = 0;
  116. }
  117. if (y2 > -100 && y2 < 100) {
  118. y2 = 0;
  119. }
  120. if (z2 > -100 && z2 < 100) {
  121. z2 = 0;
  122. }
  123. Serial.print(x2); Serial.print(", ");
  124. Serial.print(y2); Serial.print(", ");
  125. Serial.println(z2);
  126. Serial.println("------------------------------------------------------");
  127. delay(500); // Adjust the delay according to your requirements
  128.  
  129. }
  130.  
  131. //----------------------------------------------------------------
  132. void setup_Sensors() {
  133.  
  134. Wire.begin();
  135. //set the resolution to 12 bits (0-4096)
  136. analogReadResolution(12);
  137. // Initialize MPU6050 sensors with their respective addresses
  138. mpu1.initialize();
  139. mpu2.initialize();
  140.  
  141. }
  142. //----------------------------------------------------------------
  143. void connect_To_WiFi_and_Firebase() {
  144.  
  145. WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
  146. Serial.print("Connecting to Wi-Fi");
  147. while (WiFi.status() != WL_CONNECTED)
  148. {
  149. Serial.print(".");
  150. delay(300);
  151. }
  152. Serial.println();
  153. Serial.print("Connected with IP: ");
  154. Serial.println(WiFi.localIP());
  155. Serial.println();
  156.  
  157. Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
  158.  
  159. /* Assign the api key (required) */
  160. config.api_key = API_KEY;
  161.  
  162. /* Assign the user sign in credentials */
  163. auth.user.email = USER_EMAIL;
  164. auth.user.password = USER_PASSWORD;
  165.  
  166. /* Assign the RTDB URL */
  167. config.database_url = DATABASE_URL;
  168.  
  169. Firebase.reconnectWiFi(true);
  170. fbdo.setResponseSize(4096);
  171.  
  172. String base_path = "/UsersData/";
  173.  
  174. /* Assign the callback function for the long running token generation task */
  175. config.token_status_callback = tokenStatusCallback; // see addons/TokenHelper.h
  176.  
  177. /** Assign the maximum retry of token generation */
  178. config.max_token_generation_retry = 5;
  179.  
  180. /* Initialize the library with the Firebase authen and config */
  181. Firebase.begin(&config, &auth);
  182.  
  183. /** Now modify the database rules (if not yet modified)
  184.  
  185. The user, <user uid> in this case will be granted to read and write
  186. at the certain location i.e. "/UsersData/<user uid>".
  187.  
  188. If you database rules has been modified, please comment this code out.
  189.  
  190. The character $ is to make a wildcard variable (can be any name) represents any node key
  191. which located at some level in the rule structure and use as reference variable
  192. in .read, .write and .validate rules
  193.  
  194. For this case $userId represents any <user uid> node that places under UsersData node i.e.
  195. /UsersData/<user uid> which <user uid> is user UID.
  196.  
  197. Please check your the database rules to see the changes after run the below code.
  198. */
  199. String var = "$userId";
  200. String val = "($userId === auth.uid && auth.token.premium_account === true && auth.token.admin === true)";
  201. Firebase.setReadWriteRules(fbdo, base_path, var, val, val, DATABASE_SECRET);
  202.  
  203. /** path for user data is now "/UsersData/<user uid>"
  204. The user UID can be taken from auth.token.uid
  205. */
  206. }
  207. //----------------------------------------------------------------
  208.  
  209. //---------------------------------------------------------------------------
  210.  
  211. /*
  212. void setup() {
  213. // Set MPU6050 scale and sensitivity settings if needed
  214. // mpu1.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
  215. // mpu1.setFullScaleGyroRange(MPU6050_GYRO_FS_250);
  216. // mpu2.setFullScaleAccelRange(MPU6050_ACCEL_FS_2);
  217. // mpu2.setFullScaleGyroRange(MPU6050_GYRO_FS_250);
  218. }
  219. */
Add Comment
Please, Sign In to add comment