mongoose setup

linuxpaul
Posts: 43
Joined: Thu Jul 20, 2017 6:10 pm

mongoose setup

Postby linuxpaul » Sun Aug 20, 2017 11:34 am

Hi,

after some days off, I relaunched my attempts with mongoose yesterday.
The challenge: viewtopic.php?f=2&t=2695
The Code:

Code: Select all

#include "freertos/FreeRTOS.h"
#include "sdkconfig.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "spiffs_vfs.h"
#include "esp_vfs.h"
#include "esp_vfs_fat.h"
#include "driver/gpio.h"
#include <sys/fcntl.h>
#include "string.h"
#include "mongoose.h"

static struct mg_serve_http_opts s_opts;

static void mongoose_event_handler(struct mg_connection *nc, int ev, void *evData)
{
	struct http_message *hm = (struct http_message *) evData;
	if (ev == MG_EV_HTTP_REQUEST) {
	    mg_serve_http(nc, hm, s_opts);
	}
}

void mongooseTask(void *data)
{
	struct mg_mgr mgr;
	mg_mgr_init(&mgr, NULL);
	struct mg_connection *nc = mg_bind(&mgr, ":80", mongoose_event_handler);
	if (nc == NULL) {
		printf( "No connection from the mg_bind()\n");
		vTaskDelete(NULL);
		return;
	}

	mg_set_protocol_http_websocket(nc);
	memset(&s_opts, 0, sizeof(s_opts));
	s_opts.document_root= "/spiffs/";
	s_opts.index_files = "index.html";
	while (1) {
		mg_mgr_poll(&mgr, 200);
	}
	mg_mgr_free(&mgr);
}

esp_err_t wifi_event_handler(void *ctx, system_event_t *event)
{
	switch (event->event_id){
		case SYSTEM_EVENT_AP_START:
			printf("Accesspoint connected\n");
			break;

		case SYSTEM_EVENT_AP_STOP:
			printf("Accesspoint stopped\n");
			break;

		case SYSTEM_EVENT_AP_STACONNECTED:
			printf("Accesspoint Station connected\n");
			xTaskCreate(&mongooseTask, "mongooseTask", 20000, NULL, 5, NULL);
			break;

		case SYSTEM_EVENT_AP_STADISCONNECTED:
			printf("Accesspoint Station disconnected\n");
			break;

		case SYSTEM_EVENT_AP_PROBEREQRECVED:
			printf("Accesspoint Probe Request received\n");
			break;

		default:
			break;
	}
	return ESP_OK;
}

static void init_wifi(void)
{
    tcpip_adapter_init();
    tcpip_adapter_ip_info_t info = { 0, };
    IP4_ADDR(&info.ip, 192, 168, 3, 1);
    IP4_ADDR(&info.gw, 192, 168, 3, 1);
    IP4_ADDR(&info.netmask, 255, 255, 255, 0);
    ESP_ERROR_CHECK(tcpip_adapter_dhcps_stop(TCPIP_ADAPTER_IF_AP));
    ESP_ERROR_CHECK(tcpip_adapter_set_ip_info(TCPIP_ADAPTER_IF_AP, &info));
    ESP_ERROR_CHECK(tcpip_adapter_dhcps_start(TCPIP_ADAPTER_IF_AP));

    ESP_ERROR_CHECK( esp_event_loop_init(wifi_event_handler, NULL) );
    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
    ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
    ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
    wifi_config_t ap_config = {
		.ap = {
			.ssid="ESP32",
			.ssid_len=0,
			.password="pwpwpwpw",
			.channel=0,
			.authmode=WIFI_AUTH_WPA2_PSK,
			.ssid_hidden=0,
			.max_connection=4,
			.beacon_interval=100
		}
    };
    ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_AP, &ap_config) );
    ESP_ERROR_CHECK( esp_wifi_start() );
}

void app_main(void)
{
    nvs_flash_init();
    vfs_spiffs_register();
   	if (spiffs_is_mounted) { printf("==== spiffs mounted .... ====\r\n"); }

   	init_wifi();

    gpio_set_direction(GPIO_NUM_2, GPIO_MODE_OUTPUT);
    int level = 0;
    while (true) {
        gpio_set_level(GPIO_NUM_2, level);
        level = !level;
        vTaskDelay(300 / portTICK_PERIOD_MS);
    }
}

Now I was focused building the mongoose lib manually, to find out what happend,
and vola, I got a complete working lib with:

Code: Select all

xtensa-esp32-elf-gcc -mlongcalls -I/d/Source/esp/esp-idf/components/vfs/include -I/d/Source/esp/esp-idf/components/lwip/include/lwip  -I/d/Source/esp/esp-idf/components/lwip/include/lwip/port -I/d/Source/esp/esp-idf/components/esp32/include -I/d/Source/esp/webserver/build/include -I /d/Source/esp/esp-idf/components/freertos/include -I/d/Source/esp/esp-idf/components/soc/esp32/include -DESP_PLATFORM=1 -DMG_ENABLE_HTTP=1 -DMG_ENABLE_FILESYSTEM=1 -c mongoose.c -o mongoose.o
xtensa-esp32-elf-ar -rcs libmongoose.a mongoose.o
How do I bring the -mlongcalls switch to the project?

EDIT:
I also got a complete lib without -mloncalls, but this was not linkable (call8).
So the project setup meight be little different to just include the switch.

How do I make a proper setup?

linuxpaul
Posts: 43
Joined: Thu Jul 20, 2017 6:10 pm

Re: mongoose setup

Postby linuxpaul » Sun Aug 20, 2017 1:37 pm

The solution is creating a Makefile.projbuild next to mongoose.c
Makefile.projbuild:
CFLAGS += -mlongcalls -DESP_PLATFORM=1 -DMG_ENABLE_HTTP=1 -DMG_ENABLE_FILESYSTEM=1

:)
linuxpaul

Who is online

Users browsing this forum: No registered users and 118 guests