Copyright ©

Mongoose OS Forum


System restarts after running HTTP endpoint handler

I have an existing ESP32 app programmed in C++ and I'm trying to add a layer to allow Wi-fi config update through its http server. I registered an endpoint handler with the following line:
mgos_register_http_endpoint("/wifi_config/", wifi_config_update, NULL);

The handler function is as follows (I haven't yet tried to actually update the config, just checking that I'm receiving the data correctly from the http post request)

static void wifi_config_update(struct mg_connection *c, int ev, void *p,
                               void *user_data) 
    if (ev != MG_EV_HTTP_REQUEST) return;
    LOG(LL_INFO, ("Wi-Fi credentials update requested"));

    struct http_message *hm = (struct http_message *) p;
    char http_body[64];
    snprintf(http_body, hm->body.len + 1, "%s", hm->body.p);
    printf("HTTP request body: %s\n", http_body);

    mg_send_response_line(c, 200,
                          "Content-Type: text/html\r\n");
    mg_printf(c, "%s\r\n", "Wi-Fi credentials updated");
    c->flags |= (MG_F_SEND_AND_CLOSE | MG_F_USER_5);
    (void) user_data;

I send the data using a simple html form with ssid and password fields. The data is read correctly but for some reason mongoose os unmounts the file system and restarts immediately afterwards. Log extract below:

[Jan 11 15:16:01.117] mgos_http_ev         0x3ffba1ac HTTP connection from
[Jan 11 15:16:01.123] wifi_config_update   Wi-Fi credentials update requested
[Jan 11 15:16:01.130] HTTP request body: ssid=testing&password=123
[Jan 11 15:16:01.182] mgos_vfs_umount_all  Unmounting filesystems
[Jan 11 15:16:01.189] mgos_vfs_dev_close   0x3ffc1054 refs 0
[Jan 11 15:16:01.195] I (614405) wifi: state: run -> init (0)
[Jan 11 15:16:01.209] I (614415) wifi: n:11 0, o:11 0, ap:255 255, sta:11 0, prof:1
[Jan 11 15:16:01.211] I (614425) wifi: pm stop, total sleep time: 0/607722966
[Jan 11 15:16:01.218]
[Jan 11 15:16:01.221] esp32_wifi_ev        Disconnected from DEGSky, reason: 8
[Jan 11 15:16:01.231] I (614435) wifi: flush txq
[Jan 11 15:16:01.232] I (614435) wifi: stop sw txq
[Jan 11 15:16:01.233] I (614445) wifi: lmac stop hw txq
[Jan 11 15:16:01.235] I (614445) wifi: Deinit lldesc rx mblock:10
[Jan 11 15:16:01.240] mgos_system_restart  Restarting

Can someone help point out what's wrong? The restart only happens after going through the handler i.e. if I use the same html form to send the data to a different endpoint (without a handler registered), the log indicates the http post request received and continues normally (no restart triggered). I'm using the release version of mongoose os (1.23)


  • Why are you setting the MG_F_USER_5 flag to c->flags? The http_server library uses it for reloading config, and that requires reboot

    Thanked by 1farzan
  • farzanfarzan Singapore

    Ah, I see. I just followed the example given in mgos_http_server.h (reproduced below). Got a compile error with MGOS_F_RELOAD_CONFIG as it is not defined in the header file but in mgos_http_server.c (why is that, by the way?) so replaced it with its definition MG_F_USER_5. Because of the lost context, I didn't notice what the flag was doing.

      static void foo_handler(struct mg_connection *c, int ev, void *p,
                              void *user_data) {
        (void) p;
        if (ev != MG_EV_HTTP_REQUEST) return;
        LOG(LL_INFO, ("Foo requested"));
        mg_send_response_line(c, 200,
                              "Content-Type: text/html\r\n");
        mg_printf(c, "%s\r\n", "Fooooo");
        c->flags |= (MG_F_SEND_AND_CLOSE | MGOS_F_RELOAD_CONFIG);
        (void) user_data;

    So I guess I just need to replace the line with c->flags |= MG_F_SEND_AND_CLOSE; and it should work properly

Sign In or Register to comment.