I would like to create non-blocking code that is mostly made up of functions so that I can then call them in main.
This is the part of the code where I set the wifi and tcp/ip:
- /* BSD non-blocking socket 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 <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/param.h>
- #include <lwip/netdb.h>
- #include "freertos/FreeRTOS.h"
- #include "freertos/task.h"
- #include "sys/socket.h"
- #include "netdb.h"
- #include "errno.h"
- #include "esp_mac.h"
- #include "esp_system.h"
- #include "esp_event.h"
- #include "esp_log.h"
- #include "esp_wifi.h"
- #include "nvs_flash.h"
- #include "driver/gpio.h"
- #include "TCP-IP.h"
- #define SSID_WIFI "ESP32-C3 Access Point"
- #define PASSWORD_WIFI "esp32c3accesspoint"
- #define PORT_NUMBER 8001
- static const char *TAG = "wifi softAP";
- static void wifi_event_handler(void* arg, esp_event_base_t event_base,
- int32_t event_id, void* event_data)
- {
- if (event_id == WIFI_EVENT_AP_STACONNECTED) {
- wifi_event_ap_staconnected_t* event = (wifi_event_ap_staconnected_t*) event_data;
- ESP_LOGI(TAG, "station "MACSTR" join, AID=%d",
- MAC2STR(event->mac), event->aid);
- } else if (event_id == WIFI_EVENT_AP_STADISCONNECTED) {
- wifi_event_ap_stadisconnected_t* event = (wifi_event_ap_stadisconnected_t*) event_data;
- ESP_LOGI(TAG, "station "MACSTR" leave, AID=%d",
- MAC2STR(event->mac), event->aid);
- }
- }
- void WiFiInit(void)
- {
- wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
- wifi_config_t ApConfig = {
- .ap = {
- .ssid = SSID_WIFI,
- .ssid_len = strlen(SSID_WIFI),
- .password = PASSWORD_WIFI,
- .channel=6,
- .authmode=WIFI_AUTH_WPA2_PSK,
- .ssid_hidden=0,
- .max_connection=4,
- .beacon_interval=100
- },
- };
- ESP_ERROR_CHECK(nvs_flash_init());
- ESP_ERROR_CHECK(esp_netif_init());
- ESP_ERROR_CHECK(esp_event_loop_create_default());
- esp_netif_create_default_wifi_ap();
- ESP_ERROR_CHECK(esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID, &wifi_event_handler, NULL, NULL));
- esp_wifi_init(&cfg);
- esp_wifi_set_storage(WIFI_STORAGE_RAM);
- esp_wifi_set_mode(WIFI_MODE_AP);
- esp_wifi_set_config(ESP_IF_WIFI_AP, &ApConfig);
- esp_wifi_start();
- ESP_LOGI(TAG, "wifi_init_softap finished. SSID:%s password:%s", SSID_WIFI, PASSWORD_WIFI);
- }
- void socket_server_task(void) {
- struct sockaddr_in clientAddress;
- struct sockaddr_in serverAddress;
- // Create a socket that we will listen upon.
- int sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
- if (sock < 0) {
- ESP_LOGE(TAG, "socket: %d %s", sock, strerror(errno));
- goto END;
- }
- // Bind our server socket to a port.
- serverAddress.sin_family = AF_INET;
- serverAddress.sin_addr.s_addr = htonl(INADDR_ANY);
- serverAddress.sin_port = htons(PORT_NUMBER);
- int rc = bind(sock, (struct sockaddr *)&serverAddress, sizeof(serverAddress));
- if (rc < 0) {
- ESP_LOGE(TAG, "bind: %d %s", rc, strerror(errno));
- goto END;
- }
- // Flag the socket as listening for new connections.
- rc = listen(sock, 5);
- if (rc < 0) {
- ESP_LOGE(TAG, "listen: %d %s", rc, strerror(errno));
- goto END;
- }
- while (1) {
- // Listen for a new client connection.
- ESP_LOGD(TAG, "Waiting for new client connection");
- socklen_t clientAddressLength = sizeof(clientAddress);
- int clientSock = accept(sock, (struct sockaddr *)&clientAddress, &clientAddressLength);
- if (clientSock < 0) {
- ESP_LOGE(TAG, "accept: %d %s", clientSock, strerror(errno));
- goto END;
- }
- sendData(clientSock);
- }
- END:
- vTaskDelete(NULL);
- }
- static void sendData(int socket) {
- char text[80];
- int i;
- for(i=1;i<=10;i++){
- sprintf(text, "Message %d\n", i);
- send(socket, text, strlen(text), 0);
- vTaskDelay(1000/portTICK_PERIOD_MS);
- }
- close(socket);
- }
This is the main part:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "TCP-IP.h"
- void app_main(void)
- {
- WiFiInit();
- socket_server_task();
- }