Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
A
A1
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
ndl
2024-projects
A1
Commits
8499fe28
Commit
8499fe28
authored
3 months ago
by
Yuliia Denysiuk
Browse files
Options
Downloads
Plain Diff
fixing troubles with git
parents
81f11433
b0c10126
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
project_main.ino
+0
-309
0 additions, 309 deletions
project_main.ino
with
0 additions
and
309 deletions
project_main.ino
deleted
100644 → 0
+
0
−
309
View file @
81f11433
#include
<Wire.h>
#include
<BH1750.h>
#include
<Adafruit_PWMServoDriver.h>
#include
<WiFi.h>
#include
<WebServer.h>
#include
<Adafruit_NeoPixel.h>
#include
<FS.h>
#include
<SPIFFS.h>
#include
<ArduinoJson.h>
// Which pin on is connected to the NeoPixels
#define LED_PIN 6
// How many NeoPixels are attached
#define LED_COUNT 60
#define LISTEN_PORT 80
Adafruit_NeoPixel
strip
(
LED_COUNT
,
LED_PIN
,
NEO_GRB
+
NEO_KHZ800
);
const
char
*
ssid
=
"NDL_24G"
;
//name of local WiFi network in the NDL
const
char
*
password
=
"RT-AC66U"
;
WebServer
server
(
80
);
BH1750
lightsens1
;
BH1750
lightsens2
;
BH1750
lightsens3
;
TwoWire
I2C_1
=
TwoWire
(
0
);
// First I2C bus
TwoWire
I2C_2
=
TwoWire
(
1
);
// Second I2C bus
Adafruit_PWMServoDriver
pwm
=
Adafruit_PWMServoDriver
(
0x40
,
I2C_1
);
int
angle
=
210
;
// Initial angle at the middle 0
int
pulse
=
292
;
// Initial pulse of rest
int
stepSize
=
5
;
// Degree of movement per loop iteration
int
botser
=
15
;
// Big servo pin number
int
topser
=
14
;
// Small servo pin number
int
secitivity
=
10
;
// Higher the variable - slower turning
int
startDiffX
=
0
;
// Diff in luxes at the start
int
startDiffY
=
0
;
// Diff in luxes at the start
int
redPin
=
15
;
// Example GPIO pins for RGB LED
int
greenPin
=
2
;
int
bluePin
=
4
;
int
intensityPin
=
5
;
struct
LightSettings
{
String
name
;
int
red
,
green
,
blue
,
intensity
;
};
std
::
vector
<
LightSettings
>
savedLights
;
#define SERVOMIN 200 // Minimum pulse length (0 degrees)
#define SERVOMAX 500 // Maximum pulse length (180 degrees)
#define SERVO_FREQ 50 // Analog servos run at ~50 Hz
void
handleSaveLight
()
{
if
(
server
.
method
()
!=
HTTP_POST
)
{
server
.
send
(
405
,
"application/json"
,
"{
\"
status
\"
:
\"
method_not_allowed
\"
}"
);
return
;
}
// Parse the incoming JSON
DynamicJsonDocument
doc
(
1024
);
DeserializationError
error
=
deserializeJson
(
doc
,
server
.
arg
(
"plain"
));
if
(
error
)
{
server
.
send
(
400
,
"application/json"
,
"{
\"
status
\"
:
\"
invalid_json
\"
}"
);
return
;
}
// Create a new light settings structure
LightSettings
newLight
;
newLight
.
name
=
doc
[
"name"
].
as
<
String
>
();
newLight
.
red
=
doc
[
"red"
].
as
<
int
>
();
newLight
.
green
=
doc
[
"green"
].
as
<
int
>
();
newLight
.
blue
=
doc
[
"blue"
].
as
<
int
>
();
newLight
.
intensity
=
doc
[
"intensity"
].
as
<
int
>
();
// Add it to the list
savedLights
.
push_back
(
newLight
);
// Respond with success
server
.
send
(
200
,
"application/json"
,
"{
\"
status
\"
:
\"
success
\"
}"
);
}
void
handleSelectLight
()
{
// Ensure we're handling POST method
if
(
server
.
method
()
!=
HTTP_POST
)
{
server
.
send
(
405
,
"text/plain"
,
"Method Not Allowed"
);
return
;
}
// Ensure the 'plain' argument exists in the request
if
(
!
server
.
hasArg
(
"plain"
))
{
server
.
send
(
400
,
"text/plain"
,
"Bad Request"
);
return
;
}
// Parse the incoming JSON data
DynamicJsonDocument
doc
(
512
);
DeserializationError
error
=
deserializeJson
(
doc
,
server
.
arg
(
"plain"
));
if
(
error
)
{
server
.
send
(
400
,
"text/plain"
,
"Invalid JSON"
);
return
;
}
// Get the index from the JSON
int
index
=
doc
[
"index"
];
// Ensure the index is valid
if
(
index
<
0
||
index
>=
savedLights
.
size
())
{
server
.
send
(
400
,
"text/plain"
,
"Invalid Index"
);
return
;
}
// Select the light from savedLights
LightSettings
selectedLight
=
savedLights
[
index
];
// Apply the light settings to your hardware (uncomment and modify as needed)
/*analogWrite(RED_PIN, selectedLight.red);
analogWrite(GREEN_PIN, selectedLight.green);
analogWrite(BLUE_PIN, selectedLight.blue);
analogWrite(INTENSITY_PIN, selectedLight.intensity);*/
// Respond with a success message
server
.
send
(
200
,
"application/json"
,
"{
\"
message
\"
:
\"
Light applied
\"
}"
);
}
void
handleGetSavedLights
()
{
DynamicJsonDocument
doc
(
1024
);
JsonArray
array
=
doc
.
to
<
JsonArray
>
();
for
(
const
auto
&
light
:
savedLights
)
{
JsonObject
obj
=
array
.
createNestedObject
();
obj
[
"name"
]
=
light
.
name
;
obj
[
"red"
]
=
light
.
red
;
obj
[
"green"
]
=
light
.
green
;
obj
[
"blue"
]
=
light
.
blue
;
obj
[
"intensity"
]
=
light
.
intensity
;
}
String
json
;
serializeJson
(
doc
,
json
);
server
.
send
(
200
,
"application/json"
,
json
);
}
void
setup
()
{
Serial
.
begin
(
115200
);
//WEBSITE
WiFi
.
begin
(
ssid
,
password
);
// Start Wi-Fi connection to specified access point
Serial
.
println
(
"Start connecting."
);
while
(
WiFi
.
status
()
!=
WL_CONNECTED
)
{
// Wait until we are connected
delay
(
500
);
Serial
.
print
(
"."
);
}
Serial
.
print
(
"Connected to "
);
Serial
.
print
(
ssid
);
Serial
.
print
(
", IP address: "
);
Serial
.
println
(
WiFi
.
localIP
());
// Print local IP to Serial Monitor
if
(
!
SPIFFS
.
begin
(
true
))
{
Serial
.
println
(
"An Error has occurred while mounting SPIFFS"
);
return
;
}
server
.
on
(
"/"
,
HTTP_GET
,
[]()
{
File
file
=
SPIFFS
.
open
(
"/page.html"
,
"r"
);
if
(
!
file
)
{
server
.
send
(
404
,
"text/plain"
,
"File Not Found"
);
return
;
}
server
.
streamFile
(
file
,
"text/html"
);
file
.
close
();
});
server
.
on
(
"/updateLightSettings"
,
HTTP_POST
,
[]()
{
String
json
=
server
.
arg
(
"plain"
);
DynamicJsonDocument
doc
(
1024
);
deserializeJson
(
doc
,
json
);
int
red
=
doc
[
"red"
];
int
green
=
doc
[
"green"
];
int
blue
=
doc
[
"blue"
];
int
intensity
=
doc
[
"intensity"
];
// Map intensity and colors to the appropriate range
analogWrite
(
redPin
,
red
*
intensity
/
255
);
analogWrite
(
greenPin
,
green
*
intensity
/
255
);
analogWrite
(
bluePin
,
blue
*
intensity
/
255
);
Serial
.
print
(
"Red: "
);
Serial
.
print
(
red
);
Serial
.
print
(
" Green: "
);
Serial
.
print
(
green
);
Serial
.
print
(
" Blue: "
);
Serial
.
print
(
blue
);
Serial
.
print
(
" Intensity: "
);
Serial
.
println
(
intensity
);
server
.
send
(
200
,
"application/json"
,
"{
\"
status
\"
:
\"
success
\"
}"
);
});
server
.
on
(
"/selectLight"
,
HTTP_POST
,
handleSelectLight
);
server
.
on
(
"/saveLight"
,
HTTP_POST
,
handleSaveLight
);
server
.
on
(
"/getSavedLights"
,
HTTP_GET
,
handleGetSavedLights
);
server
.
begin
();
Serial
.
println
(
"HTTP server started"
);
//END WEBSITE
//LIGHTS
strip
.
begin
();
// INITIALIZE NeoPixel strip object (REQUIRED)
strip
.
show
();
// Turn OFF all pixels ASAP
strip
.
setBrightness
(
50
);
// Set BRIGHTNESS to about 1/5 (max = 255)
//END LIGHTS
//LIGHT SENSORS
// Initialize the first I2C bus (I2C_1) on pins GPIO 21 (SDA) and GPIO 22 (SCL)
I2C_1
.
begin
(
21
,
22
);
// SDA, SCL
// Initialize the second I2C bus (I2C_2) on different pins, for example GPIO 25 (SDA) and GPIO 26 (SCL)
I2C_2
.
begin
(
25
,
26
);
// SDA, SCL
if
(
lightsens1
.
begin
(
BH1750
::
CONTINUOUS_HIGH_RES_MODE
,
0x23
,
&
I2C_1
))
{
Serial
.
println
(
F
(
"BH1750 Advanced begin 1"
));
}
else
{
Serial
.
println
(
F
(
"Error initialising BH1750 1"
));
}
if
(
lightsens2
.
begin
(
BH1750
::
CONTINUOUS_HIGH_RES_MODE
,
0x5C
,
&
I2C_1
))
{
Serial
.
println
(
F
(
"BH1750 Advanced begin 2"
));
}
else
{
Serial
.
println
(
F
(
"Error initialising BH1750 2"
));
}
if
(
lightsens3
.
begin
(
BH1750
::
CONTINUOUS_HIGH_RES_MODE
,
0x23
,
&
I2C_2
))
{
Serial
.
println
(
F
(
"BH1750 Advanced begin 3"
));
}
else
{
Serial
.
println
(
F
(
"Error initialising BH1750 3"
));
}
uint16_t
strtlux1
=
lightsens1
.
readLightLevel
();
uint16_t
strtlux2
=
lightsens2
.
readLightLevel
();
uint16_t
strtlux3
=
lightsens3
.
readLightLevel
();
startDiffX
=
strtlux1
-
strtlux2
;
startDiffY
=
strtlux3
-
(
strtlux1
+
strtlux2
)
/
2
;
//END LIGHT SENSORS
//SERVOS
pwm
.
begin
();
pwm
.
setOscillatorFrequency
(
27000000
);
pwm
.
setPWMFreq
(
SERVO_FREQ
);
// Set frequency to 50 Hz for servo control
pwm
.
setPWM
(
botser
,
0
,
pulse
);
pwm
.
setPWM
(
topser
,
0
,
angle
);
//END SERVOS
Serial
.
println
(
"Waiting..."
);
delay
(
1000
);
Serial
.
println
(
"Finished setup"
);
}
// Calculate the difference in light levels
int
difToMove
(
int
lux1
,
int
lux2
,
int
startDiff
)
{
int
diff
=
0
;
//diff = (lux1 - lux2 - startDiff)/secitivity;
diff
=
(
lux1
-
lux2
)
/
secitivity
;
return
diff
;
}
void
loop
()
{
for
(
int
i
=
0
;
i
<
strip
.
numPixels
();
i
++
){
strip
.
setPixelColor
(
i
,
strip
.
Color
(
127
,
127
,
127
));
}
int
p
=
pulse
;
// update pulse to the initial state every loop so it would not move forever
uint16_t
lux1
=
lightsens1
.
readLightLevel
();
uint16_t
lux2
=
lightsens2
.
readLightLevel
();
uint16_t
lux3
=
lightsens3
.
readLightLevel
();
uint16_t
lux4
=
(
lux1
+
lux2
)
/
2
;
// adding this for easier control of top servo
Serial
.
printf
(
"light1= %d
\n
light2= %d
\n
light3= %d
\n
combined top light = %d
\n
"
,
lux1
,
lux2
,
lux3
,
lux4
);
//Serial.printf("light1= %d \nlight2= %d", lux1, lux2);
int
xdiff
=
difToMove
(
lux2
,
lux1
,
startDiffX
);
// Determine the step based on light difference
int
ydiff
=
difToMove
(
lux3
,
lux4
,
startDiffY
);
Serial
.
printf
(
"xdiff: %d
\n
ydiff: %d
\n
"
,
xdiff
,
ydiff
);
//Serial.printf("xdiff: %d \n", xdiff);
// Add after the small servo have been connected
angle
=
angle
+
ydiff
;
angle
=
max
(
SERVOMIN
,
angle
);
angle
=
min
(
SERVOMAX
,
angle
);
p
=
p
+
xdiff
;
//We can add some restrictions, but so far there are no way we can go over max or under min
Serial
.
printf
(
"New pulse: %d
\n
New angle: %d
\n
"
,
p
,
angle
);
pwm
.
setPWM
(
botser
,
0
,
p
);
pwm
.
setPWM
(
topser
,
0
,
angle
);
delay
(
50
);
// Small delay before the next loop iteration
server
.
handleClient
();
}
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment