#include "WiFi.h"
#include "time.h"
#include <Firebase_ESP_Client.h>
//Provide the token generation process info.
#include <addons/TokenHelper.h>
const int sensor = 34; //analog input pin constant
int reading; // temperature sensor raw readings
float voltage; // variable for storing voltage
float temperature; // actual temperature variable
int state;//FOR PIR
int pirPin = 2;//FOR PIR
int buzz=4; //FOR BUZZER
const char* ssid = "Wokwi-GUEST";
const char* password = "";
const char* ntpServer = "pool.ntp.org";
const long gmtOffset_sec =10800; // 60*60* (+3 GMT) =10800 Jordan TimeZone
const int daylightOffset_sec = 1; //no Daylight saving in Jordan Currently
// Insert Firebase project API Key
#define API_KEY ""
#define DATABASE_URL "" //Sajeda's indoor
// Insert Authorized Email and Corresponding Password
#define USER_EMAIL ""
#define USER_PASSWORD ""
String uid;
//Define Firebase Data objects
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
unsigned long sendDataPrevMillis = 0;
int count = 0;
bool signupOK = false;
char timestring[1024];
struct tm timeinfo;
void setup()
{
// start the serial port at 9600 baud
Serial.begin(9600);
pinMode(pirPin, INPUT); //FOR PIR
pinMode(buzz, OUTPUT); //FOR BUZZER
WiFi.mode(WIFI_STA); //Optional
WiFi.begin(ssid, password);
Serial.println("\nConnecting");
while(WiFi.status() != WL_CONNECTED){
Serial.print(".");
delay(100);
}
Serial.println("\nConnected to the WiFi network");
Serial.print("Local ESP32 IP: ");
Serial.println(WiFi.localIP());
// Init and get the time
configTime(gmtOffset_sec, daylightOffset_sec, ntpServer);
/* Assign the api key (required) */
config.api_key = API_KEY;
/* Assign the RTDB URL (required) */
config.database_url = DATABASE_URL;
config.timeout.serverResponse = 10 * 1000;
/////////////////TEST Firebase manual auth
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
Firebase.reconnectWiFi(true);
fbdo.setResponseSize(8192);
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
config.max_token_generation_retry = 5;
Firebase.begin(&config, &auth);
Serial.println("Getting User UID");
while ((auth.token.uid) == "") {
Serial.print('.');
delay(50);
}
uid = auth.token.uid.c_str();
Serial.print("User UID: ");
Serial.println(uid);
/////////////////TEST Firebase manual auth
}
void printLocalTime(){
if(!getLocalTime(&timeinfo)){
Serial.println("Failed to obtain time");
return;
}
strftime(timestring, sizeof(timestring), "%A, %B %d %Y %H:%M:%S", &timeinfo);
Serial.println(timestring);
Serial.println();
}
void loop()
{
state = digitalRead(pirPin);
if (state == HIGH){
Serial.println (" motion detected ");
tone(4, 262, 2500); // Plays 262Hz tone for 0.250 seconds //FOR BUZZER
printLocalTime();
//read the temp sensor and store it in tempVal
reading = analogRead(sensor);
voltage = reading/1023.0; // normalize by the maximum temperature raw reading range
temperature = (voltage - 0.5) * 100 ; //calculate temperature celsius from voltage as per the equation found on the sensor spec sheet.
/*
voltage = reading * (5000 / 1024.0);
// Convert the voltage into the temperature in Celsius:
temperature = (voltage - 500) / 10;
*/
Serial.print(" Temperature is: "); // print out the following string to the serial monitor
Serial.print(temperature); // in the same line print the temperature
Serial.println (" degrees C"); // still in the same line print degrees C, then go to next line.
if (Firebase.ready()){ //for checking authuntication
sendDataPrevMillis = millis();
Serial.println("Firebase is Ready");
//Firebase.RTDB.setInt(&fbdo,"Timestamp/"+String(boardsStruct[timeIndex].timeStampRAP) + "/" + "Coordinates/X",x);
Firebase.RTDB.setInt(&fbdo,String(timestring) + "/Temp",temperature);
}
delay(2000); // wait for 1 second or 1000 milliseconds before taking the next reading.
tone(4, 0, 250); // Plays 262Hz tone for 0.250 seconds //FOR BUZZER
}
else {
Serial.println (" no motion detected");
}
delay(1000);
}