Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
N
neOCampus-arduino
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
GIS-neOCampus
neOSensor
neOCampus-arduino
Commits
519e117b
Commit
519e117b
authored
2 years ago
by
thiebolt
Browse files
Options
Downloads
Patches
Plain Diff
update
parent
7617c063
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
tests/nvs_namespace/nvs_namespace.ino
+114
-15
114 additions, 15 deletions
tests/nvs_namespace/nvs_namespace.ino
with
114 additions
and
15 deletions
tests/nvs_namespace/nvs_namespace.ino
+
114
−
15
View file @
519e117b
...
@@ -5,6 +5,8 @@
...
@@ -5,6 +5,8 @@
* ---
* ---
* TODO:
* TODO:
* - add support for JSON structure to embedds several WiFi credentials
* - add support for JSON structure to embedds several WiFi credentials
* e.g NVS-WiFi namespace --> key("credentials") = [ ["ssid1","pass1"], ["ssid2","pass2"], ... ]
* - add encryption with MAC addr or APpassword for example
* ---
* ---
* F.Thiebolt may.23 initial release
* F.Thiebolt may.23 initial release
*/
*/
...
@@ -71,6 +73,9 @@ void endLoop( void ) {
...
@@ -71,6 +73,9 @@ void endLoop( void ) {
// --- MAIN --------------------------------------------------------------------
// --- MAIN --------------------------------------------------------------------
void
setup
()
{
void
setup
()
{
char
_answer
,
_tmp
[
256
];
String
_str
;
delay
(
5000
);
delay
(
5000
);
Serial
.
begin
(
115200
);
// debug link
Serial
.
begin
(
115200
);
// debug link
Serial
.
println
(
F
(
"
\n\n\n
[NVS] namespace demo ..."
));
Serial
.
flush
();
Serial
.
println
(
F
(
"
\n\n\n
[NVS] namespace demo ..."
));
Serial
.
flush
();
...
@@ -79,27 +84,121 @@ void setup() {
...
@@ -79,27 +84,121 @@ void setup() {
#ifdef ESP32
#ifdef ESP32
/* NVS @ ESP32 */
/* NVS @ ESP32 */
Preferences
nvs_wifi
;
Preferences
nvs_wifi
;
if
(
nvs_wifi
.
begin
(
WIFI_NVS_NAMESPACE
,
true
)
)
{
// readonly mode
Serial
.
print
(
F
(
"
\n
[wifiParams] opened NVS WiFi credentials namespace ..."
));
Serial
.
flush
();
/*
// retrieve SSID/psk
* Open NVS-WiFi namespace to retrieve SSID / PSK
nvs_wifi
.
getBytes
(
WIFI_NVS_SSID_KEY
,
mySSID
,
sizeof
(
mySSID
));
*/
nvs_wifi
.
getBytes
(
WIFI_NVS_PASS_KEY
,
myPSK
,
sizeof
(
myPSK
));
if
(
not
nvs_wifi
.
begin
(
WIFI_NVS_NAMESPACE
,
true
)
)
{
// readonly mode
if
(
strlen
(
mySSID
)
and
strlen
(
myPSK
)
)
{
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] unable to open NVS namespace '"
));
Serial
.
print
(
WIFI_NVS_NAMESPACE
);
Serial
.
print
(
F
(
"' ... reboot !"
));
Serial
.
flush
();
// using retrived credentials to connect
delay
(
2000
);
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] retreieved WiFi credentials from NVS :)"
));
Serial
.
flush
();
ESP
.
restart
();
}
delay
(
5000
);
else
{
}
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] SSID and/or PSK missing ... cancel :("
));
Serial
.
flush
();
mySSID
[
0
]
=
'\0'
;
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] opened NVS WiFi credentials namespace ..."
));
Serial
.
flush
();
myPSK
[
0
]
=
'\0'
;
// retrieve SSID/psk
mySSID
[
0
]
=
'\0'
;
myPSK
[
0
]
=
'\0'
;
if
(
nvs_wifi
.
isKey
(
WIFI_NVS_SSID_KEY
)
)
{
nvs_wifi
.
getBytes
(
WIFI_NVS_SSID_KEY
,
mySSID
,
sizeof
(
mySSID
));
}
if
(
nvs_wifi
.
isKey
(
WIFI_NVS_PASS_KEY
)
)
{
nvs_wifi
.
getBytes
(
WIFI_NVS_PASS_KEY
,
myPSK
,
sizeof
(
myPSK
));
}
// close NVS namespace
nvs_wifi
.
end
();
if
(
strlen
(
mySSID
)
and
strlen
(
myPSK
)
)
{
// using retrieved credentials to connect
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] retrieved WiFi credentials from NVS :)"
));
Serial
.
flush
();
snprintf
(
_tmp
,
sizeof
(
_tmp
),
"
\n\t
SSID : %s
\n\t
PASS : %s"
,
mySSID
,
myPSK
);
Serial
.
print
(
_tmp
);
Serial
.
flush
();
delay
(
1000
);
// do we need to change them ?
Serial
.
setTimeout
(
3000
);
Serial
.
print
(
F
(
"
\n
"
));
Serial
.
print
(
F
(
"
\n
Do we need to change them (Y/n) ? "
));
Serial
.
flush
();
_answer
=
'\0'
;
if
(
not
Serial
.
readBytes
(
&
_answer
,
1
)
or
(
_answer
!=
'Y'
and
_answer
!=
'y'
)
)
{
Serial
.
print
(
F
(
"
\n\t
... ok let's keep current WiFi credentials ... "
));
delay
(
1000
);
return
;
}
}
}
/*
* Entering SSID / pass to save to NVS
*/
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] now setting WiFi credentials (15s timeout) ... "
));
Serial
.
flush
();
Serial
.
print
(
F
(
"
\n\t
SSID --> "
));
Serial
.
flush
();
Serial
.
setTimeout
(
15000
);
_str
=
Serial
.
readString
();
//read until timeout
_str
.
trim
();
// remove any \r \n whitespace at the end of the String
if
(
not
_str
.
length
()
)
{
Serial
.
print
(
F
(
"
\n\t
no input detected ... reboot !"
));
Serial
.
flush
();
delay
(
1000
);
ESP
.
restart
();
delay
(
5000
);
}
strncpy
(
mySSID
,
_str
.
c_str
(),
sizeof
(
mySSID
));
mySSID
[
sizeof
(
mySSID
)
-
1
]
=
'\0'
;
Serial
.
print
(
F
(
"
\n\t
PASS --> "
));
Serial
.
flush
();
Serial
.
setTimeout
(
15000
);
_str
=
Serial
.
readString
();
//read until timeout
_str
.
trim
();
// remove any \r \n whitespace at the end of the String
if
(
not
_str
.
length
()
)
{
Serial
.
print
(
F
(
"
\n\t
no input detected ... reboot !"
));
Serial
.
flush
();
delay
(
1000
);
ESP
.
restart
();
delay
(
5000
);
}
strncpy
(
myPSK
,
_str
.
c_str
(),
sizeof
(
myPSK
));
myPSK
[
sizeof
(
myPSK
)
-
1
]
=
'\0'
;
// save it to NVS ?
Serial
.
print
(
F
(
"
\n
"
));
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] entered crdentials :)"
));
Serial
.
flush
();
snprintf
(
_tmp
,
sizeof
(
_tmp
),
"
\n\t
SSID : %s
\n\t
PASS : %s"
,
mySSID
,
myPSK
);
Serial
.
print
(
_tmp
);
Serial
.
flush
();
delay
(
1000
);
Serial
.
setTimeout
(
3000
);
Serial
.
print
(
F
(
"
\n
Save it to NVS (Y/n) ? "
));
Serial
.
flush
();
_answer
=
'\0'
;
if
(
not
Serial
.
readBytes
(
&
_answer
,
1
)
or
(
_answer
!=
'Y'
and
_answer
!=
'y'
)
)
{
Serial
.
print
(
F
(
"
\n\t
... NOT saving WiFi credentials ... reboot ... "
));
delay
(
1000
);
ESP
.
restart
();
delay
(
5000
);
}
// saving to namespace :)
if
(
not
nvs_wifi
.
begin
(
WIFI_NVS_NAMESPACE
,
false
)
)
{
// R/W mode
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] unable to open NVS namespace '"
));
Serial
.
print
(
WIFI_NVS_NAMESPACE
);
Serial
.
print
(
F
(
"' ... reboot !"
));
Serial
.
flush
();
delay
(
2000
);
ESP
.
restart
();
delay
(
5000
);
}
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] save WiFi credentials to NVS namespace '"
));
Serial
.
print
(
WIFI_NVS_NAMESPACE
);
Serial
.
print
(
F
(
"' ... "
));
Serial
.
flush
();
if
(
nvs_wifi
.
putBytes
(
WIFI_NVS_SSID_KEY
,
mySSID
,
strlen
(
mySSID
)
+
1
)
!=
strlen
(
mySSID
)
+
1
)
{
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] ERROR while saving SSID to NVS ?!?!"
));
Serial
.
flush
();
delay
(
1000
);
ESP
.
restart
();
delay
(
5000
);
}
if
(
nvs_wifi
.
putBytes
(
WIFI_NVS_PASS_KEY
,
myPSK
,
strlen
(
myPSK
)
+
1
)
!=
strlen
(
myPSK
)
+
1
)
{
Serial
.
print
(
F
(
"
\n
[NVS-WiFi] ERROR while saving PSK to NVS ?!?!"
));
Serial
.
flush
();
delay
(
1000
);
ESP
.
restart
();
delay
(
5000
);
}
// close NVS namespace
nvs_wifi
.
end
();
// successfully saved WiFi credentials to NVS :)
Serial
.
print
(
F
(
"
\n\n
[NVS-WiFi] successfully saved WiFi credentials to NVS :)"
));
return
;
#elif defined (ESP8266)
#elif defined (ESP8266)
/* ESP8266 retrieve WiFi credentials from previous connexion */
/* ESP8266 retrieve WiFi credentials from previous connexion */
#error "NOT YET IMPLEMENTED
#error "NOT YET IMPLEMENTED
#endif
#endif
}
// start WiFi connexion either with DEFAULTS WiFi credentials or from retrieved ones
// start WiFi connexion either with DEFAULTS WiFi credentials or from retrieved ones
//to be continued
//to be continued
...
@@ -110,7 +209,7 @@ void setup() {
...
@@ -110,7 +209,7 @@ void setup() {
// --- LOOP --------------------------------------------------------------------
// --- LOOP --------------------------------------------------------------------
void
loop
()
{
void
loop
()
{
// do we want to reboot ?
/*
/*
* do something here ...
* do something here ...
*/
*/
...
...
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