diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 46961f1..0a4bebd 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -1,2 +1,3 @@ -idf_component_register(SRCS "main.c" "font.c" "display.c" "text.c" "wlan.c" +idf_component_register(SRCS "main.c" "font.c" "display.c" "text.c" "wlan.c" "http.c" + REQUIRES esp_driver_gpio esp_wifi nvs_flash esp_timer INCLUDE_DIRS "") diff --git a/main/display.c b/main/display.c index ebdf153..cf36e73 100644 --- a/main/display.c +++ b/main/display.c @@ -25,7 +25,7 @@ void latch(){ gpio_set_level(GPIO_LAT, 1); } -void clock(){ +static void clock(){ gpio_set_level(GPIO_CLK, 1); gpio_set_level(GPIO_CLK, 0); } diff --git a/main/display.h b/main/display.h index 5c57952..1f3b340 100644 --- a/main/display.h +++ b/main/display.h @@ -29,7 +29,6 @@ extern uint8_t fb[64][128][3]; void write_bits(uint8_t r1, uint8_t g1, uint8_t b1, uint8_t r2, uint8_t g2, uint8_t b2); void write_address(uint8_t addr); void latch(void); -void clock(void); void display_init(void); void display_cycle(void* arg); diff --git a/main/http.c b/main/http.c new file mode 100644 index 0000000..d10372f --- /dev/null +++ b/main/http.c @@ -0,0 +1,81 @@ +#include "http.h" +#include "esp_err.h" +#include "esp_log.h" + +#include "lwip/err.h" +#include "lwip/sockets.h" +#include "lwip/sys.h" +#include "lwip/netdb.h" +#include "lwip/dns.h" + +static const char *TAG = "http"; + +static const char *REQUEST = "GET " WEB_PATH " HTTP/1.0\r\n" + "Host: "WEB_SERVER":"WEB_PORT"\r\n" + "User-Agent: esp-idf/1.0 esp32\r\n" + "\r\n"; + +esp_err_t http_request(char* buf) +{ + const struct addrinfo hints = { + .ai_family = AF_INET, + .ai_socktype = SOCK_STREAM, + }; + struct addrinfo *res; + struct in_addr *addr; + int s, r; + + int err = getaddrinfo(WEB_SERVER, WEB_PORT, &hints, &res); + + if(err != 0 || res == NULL) { + ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res); + return ESP_ERR_NOT_FOUND; + } + + /* Code to print the resolved IP. + * Note: inet_ntoa is non-reentrant, look at ipaddr_ntoa_r for "real" code */ + addr = &((struct sockaddr_in *)res->ai_addr)->sin_addr; + ESP_LOGI(TAG, "DNS lookup succeeded. IP=%s", inet_ntoa(*addr)); + + s = socket(res->ai_family, res->ai_socktype, 0); + if(s < 0) { + ESP_LOGE(TAG, "... Failed to allocate socket."); + freeaddrinfo(res); + return -1; + } + ESP_LOGI(TAG, "... allocated socket"); + + if(connect(s, res->ai_addr, res->ai_addrlen) != 0) { + ESP_LOGE(TAG, "... socket connect failed errno=%d", errno); + close(s); + freeaddrinfo(res); + return -1; + } + + ESP_LOGI(TAG, "... connected"); + freeaddrinfo(res); + + if (write(s, REQUEST, strlen(REQUEST)) < 0) { + ESP_LOGE(TAG, "... socket send failed"); + close(s); + return -1; + } + ESP_LOGI(TAG, "... socket send success"); + + struct timeval receiving_timeout; + receiving_timeout.tv_sec = 5; + receiving_timeout.tv_usec = 0; + if (setsockopt(s, SOL_SOCKET, SO_RCVTIMEO, &receiving_timeout, + sizeof(receiving_timeout)) < 0) { + ESP_LOGE(TAG, "... failed to set socket receiving timeout"); + close(s); + return -1; + } + ESP_LOGI(TAG, "... set socket receiving timeout success"); + + /* Read HTTP response */ + r = read(s, buf, RCV_BUFSIZE); + if(r >= RCV_BUFSIZE) + return -1; + return 0; +} diff --git a/main/main.c b/main/main.c index dd5df8f..d3e60d2 100644 --- a/main/main.c +++ b/main/main.c @@ -7,16 +7,18 @@ CONDITIONS OF ANY KIND, either express or implied. */ #include +#include #include "sdkconfig.h" #include "freertos/FreeRTOS.h" #include "freertos/task.h" #include "esp_system.h" -#include "nvs_flash.h" #include "esp_timer.h" +#include "esp_log.h" #include "display.h" #include "text.h" #include "wlan.h" +#include "http.h" #ifdef CONFIG_IDF_TARGET_ESP32 #define CHIP_NAME "ESP32" @@ -41,17 +43,22 @@ void app_main(void) put_line(fb, 1, "MauMau", 1, 1); - //Initialize WiFi - 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); - wifi_init_sta(); + wlan_init(); put_line(fb, 2, "wifi", 1, 1); + //xTaskCreate(&http_get_task, "http_get_task", 4096, NULL, 5, NULL); + char buf[RCV_BUFSIZE]; + bzero(buf, RCV_BUFSIZE); + http_request(buf); + for(uint32_t i=0; i