Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <FirebaseArduino.h>
- #include <DHT.h>
- #define DHTPIN 2
- #define DHTTYPE DHT11
- #include <SoftwareSerial.h>
- SoftwareSerial ESP8266(10,11);
- #define DEBUG true
- #define TRIGGER_PIN 4
- #define ECHO_PIN 3
- DHT dht(DHTPIN, DHTTYPE, 11);
- void setup() {
- Serial.begin(115200);
- ESP8266.begin(9600);
- dht.begin();
- pinMode(TRIGGER_PIN, OUTPUT);
- pinMode(ECHO_PIN, INPUT);
- ESP8266.println("AT+RST");
- delay(2000);
- Serial.println("Connecting to Wifi");
- while(check_connection==0)
- {
- Serial.print(".");
- ESP8266.print("AT+CWJAP="c902b6","262459105"rn");
- //ESP8266.print("AT+CWJAP="FAKE_WIFI","bartender"rn");
- ESP8266.setTimeout(5000);
- if(ESP8266.find("WIFI CONNECTEDrn")==1)
- {
- Serial.println("WIFI CONNECTED");
- break;
- }
- times_check++;
- if(times_check>3)
- {
- times_check=0;
- Serial.println("Trying to Reconnect..");
- }
- }
- Firebase.begin("thesis1-69.firebaseio.com",
- "AIzaSyCNZM0KCYczftfCKtsie2l1WBFnlFLGwNo");
- }
- bool incuID = 0;
- void loop() {
- int hum = dht.readHumidity();
- int temp = dht.readTemperature();
- digitalWrite(TRIGGER_PIN, LOW);
- delayMicroseconds(2);
- digitalWrite(TRIGGER_PIN, HIGH);
- delayMicroseconds(10);
- digitalWrite(TRIGGER_PIN, LOW);
- duration = pulseIn(ECHO_PIN,HIGH);
- distance = duration*0.034/2;
- String name = Firebase.push("data", 0);
- Serial.print("pushed: /logs/");
- delay(1000);
- Serial.println(name);
- if (Firebase.failed()) {
- Serial.print("setting /number failed:");
- Serial.println(Firebase.error());
- return;
- }
- if (!incuID) {
- Firebase.set("data/" + name + "/Distance", 0);
- delay(1000);
- Firebase.set("data/" + name + "/Temperature", 0);
- delay(1000);
- Firebase.set("data/" + name + "/Humidity", 0);
- delay(1000);
- incuID = true;
- if (Firebase.failed()) {
- Serial.print("setting /number failed:");
- Serial.println(Firebase.error());
- return;
- }
- } else {
- Firebase.set("data/" + name + "/distance", distance);
- Serial.println(distance);
- delay(1000);
- Firebase.set("data/" + name + "/temp", temp);
- Serial.println(t);
- delay(1000);
- Firebase.set("data/" + name + "/hum", hum);
- Serial.println(h);
- delay(1000);
- if (Firebase.failed()) {
- Serial.print("setting /number failed:");
- Serial.println(Firebase.error());
- return;
- }
- }
Add Comment
Please, Sign In to add comment