2016-09-21 00:57:02 -04:00
|
|
|
/* HTTP GET Example using plain POSIX sockets
|
|
|
|
|
|
|
|
This example code is in the Public Domain (or CC0 licensed, at your option.)
|
|
|
|
|
|
|
|
Unless required by applicable law or agreed to in writing, this
|
|
|
|
software is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
|
|
|
|
CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
*/
|
|
|
|
#include <string.h>
|
|
|
|
#include "freertos/FreeRTOS.h"
|
|
|
|
#include "freertos/task.h"
|
2016-09-26 21:16:40 -04:00
|
|
|
#include "esp_system.h"
|
2016-09-21 00:57:02 -04:00
|
|
|
#include "esp_wifi.h"
|
2019-04-11 06:54:26 -04:00
|
|
|
#include "esp_event.h"
|
2016-09-21 00:57:02 -04:00
|
|
|
#include "esp_log.h"
|
2016-09-26 21:16:40 -04:00
|
|
|
#include "nvs_flash.h"
|
2019-04-11 06:54:26 -04:00
|
|
|
#include "protocol_examples_common.h"
|
2016-09-21 00:57:02 -04:00
|
|
|
|
|
|
|
#include "lwip/err.h"
|
|
|
|
#include "lwip/sockets.h"
|
|
|
|
#include "lwip/sys.h"
|
|
|
|
#include "lwip/netdb.h"
|
|
|
|
#include "lwip/dns.h"
|
|
|
|
|
|
|
|
/* Constants that aren't configurable in menuconfig */
|
|
|
|
#define WEB_SERVER "example.com"
|
|
|
|
#define WEB_PORT 80
|
|
|
|
#define WEB_URL "http://example.com/"
|
|
|
|
|
|
|
|
static const char *TAG = "example";
|
|
|
|
|
2017-04-07 05:56:13 -04:00
|
|
|
static const char *REQUEST = "GET " WEB_URL " HTTP/1.0\r\n"
|
|
|
|
"Host: "WEB_SERVER"\r\n"
|
|
|
|
"User-Agent: esp-idf/1.0 esp32\r\n"
|
|
|
|
"\r\n";
|
2016-09-21 00:57:02 -04:00
|
|
|
|
|
|
|
static void http_get_task(void *pvParameters)
|
|
|
|
{
|
|
|
|
const struct addrinfo hints = {
|
|
|
|
.ai_family = AF_INET,
|
|
|
|
.ai_socktype = SOCK_STREAM,
|
|
|
|
};
|
|
|
|
struct addrinfo *res;
|
|
|
|
struct in_addr *addr;
|
|
|
|
int s, r;
|
|
|
|
char recv_buf[64];
|
|
|
|
|
|
|
|
while(1) {
|
|
|
|
int err = getaddrinfo(WEB_SERVER, "80", &hints, &res);
|
|
|
|
|
|
|
|
if(err != 0 || res == NULL) {
|
|
|
|
ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
|
2016-12-21 20:42:21 -05:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-09-21 00:57:02 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 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);
|
2016-12-21 20:42:21 -05:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-09-21 00:57:02 -04:00
|
|
|
continue;
|
|
|
|
}
|
2017-09-10 23:15:39 -04:00
|
|
|
ESP_LOGI(TAG, "... allocated socket");
|
2016-09-21 00:57:02 -04:00
|
|
|
|
|
|
|
if(connect(s, res->ai_addr, res->ai_addrlen) != 0) {
|
|
|
|
ESP_LOGE(TAG, "... socket connect failed errno=%d", errno);
|
|
|
|
close(s);
|
|
|
|
freeaddrinfo(res);
|
2016-12-21 20:42:21 -05:00
|
|
|
vTaskDelay(4000 / portTICK_PERIOD_MS);
|
2016-09-21 00:57:02 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "... connected");
|
|
|
|
freeaddrinfo(res);
|
|
|
|
|
|
|
|
if (write(s, REQUEST, strlen(REQUEST)) < 0) {
|
|
|
|
ESP_LOGE(TAG, "... socket send failed");
|
|
|
|
close(s);
|
2016-12-21 20:42:21 -05:00
|
|
|
vTaskDelay(4000 / portTICK_PERIOD_MS);
|
2016-09-21 00:57:02 -04:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "... socket send success");
|
|
|
|
|
2017-09-10 23:15:39 -04:00
|
|
|
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);
|
|
|
|
vTaskDelay(4000 / portTICK_PERIOD_MS);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "... set socket receiving timeout success");
|
|
|
|
|
2016-09-21 00:57:02 -04:00
|
|
|
/* Read HTTP response */
|
|
|
|
do {
|
|
|
|
bzero(recv_buf, sizeof(recv_buf));
|
|
|
|
r = read(s, recv_buf, sizeof(recv_buf)-1);
|
|
|
|
for(int i = 0; i < r; i++) {
|
|
|
|
putchar(recv_buf[i]);
|
|
|
|
}
|
|
|
|
} while(r > 0);
|
|
|
|
|
2019-04-11 06:54:26 -04:00
|
|
|
ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d.", r, errno);
|
2016-09-21 00:57:02 -04:00
|
|
|
close(s);
|
|
|
|
for(int countdown = 10; countdown >= 0; countdown--) {
|
|
|
|
ESP_LOGI(TAG, "%d... ", countdown);
|
2016-12-21 20:42:21 -05:00
|
|
|
vTaskDelay(1000 / portTICK_PERIOD_MS);
|
2016-09-21 00:57:02 -04:00
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "Starting again!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-07-16 05:33:30 -04:00
|
|
|
void app_main(void)
|
2016-09-21 00:57:02 -04:00
|
|
|
{
|
2017-03-14 09:39:44 -04:00
|
|
|
ESP_ERROR_CHECK( nvs_flash_init() );
|
2019-04-11 06:54:26 -04:00
|
|
|
tcpip_adapter_init();
|
|
|
|
ESP_ERROR_CHECK(esp_event_loop_create_default());
|
|
|
|
|
|
|
|
/* This helper function configures Wi-Fi or Ethernet, as selected in menuconfig.
|
|
|
|
* Read "Establishing Wi-Fi or Ethernet Connection" section in
|
|
|
|
* examples/protocols/README.md for more information about this function.
|
|
|
|
*/
|
|
|
|
ESP_ERROR_CHECK(example_connect());
|
|
|
|
|
2017-04-11 06:38:50 -04:00
|
|
|
xTaskCreate(&http_get_task, "http_get_task", 4096, NULL, 5, NULL);
|
2016-09-21 00:57:02 -04:00
|
|
|
}
|