Hi guys, I want to merge two codes adding some conditions.
Here the first one:
//Pin definition
int pirPin = 16; //il PIN di Arduino a cui è collegato il sensore
// Calibration time
int calibrationTime = 30;
//How long the output is low
long unsigned int lowIn;
// Value in ms, how long we suppose there's "quiet"
long int pausa = 5000;
boolean lockLow = true;
boolean takeLowTime;
// Sensor settings
void setup() {
Serial.begin(9600);
pinMode(pirPin, INPUT);
digitalWrite(pirPin, LOW);
//Calibration phase
Serial.print("PIR calibration ");
for (int i = 0; i < calibrationTime; i++) {
Serial.print(".");
delay(1000);
}
Serial.println("Done");
delay(50);
}
void loop() {
// If condition to see if there's movement
if (digitalRead(pirPin) == HIGH) {
if (lockLow) {
lockLow = false;
Serial.println("---");
Serial.print("Spotted movement at: ");
Serial.print(millis() / 1000);
Serial.println(" sec");
delay(50);
}
takeLowTime = true;
}
// If condition to check if the movement ended
if (digitalRead(pirPin) == LOW) {
if (takeLowTime) {
lowIn = millis();
takeLowTime = false;
}
if (!lockLow && (millis() - lowIn > pausa)) {
lockLow = true;
Serial.print("Movement ended at "); //output
Serial.print((millis() - pausa) / 1000);
Serial.println(" sec");
delay(50);
}
}
}
and here the second one:
#if defined(ESP32)
#include <WiFi.h>
#elif defined(ESP8266)
#include <ESP8266WiFi.h>
#endif
#include <Firebase_ESP_Client.h>
//Provide the token generation process info.
#include "addons/TokenHelper.h"
//Provide the RTDB payload printing info and other helper functions.
#include "addons/RTDBHelper.h"
//Libreries
#include <Adafruit_BMP085.h>
#include <TimeLib.h>
//Costants - Firebase
//Define the WiFi credentials - Define the API Key - Define the RTDB URL - Define the user Email and password that alreadey registerd or added in your project
#define WIFI_SSID "***"
#define WIFI_PASSWORD "***"
#define API_KEY "***"
#define DATABASE_URL "***" //<databaseName>.firebaseio.com or <databaseName>.<region>.firebasedatabase.app
#define USER_EMAIL "***"
#define USER_PASSWORD "***"
// Constants - Sensors
#define DELAY 1000 // Delay between two measurements in ms
#define VIN 3.3 // V power voltage
#define R 10000 // Ohm resistance value
#define sensorPin 35 // Pin connected to photoresistor
//Define Firebase Data object
FirebaseData fbdo;
FirebaseAuth auth;
FirebaseConfig config;
//Objects
Adafruit_BMP085 bmp;
//Variables
unsigned long sendDataPrevMillis = 0;
int16_t sensorVal; // Analog value from the sensor
int16_t lux; //Lux value
int16_t count = 0;
void setup() {
Serial.begin(9600);
pinMode(sensorPin, INPUT);
WiFi.begin(WIFI_SSID, WIFI_PASSWORD);
while (WiFi.status() != WL_CONNECTED)
{
delay(300);
}
Serial.println();
Serial.printf("Connected with IP: ");
Serial.println(WiFi.localIP());
Serial.println();
config.api_key = API_KEY;
auth.user.email = USER_EMAIL;
auth.user.password = USER_PASSWORD;
config.database_url = DATABASE_URL;
config.token_status_callback = tokenStatusCallback; //see addons/TokenHelper.h
fbdo.setResponseSize(2048); // Limit the size of response payload to be collected in FirebaseData
Firebase.begin(&config, &auth);
Firebase.reconnectWiFi(true);
Firebase.setDoubleDigits(5);
config.timeout.serverResponse = 10 * 1000;
bmp.begin();
}
void loop() {
if (Firebase.ready() && (millis() - sendDataPrevMillis > 15000 || sendDataPrevMillis == 0))
{
sendDataPrevMillis = millis();
float temp = bmp.readTemperature();
int pres = bmp.readPressure();
float alt = bmp.readAltitude(101500);
lux = sensorRawToPhys(analogRead(sensorPin));
String path = "/UsersData/";
path += auth.token.uid.c_str(); //<- user uid of current user that sign in with Emal/Password
path += "/Home/";
Firebase.RTDB.setFloat(&fbdo, path + "RealtimeDataStream/Temperature", temp);
Firebase.RTDB.setInt(&fbdo, path + "RealtimeDataStream/Pressure", pres);
Firebase.RTDB.setFloat(&fbdo, path + "RealtimeDataStream/Altitude", alt);
Firebase.RTDB.setInt(&fbdo, path + "RealtimeDataStream/Luminosity", lux);
Firebase.RTDB.setTimestamp(&fbdo, path + "/Timestamp");
Firebase.RTDB.getDouble(&fbdo, path + "/Timestamp");
double timeStamp = fbdo.to<uint64_t>();
Serial.println("- - - - - - - - - - - - -");
char buff[32];
sprintf(buff, "%02d.%02d.%02d %02d:%02d:%02d", day(timeStamp / 1000), month(timeStamp / 1000), year(timeStamp / 1000), hour(timeStamp / 1000) + 2, minute(timeStamp / 1000), second(timeStamp / 1000));
Serial.println(buff);
Serial.println("Temperature: " + (String)temp);
Serial.println("Pressure: " + (String)pres);
Serial.println("Altitude: " + (String)alt);
Serial.println("Luminosity: " + (String)lux);
Serial.println("Motion: " + (String)motion);
if (count == 10)
{
FirebaseJson json;
json.add("Temperature", temp);
json.add("Pressure", pres);
json.add("Altitude", alt);
json.add("Luminosity", lux);
json.add("Motion duration (sec)", (millis() - pausa) / 1000);
json.add("Timestamp", timeStamp);
Serial.printf("Set json... %s\n", Firebase.RTDB.push(&fbdo, path + "/DataCollection", &json) ? "ok" : fbdo.errorReason().c_str());
json.clear();
count = 0;
}
Serial.println("+" + (String)count);
count++;
}
int sensorRawToPhys(int raw)
{
// Conversion rule
float Vout = float(raw) * (VIN / float(4096));// Conversion analog to voltage
float RLDR = (R * (VIN - Vout)) / Vout; // Conversion voltage to resistance
int phys = 500 / (RLDR / 1000); // Conversion resitance to lumen
return phys;
}
}
- First question is: how do I need to modify my code for when the PIR sensor detect a movement? Maybe, adding a condition in the second code, first if condition, in the loop, a digitalRead and it will instantly update the realtime database (only one time -> adding a variable if I've already wrote set to 1, when the movement end set it to 0 again) but in this way I don't know how to update the other sensor values every 15sec and when the movement end.
- Second question is: how I can add a record every 15 cycle of measurements and also when the movement end with its duration? Since I'm already using the millis() function to update the the sensors values every 15sec but I also need it to know how long the movement lasted.
Thanks in advance,
Federico