Setting connection data in c++

stefanoxjx
Posts: 25
Joined: Mon Feb 12, 2018 6:26 pm

Setting connection data in c++

Postby stefanoxjx » Tue Feb 20, 2018 9:44 am

Hi, I'm studying C++ and I'm trying to write a class to connect to AP using esp-idf.
During compilation, I've these errors:

Code: Select all

$ make
CXX build/main/wificonn.o
In file included from /home/stefano/Programs/ESP32/.dev-tools/esp-idf/components/esp32/include/esp_timer.h:44:0,
                 from /home/stefano/Programs/ESP32/.dev-tools/esp-idf/components/freertos/include/freertos/portmacro.h:83,
                 from /home/stefano/Programs/ESP32/.dev-tools/esp-idf/components/freertos/include/freertos/portable.h:94,
                 from /home/stefano/Programs/ESP32/.dev-tools/esp-idf/components/freertos/include/freertos/FreeRTOS.h:105,
                 from /home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.h:5,
                 from /home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:2:
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp: In constructor 'WIFIConn::WIFIConn(std::__cxx11::string, std::__cxx11::string)':
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:21:70: error: invalid use of non-static member function
     ESP_ERROR_CHECK(esp_event_loop_init(WIFIConn::event_handler, NULL));
                                                                      ^
/home/stefano/Programs/ESP32/.dev-tools/esp-idf/components/esp32/include/esp_err.h:72:31: note: in definition of macro 'ESP_ERROR_CHECK'
         esp_err_t __err_rc = (x);                                       \
                               ^
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:37:5: error: name 'ssid' used in a GNU-style designated initializer for an array
     };
     ^
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:37:5: error: name 'password' used in a GNU-style designated initializer for an array
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:34:34: error: invalid conversion from 'const char*' to 'uint8_t {aka unsigned char}' [-fpermissive]
           .ssid = WIFI_SSID.c_str(),
                                  ^
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:35:38: error: invalid conversion from 'const char*' to 'uint8_t {aka unsigned char}' [-fpermissive]
           .password = WIFI_PASS.c_str(),
                                      ^
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:37:5: warning: missing initializer for member 'wifi_sta_config_t::password' [-Wmissing-field-initializers]
     };
     ^
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:37:5: warning: missing initializer for member 'wifi_sta_config_t::scan_method' [-Wmissing-field-initializers]
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:37:5: warning: missing initializer for member 'wifi_sta_config_t::bssid_set' [-Wmissing-field-initializers]
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:37:5: warning: missing initializer for member 'wifi_sta_config_t::bssid' [-Wmissing-field-initializers]
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:37:5: warning: missing initializer for member 'wifi_sta_config_t::channel' [-Wmissing-field-initializers]
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:37:5: warning: missing initializer for member 'wifi_sta_config_t::sort_method' [-Wmissing-field-initializers]
/home/stefano/Programs/ESP32/esempi/WiFiConnect/main/./wificonn.cpp:37:5: warning: missing initializer for member 'wifi_sta_config_t::threshold' [-Wmissing-field-initializers]
/home/stefano/Programs/ESP32/.dev-tools/esp-idf/make/component_wrapper.mk:273: set di istruzioni per l'obiettivo "wificonn.o" non riuscito
make[1]: *** [wificonn.o] Errore 1
/home/stefano/Programs/ESP32/.dev-tools/esp-idf/make/project.mk:450: set di istruzioni per l'obiettivo "component-main-build" non riuscito
make: *** [component-main-build] Errore 2
The problem is created from the struct:

Code: Select all

// configure the wifi connection and start the interface
    wifi_config_t wifi_config=
    {
       .sta=
       {
          .ssid = WIFI_SSID.c_str(),
          .password = WIFI_PASS.c_str(),
       },
    };
I'm newly of C++ and don't understand how I can solve this problem :(

Can you help me?
Thanks.
Regards.

Stefano

WiFive
Posts: 3529
Joined: Tue Dec 01, 2015 7:35 am

Re: Setting connection data in c++

Postby WiFive » Tue Feb 20, 2018 10:10 am


stefanoxjx
Posts: 25
Joined: Mon Feb 12, 2018 6:26 pm

Re: Setting connection data in c++

Postby stefanoxjx » Tue Feb 20, 2018 11:21 am

Hi, many thanks for link.
Now I've solved this issue, but I've another problem.
When I compile my class, I've these error:

Code: Select all

$ make
LD build/nvs.elf
/home/stefano/Programs/ESP32/esempi/WiFiConnect/build/main/libmain.a(wificonn.o):(.literal._ZN8WIFIConn13event_handlerEPvP14system_event_t+0x0): undefined reference to `WIFIConn::wifi_event_group'
collect2: error: ld returned 1 exit status
/home/stefano/Programs/ESP32/.dev-tools/esp-idf/make/project.mk:388: set di istruzioni per l'obiettivo "/home/stefano/Programs/ESP32/esempi/WiFiConnect/build/nvs.elf" non riuscito
make: *** [/home/stefano/Programs/ESP32/esempi/WiFiConnect/build/nvs.elf] Errore 1
My class is this:
header:

Code: Select all

#ifndef WIFICONN_H
#define WIFICONN_H

#include <string>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

using namespace std;

class WIFIConn
{
public:
    WIFIConn(string WIFI_SSID, string WIFI_PASS);
    ~WIFIConn();

private:
    uint8_t wifiErr;
    static EventGroupHandle_t wifi_event_group;
    static const int CONNECTED_BIT = BIT0;
    static esp_err_t event_handler(void *ctx, system_event_t *event);
};

#endif // WIFICONN_H
cpp

Code: Select all

#include <iostream>
#include <cstring>
#include "wificonn.h"

using namespace std;

WIFIConn::WIFIConn(string WIFI_SSID, string WIFI_PASS)
{
    // disable the default wifi logging
    esp_log_level_set("wifi", ESP_LOG_NONE);

    // initialize NVS
    ESP_ERROR_CHECK(nvs_flash_init());

    // create the event group to handle wifi events
    wifi_event_group = xEventGroupCreate();

    // initialize the tcp stack
    tcpip_adapter_init();

    // initialize the wifi event handler
    ESP_ERROR_CHECK(::esp_event_loop_init(WIFIConn::event_handler, NULL));

    // initialize the wifi stack in STAtion mode with config in RAM
    wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

    // configure the wifi connection and start the interface
    wifi_config_t wifi_config = {};

    strcpy((char*)wifi_config.sta.ssid, (const char*)WIFI_SSID.c_str());
    strcpy((char*)wifi_config.sta.password, (const char*)WIFI_PASS.c_str());

    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());
    cout << "Connecting to" << WIFI_SSID << endl;
}

WIFIConn::~WIFIConn()
{
}

// Wifi event handler
esp_err_t WIFIConn::event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id)
    {
       case SYSTEM_EVENT_STA_START:
           esp_wifi_connect();
           break;

       case SYSTEM_EVENT_STA_GOT_IP:
           xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
           break;

       case SYSTEM_EVENT_STA_DISCONNECTED:
           xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
           break;

       default:
           break;
    }

    return ESP_OK;
}
and in main.cpp I instance the class with:

Code: Select all

...
#include "wificonn.h"

WIFIConn wifi("myssid", "mypass");
...
What is wrong about my code?

Many thanks for patience.

Stefano

OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Re: Setting connection data in c++

Postby OneTwo » Wed Feb 21, 2018 1:29 pm

stefanoxjx wrote:Hi, many thanks for link.
Now I've solved this issue, but I've another problem.
When I compile my class, I've these error:

Code: Select all

$ make
LD build/nvs.elf
/home/stefano/Programs/ESP32/esempi/WiFiConnect/build/main/libmain.a(wificonn.o):(.literal._ZN8WIFIConn13event_handlerEPvP14system_event_t+0x0): undefined reference to `WIFIConn::wifi_event_group'
collect2: error: ld returned 1 exit status
/home/stefano/Programs/ESP32/.dev-tools/esp-idf/make/project.mk:388: set di istruzioni per l'obiettivo "/home/stefano/Programs/ESP32/esempi/WiFiConnect/build/nvs.elf" non riuscito
make: *** [/home/stefano/Programs/ESP32/esempi/WiFiConnect/build/nvs.elf] Errore 1
My class is this:
header:

Code: Select all

#ifndef WIFICONN_H
#define WIFICONN_H

#include <string>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"

using namespace std;

class WIFIConn
{
public:
    WIFIConn(string WIFI_SSID, string WIFI_PASS);
    ~WIFIConn();

private:
    uint8_t wifiErr;
    static EventGroupHandle_t wifi_event_group;
    static const int CONNECTED_BIT = BIT0;
    static esp_err_t event_handler(void *ctx, system_event_t *event);
};

#endif // WIFICONN_H
cpp

Code: Select all

#include <iostream>
#include <cstring>
#include "wificonn.h"

using namespace std;

WIFIConn::WIFIConn(string WIFI_SSID, string WIFI_PASS)
{
    // disable the default wifi logging
    esp_log_level_set("wifi", ESP_LOG_NONE);

    // initialize NVS
    ESP_ERROR_CHECK(nvs_flash_init());

    // create the event group to handle wifi events
    wifi_event_group = xEventGroupCreate();

    // initialize the tcp stack
    tcpip_adapter_init();

    // initialize the wifi event handler
    ESP_ERROR_CHECK(::esp_event_loop_init(WIFIConn::event_handler, NULL));

    // initialize the wifi stack in STAtion mode with config in RAM
    wifi_init_config_t wifi_init_config = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&wifi_init_config));
    ESP_ERROR_CHECK(esp_wifi_set_storage(WIFI_STORAGE_RAM));
    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));

    // configure the wifi connection and start the interface
    wifi_config_t wifi_config = {};

    strcpy((char*)wifi_config.sta.ssid, (const char*)WIFI_SSID.c_str());
    strcpy((char*)wifi_config.sta.password, (const char*)WIFI_PASS.c_str());

    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config));
    ESP_ERROR_CHECK(esp_wifi_start());
    cout << "Connecting to" << WIFI_SSID << endl;
}

