Skip to content

Instantly share code, notes, and snippets.

@todbot
Last active September 8, 2025 02:22
Show Gist options
  • Save todbot/020b685bc3045bf777de7461b624ce3b to your computer and use it in GitHub Desktop.
Save todbot/020b685bc3045bf777de7461b624ce3b to your computer and use it in GitHub Desktop.
simple "HTTP by hand" in ESP-IDF
// simple "HTTP by hand" in ESP-IDF
// add "REQUIRES esp_wifi esp_netif nvs_flash" to CMakeLists.txt
#include <string.h>
#include <sys/socket.h>
#include <netdb.h> // getaddrinfo()
#include "esp_wifi.h"
#include "esp_event.h"
#include "esp_log.h"
#include "nvs_flash.h"
const char* my_wifi_ssid = "myssidismypassport";
const char* my_wifi_pass = "setectastronomy";
const char* url_hostname = "todbot.com";
const char* url_path = "/tst/randcolor.php";
static const char *TAG = "HTTP_TOD";
// note this does not wait for wifi to connect
static void wifi_init(const char *wifi_ssid, const char *wifi_pass) {
ESP_ERROR_CHECK(esp_netif_init());
ESP_ERROR_CHECK(esp_event_loop_create_default());
esp_netif_create_default_wifi_sta();
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK(esp_wifi_init(&cfg));
/* if using #defines, can do this */
/* wifi_config_t wifi_config = { */
/* .sta = { */
/* .ssid = WIFI_SSID, */
/* .password = WIFI_PASS, */
/* }, */
/* }; */
/* but for in-memory strings, need to strncpy() the exact length */
wifi_config_t wifi_config = {0}; // empty config
strncpy((char *)wifi_config.sta.ssid, wifi_ssid, sizeof(wifi_config.sta.ssid));
strncpy((char *)wifi_config.sta.password, wifi_pass, sizeof(wifi_config.sta.password));
ESP_ERROR_CHECK(esp_wifi_set_mode(WIFI_MODE_STA));
ESP_ERROR_CHECK(esp_wifi_set_config(WIFI_IF_STA, &wifi_config));
ESP_ERROR_CHECK(esp_wifi_start());
ESP_ERROR_CHECK(esp_wifi_connect());
}
static void http_get(const char *host, const char *path) {
struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res;
int s;
char port_str[] = "80";
int err = getaddrinfo(host, port_str, &hints, &res);
if (err != 0 || res == NULL) {
ESP_LOGE(TAG, "DNS lookup failed for %s", host);
return;
}
s = socket(res->ai_family, res->ai_socktype, 0);
if (s < 0) {
ESP_LOGE(TAG, "socket failed");
freeaddrinfo(res);
return;
}
if (connect(s, res->ai_addr, res->ai_addrlen) != 0) {
ESP_LOGE(TAG, "connect failed");
close(s);
freeaddrinfo(res);
return;
}
freeaddrinfo(res);
char req[128];
snprintf(req, sizeof(req),
"GET %s HTTP/1.0\r\n"
"Host: %s\r\n"
"User-Agent: esp-idf/5.5\r\n"
"\r\n",
path, host);
if (write(s, req, strlen(req)) < 0) {
ESP_LOGE(TAG, "send failed");
close(s);
return;
}
char recv_buf[512];
int r;
while ((r = read(s, recv_buf, sizeof(recv_buf) - 1)) > 0) {
recv_buf[r] = 0; // null terminate
printf("%s", recv_buf);
}
close(s);
}
void app_main(void) {
printf("-------- app startup \n"); // this eliminates the USB disconnect/reconnect?
ESP_LOGI(TAG, "-------- App startup \n");
ESP_ERROR_CHECK(nvs_flash_init());
ESP_LOGI(TAG, "-------- Waiting 5 seconds for USB to start\n");
vTaskDelay(pdMS_TO_TICKS(5000)); // wait for USB to connect
ESP_LOGI(TAG, "-------- Connecting to WiFi and waiting 5 seconds...\n");
wifi_init(my_wifi_ssid, my_wifi_pass);
ESP_LOGI(TAG, "-------- Connected.\n");
vTaskDelay(pdMS_TO_TICKS(5000)); // wait for WiFi to connect
while(1) {
http_get(url_hostname, url_path);
printf("\n");
vTaskDelay(pdMS_TO_TICKS(10*1000)); // wait before doing it all again
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment