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 01:48:36 -04:00
|
|
|
#include "freertos/event_groups.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"
|
2016-09-26 21:16:40 -04:00
|
|
|
#include "esp_event_loop.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"
|
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"
|
|
|
|
|
|
|
|
/* The examples use simple WiFi configuration that you can set via
|
|
|
|
'make menuconfig'.
|
|
|
|
|
|
|
|
If you'd rather not, just change the below entries to strings with
|
|
|
|
the config you want - ie #define EXAMPLE_WIFI_SSID "mywifissid"
|
|
|
|
*/
|
|
|
|
#define EXAMPLE_WIFI_SSID CONFIG_WIFI_SSID
|
|
|
|
#define EXAMPLE_WIFI_PASS CONFIG_WIFI_PASSWORD
|
|
|
|
|
2016-09-26 01:48:36 -04:00
|
|
|
/* FreeRTOS event group to signal when we are connected & ready to make a request */
|
|
|
|
static EventGroupHandle_t wifi_event_group;
|
|
|
|
|
|
|
|
/* The event group allows multiple bits for each event,
|
|
|
|
but we only care about one event - are we connected
|
|
|
|
to the AP with an IP? */
|
|
|
|
const int CONNECTED_BIT = BIT0;
|
2016-09-21 00:57:02 -04:00
|
|
|
|
|
|
|
/* 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
|
|
|
|
2016-09-26 21:16:40 -04:00
|
|
|
static esp_err_t event_handler(void *ctx, system_event_t *event)
|
2016-09-21 00:57:02 -04:00
|
|
|
{
|
|
|
|
switch(event->event_id) {
|
2016-09-26 01:48:36 -04:00
|
|
|
case SYSTEM_EVENT_STA_START:
|
|
|
|
esp_wifi_connect();
|
|
|
|
break;
|
2016-09-21 00:57:02 -04:00
|
|
|
case SYSTEM_EVENT_STA_GOT_IP:
|
2016-09-26 01:48:36 -04:00
|
|
|
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
|
2016-09-21 00:57:02 -04:00
|
|
|
break;
|
|
|
|
case SYSTEM_EVENT_STA_DISCONNECTED:
|
2016-09-26 01:48:36 -04:00
|
|
|
/* This is a workaround as ESP32 WiFi libs don't currently
|
|
|
|
auto-reassociate. */
|
|
|
|
esp_wifi_connect();
|
|
|
|
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
|
2016-09-21 00:57:02 -04:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
return ESP_OK;
|
|
|
|
}
|
|
|
|
|
2016-09-26 21:16:40 -04:00
|
|
|
static void initialise_wifi(void)
|
2016-09-21 00:57:02 -04:00
|
|
|
{
|
2016-09-26 21:16:40 -04:00
|
|
|
tcpip_adapter_init();
|
|
|
|
wifi_event_group = xEventGroupCreate();
|
|
|
|
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
|
2016-09-27 05:31:49 -04:00
|
|
|
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
|
2016-09-26 21:16:40 -04:00
|
|
|
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
|
|
|
|
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
|
2016-09-21 00:57:02 -04:00
|
|
|
wifi_config_t wifi_config = {
|
|
|
|
.sta = {
|
|
|
|
.ssid = EXAMPLE_WIFI_SSID,
|
|
|
|
.password = EXAMPLE_WIFI_PASS,
|
|
|
|
},
|
|
|
|
};
|
|
|
|
ESP_LOGI(TAG, "Setting WiFi configuration SSID %s...", wifi_config.sta.ssid);
|
2016-09-26 21:16:40 -04:00
|
|
|
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
|
2016-11-08 04:45:17 -05:00
|
|
|
ESP_ERROR_CHECK( esp_wifi_set_config(ESP_IF_WIFI_STA, &wifi_config) );
|
2016-09-26 21:16:40 -04:00
|
|
|
ESP_ERROR_CHECK( esp_wifi_start() );
|
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) {
|
2016-09-26 01:48:36 -04:00
|
|
|
/* Wait for the callback to set the CONNECTED_BIT in the
|
|
|
|
event group.
|
|
|
|
*/
|
|
|
|
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
|
|
|
|
false, true, portMAX_DELAY);
|
2016-09-21 00:57:02 -04:00
|
|
|
ESP_LOGI(TAG, "Connected to AP");
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
ESP_LOGI(TAG, "... allocated socket\r\n");
|
|
|
|
|
|
|
|
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");
|
|
|
|
|
|
|
|
/* 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);
|
|
|
|
|
|
|
|
ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d\r\n", r, errno);
|
|
|
|
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!");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void app_main()
|
|
|
|
{
|
2017-03-14 09:39:44 -04:00
|
|
|
ESP_ERROR_CHECK( nvs_flash_init() );
|
2016-09-26 21:16:40 -04:00
|
|
|
initialise_wifi();
|
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
|
|
|
}
|