Skip to content

Commit

Permalink
Init upload
Browse files Browse the repository at this point in the history
  • Loading branch information
hayschan committed May 30, 2024
1 parent 2a198a1 commit dbc7508
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 12 deletions.
3 changes: 0 additions & 3 deletions Squareline/README.md

This file was deleted.

27 changes: 27 additions & 0 deletions main/Kconfig
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
menu "WiFi Configuration"

config WIFI_SSID
string "WiFi SSID"
default "Wokwi-GUEST"
help
Specify the WiFi SSID to connect to.

config WIFI_PASSWORD
string "WiFi Password"
default ""
help
Specify the WiFi password.

config WIFI_AUTH_MODE
hex "WiFi Authentication Mode"
default 0
help
Specify the authentication mode:
0 - WIFI_AUTH_OPEN
1 - WIFI_AUTH_WEP
2 - WIFI_AUTH_WPA_PSK
3 - WIFI_AUTH_WPA2_PSK
4 - WIFI_AUTH_WPA_WPA2_PSK
5 - WIFI_AUTH_WPA2_ENTERPRISE

endmenu
130 changes: 121 additions & 9 deletions main/main.c
Original file line number Diff line number Diff line change
@@ -1,13 +1,125 @@
#include <stdio.h>
/*c语言开发*/
void app_main(void)
{
printf("the template successfully created\n");
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "esp_system.h"
#include "esp_log.h"
#include "esp_event.h"
#include "nvs_flash.h"
#include "esp_wifi.h"
#include "esp_netif.h"
#include "esp_http_client.h"
#include "cJSON.h"

#define WIFI_SSID "Wokwi-GUEST" // Open access point, no password required
#define TAG "GEOLOCATION"
#define URL "http://ip-api.com/json"

static EventGroupHandle_t s_wifi_event_group;
const int WIFI_CONNECTED_BIT = BIT0;
const int WIFI_FAIL_BIT = BIT1;

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) {
esp_wifi_connect();
ESP_LOGI(TAG, "retry to connect to the AP");
xEventGroupSetBits(s_wifi_event_group, WIFI_FAIL_BIT);
} 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:%s", ip4addr_ntoa(&event->ip_info.ip));
xEventGroupSetBits(s_wifi_event_group, WIFI_CONNECTED_BIT);
}
}

void wifi_init_sta(void) {
s_wifi_event_group = xEventGroupCreate();
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();

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 = WIFI_SSID,
.password = "",
.threshold.authmode = WIFI_AUTH_OPEN,
},
};

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.");

EventBits_t bits = xEventGroupWaitBits(s_wifi_event_group, WIFI_CONNECTED_BIT | WIFI_FAIL_BIT, pdFALSE, pdFALSE, portMAX_DELAY);

if (bits & WIFI_CONNECTED_BIT) {
ESP_LOGI(TAG, "connected to ap SSID:%s", WIFI_SSID);
} else if (bits & WIFI_FAIL_BIT) {
ESP_LOGI(TAG, "Failed to connect to SSID:%s", WIFI_SSID);
} else {
ESP_LOGE(TAG, "UNEXPECTED EVENT");
}
}

esp_err_t http_event_handler(esp_http_client_event_t *evt) {
switch (evt->event_id) {
case HTTP_EVENT_ON_DATA:
if (!esp_http_client_is_chunked_response(evt->client)) {
cJSON *json = cJSON_Parse(evt->data);
if (json == NULL) {
ESP_LOGE(TAG, "JSON parsing error");
break;
}

cJSON *json_item = json->child;
while (json_item) {
ESP_LOGI(TAG, "%s: %s", json_item->string, cJSON_GetStringValue(json_item));
json_item = json_item->next;
}

cJSON_Delete(json);
}
break;
default:
break;
}
return ESP_OK;
}

void http_get_task(void *pvParameters) {
esp_http_client_config_t config = {
.url = URL,
.event_handler = http_event_handler,
.method = HTTP_METHOD_POST
};

esp_http_client_handle_t client = esp_http_client_init(&config);
esp_err_t err = esp_http_client_perform(client);

/*c++语言开发*/
// extern "C" void app_main(void)
// {
if (err == ESP_OK) {
ESP_LOGI(TAG, "HTTP POST Status = %d, content_length = %d",
esp_http_client_get_status_code(client),
esp_http_client_get_content_length(client));
} else {
ESP_LOGE(TAG, "HTTP POST request failed: %s", esp_err_to_name(err));
}

// }
esp_http_client_cleanup(client);
vTaskDelete(NULL);
}

void app_main() {
nvs_flash_init();
wifi_init_sta();
xTaskCreate(&http_get_task, "http_get_task", 8192, NULL, 5, NULL);
}

0 comments on commit dbc7508

Please sign in to comment.