/* The example of esp-free-rtos
*
* This sample code is in the public domain.
*/
#include <stdlib.h>
#include <string.h>
#include "espressif/esp_common.h"
#include "esp/uart.h"
#include "FreeRTOS.h"
#include "task.h"
#include "esp8266.h"
#include "lwip/err.h"
#include "lwip/sockets.h"
#include "lwip/sys.h"
#include "lwip/netdb.h"
#include "lwip/dns.h"
#define SERVER_IP "192.168.10.10"
#define SERVER_PORT 9876
int TaskIsAlive = 1;
SemaphoreHandle_t xSemaphore;
void showNetworkInfo() {
uint8_t hwaddr[6];
if (sdk_wifi_get_macaddr(STATION_IF, hwaddr)) {
printf("[%s] MAC
address="MACSTR"\n",pcTaskGetName(0),MAC2STR(hwaddr));
}
struct ip_info info;
if (sdk_wifi_get_ip_info(STATION_IF, &info)) {
printf("[%s] IP
address="IPSTR"\n",pcTaskGetName(0),IP2STR(&info.ip));
printf("[%s]
Netmask="IPSTR"\n",pcTaskGetName(0),IP2STR(&info.netmask));
printf("[%s]
Gateway="IPSTR"\n",pcTaskGetName(0),IP2STR(&info.gw));
}
}
// Socket Client Task
void task1(void *pvParameters)
{
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d] Start\n",pcTaskGetName(0),nowTick);
/* Wait until we have joined AP and are assigned an
IP */
int wifiStatus;
while (1) {
wifiStatus =
sdk_wifi_station_get_connect_status();
nowTick = xTaskGetTickCount();
printf("[%s:%d]
wifiStatus=%d\n",pcTaskGetName(0),nowTick,wifiStatus);
//if
(sdk_wifi_station_get_connect_status() == STATION_GOT_IP)
break;
if (wifiStatus == STATION_GOT_IP)
break;
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
nowTick = xTaskGetTickCount();
printf("[%s:%d] Connected to
AP\n",pcTaskGetName(0),nowTick);
showNetworkInfo();
/* Now start task2 */
xSemaphoreGive(xSemaphore);
/* set up address to connect to */
struct sockaddr_in addr;
memset(&addr, 0, sizeof(addr));
//addr.sin_len = sizeof(addr);
addr.sin_family = AF_INET;
//addr.sin_port = PP_HTONS(SERVER_PORT);
addr.sin_port = htons(SERVER_PORT);
addr.sin_addr.s_addr = inet_addr(SERVER_IP);
/* create the socket */
int fd;
int ret;
fd = lwip_socket(AF_INET, SOCK_STREAM, 0);
LWIP_ASSERT("fd >= 0", fd >= 0);
/* connect */
ret = lwip_connect(fd, (struct sockaddr*)&addr,
sizeof(addr));
/* should succeed */
LWIP_ASSERT("ret == 0", ret == 0);
/* write something */
ret = lwip_write(fd, "test", 4);
LWIP_ASSERT("ret == 4", ret == 4);
nowTick = xTaskGetTickCount();
printf("[%s:%d] lwip_write
ret=%d\n",pcTaskGetName(0),nowTick,ret);
/* read something */
char buf[256];
memset(buf,0,sizeof(buf));
ret = lwip_read(fd, buf, sizeof(buf));
LWIP_ASSERT("ret > 0", ret > 0);
nowTick = xTaskGetTickCount();
printf("[%s:%d] lwip_read
ret=%d\n",pcTaskGetName(0),nowTick,ret);
if (ret > 0) {
nowTick = xTaskGetTickCount();
printf("[%s:%d] ret=%d
buf=%s\n",pcTaskGetName(0),nowTick,ret,buf);
}
/* close */
ret = lwip_close(fd);
LWIP_ASSERT("ret == 0", ret == 0);
TaskIsAlive = 0;
vTaskDelete( NULL );
}
// Dummy Task
void task2(void *pvParameters)
{
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d] Start\n",pcTaskGetName(0),nowTick);
xSemaphoreTake(xSemaphore, portMAX_DELAY);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Take
Semaphore\n",pcTaskGetName(0),nowTick);
while(TaskIsAlive) {
nowTick = xTaskGetTickCount();
printf("[%s:%d]
Running...\n",pcTaskGetName(0),nowTick);
vTaskDelay(1);
}
vTaskDelete( NULL );
}
void user_init(void)
{
uart_set_baud(0, 115200);
printf("SDK version:%s\n",
sdk_system_get_sdk_version());
printf("pcTaskGetName=%s\n",pcTaskGetName(0));
/* Create Semaphore */
xSemaphore = xSemaphoreCreateBinary();
/* Check everything was created. */
configASSERT( xSemaphore );
xTaskCreate(task1, "Client", 384, NULL, 2, NULL);
xTaskCreate(task2, "Dummy", 256, NULL, 2, NULL);
}
|