// // Copyright 2015 Google Inc. // // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. // // FirebaseRoom_ESP8266 is a sample that demo using multiple sensors // and actuactor with the FirebaseArduino library. #include #include // Set these to run example. #define FIREBASE_HOST "realtime-distance-sensor.firebaseio.com" #define FIREBASE_AUTH "O5IKDBWKoCCPDYGeqxmLhM3Hn5maQZ4Yj3puRSSF" #define WIFI_SSID "AndroidHotspot8432" //"o2-WLAN93" #define WIFI_PASSWORD "5ea71cf6ed4c" //"9592132995009940" #define echoPin D7 // Echo Pin #define trigPin D6 // Trigger Pin long duration, distance; // Duration used to calculate distance void setup() { Serial.begin(115200); pinMode(trigPin, OUTPUT); pinMode(echoPin, INPUT); // connect to wifi. WiFi.begin(WIFI_SSID, WIFI_PASSWORD); Serial.print("connecting"); while (WiFi.status() != WL_CONNECTED) { Serial.print("."); delay(500); } Serial.println(); Serial.print("connected: "); Serial.println(WiFi.localIP()); Firebase.begin(FIREBASE_HOST, FIREBASE_AUTH); } void loop() { digitalWrite(trigPin, LOW); delayMicroseconds(2); digitalWrite(trigPin, HIGH); delayMicroseconds(10); digitalWrite(trigPin, LOW); duration = pulseIn(echoPin, HIGH); //Calculate the distance (in cm) based on the speed of sound. float distance = duration*0.0343/2; Serial.println(distance); Serial.println ( " cm"); delay (200); digitalWrite(echoPin, HIGH); //send data to firebase Firebase.setFloat("dist", distance); digitalWrite(echoPin, LOW); delay(1000); }