Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
2
2023-262
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
2023-262
2023-262
Commits
363d0ccf
Commit
363d0ccf
authored
Sep 07, 2023
by
NishanMadush
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Arduno Code
parent
4c2c7548
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
372 additions
and
0 deletions
+372
-0
device.ino
device.ino
+372
-0
No files found.
device.ino
0 → 100644
View file @
363d0ccf
#include <Arduino.h>
#include <WiFi.h>
#include <Firebase_ESP_Client.h>
#include "time.h"
#include "addons/TokenHelper.h"
#include "addons/RTDBHelper.h"
#include "DHT.h"
#include <Wire.h>
#include <BH1750.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#define WIFI_SSID "LANKI FERN"
#define WIFI_PASSWORD "20722r1998"
#define API_KEY "AIzaSyBf3T8Bm21uWx_KQ-9QcncYaP60tr1Td5o"
#define USER_EMAIL "user@gmail.com"
#define USER_PASSWORD "User@123"
#define DATABASE_URL "https://plantation-flower-default-rtdb.firebaseio.com"
// Define Firebase objects
FirebaseData
fbdo
;
FirebaseAuth
auth
;
FirebaseConfig
config
;
#define buzzer 17
#define ldr 19
#define trigPin 25
#define echoPin 26
#define oneWireBus 13
#define DHTPIN 15
#define turbidity 36
#define potPin 34
#define soil 32
#define relay1 2
#define relay2 4
#define relay3 16
#define DHTTYPE DHT22
#define SOUND_SPEED 0.034
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
int
ldr_data
,
t
,
h
,
temperatureC
,
distanceCm
,
lux
,
valur_t
,
Value
,
soil_data
,
timestamp
;
String
soil_sta
,
ldr_sta
;
float
ph
;
long
duration
;
const
char
*
ntpServer
=
"pool.ntp.org"
;
String
uid
,
historyPath
,
histry
;
unsigned
long
lastTime
=
0
;
unsigned
long
timerDelay
=
10000
;
unsigned
long
lastTime2
=
0
;
unsigned
long
timerDelay2
=
30000
;
BH1750
lightMeter
;
OneWire
oneWire
(
oneWireBus
);
DHT
dht
(
DHTPIN
,
DHTTYPE
);
DallasTemperature
sensors
(
&
oneWire
);
FirebaseJson
json
;
Adafruit_SSD1306
display
(
SCREEN_WIDTH
,
SCREEN_HEIGHT
,
&
Wire
,
-
1
);
void
initWiFi
()
{
WiFi
.
begin
(
WIFI_SSID
,
WIFI_PASSWORD
);
Serial
.
print
(
"Connecting to WiFi .."
);
while
(
WiFi
.
status
()
!=
WL_CONNECTED
)
{
Serial
.
print
(
'.'
);
delay
(
1000
);
}
Serial
.
println
(
WiFi
.
localIP
());
Serial
.
println
();
}
unsigned
long
getTime
()
{
time_t
now
;
struct
tm
timeinfo
;
if
(
!
getLocalTime
(
&
timeinfo
))
{
//Serial.println("Failed to obtain time");
return
(
0
);
}
time
(
&
now
);
return
now
;
}
void
setup
()
{
Serial
.
begin
(
9600
);
pinMode
(
buzzer
,
OUTPUT
);
pinMode
(
ldr
,
INPUT
);
pinMode
(
trigPin
,
OUTPUT
);
pinMode
(
echoPin
,
INPUT
);
pinMode
(
turbidity
,
INPUT
);
pinMode
(
potPin
,
INPUT
);
pinMode
(
soil
,
INPUT
);
pinMode
(
relay1
,
OUTPUT
);
pinMode
(
relay2
,
OUTPUT
);
pinMode
(
relay3
,
OUTPUT
);
Wire
.
begin
();
dht
.
begin
();
sensors
.
begin
();
lightMeter
.
begin
();
if
(
!
display
.
begin
(
SSD1306_SWITCHCAPVCC
,
0x3C
))
{
// Address 0x3D for 128x64
Serial
.
println
(
F
(
"SSD1306 allocation failed"
));
for
(;;);
}
delay
(
2000
);
display
.
clearDisplay
();
display
.
setTextSize
(
1
);
display
.
setTextColor
(
WHITE
);
digitalWrite
(
buzzer
,
LOW
);
digitalWrite
(
relay1
,
HIGH
);
digitalWrite
(
relay2
,
HIGH
);
digitalWrite
(
relay3
,
HIGH
);
initWiFi
();
configTime
(
0
,
0
,
ntpServer
);
// 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
;
Firebase
.
reconnectWiFi
(
true
);
fbdo
.
setResponseSize
(
4096
);
// Assign the callback function for the long running token generation task */
config
.
token_status_callback
=
tokenStatusCallback
;
//see addons/TokenHelper.h
// Assign the maximum retry of token generation
config
.
max_token_generation_retry
=
5
;
// Initialize the library with the Firebase authen and config
Firebase
.
begin
(
&
config
,
&
auth
);
// Getting the user UID might take a few seconds
Serial
.
println
(
"Getting User UID"
);
while
((
auth
.
token
.
uid
)
==
""
)
{
Serial
.
print
(
'.'
);
delay
(
1000
);
}
// Print user UID
uid
=
auth
.
token
.
uid
.
c_str
();
Serial
.
print
(
"User UID: "
);
Serial
.
println
(
uid
);
historyPath
=
"/history"
;
notification
();
}
void
loop
()
{
timestamp
=
getTime
();
Serial
.
println
((
String
)
timestamp
+
"000"
);
displayData
();
luxRead
();
ldrRead
();
ultRead
();
Read18b20
();
dhtRead
();
phRead
();
displayData
();
turbidityData
();
soilData
();
liveData
();
historyData
();
databaseRead
();
displayData
();
}
void
luxRead
()
{
lux
=
lightMeter
.
readLightLevel
();
Serial
.
print
(
"Light: "
);
Serial
.
print
(
lux
);
Serial
.
println
(
" lx"
);
delay
(
1000
);
}
void
ldrRead
()
{
ldr_data
=
digitalRead
(
ldr
);
Serial
.
print
(
"LDR: "
);
Serial
.
println
(
ldr_data
);
if
(
ldr_data
==
0
){
ldr_sta
=
"Day"
;
}
else
{
ldr_sta
=
"Night"
;
}
}
void
ultRead
()
{
digitalWrite
(
trigPin
,
LOW
);
delayMicroseconds
(
2
);
digitalWrite
(
trigPin
,
HIGH
);
delayMicroseconds
(
10
);
digitalWrite
(
trigPin
,
LOW
);
duration
=
pulseIn
(
echoPin
,
HIGH
);
distanceCm
=
duration
*
SOUND_SPEED
/
2
;
Serial
.
print
(
"Distance (cm): "
);
Serial
.
println
(
distanceCm
);
}
void
Read18b20
()
{
sensors
.
requestTemperatures
();
temperatureC
=
sensors
.
getTempCByIndex
(
0
);
Serial
.
print
(
temperatureC
);
Serial
.
println
(
"ºC"
);
}
void
dhtRead
()
{
h
=
dht
.
readHumidity
();
t
=
dht
.
readTemperature
();
if
(
isnan
(
h
)
||
isnan
(
t
))
{
Serial
.
println
(
F
(
"Failed to read from DHT sensor!"
));
return
;
}
Serial
.
print
(
F
(
"Humidity: "
));
Serial
.
print
(
h
);
Serial
.
print
(
F
(
"% Temperature: "
));
Serial
.
print
(
t
);
Serial
.
println
(
F
(
"°C "
));
}
void
phRead
()
{
Value
=
analogRead
(
potPin
);
Serial
.
print
(
Value
);
Serial
.
print
(
" | "
);
float
voltage
=
Value
*
(
3.3
/
4095.0
);
voltage
=
voltage
;
ph
=
(
3.3
*
voltage
);
Serial
.
println
(
ph
);
}
void
turbidityData
()
{
valur_t
=
analogRead
(
turbidity
);
Serial
.
print
(
"Turbidity= "
);
valur_t
=
map
(
valur_t
,
0
,
4095
,
0
,
100
);
Serial
.
print
(
valur_t
);
Serial
.
println
(
" %"
);
}
void
soilData
()
{
soil_data
=
digitalRead
(
soil
);
Serial
.
print
(
"soil data= "
);
Serial
.
println
(
soil_data
);
if
(
soil_data
==
0
){
soil_sta
=
"true"
;
}
else
{
soil_sta
=
"false"
;
}
}
void
liveData
()
{
if
((
millis
()
-
lastTime
)
>
timerDelay
)
{
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/live_data/temperature"
,
t
);
delay
(
200
);
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/live_data/humidity"
,
h
);
delay
(
200
);
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/live_data/soil"
,
soil_sta
);
delay
(
200
);
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/live_data/water_level"
,
distanceCm
);
delay
(
200
);
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/live_data/ldr_status"
,
ldr_sta
);
delay
(
200
);
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/live_data/ph"
,
ph
);
delay
(
200
);
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/live_data/water_temperature"
,
temperatureC
);
delay
(
200
);
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/live_data/lux"
,
lux
);
delay
(
200
);
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/live_data/turbidity"
,
valur_t
);
delay
(
200
);
lastTime
=
millis
();
}
}
String
data_temperature
=
"/temperature"
;
String
data_humidity
=
"/humidity"
;
String
data_soil
=
"/soil"
;
String
data_water_level
=
"/water_level"
;
String
data_ldr_status
=
"/ldr_status"
;
String
data_ph
=
"/ph"
;
String
data_water_temperature
=
"/water_temperature"
;
String
data_lux
=
"/lux"
;
String
data_pump
=
"/pump"
;
String
data_fan
=
"/fan"
;
String
data_humidity_fire
=
"/humidity_fire"
;
String
data_turbidity
=
"/turbidity"
;
String
data_timestp
=
"/timestamp"
;
void
historyData
()
{
if
((
millis
()
-
lastTime2
)
>
timerDelay2
)
{
histry
=
historyPath
+
"/"
+
String
(
timestamp
)
+
"000"
;
json
.
set
(
data_temperature
.
c_str
(),
t
);
json
.
set
(
data_humidity
.
c_str
(),
h
);
json
.
set
(
data_soil
.
c_str
(),
true
);
json
.
set
(
data_water_level
.
c_str
(),
distanceCm
);
json
.
set
(
data_ldr_status
.
c_str
(),
"Day"
);
json
.
set
(
data_ph
.
c_str
(),
ph
);
json
.
set
(
data_water_temperature
.
c_str
(),
temperatureC
);
json
.
set
(
data_lux
.
c_str
(),
lux
);
json
.
set
(
data_pump
.
c_str
(),
true
);
json
.
set
(
data_fan
.
c_str
(),
true
);
json
.
set
(
data_humidity_fire
.
c_str
(),
true
);
json
.
set
(
data_turbidity
.
c_str
(),
valur_t
);
json
.
set
(
data_timestp
,
String
(
timestamp
)
+
"000"
);
Serial
.
printf
(
"Set json... %s
\n
"
,
Firebase
.
RTDB
.
setJSON
(
&
fbdo
,
histry
.
c_str
(),
&
json
)
?
"ok"
:
fbdo
.
errorReason
().
c_str
());
lastTime2
=
millis
();
}
}
void
notification
()
{
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/notification/description"
,
"HI"
);
delay
(
200
);
Firebase
.
RTDB
.
setString
(
&
fbdo
,
"/notification/status"
,
"0"
);
delay
(
200
);
}
void
databaseRead
()
{
if
(
Firebase
.
RTDB
.
getString
(
&
fbdo
,
"/live_data/pump"
))
{
if
(
fbdo
.
dataType
()
==
"string"
)
{
String
pump_read
=
fbdo
.
stringData
();
if
(
pump_read
==
"true"
)
{
digitalWrite
(
relay1
,
LOW
);
}
else
{
digitalWrite
(
relay1
,
HIGH
);
}
}
}
if
(
Firebase
.
RTDB
.
getString
(
&
fbdo
,
"/live_data/fan"
))
{
if
(
fbdo
.
dataType
()
==
"string"
)
{
String
fan_read
=
fbdo
.
stringData
();
if
(
fan_read
==
"true"
)
{
digitalWrite
(
relay2
,
LOW
);
}
else
{
digitalWrite
(
relay2
,
HIGH
);
}
}
}
if
(
Firebase
.
RTDB
.
getString
(
&
fbdo
,
"/live_data/humidity_fire"
))
{
if
(
fbdo
.
dataType
()
==
"string"
)
{
String
humidity_fire_read
=
fbdo
.
stringData
();
if
(
humidity_fire_read
==
"true"
)
{
digitalWrite
(
relay3
,
LOW
);
}
else
{
digitalWrite
(
relay3
,
HIGH
);
}
}
}
}
void
displayData
(){
display
.
clearDisplay
();
display
.
setTextSize
(
1
);
display
.
setTextColor
(
WHITE
);
display
.
setCursor
(
35
,
0
);
display
.
println
(
"Bloomerang"
);
display
.
setCursor
(
0
,
20
);
display
.
print
(
"Temperature : "
);
display
.
println
(
t
);
display
.
print
(
"Humidity : "
);
display
.
println
(
h
);
display
.
print
(
"Ph level : "
);
display
.
println
(
ph
);
display
.
print
(
"water temp : "
);
display
.
println
(
temperatureC
);
display
.
print
(
"Turbidity : "
);
display
.
print
(
valur_t
);
display
.
println
(
" %"
);
display
.
display
();
}
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment