Wifi fully seperate from web-workflow
This commit is contained in:
parent
9eaae37dfc
commit
5869af395e
|
@ -68,12 +68,14 @@ conflicts with user created NUS services.
|
||||||
Read-only characteristic that returns the UTF-8 encoded version string.
|
Read-only characteristic that returns the UTF-8 encoded version string.
|
||||||
|
|
||||||
## Web
|
## Web
|
||||||
|
If the keys `CIRCUITPY_WIFI_SSID` and `CIRCUITPY_WIFI_PASSWORD` are set in `settings.toml`,
|
||||||
|
CircuitPython will automatically connect to the given Wi-Fi network on boot and upon reload.
|
||||||
|
|
||||||
The web workflow is depends on adding Wi-Fi credentials into the `settings.toml` file. The keys are
|
If `CIRCUITPY_WEB_API_PASSWORD` is also set, the web workflow will also start.
|
||||||
`CIRCUITPY_WIFI_SSID` and `CIRCUITPY_WIFI_PASSWORD`. Once these are defined, CircuitPython will
|
The web workflow will only be enabled if the Wi-Fi connection succeeds upon boot.
|
||||||
automatically connect to the network and start the webserver used for the workflow. The webserver
|
|
||||||
is on port 80 unless overridden by `CIRCUITPY_WEB_API_PORT`. It also enables MDNS. The name
|
The webserver is on port 80 unless overridden by `CIRCUITPY_WEB_API_PORT`. It also enables MDNS.
|
||||||
of the board as advertised to the network can be overridden by `CIRCUITPY_WEB_INSTANCE_NAME`.
|
The name of the board as advertised to the network can be overridden by `CIRCUITPY_WEB_INSTANCE_NAME`.
|
||||||
|
|
||||||
Here is an example `/settings.toml`:
|
Here is an example `/settings.toml`:
|
||||||
|
|
||||||
|
@ -82,7 +84,7 @@ Here is an example `/settings.toml`:
|
||||||
CIRCUITPY_WIFI_SSID="scottswifi"
|
CIRCUITPY_WIFI_SSID="scottswifi"
|
||||||
CIRCUITPY_WIFI_PASSWORD="secretpassword"
|
CIRCUITPY_WIFI_PASSWORD="secretpassword"
|
||||||
|
|
||||||
# To enable modifying files from the web. Change this too!
|
# To enable the the webserver. Change this too!
|
||||||
# Leave the User field blank in the browser.
|
# Leave the User field blank in the browser.
|
||||||
CIRCUITPY_WEB_API_PASSWORD="passw0rd"
|
CIRCUITPY_WEB_API_PASSWORD="passw0rd"
|
||||||
|
|
||||||
|
|
|
@ -258,19 +258,9 @@ void supervisor_web_workflow_status(void) {
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
bool supervisor_start_web_workflow(void) {
|
bool supervisor_start_web_workflow(bool reload) {
|
||||||
#if CIRCUITPY_WEB_WORKFLOW && CIRCUITPY_WIFI && CIRCUITPY_OS_GETENV
|
#if CIRCUITPY_WEB_WORKFLOW && CIRCUITPY_WIFI && CIRCUITPY_OS_GETENV
|
||||||
|
|
||||||
// Skip starting the workflow if we're not starting from power on or reset.
|
|
||||||
const mcu_reset_reason_t reset_reason = common_hal_mcu_processor_get_reset_reason();
|
|
||||||
if (reset_reason != RESET_REASON_POWER_ON &&
|
|
||||||
reset_reason != RESET_REASON_RESET_PIN &&
|
|
||||||
reset_reason != RESET_REASON_DEEP_SLEEP_ALARM &&
|
|
||||||
reset_reason != RESET_REASON_UNKNOWN &&
|
|
||||||
reset_reason != RESET_REASON_SOFTWARE) {
|
|
||||||
return false;
|
|
||||||
}
|
|
||||||
|
|
||||||
char ssid[33];
|
char ssid[33];
|
||||||
char password[64];
|
char password[64];
|
||||||
|
|
||||||
|
@ -287,11 +277,6 @@ bool supervisor_start_web_workflow(void) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
result = common_hal_os_getenv_str("CIRCUITPY_WEB_INSTANCE_NAME", web_instance_name, sizeof(web_instance_name));
|
|
||||||
if (result != GETENV_OK || web_instance_name[0] == '\0') {
|
|
||||||
strcpy(web_instance_name, MICROPY_HW_BOARD_NAME);
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj)) {
|
if (!common_hal_wifi_radio_get_enabled(&common_hal_wifi_radio_obj)) {
|
||||||
common_hal_wifi_init(false);
|
common_hal_wifi_init(false);
|
||||||
common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, true);
|
common_hal_wifi_radio_set_enabled(&common_hal_wifi_radio_obj, true);
|
||||||
|
@ -303,6 +288,7 @@ bool supervisor_start_web_workflow(void) {
|
||||||
// We can all connect again because it will return early if we're already connected to the
|
// We can all connect again because it will return early if we're already connected to the
|
||||||
// network. If we are connected to a different network, then it will disconnect before
|
// network. If we are connected to a different network, then it will disconnect before
|
||||||
// attempting to connect to the given network.
|
// attempting to connect to the given network.
|
||||||
|
|
||||||
_wifi_status = common_hal_wifi_radio_connect(
|
_wifi_status = common_hal_wifi_radio_connect(
|
||||||
&common_hal_wifi_radio_obj, (uint8_t *)ssid, strlen(ssid), (uint8_t *)password, strlen(password),
|
&common_hal_wifi_radio_obj, (uint8_t *)ssid, strlen(ssid), (uint8_t *)password, strlen(password),
|
||||||
0, 8, NULL, 0);
|
0, 8, NULL, 0);
|
||||||
|
@ -312,6 +298,24 @@ bool supervisor_start_web_workflow(void) {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Skip starting the workflow if we're not starting from power on or reset.
|
||||||
|
const mcu_reset_reason_t reset_reason = common_hal_mcu_processor_get_reset_reason();
|
||||||
|
if (reset_reason != RESET_REASON_POWER_ON &&
|
||||||
|
reset_reason != RESET_REASON_RESET_PIN &&
|
||||||
|
reset_reason != RESET_REASON_DEEP_SLEEP_ALARM &&
|
||||||
|
reset_reason != RESET_REASON_UNKNOWN &&
|
||||||
|
reset_reason != RESET_REASON_SOFTWARE) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool initialized = pool.base.type == &socketpool_socketpool_type;
|
||||||
|
|
||||||
|
if (!initialized && !reload) {
|
||||||
|
result = common_hal_os_getenv_str("CIRCUITPY_WEB_INSTANCE_NAME", web_instance_name, sizeof(web_instance_name));
|
||||||
|
if (result != GETENV_OK || web_instance_name[0] == '\0') {
|
||||||
|
strcpy(web_instance_name, MICROPY_HW_BOARD_NAME);
|
||||||
|
}
|
||||||
|
|
||||||
// (leaves new_port unchanged on any failure)
|
// (leaves new_port unchanged on any failure)
|
||||||
(void)common_hal_os_getenv_int("CIRCUITPY_WEB_API_PORT", &web_api_port);
|
(void)common_hal_os_getenv_int("CIRCUITPY_WEB_API_PORT", &web_api_port);
|
||||||
|
|
||||||
|
@ -320,13 +324,10 @@ bool supervisor_start_web_workflow(void) {
|
||||||
if (result == GETENV_OK) {
|
if (result == GETENV_OK) {
|
||||||
_api_password[0] = ':';
|
_api_password[0] = ':';
|
||||||
_base64_in_place(_api_password, strlen(_api_password), sizeof(_api_password) - 1);
|
_base64_in_place(_api_password, strlen(_api_password), sizeof(_api_password) - 1);
|
||||||
} else {
|
} else { // Skip starting web-workflow when no password is passed.
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool first_start = pool.base.type != &socketpool_socketpool_type;
|
|
||||||
|
|
||||||
if (first_start) {
|
|
||||||
pool.base.type = &socketpool_socketpool_type;
|
pool.base.type = &socketpool_socketpool_type;
|
||||||
common_hal_socketpool_socketpool_construct(&pool, &common_hal_wifi_radio_obj);
|
common_hal_socketpool_socketpool_construct(&pool, &common_hal_wifi_radio_obj);
|
||||||
|
|
||||||
|
@ -336,6 +337,9 @@ bool supervisor_start_web_workflow(void) {
|
||||||
websocket_init();
|
websocket_init();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
initialized = pool.base.type == &socketpool_socketpool_type;
|
||||||
|
|
||||||
|
if (initialized){
|
||||||
if (!common_hal_socketpool_socket_get_closed(&active)) {
|
if (!common_hal_socketpool_socket_get_closed(&active)) {
|
||||||
common_hal_socketpool_socket_close(&active);
|
common_hal_socketpool_socket_close(&active);
|
||||||
}
|
}
|
||||||
|
@ -343,7 +347,8 @@ bool supervisor_start_web_workflow(void) {
|
||||||
#if CIRCUITPY_MDNS
|
#if CIRCUITPY_MDNS
|
||||||
// Try to start MDNS if the user deinited it.
|
// Try to start MDNS if the user deinited it.
|
||||||
if (mdns.base.type != &mdns_server_type ||
|
if (mdns.base.type != &mdns_server_type ||
|
||||||
common_hal_mdns_server_deinited(&mdns)) {
|
common_hal_mdns_server_deinited(&mdns) ||
|
||||||
|
reload) { // Always reconstruct on reload, since we don't know if the net changed.
|
||||||
mdns_server_construct(&mdns, true);
|
mdns_server_construct(&mdns, true);
|
||||||
mdns.base.type = &mdns_server_type;
|
mdns.base.type = &mdns_server_type;
|
||||||
if (!common_hal_mdns_server_deinited(&mdns)) {
|
if (!common_hal_mdns_server_deinited(&mdns)) {
|
||||||
|
@ -367,6 +372,8 @@ bool supervisor_start_web_workflow(void) {
|
||||||
#endif
|
#endif
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
void web_workflow_send_raw(socketpool_socket_obj_t *socket, const uint8_t *buf, int len) {
|
void web_workflow_send_raw(socketpool_socket_obj_t *socket, const uint8_t *buf, int len) {
|
||||||
int total_sent = 0;
|
int total_sent = 0;
|
||||||
|
|
|
@ -36,7 +36,7 @@
|
||||||
void supervisor_web_workflow_background(void *data);
|
void supervisor_web_workflow_background(void *data);
|
||||||
bool supervisor_web_workflow_status_dirty(void);
|
bool supervisor_web_workflow_status_dirty(void);
|
||||||
void supervisor_web_workflow_status(void);
|
void supervisor_web_workflow_status(void);
|
||||||
bool supervisor_start_web_workflow(void);
|
bool supervisor_start_web_workflow(bool);
|
||||||
void supervisor_stop_web_workflow(void);
|
void supervisor_stop_web_workflow(void);
|
||||||
|
|
||||||
// Share the MDNS object with user code.
|
// Share the MDNS object with user code.
|
||||||
|
|
|
@ -56,8 +56,9 @@ void supervisor_workflow_reset(void) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if CIRCUITPY_WEB_WORKFLOW
|
#if CIRCUITPY_WEB_WORKFLOW
|
||||||
|
bool result = supervisor_start_web_workflow(true);
|
||||||
if (workflow_background_cb.fun) {
|
if (workflow_background_cb.fun) {
|
||||||
if (supervisor_start_web_workflow()) {
|
if (result) {
|
||||||
supervisor_workflow_request_background();
|
supervisor_workflow_request_background();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -105,7 +106,7 @@ void supervisor_workflow_start(void) {
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if CIRCUITPY_WEB_WORKFLOW
|
#if CIRCUITPY_WEB_WORKFLOW
|
||||||
if (supervisor_start_web_workflow()) {
|
if (supervisor_start_web_workflow(false)) {
|
||||||
// Enable background callbacks if web_workflow startup successful
|
// Enable background callbacks if web_workflow startup successful
|
||||||
memset(&workflow_background_cb, 0, sizeof(workflow_background_cb));
|
memset(&workflow_background_cb, 0, sizeof(workflow_background_cb));
|
||||||
workflow_background_cb.fun = supervisor_web_workflow_background;
|
workflow_background_cb.fun = supervisor_web_workflow_background;
|
||||||
|
|
Loading…
Reference in New Issue