thank you for your time.
Code: Select all
/* HTTP GET Example using plain POSIX sockets
Excerpts from Public Domain code
Setup sdkconfig using make "menuconfig". this is were you put the ssid, pswd, etc.
add your desktop ip address in the code below under "define web_server"
use make flash to build and flash device
once flashed to ESP32, use "make monitor" to observe progress.
Use the included "echotool.exe" to connect and echo the tcp/ip packets "echotool.exe /p tcp /s 7777"
attached scope to pin 5 to observe toggle pin thread. delays can be added to simulate a worker thread.
2/26/18 softAP mode is working. smoother connection.
*/
#include <string.h>
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_wifi.h"
#include "esp_event_loop.h"
#include "esp_log.h"
#include "nvs_flash.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#include "driver/gpio.h"
#include "sdkconfig.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
#define AP_SSID "ThePartyHouse" // for soft-AP, set variable "ap" to "1" below
#define OUT_GPIO 5
/* 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;
/* Constants that aren't configurable in menuconfig */
//#define WEB_SERVER "192.168.4.2" // address of your echo client (desktop computer, etc.)
#define WEB_PORT 7777
#define AP 1 // set to 1 if you want SoftAP, otherwise it will connect to the AP specified in sdkconfig (menuconfig)
#if AP
#define WEB_SERVER "192.168.4.2" //softAP mode. this is the default IP of the first connected station (change if not the case)
#else
#define WEB_SERVER "192.168.1.213" //station mode. this is the IP address of the echo server (usually a desktop)
#endif
static const char *TAG = ">";
static const char *REQUEST = "message from server1";
int s, r;
//int ap = 1; // set to "1" for soft-AP
char recv_buf[64];
static esp_err_t event_handler(void *ctx, system_event_t *event)
{
switch(event->event_id) {
case SYSTEM_EVENT_STA_START:
esp_wifi_connect();
break;
case SYSTEM_EVENT_AP_START:
ESP_LOGI(TAG, "Soft-AP started" );
//esp_wifi_connect();
break;
case SYSTEM_EVENT_AP_STACONNECTED:
ESP_LOGI(TAG, "station connected to this soft-AP: %s", AP_SSID );
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_GOT_IP:
xEventGroupSetBits(wifi_event_group, CONNECTED_BIT);
break;
case SYSTEM_EVENT_STA_DISCONNECTED:
/* This is a workaround as ESP32 WiFi libs don't currently
auto-reassociate. */
esp_wifi_connect();
xEventGroupClearBits(wifi_event_group, CONNECTED_BIT);
break;
default:
break;
}
return ESP_OK;
}
static void initialise_wifi(void)
{
tcpip_adapter_init();
wifi_event_group = xEventGroupCreate();
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) );
if (AP != 0)
{
ESP_LOGI(TAG, "Setting WiFi mode to Soft-AP" );
wifi_config_t ap_config = {
.ap = {
.ssid = AP_SSID,
.channel = 0,
.authmode = WIFI_AUTH_OPEN,
.ssid_hidden = 0,
.max_connection = 2,
.beacon_interval = 100
}
};
ESP_ERROR_CHECK( esp_wifi_set_mode(WIFI_MODE_AP) );
ESP_ERROR_CHECK( esp_wifi_set_config(WIFI_IF_AP, &ap_config));
}
else
{
ESP_LOGI(TAG, "Setting WiFi mode to station" );
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 );
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() );
}
static void rxtxpacket(void *pvParameters)
{
while (1)
{
if (write(s, REQUEST, strlen(REQUEST)) < 0) {
ESP_LOGE(TAG, "... socket send failed");
close(s);
vTaskDelay(4000 / portTICK_RATE_MS);
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 == (sizeof(recv_buf)-1));
ESP_LOGI(TAG, "... done reading from socket. Last read return=%d errno=%d\r\n", r, errno);
}
}
static void connectToServer(void)
{
const struct addrinfo hints = {
.ai_family = AF_INET,
.ai_socktype = SOCK_STREAM,
};
struct addrinfo *res;
struct in_addr *addr;
/* Wait for the callback to set the CONNECTED_BIT in the
event group.
*/
xEventGroupWaitBits(wifi_event_group, CONNECTED_BIT,
false, true, portMAX_DELAY);
if (!AP)
{
ESP_LOGI(TAG, "Connected to AP");
}
int err = getaddrinfo(WEB_SERVER, "7777", &hints, &res);
if(err != 0 || res == NULL) {
ESP_LOGE(TAG, "DNS lookup failed err=%d res=%p", err, res);
vTaskDelay(1000 / portTICK_RATE_MS);
//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);
vTaskDelay(1000 / portTICK_RATE_MS);
//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. make sure echo server is listening on port 7777 and restart make monitor.", errno);
close(s);
freeaddrinfo(res);
vTaskDelay(4000 / portTICK_RATE_MS);
//continue;
}
else
{
ESP_LOGI(TAG, "... connected");
freeaddrinfo(res);
}
}
void toggle_pin(void *pvParameter)
{
gpio_pad_select_gpio(OUT_GPIO);
gpio_set_direction(OUT_GPIO, GPIO_MODE_OUTPUT);
while(1) {
gpio_set_level(OUT_GPIO, 1);
vTaskDelay(8 / portTICK_RATE_MS); // 9ms or less delay on either side of pin change always produces 3.3v, 64KHz square wave on scope. but 10ms drops to 50hz. why non-linear?
gpio_set_level(OUT_GPIO, 0);
vTaskDelay(8 / portTICK_RATE_MS);
}
}
void app_main()
{
nvs_flash_init();
initialise_wifi();
vTaskDelay(2000 / portTICK_RATE_MS);
connectToServer();
//xTaskCreate(&toggle_pin, "toggle_pin", 512, NULL, 10, NULL);
xTaskCreate(&rxtxpacket, "rxtxpacket", 2048, NULL, 5, NULL);
}