From f4b0d0f07e3300bb8d6be28e119d8bbc3085adc3 Mon Sep 17 00:00:00 2001 From: Eggert Jung Date: Sun, 8 Nov 2020 17:58:10 +0100 Subject: [PATCH] stuck with crashing json parser --- main/hello_world_main.c | 226 +++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 216 insertions(+), 10 deletions(-) diff --git a/main/hello_world_main.c b/main/hello_world_main.c index 1512f14..1298e15 100644 --- a/main/hello_world_main.c +++ b/main/hello_world_main.c @@ -14,6 +14,53 @@ #include "esp_spi_flash.h" #include "driver/gpio.h" +#include "esp_http_client.h" +#include "esp_wifi.h" +#include "esp_netif.h" +#include "esp_eth.h" + +#include +#include +#include + +#include "esp_wifi.h" +#include "esp_event.h" +#include "esp_log.h" +#include "esp_system.h" +#include "esp_spiffs.h" +#include +#include + +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/event_groups.h" +#include "freertos/queue.h" + +#include "driver/gpio.h" +#include "driver/ledc.h" + +#include +#include +#include + +#include + +#include "esp_netif.h" +#include "esp_eth.h" +#include + +#define WIFI_SSID "ags-iot" +#define WIFI_PASS "internetOfClocksAndCoffee" +//#define WIFI_SSID "AndroidAP_3324" +//#define WIFI_PASS "Hella1234" + +char http_output_buffer[98304]; +uint32_t buffer_pointer = 0; + +// Event group +static EventGroupHandle_t wifi_event_group; +const int CONNECTED_BIT = BIT0; +static xQueueHandle gpio_evt_queue = NULL; #ifdef CONFIG_IDF_TARGET_ESP32 #define CHIP_NAME "ESP32" @@ -141,6 +188,117 @@ const unsigned char font[96][7] = { {0x00,0x00,0x00,0x00,0x00,0x00,0x00} }; +void connect_to_wifi(char* new_ssid, char* new_psk){ + printf("ssid: \'%s\'\n", new_ssid); + printf("psk:\'%s\'\n", new_psk); + + ESP_ERROR_CHECK(esp_wifi_stop()); + + 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 = { + .sta = { + .ssid = {*new_ssid}, + .password = {*new_psk} + } + }; + strcpy(&(wifi_config.sta.ssid), new_ssid); + strcpy(&(wifi_config.sta.password), new_psk); + + printf("ssid: \'%s\'\n", wifi_config.sta.ssid); + printf("psk: \'%s\'\n", wifi_config.sta.password); + + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); +} + +// Wifi event handler +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: + xEventGroupSetBits(wifi_event_group, CONNECTED_BIT); + break; + + case SYSTEM_EVENT_STA_DISCONNECTED: + xEventGroupClearBits(wifi_event_group, CONNECTED_BIT); + printf("disconnected!\n"); + break; + + default: + break; + } + + return ESP_OK; +} + +#define TAG "test" +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"); + buffer_pointer = 0; + break; + case HTTP_EVENT_HEADER_SENT: + ESP_LOGI(TAG, "HTTP_EVENT_HEADER_SENT"); + 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)) { + memcpy(&http_output_buffer[buffer_pointer], evt->data, evt->data_len); + buffer_pointer += evt->data_len; + } + + break; + case HTTP_EVENT_ON_FINISH: + ESP_LOGI(TAG, "HTTP_EVENT_ON_FINISH"); + http_output_buffer[buffer_pointer+1] = 0; + //printf("%s", http_output_buffer); + printf("PRINTING RECIEVED STRING:\n"); + printf("%.*s\n", buffer_pointer, http_output_buffer); + printf("END OF RECIEVED STRING\n"); + cJSON *root = cJSON_Parse(http_output_buffer); + const char *error_ptr = cJSON_GetErrorPtr(); + //printf("error: %s\n", error_ptr-100); + //printf("%s", cJSON_Print(root)); + printf("size is: %d\n", buffer_pointer); + if(root != NULL) { + cJSON* departureListObject = cJSON_GetObjectItem(root, "departureList"); + if(departureListObject != NULL) { + cJSON* departureListObject = cJSON_GetObjectItem(root, "departureList"); + printf("%s", cJSON_Print(departureListObject)); + } + else + printf("dl empty\n"); + } + else + printf("root empty\n"); + + break; + case HTTP_EVENT_DISCONNECTED: + ESP_LOGI(TAG, "HTTP_EVENT_DISCONNECTED"); + break; + } + return ESP_OK; +} + void write_bits(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t r2, uint8_t g2, uint8_t b2){ gpio_set_level(GPIO_R1, r1); gpio_set_level(GPIO_G1, g1); @@ -161,7 +319,7 @@ void write_address(uint8_t addr){ void put_chr(uint8_t line, uint8_t pos, uint8_t chr[]){ for(int i=0;i<8;i++) for(int j=0;j<7;j++) - fb[1+(line*10)+i][(pos*8)+j][0] = (chr[j]&(1<>i; + fb[1+(line*10)+i][(pos*7)+j][0] = (chr[j]&(1<>i; } void put_line(uint8_t line, char *str){ @@ -177,7 +335,7 @@ void latch(){ gpio_set_level(GPIO_LAT, 1); } -void clock(){ +void clock_cycle(){ gpio_set_level(GPIO_CLK, 1); gpio_set_level(GPIO_CLK, 0); } @@ -186,8 +344,47 @@ void app_main(void) { printf("Hello world!\n"); - put_line(0, "429 Amalienplatz"); - put_line(1, "0123abcd"); + ESP_ERROR_CHECK(nvs_flash_init()); + + wifi_event_group = xEventGroupCreate(); + tcpip_adapter_init(); + ESP_ERROR_CHECK(esp_event_loop_init(event_handler, NULL)); + + printf("1!\n"); + 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)); + printf("2!\n"); + wifi_config_t wifi_config = { + .sta = { + .ssid = WIFI_SSID, + .password = WIFI_PASS, + }, + }; + ESP_ERROR_CHECK(esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config)); + ESP_ERROR_CHECK(esp_wifi_start()); + printf("Connecting to %s\n", WIFI_SSID); + EventBits_t uxBits; + uxBits = xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT, false, true, 5000 / portTICK_PERIOD_MS); + printf("3!\n"); + if( ( uxBits & CONNECTED_BIT ) != 0 ) + { + printf("Connected!\n"); + } + else{ + printf("Not connected!\n"); + } + +// put_line(0, "429 Amalienplatz"); +// put_line(1, "0123abcd"); +// put_line(2, "ags"); + put_line(0, "Wissenschaftliche"); + put_line(1, "Arbeitsgemein-"); + put_line(2, "schaft für Studio-"); + put_line(3, "und Senderfragen"); + put_line(4, "an der"); + put_line(5, "TU Braunschweig"); gpio_config_t io_conf; //disable interrupt @@ -206,13 +403,21 @@ void app_main(void) //configure GPIO with the given settings gpio_config(&io_conf); - gpio_set_level(GPIO_OE, 0); + esp_http_client_config_t config = { + //.url = "https://bsvg.efa.de/bsvagstd/XML_DM_REQUEST?outputFormat=JSON&stateless=1&locationServerActive=1&type_dm=stop&name_dm=\"Saarplatz,Braunschweig\"&mode=direct&ptOptionsActive=1&useRealtime=1", + .url = "http://mustbehax.de/web/bsvg.json", + .event_handler = _http_event_handle, + }; + esp_http_client_handle_t client = esp_http_client_init(&config); + esp_err_t err = esp_http_client_perform(client); - write_bits(1,0,0,1,0,0); - for(uint8_t i=0;i<128;i++){ - clock(); + if (err == ESP_OK) { + ESP_LOGI(TAG, "Status = %d, content_length = %d", + esp_http_client_get_status_code(client), + esp_http_client_get_content_length(client)); } - latch(); + esp_http_client_cleanup(client); + gpio_set_level(GPIO_OE, 1); gpio_set_level(GPIO_OE, 0); @@ -220,7 +425,8 @@ void app_main(void) for (;;) { for(uint8_t i=0;i<128;i++){ write_bits(fb[line][i][0],fb[line][i][1],fb[line][i][2],fb[32+line][i][0],fb[32+line][i][1],fb[32+line][i][2]); - clock(); + //write_bits(1,1,1,1,1,1); + clock_cycle(); } gpio_set_level(GPIO_OE, 1); latch();