first commit
This commit is contained in:
103
main/wifi_service/http_client.c
Normal file
103
main/wifi_service/http_client.c
Normal file
@@ -0,0 +1,103 @@
|
||||
/*
|
||||
* web_client.c
|
||||
*
|
||||
* Created on: 4. aug. 2023
|
||||
* Author: OZ1CM
|
||||
*/
|
||||
#include "http_client.h"
|
||||
|
||||
|
||||
#include "esp_log.h"
|
||||
|
||||
#define TAG "http_client"
|
||||
|
||||
esp_err_t _http_event_handle(esp_http_client_event_t *evt)
|
||||
{
|
||||
switch(evt->event_id) {
|
||||
case HTTP_EVENT_ERROR:
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_ERROR");
|
||||
break;
|
||||
case HTTP_EVENT_ON_CONNECTED:
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_ON_CONNECTED");
|
||||
break;
|
||||
case HTTP_EVENT_HEADER_SENT:
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT");
|
||||
// printf("%.*s", evt->data_len, (char*)evt->data);
|
||||
break;
|
||||
case HTTP_EVENT_ON_HEADER:
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_ON_HEADER");
|
||||
printf("%.*s", evt->data_len, (char*)evt->data);
|
||||
break;
|
||||
case HTTP_EVENT_ON_DATA:
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_ON_DATA, len=%d", evt->data_len);
|
||||
//if (!esp_http_client_is_chunked_response(evt->client)) {
|
||||
ESP_LOGI(TAG,"Response from server: %s",(char*)evt->data);
|
||||
|
||||
//}
|
||||
|
||||
break;
|
||||
case HTTP_EVENT_ON_FINISH:
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH");
|
||||
break;
|
||||
case HTTP_EVENT_DISCONNECTED:
|
||||
ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED");
|
||||
break;
|
||||
default:
|
||||
break;
|
||||
}
|
||||
return ESP_OK;
|
||||
}
|
||||
|
||||
int http_client_sendUrl(http_client_t *inst,char *url){
|
||||
|
||||
esp_http_client_config_t config = {
|
||||
.url = url,
|
||||
.method = HTTP_METHOD_GET,
|
||||
//.event_handler = _http_event_handle,
|
||||
};
|
||||
|
||||
|
||||
|
||||
|
||||
inst->client = esp_http_client_init(&config);
|
||||
|
||||
esp_err_t err;
|
||||
|
||||
err = esp_http_client_perform(inst->client);
|
||||
|
||||
// some error logging..
|
||||
if(err == ESP_OK){
|
||||
|
||||
int status_code = esp_http_client_get_status_code(inst->client);
|
||||
|
||||
switch (status_code){
|
||||
|
||||
case 200:
|
||||
ESP_LOGI(TAG, "Message sent Successfully");
|
||||
esp_http_client_cleanup(inst->client);
|
||||
return 0;
|
||||
break;
|
||||
|
||||
default:
|
||||
ESP_LOGE(TAG, "Message sent Failed");
|
||||
esp_http_client_cleanup(inst->client);
|
||||
return 1;
|
||||
break;
|
||||
|
||||
}
|
||||
|
||||
|
||||
}else{
|
||||
ESP_LOGE(TAG, "Message sent Failed");
|
||||
esp_http_client_cleanup(inst->client);
|
||||
}
|
||||
|
||||
|
||||
|
||||
// ESP_LOGI(TAG, "URL: %s", inst->client->);
|
||||
//ESP_LOGI(TAG,"hejmeddig");
|
||||
|
||||
//esp_http_client_set_header(inst->client, "Content-Type", "application/x-www-form-urlencoded");
|
||||
return 1;
|
||||
|
||||
}
|
||||
24
main/wifi_service/http_client.h
Normal file
24
main/wifi_service/http_client.h
Normal file
@@ -0,0 +1,24 @@
|
||||
/*
|
||||
* web_client.h
|
||||
*
|
||||
* Created on: 4. aug. 2023
|
||||
* Author: OZ1CM
|
||||
*/
|
||||
|
||||
#ifndef MAIN_INCLUDE_HTTP_CLIENT_H_
|
||||
#define MAIN_INCLUDE_HTTP_CLIENT_H_
|
||||
#include "esp_http_client.h"
|
||||
|
||||
typedef struct {
|
||||
|
||||
// Config
|
||||
esp_http_client_config_t config;
|
||||
esp_http_client_handle_t client;
|
||||
|
||||
}http_client_t;
|
||||
|
||||
int http_client_sendUrl(http_client_t *inst,char *url);
|
||||
|
||||
|
||||
|
||||
#endif /* MAIN_INCLUDE_HTTP_CLIENT_H_ */
|
||||
185
main/wifi_service/wifi_service.c
Normal file
185
main/wifi_service/wifi_service.c
Normal file
@@ -0,0 +1,185 @@
|
||||
/*
|
||||
* wifi_service.c
|
||||
*
|
||||
* Created on: 26 May 2026
|
||||
* Author: Christian Lind Vie Madsen
|
||||
*/
|
||||
#include "wifi_service.h"
|
||||
#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.h"
|
||||
#include "esp_log.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "../board_defines/wifi_info.h"
|
||||
|
||||
#include "lwip/err.h"
|
||||
#include "lwip/sys.h"
|
||||
|
||||
#define ESP_HOSTNAME "ws_wifi_client"
|
||||
#define EXAMPLE_ESP_MAXIMUM_RETRY 5
|
||||
|
||||
/* FreeRTOS event group to signal when we are connected*/
|
||||
static EventGroupHandle_t s_wifi_event_group;
|
||||
static int s_retry_num = 0;
|
||||
|
||||
/* The event group allows multiple bits for each event, but we only care about two events:
|
||||
* - we are connected to the AP with an IP
|
||||
* - we failed to connect after the maximum amount of retries */
|
||||
#define WIFI_CONNECTED_BIT BIT0
|
||||
#define WIFI_FAIL_BIT BIT1
|
||||
|
||||
#define TAG "wifi_driver"
|
||||
|
||||
static void event_handler(void *arg, esp_event_base_t event_base,
|
||||
int32_t event_id, void *event_data) {
|
||||
if (event_base == WIFI_EVENT && event_id == WIFI_EVENT_STA_START) {
|
||||
esp_wifi_connect();
|
||||
} else if (event_base == WIFI_EVENT
|
||||
&& event_id == WIFI_EVENT_STA_DISCONNECTED) {
|
||||
if (s_retry_num < EXAMPLE_ESP_MAXIMUM_RETRY) {
|
||||
esp_wifi_connect();
|
||||
//s_retry_num++;
|
||||
ESP_LOGI(TAG, "retry to connect to the AP");
|
||||
} else {
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
|
||||
}
|
||||
ESP_LOGI(TAG, "connect to the AP fail");
|
||||
} else if (event_base == IP_EVENT && event_id == IP_EVENT_STA_GOT_IP) {
|
||||
ip_event_got_ip_t *event = (ip_event_got_ip_t*) event_data;
|
||||
ESP_LOGI(TAG, "got ip:" IPSTR, IP2STR(&event->ip_info.ip));
|
||||
s_retry_num = 0;
|
||||
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @brief Get RSSI of currently connected WiFi AP
|
||||
* @return RSSI in dBm (e.g. -45), or 0 if not connected / error
|
||||
*/
|
||||
int wifi_get_rssi(void)
|
||||
{
|
||||
wifi_ap_record_t ap_info;
|
||||
|
||||
esp_err_t err = esp_wifi_sta_get_ap_info(&ap_info);
|
||||
if (err != ESP_OK) {
|
||||
return -113; // not connected or error
|
||||
}
|
||||
|
||||
return ap_info.rssi;
|
||||
}
|
||||
|
||||
int wifi_IsConnected(){
|
||||
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
if (bits & WIFI_CONNECTED_BIT) return 1;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void wifi_checkConnection(){
|
||||
|
||||
static uint32_t runtimer = 0;
|
||||
|
||||
|
||||
if((runtimer++ % 50) == 0){
|
||||
EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group,
|
||||
WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);
|
||||
|
||||
/* xEventGroupWaitBits() returns the bits before the call returned, hence we can test which event actually
|
||||
* happened. */
|
||||
if (bits & WIFI_CONNECTED_BIT) {
|
||||
ESP_LOGI(TAG, "connected to ap ");
|
||||
} else if (bits & WIFI_FAIL_BIT) {
|
||||
ESP_LOGI(TAG, "Failed to connect to SSID");
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_stop());
|
||||
vTaskDelay(1000);
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
} else {
|
||||
ESP_LOGE(TAG, "UNEXPECTED EVENT");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int wifi_scan_to_buffer(char ssid[][33], int *rssi, int max_len)
|
||||
{
|
||||
uint16_t ap_count = 0;
|
||||
wifi_ap_record_t ap_info[25];
|
||||
|
||||
memset(ap_info, 0, sizeof(ap_info));
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_scan_start(NULL, true));
|
||||
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_num(&ap_count));
|
||||
|
||||
if (ap_count > max_len) {
|
||||
ap_count = max_len;
|
||||
}
|
||||
|
||||
ESP_ERROR_CHECK(esp_wifi_scan_get_ap_records(&ap_count, ap_info));
|
||||
|
||||
for (int i = 0; i < ap_count; i++) {
|
||||
strncpy(ssid[i], (char *)ap_info[i].ssid, 13);
|
||||
ssid[i][13] = '\0'; // safety null-terminate
|
||||
|
||||
rssi[i] = ap_info[i].rssi;
|
||||
}
|
||||
|
||||
return ap_count;
|
||||
}
|
||||
|
||||
void wifi_init_start(void) {
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
|
||||
s_wifi_event_group = xEventGroupCreate();
|
||||
|
||||
ESP_ERROR_CHECK(esp_netif_init());
|
||||
|
||||
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
||||
esp_netif_t *netif = esp_netif_create_default_wifi_sta();
|
||||
|
||||
esp_netif_set_hostname(netif, ESP_HOSTNAME);
|
||||
|
||||
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
||||
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
|
||||
|
||||
esp_event_handler_instance_t instance_any_id;
|
||||
esp_event_handler_instance_t instance_got_ip;
|
||||
ESP_ERROR_CHECK(
|
||||
esp_event_handler_instance_register(WIFI_EVENT, ESP_EVENT_ANY_ID,
|
||||
&event_handler, NULL, &instance_any_id));
|
||||
ESP_ERROR_CHECK(
|
||||
esp_event_handler_instance_register(IP_EVENT, IP_EVENT_STA_GOT_IP,
|
||||
&event_handler, NULL, &instance_got_ip));
|
||||
|
||||
wifi_config_t wifi_config = { .sta = { .ssid = SSID,
|
||||
.password = PASSWORD,
|
||||
/* Setting a password implies station will connect to all security modes including WEP/WPA.
|
||||
* However these modes are deprecated and not advisable to be used. Incase your Access point
|
||||
* doesn't support WPA2, these mode can be enabled by commenting below line */
|
||||
.threshold.authmode = WIFI_AUTH_WPA2_PSK, }, };
|
||||
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
|
||||
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
|
||||
ESP_ERROR_CHECK(esp_wifi_start());
|
||||
|
||||
ESP_LOGI(TAG, "wifi_init_sta finished.");
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
20
main/wifi_service/wifi_service.h
Normal file
20
main/wifi_service/wifi_service.h
Normal file
@@ -0,0 +1,20 @@
|
||||
/*
|
||||
* wifi_service.h
|
||||
*
|
||||
* Created on: 26 May 2026
|
||||
* Author: Christian Lind Vie Madsen
|
||||
*/
|
||||
|
||||
#ifndef MAIN_WIFI_SERVICE_WIFI_SERVICE_H_
|
||||
#define MAIN_WIFI_SERVICE_WIFI_SERVICE_H_
|
||||
|
||||
|
||||
|
||||
|
||||
int wifi_scan_to_buffer(char ssid[][33], int *rssi, int max_len);
|
||||
void wifi_init_start(void);
|
||||
int wifi_IsConnected();
|
||||
int wifi_get_rssi(void);
|
||||
|
||||
|
||||
#endif /* MAIN_WIFI_SERVICE_WIFI_SERVICE_H_ */
|
||||
Reference in New Issue
Block a user