Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Smart_Agro
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
28
Issues
28
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
TMP-23-236
Smart_Agro
Commits
853c13dd
Commit
853c13dd
authored
Sep 07, 2023
by
RR Nimesha Manchalee
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Upload New File
parent
ccd54558
Changes
1
Show whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
292 additions
and
0 deletions
+292
-0
Soil monitoring/device.ino
Soil monitoring/device.ino
+292
-0
No files found.
Soil monitoring/device.ino
0 → 100644
View file @
853c13dd
#include <WiFi.h>
#include <HTTPClient.h>
#include <ArduinoJson.h>
#include <Arduino.h>
#include <OneWire.h>
#include <DallasTemperature.h>
#include <Wire.h>
#include <Adafruit_GFX.h>
#include <Adafruit_SSD1306.h>
#include <TinyGPS++.h>
HardwareSerial
Sender
(
1
);
const
char
*
ssid
=
"Chamith rulz"
;
const
char
*
password
=
"Chamith17321"
;
String
serverName1
=
"http://192.168.8.128:8002/api/water-prediction/create"
;
String
serverName2
=
"http://192.168.8.128:8002/api/main-device-prediction/create"
;
unsigned
long
lastTime1
=
0
;
unsigned
long
timerDelay1
=
15000
;
unsigned
long
lastTime2
=
0
;
unsigned
long
timerDelay2
=
20000
;
#define TdsSensorPin 39
#define oneWireBus 15
#define soil 34
#define turbidity 35
#define RXD2 16
#define TXD2 17
#define trigPin1 18
#define echoPin1 19
#define VREF 3.3
#define SCOUNT 30
#define SCREEN_WIDTH 128
#define SCREEN_HEIGHT 64
#define SOUND_SPEED 0.034
int
analogBuffer
[
SCOUNT
];
int
analogBufferTemp
[
SCOUNT
];
int
analogBufferIndex
=
0
;
int
copyIndex
=
0
;
float
averageVoltage
=
0
;
float
tdsValue
=
0
;
float
temperature
=
25
;
String
Latitude
,
Longitude
;
long
duration1
;
float
temperatureC
,
soildata
,
turbiditydata
,
water_temp
,
distanceCm1
,
distanceCm
;
OneWire
oneWire
(
oneWireBus
);
DallasTemperature
sensors
(
&
oneWire
);
TinyGPSPlus
gps
;
DeviceAddress
sensor1
=
{
0x28
,
0x5C
,
0x88
,
0x81
,
0xE3
,
0xF6
,
0x3C
,
0xE3
};
DeviceAddress
sensor2
=
{
0x28
,
0x76
,
0xD6
,
0x81
,
0xE3
,
0x3D
,
0x3C
,
0x3A
};
Adafruit_SSD1306
display
(
SCREEN_WIDTH
,
SCREEN_HEIGHT
,
&
Wire
,
-
1
);
void
setup
()
{
Serial
.
begin
(
9600
);
Sender
.
begin
(
9600
,
SERIAL_8N1
,
RXD2
,
TXD2
);
sensors
.
begin
();
pinMode
(
TdsSensorPin
,
INPUT
);
pinMode
(
soil
,
INPUT
);
pinMode
(
turbidity
,
INPUT
);
pinMode
(
trigPin1
,
OUTPUT
);
pinMode
(
echoPin1
,
INPUT
);
if
(
!
display
.
begin
(
SSD1306_SWITCHCAPVCC
,
0x3C
))
{
Serial
.
println
(
F
(
"SSD1306 allocation failed"
));
for
(;;);
}
WiFi
.
begin
(
ssid
,
password
);
Serial
.
println
(
"Connecting"
);
while
(
WiFi
.
status
()
!=
WL_CONNECTED
)
{
delay
(
500
);
Serial
.
print
(
"."
);
}
Serial
.
println
(
""
);
Serial
.
print
(
"Connected to WiFi network with IP Address: "
);
Serial
.
println
(
WiFi
.
localIP
());
}
void
loop
()
{
tdsRead
();
probe18b20Read
();
probe18b20Read2
();
soilRead
();
turbidityRead
();
displayData
();
ultRead1
();
if
((
millis
()
-
lastTime1
)
>
timerDelay1
)
{
water_data_send
();
lastTime1
=
millis
();
}
if
((
millis
()
-
lastTime2
)
>
timerDelay2
)
{
device_data_send
();
lastTime2
=
millis
();
}
while
(
Sender
.
available
()
>
0
)
if
(
gps
.
encode
(
Sender
.
read
()))
displayInfo
();
}
void
tdsRead
()
{
static
unsigned
long
analogSampleTimepoint
=
millis
();
if
(
millis
()
-
analogSampleTimepoint
>
40U
)
{
analogSampleTimepoint
=
millis
();
analogBuffer
[
analogBufferIndex
]
=
analogRead
(
TdsSensorPin
);
analogBufferIndex
++
;
if
(
analogBufferIndex
==
SCOUNT
)
{
analogBufferIndex
=
0
;
}
}
static
unsigned
long
printTimepoint
=
millis
();
if
(
millis
()
-
printTimepoint
>
800U
)
{
printTimepoint
=
millis
();
for
(
copyIndex
=
0
;
copyIndex
<
SCOUNT
;
copyIndex
++
)
{
analogBufferTemp
[
copyIndex
]
=
analogBuffer
[
copyIndex
];
averageVoltage
=
getMedianNum
(
analogBufferTemp
,
SCOUNT
)
*
(
float
)
VREF
/
4096.0
;
float
compensationCoefficient
=
1.0
+
0.02
*
(
temperature
-
25.0
);
float
compensationVoltage
=
averageVoltage
/
compensationCoefficient
;
tdsValue
=
(
133.42
*
compensationVoltage
*
compensationVoltage
*
compensationVoltage
-
255.86
*
compensationVoltage
*
compensationVoltage
+
857.39
*
compensationVoltage
)
*
0.5
;
Serial
.
print
(
"TDS Value: "
);
Serial
.
print
(
tdsValue
,
2
);
Serial
.
println
(
"ppm"
);
}
}
}
int
getMedianNum
(
int
bArray
[],
int
iFilterLen
)
{
int
bTab
[
iFilterLen
];
for
(
byte
i
=
0
;
i
<
iFilterLen
;
i
++
)
bTab
[
i
]
=
bArray
[
i
];
int
i
,
j
,
bTemp
;
for
(
j
=
0
;
j
<
iFilterLen
-
1
;
j
++
)
{
for
(
i
=
0
;
i
<
iFilterLen
-
j
-
1
;
i
++
)
{
if
(
bTab
[
i
]
>
bTab
[
i
+
1
])
{
bTemp
=
bTab
[
i
];
bTab
[
i
]
=
bTab
[
i
+
1
];
bTab
[
i
+
1
]
=
bTemp
;
}
}
}
if
((
iFilterLen
&
1
)
>
0
)
{
bTemp
=
bTab
[(
iFilterLen
-
1
)
/
2
];
}
else
{
bTemp
=
(
bTab
[
iFilterLen
/
2
]
+
bTab
[
iFilterLen
/
2
-
1
])
/
2
;
}
return
bTemp
;
}
void
probe18b20Read
()
{
sensors
.
requestTemperatures
();
temperatureC
=
sensors
.
getTempC
(
sensor1
);
Serial
.
print
(
"Soil temperature : "
);
Serial
.
print
(
temperatureC
);
Serial
.
println
(
"ºC"
);
}
void
probe18b20Read2
()
{
sensors
.
requestTemperatures
();
water_temp
=
sensors
.
getTempC
(
sensor2
);
Serial
.
print
(
"water_temp : "
);
Serial
.
print
(
water_temp
);
Serial
.
println
(
"ºC"
);
}
void
soilRead
()
{
soildata
=
analogRead
(
soil
);
soildata
=
map
(
soildata
,
0
,
4095
,
0
,
100
);
Serial
.
print
(
"Soil Data : "
);
Serial
.
println
(
soildata
);
}
void
turbidityRead
()
{
turbiditydata
=
analogRead
(
turbidity
);
turbiditydata
=
map
(
turbiditydata
,
0
,
4095
,
0
,
100
);
Serial
.
print
(
"Turbidity Data : "
);
Serial
.
println
(
turbiditydata
);
}
void
displayData
()
{
display
.
clearDisplay
();
display
.
setTextSize
(
1
);
display
.
setTextColor
(
WHITE
);
display
.
setCursor
(
16
,
0
);
display
.
println
(
"Welcome to Soil"
);
display
.
setCursor
(
40
,
10
);
display
.
println
(
"Analyzer"
);
display
.
setCursor
(
0
,
30
);
display
.
print
(
"TDS Value : "
);
display
.
println
(
tdsValue
);
display
.
print
(
"Soil temp : "
);
display
.
println
(
temperatureC
);
display
.
print
(
"Soil Data : "
);
display
.
println
(
soildata
);
display
.
print
(
"Turbidity : "
);
display
.
print
(
turbiditydata
);
display
.
display
();
}
void
displayInfo
()
{
if
(
gps
.
location
.
isValid
())
{
Latitude
=
gps
.
location
.
lat
();
Longitude
=
gps
.
location
.
lng
();
Serial
.
print
(
F
(
"Latitude: "
));
Serial
.
print
(
gps
.
location
.
lat
(),
6
);
Serial
.
print
(
F
(
","
));
Serial
.
print
(
F
(
"Longitude: "
));
Serial
.
println
(
gps
.
location
.
lng
(),
6
);
}
else
{
Serial
.
print
(
F
(
"INVALID"
));
}
Serial
.
println
();
}
void
ultRead1
()
{
digitalWrite
(
trigPin1
,
LOW
);
delayMicroseconds
(
2
);
digitalWrite
(
trigPin1
,
HIGH
);
delayMicroseconds
(
10
);
digitalWrite
(
trigPin1
,
LOW
);
duration1
=
pulseIn
(
echoPin1
,
HIGH
);
distanceCm1
=
duration1
*
SOUND_SPEED
/
2
;
distanceCm
=
map
(
distanceCm1
,
0
,
50
,
100
,
0
);
Serial
.
print
(
"Distance 1 (cm): "
);
Serial
.
println
(
distanceCm
);
}
void
water_data_send
(){
if
(
WiFi
.
status
()
==
WL_CONNECTED
)
{
HTTPClient
http
;
WiFiClient
client
;
String
serverPath
=
serverName1
+
"?land=1&temprature="
+
water_temp
+
"&tds="
+
tdsValue
+
"&turbidity="
+
turbiditydata
+
"&ph=15&tanklevel="
+
distanceCm
+
""
;
Serial
.
println
(
serverPath
);
http
.
begin
(
client
,
serverPath
.
c_str
());
int
httpResponseCode
=
http
.
GET
();
if
(
httpResponseCode
>
0
)
{
Serial
.
print
(
"HTTP Response code: "
);
Serial
.
println
(
httpResponseCode
);
String
payload
=
http
.
getString
();
Serial
.
println
(
payload
);
}
else
{
Serial
.
print
(
"Error code: "
);
Serial
.
println
(
httpResponseCode
);
}
http
.
end
();
}
else
{
Serial
.
println
(
"WiFi Disconnected"
);
}
}
void
device_data_send
(){
if
(
WiFi
.
status
()
==
WL_CONNECTED
)
{
HTTPClient
http
;
WiFiClient
client
;
String
serverPath
=
serverName2
+
"?land=1&soil_moisture="
+
soildata
+
"&temperature="
+
temperatureC
+
"&nitrogen=48&phosphorus=12&potassium=124&ph_level=50&lat="
+
Latitude
+
"&long="
+
Longitude
+
""
;
Serial
.
println
(
serverPath
);
http
.
begin
(
client
,
serverPath
.
c_str
());
int
httpResponseCode
=
http
.
GET
();
if
(
httpResponseCode
>
0
)
{
Serial
.
print
(
"HTTP Response code: "
);
Serial
.
println
(
httpResponseCode
);
String
payload
=
http
.
getString
();
Serial
.
println
(
payload
);
}
else
{
Serial
.
print
(
"Error code: "
);
Serial
.
println
(
httpResponseCode
);
}
http
.
end
();
}
else
{
Serial
.
println
(
"WiFi Disconnected"
);
}
}
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