add http read
This commit is contained in:
@@ -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 "")
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
@@ -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);
|
||||
|
||||
|
||||
81
main/http.c
Normal file
81
main/http.c
Normal file
@@ -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;
|
||||
}
|
||||
25
main/main.c
25
main/main.c
@@ -7,16 +7,18 @@
|
||||
CONDITIONS OF ANY KIND, either express or implied.
|
||||
*/
|
||||
#include <stdio.h>
|
||||
#include <strings.h>
|
||||
#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<RCV_BUFSIZE; i++){
|
||||
if(buf[i] == 0)
|
||||
break;
|
||||
putchar(buf[i]);
|
||||
}
|
||||
|
||||
put_line(fb, 3, "http", 1, 1);
|
||||
|
||||
for (;;) {
|
||||
vTaskDelay(1);
|
||||
}
|
||||
|
||||
14
main/wlan.c
14
main/wlan.c
@@ -1,10 +1,11 @@
|
||||
#include "sdkconfig.h"
|
||||
#include "esp_system.h"
|
||||
#include "esp_wifi.h"
|
||||
#include "esp_event.h"
|
||||
#include "nvs_flash.h"
|
||||
#include "esp_log.h"
|
||||
#include "esp_wifi.h"
|
||||
|
||||
static const char *TAG = "wifi station";
|
||||
static const char *TAG = "wlan";
|
||||
|
||||
/* 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
|
||||
@@ -103,3 +104,12 @@ void wifi_init_sta(void)
|
||||
}
|
||||
}
|
||||
|
||||
void wlan_init(){
|
||||
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();
|
||||
}
|
||||
|
||||
@@ -1 +1 @@
|
||||
void wifi_init_sta(void);
|
||||
void wlan_init(void);
|
||||
|
||||
@@ -854,7 +854,7 @@ CONFIG_ESP_SYSTEM_PANIC_REBOOT_DELAY_SECONDS=0
|
||||
|
||||
CONFIG_ESP_SYSTEM_EVENT_QUEUE_SIZE=32
|
||||
CONFIG_ESP_SYSTEM_EVENT_TASK_STACK_SIZE=2304
|
||||
CONFIG_ESP_MAIN_TASK_STACK_SIZE=3584
|
||||
CONFIG_ESP_MAIN_TASK_STACK_SIZE=50000
|
||||
CONFIG_ESP_MAIN_TASK_AFFINITY_CPU0=y
|
||||
# CONFIG_ESP_MAIN_TASK_AFFINITY_CPU1 is not set
|
||||
# CONFIG_ESP_MAIN_TASK_AFFINITY_NO_AFFINITY is not set
|
||||
@@ -1774,7 +1774,7 @@ CONFIG_ESP32_PANIC_PRINT_REBOOT=y
|
||||
# CONFIG_ESP32_PANIC_GDBSTUB is not set
|
||||
CONFIG_SYSTEM_EVENT_QUEUE_SIZE=32
|
||||
CONFIG_SYSTEM_EVENT_TASK_STACK_SIZE=2304
|
||||
CONFIG_MAIN_TASK_STACK_SIZE=3584
|
||||
CONFIG_MAIN_TASK_STACK_SIZE=50000
|
||||
CONFIG_CONSOLE_UART_DEFAULT=y
|
||||
# CONFIG_CONSOLE_UART_CUSTOM is not set
|
||||
# CONFIG_CONSOLE_UART_NONE is not set
|
||||
|
||||
Reference in New Issue
Block a user