Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <ESP8266WiFi.h>
- #include <FirebaseESP8266.h>
- #include <addons/TokenHelper.h>
- #include <addons/RTDBHelper.h>
- #define WIFI_SSID "HUAWEI-930D"
- #define WIFI_PASSWORD "49418874"
- #define API_KEY "AIzaSyBApzVK8nVRR3G-T0Nn4Bk-BDCpFCn9ujk"
- #define DATABASE_URL "mw-ultra-water-level-indicator-default-rtdb.firebaseio.com"
- #define USER_EMAIL "[email protected]"
- #define USER_PASSWORD "123456789"
- ////////////////////////////////////////////////////
- int maxdistance = 23;
- ////////////////////////////////////////////////////
- long duration;
- float distance;
- const int TRIGGER = 12; //D6 = GPIO12
- const int ECHO = 14; //D5 = GPIO14
- FirebaseData fbdo;
- FirebaseAuth auth;
- FirebaseConfig config;
- unsigned long sendDataPrevMillis = 0;
- int count = 0;
- void setup()
- {
- pinMode(TRIGGER, OUTPUT);
- pinMode(ECHO, INPUT);
- Serial.begin(115200);
- Serial.println();
- Serial.println();
- WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
- Serial.print("Connecting to Wi-Fi");
- while (WiFi.status() != WL_CONNECTED)
- {
- Serial.print(".");
- delay(300);
- }
- Serial.println();
- Serial.print("Connected with IP: ");
- Serial.println(WiFi.localIP());
- Serial.println();
- Serial.printf("Firebase Client v%s\n\n", FIREBASE_CLIENT_VERSION);
- /* Assign the api key (required) */
- config.api_key = API_KEY;
- /* Assign the user sign in credentials */
- auth.user.email = USER_EMAIL;
- auth.user.password = USER_PASSWORD;
- /* Assign the RTDB URL (required) */
- config.database_url = DATABASE_URL;
- /* Assign the callback function for the long running token generation task */
- config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
- //Or use legacy authenticate method
- //config.database_url = DATABASE_URL;
- //config.signer.tokens.legacy_token = "<database secret>";
- Firebase.begin(&config, &auth);
- Firebase.reconnectWiFi(true);
- #if defined(ESP8266)
- fbdo.setBSSLBufferSize(512, 2048);
- #endif
- }
- void loop()
- {
- digitalWrite(TRIGGER, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIGGER, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIGGER, LOW);
- duration = pulseIn(ECHO, HIGH);
- distance = (duration / 2) / 29.1;
- Serial.print(distance);
- Serial.println("Centimeter:");
- if (distance > maxdistance) {
- distance = maxdistance;
- }
- count = map(distance, maxdistance , 4, 0, 100);
- if (count > 100) {
- count = 100;
- }
- Serial.print(count);
- Serial.println("%");
- if (Firebase.ready() && (millis() - sendDataPrevMillis > 1000 || sendDataPrevMillis == 0))
- {
- sendDataPrevMillis = millis();
- Serial.print("Set int async... ");
- for (size_t i = 0; i < 10; i++)
- {
- Firebase.setIntAsync(fbdo, "/WL", count);
- }
- Serial.println("ok");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment