Arduino Http component not working with ESP IDF launched WiFi

anslex
Posts: 3
Joined: Sun Sep 08, 2019 9:30 pm

Arduino Http component not working with ESP IDF launched WiFi

Postby anslex » Sun Sep 08, 2019 9:46 pm

Hello Everybody,

I am testing your technology and I am trying to use esp-idf with a few arduino components.

I would like to hear your advice why arduino component has no wifi connection whereas the WiFi was launched by esp-idf task in this basic exemple?

I tried to pin tasks to the same core as wifi, but without any success

esp-arduino version: 1.0.3-rc3
esp-idf version: v3.2

Here is the code or raw text below:
https://github.com/anslex/wifi_station_espidf_arduino

main.cpp
https://github.com/anslex/wifi_station_ ... n/main.cpp

Code: Select all

#include "station.h"
#include "Arduino.h"
#include "WiFi.h"
#include <HTTPClient.h>

extern "C" void wifi_init_sta();

static void activate_wifi()
{
  //Initialize NVS
  esp_err_t ret = nvs_flash_init();
  if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {
    ESP_ERROR_CHECK(nvs_flash_erase());
    ret = nvs_flash_init();
  }
  ESP_ERROR_CHECK(ret);

  ESP_LOGI(TAG, "ESP_WIFI_MODE_STA");
  wifi_init_sta();
}

static void http_get_task(void *pvParameters)
{
    while(1) {
      // xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT,
      //                     false, true, portMAX_DELAY);
      //
      // ESP_LOGI(TAG, "Connected to AP");
      if(WiFi.status() == WL_CONNECTED){   //Check WiFi connection status

          HTTPClient http;

          http.begin("http://jsonplaceholder.typicode.com/posts");  //Specify destination for HTTP request
          http.addHeader("Content-Type", "text/plain");             //Specify content-type header

          int httpResponseCode = http.POST("POSTING from ESP32");   //Send the actual POST request

          if(httpResponseCode>0){

           String response = http.getString();                       //Get the response to the request

           Serial.println(httpResponseCode);   //Print return code
           Serial.println(response);           //Print request answer

          }else{

           Serial.print("Error on sending POST: ");
           Serial.println(httpResponseCode);

          }

          http.end();  //Free resources

        }else{
           Serial.println("Error in WiFi connection");
        }
    }
}

void serialSetup() {
    Serial.begin(115200);
    Serial.println("Serial.begin(115200) DONE");
    delay(10);
    Serial.println("serialSetup done");
}

extern "C" void app_main()
{
    initArduino();
    serialSetup();
    activate_wifi();
    xTaskCreate(&http_get_task, "http_get_task", 44096, NULL, 5, NULL);
}
station.h
https://github.com/anslex/wifi_station_ ... /station.h

Code: Select all

/* WiFi station Example
   This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#pragma once
#include <string.h>
#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"

#include "lwip/err.h"
#include "lwip/sys.h"

/* The examples use WiFi configuration that you can set via 'make menuconfig'.
   If you'd rather not, just change the below entries to strings with
   the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
*/
#define EXAMPLE_ESP_WIFI_SSID      CONFIG_ESP_WIFI_SSID
#define EXAMPLE_ESP_WIFI_PASS      CONFIG_ESP_WIFI_PASSWORD

#define EXAMPLE_ESP_MAXIMUM_RETRY  CONFIG_ESP_MAXIMUM_RETRY

/* FreeRTOS event group to signal when we are connected*/
static EventGroupHandle_t s_wifi_event_group;

/* The event group allows multiple bits for each event, but we only care about one event
 * - are we connected to the AP with an IP? */
const int WIFI_CONNECTED_BIT = BIT0;

static const char *TAG = "wifi station";

static int s_retry_num = 0;

static esp_err_t event_handler(void *ctx, system_event_t *event);
station.c
https://github.com/anslex/wifi_station_ ... /station.c

Code: Select all

/* WiFi station Example
   This example code is in the Public Domain (or CC0 licensed, at your option.)
   Unless required by applicable law or agreed to in writing, this
   software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
   CONDITIONS OF ANY KIND, either express or implied.
*/
#include "station.h"

static esp_err_t 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:
        ESP_LOGI(TAG, "got ip:%s",
                 ip4addr_ntoa(&event->event_info.got_ip.ip_info.ip));
        s_retry_num = 0;
        xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
        break;
    case SYSTEM_EVENT_STA_DISCONNECTED:
        {
            if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
                esp_wifi_connect();
                xEventGroupClearBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
                s_retry_num++;
                ESP_LOGI(TAG,"retry to connect to the AP");
            }
            ESP_LOGI(TAG,"connect to the AP fail\n");
            break;
        }
    default:
        break;
    }
    return ESP_OK;
}

void wifi_init_sta()
{
    tcpip_adapter_init();
    s_wifi_event_group = xEventGroupCreate();

    ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL) );

    wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
    ESP_ERROR_CHECK(esp_wifi_init(&cfg));
    wifi_config_t wifi_config = {
        .sta = {
            .ssid = EXAMPLE_ESP_WIFI_SSID,
            .password = EXAMPLE_ESP_WIFI_PASS
        },
    };

    ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA) );
    ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
    ESP_ERROR_CHECK(esp_wifi_start() );

    ESP_LOGI(TAG, "wifi_init_sta finished.");
    ESP_LOGI(TAG, "connect to ap SSID:%s password:%s",
             EXAMPLE_ESP_WIFI_SSID, EXAMPLE_ESP_WIFI_PASS);
}

Thank you !!!

boarchuz
Posts: 605
Joined: Tue Aug 21, 2018 5:28 am

Re: Arduino Http component not working with ESP IDF launched WiFi

Postby boarchuz » Mon Sep 09, 2019 7:18 am

Because you've implemented your own event handler instead. You should be using WIFI_CONNECTED_BIT of s_wifi_event_group to check WiFi connection state.

anslex
Posts: 3
Joined: Sun Sep 08, 2019 9:30 pm

Re: Arduino Http component not working with ESP IDF launched WiFi

Postby anslex » Tue Oct 01, 2019 8:51 am

Thank you for your response. You are right. I noticed that it is better to stop using Arduino components, so I am rewriting some libraries in esp-idf style

Who is online

Users browsing this forum: Baidu [Spider] and 57 guests