Skip to content

Instantly share code, notes, and snippets.

@projectgus
Created July 23, 2018 05:38
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save projectgus/82f883588f5b26a68bf03f26e5c9097a to your computer and use it in GitHub Desktop.
Save projectgus/82f883588f5b26a68bf03f26e5c9097a to your computer and use it in GitHub Desktop.
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "esp_wifi.h"
#include "esp_system.h"
#include "esp_event.h"
#include "esp_event_loop.h"
#include "nvs_flash.h"
#include "lwip/api.h"
#include "lwip/tcp.h"
esp_err_t event_handler(void *ctx, system_event_t *event)
{
return ESP_OK;
}
void app_main(void)
{
nvs_flash_init();
tcpip_adapter_init();
ESP_ERROR_CHECK( esp_event_loop_init(event_handler, NULL) );
wifi_init_config_t cfg = WIFI_INIT_CONFIG_DEFAULT();
ESP_ERROR_CHECK( esp_wifi_init(&cfg) );
ESP_ERROR_CHECK( esp_wifi_set_storage(WIFI_STORAGE_RAM) );
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_STA) );
wifi_config_t sta_config = {
.sta = {
.ssid = "SSID",
.password = "pass",
.bssid_set = false
}
};
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_STA, &sta_config) );
ESP_ERROR_CHECK( esp_wifi_start() );
ESP_ERROR_CHECK( esp_wifi_connect() );
while (1) {
struct netconn *conn = netconn_new(NETCONN_TCP);
ip_addr_t remote;
netconn_gethostbyname("my_ip_here", &remote);
err_t err = netconn_connect(conn, &remote, 8000);
if (err != ERR_OK) {
printf("Connect failed\n");
vTaskDelay(1000 / portTICK_PERIOD_MS);
continue;
}
printf("Connected!");
tcp_nagle_disable(conn->pcb.tcp); /* HACK */
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 5; j++) {
const char *data= "OHAI!\n";
netconn_write(conn, data, strlen(data), NETCONN_NOCOPY);
}
vTaskDelay(100 / portTICK_PERIOD_MS);
}
printf("Done!");
netconn_close(conn);
netconn_delete(conn);
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment