/* 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 UDP_PORT 9876
#define BUFLEN 512
SemaphoreHandle_t xSemaphore;
static 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));
}
}
// WiFi 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);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Give to
task2\n",pcTaskGetName(0),nowTick);
while(1) {
vTaskDelay(1000 / portTICK_PERIOD_MS);
}
}
// UDP Server Task
void task2(void *pvParameters)
{
TickType_t nowTick;
nowTick = xTaskGetTickCount();
printf("[%s:%d] Start\n",pcTaskGetName(0),nowTick);
/* wait for Semaphore */
xSemaphoreTake(xSemaphore, portMAX_DELAY);
nowTick = xTaskGetTickCount();
printf("[%s:%d] Take\n",pcTaskGetName(0),nowTick);
/* set up address to connect to */
struct sockaddr_in myaddr; // address of the
server
struct sockaddr_in claddr; // address of the
client
memset((char *)&myaddr, 0, sizeof(myaddr));
myaddr.sin_family = AF_INET;
myaddr.sin_addr.s_addr = htonl(INADDR_ANY);
myaddr.sin_port = htons(UDP_PORT);
/* create the socket */
int fd;
int ret;
fd = lwip_socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP
); // Create a UDP socket.
//fd = lwip_socket(AF_INET, SOCK_DGRAM, 0 ); //
Create a UDP socket.
LWIP_ASSERT("fd >= 0", fd >= 0);
/* bind server address to socket descriptor */
ret = bind(fd, (struct sockaddr *)&myaddr,
sizeof(myaddr));
LWIP_ASSERT("ret >= 0", ret >= 0);
char buf[BUFLEN];
long recvlen;
unsigned int addrlen = sizeof(claddr);
while (1) {
/* receive the datagram */
recvlen = recvfrom(fd, buf,
BUFLEN, 0, (struct sockaddr *)&claddr, &addrlen);
printf("Received %ld bytes\n",recvlen);
if (recvlen > 0) {
buf[recvlen] = 0;
printf("Client IP %s, PORT
%u\n", inet_ntoa(claddr.sin_addr), ntohs(claddr.sin_port)
);
printf("Received message:
[%s]\n",buf);
for (int i=0; i<
recvlen; i++){ // bufの中の小文字を大文字に変換
if(isalpha((int)buf[i])) {
if(islower((int)buf[i])) {
buf[i] = toupper((int)buf[i]);
}
else {
buf[i] = tolower((int)buf[i]);
}
}
}
/* send the response */
ret = sendto(fd, buf,
recvlen, 0, (struct sockaddr *)&claddr,
sizeof(claddr));
LWIP_ASSERT("ret ==
recvlen", ret == recvlen);
}
}
/* close */
ret = lwip_close(fd);
LWIP_ASSERT("ret == 0", ret == 0);
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 );
// Create Task
xTaskCreate(task1, "WiFi", 256, NULL, 2, NULL);
xTaskCreate(task2, "Server", 1024, NULL, 2, NULL);
}
|