WIFIConn::~WIFIConn()
{
}

// Wifi event handler
esp_err_t WIFIConn::event_handler(void *ctx, system_event_t *event)
{
    switch(event->event_id)
    {
       case SYSTEM_EVENT_STA_START:
           esp_wifi_connect();
           break;

       case SYSTEM_EVENT_STA_GOT_IP:
           xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
           break;

       case SYSTEM_EVENT_STA_DISCONNECTED:
           xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
           break;

       default:
           break;
    }

    return ESP_OK;
}
and in main.cpp I instance the class with:

Code: Select all

...
#include "wificonn.h"

WIFIConn wifi("myssid", "mypass");
...
What is wrong about my code?

Many thanks for patience.

Stefano
Your static variable

Code: Select all

static EventGroupHandle_t wifi_event_group;
is not initialized!

Put this in a cpp/definition file

Code: Select all

EventGroupHandle_t WIFIConn::wifi_event_group = ....whatever....
Btw. embedded programming is not a good start to learn C++.

stefanoxjx
Posts: 25
Joined: Mon Feb 12, 2018 6:26 pm

Re: Setting connection data in c++

Postby stefanoxjx » Wed Feb 21, 2018 1:36 pm

Many thanks for answer.
Btw. embedded programming is not a good start to learn C++.
Infact, now I've decided to develop my project in pure C :D

Stefano

OneTwo
Posts: 20
Joined: Mon Jan 15, 2018 9:24 am

Re: Setting connection data in c++

Postby OneTwo » Wed Feb 21, 2018 3:20 pm

stefanoxjx wrote:Many thanks for answer.
Btw. embedded programming is not a good start to learn C++.
Infact, now I've decided to develop my project in pure C :D

Stefano
I didn't want to stop you. It's just that you can find more (esp32) examples written in C instead of C++.
C makes it easy to shoot yourself in the foot; C++ makes it harder, but when you do it blows your whole leg off.

stefanoxjx
Posts: 25
Joined: Mon Feb 12, 2018 6:26 pm

Re: Setting connection data in c++

Postby stefanoxjx » Wed Feb 21, 2018 3:28 pm

I didn't want to stop you. It's just that you can find more (esp32) examples written in C instead of C++.
Yes, I know, but I think that to learn C++ maybe is better starting from desktop applications.
I have to learn a lot again.


Stefano

Who is online

Users browsing this forum: No registered users and 99 guests