본문 바로가기

잡지/WIZnet 전자회로

[W5500-EVB Project] Easy Web Server

This project is using the W5500-EVB and operating embedded web server Plese check the below picture Embedded web server

First, You Look for Desription Sequential Flow chart (Block Diagram)

Sequence = HTTP Request Parser -> HTTP GET Method -> Requested Web content Found -> Load the Web content from storage -> HTTP Response HTTP/1.1 200 OK + HTTP body -> Send HTTP Response.
Second,This project Use to HTTP Protocol based and HTTP header GET Method. But Not Used POST, PUT, HEAD etc.. header
For more information about Hypertext Transfer Protocol please also refer to the Wiki page
HTTP is Configuration to Server / Client.
– HTTP Server : Embedded device(W5500-EVB)
– HTTP Client : Web browser (Internet Explore, Chrome etc..)
Web server is operating for HTTP based.
Request to Web server from Web browser -> Web server is Transport to response message to web browser(web page)
This project is Not Using Javascript & JSON. Just Using C & HTML Language. It is vey simply source code.

그림1123 This project is Not Using Javascript & JSON. Just Using C & HTML Language. It is vey simply source code

[code language="c"]

/*
===============================================================================
 Name        : W5500EVB.c
 Author      : $(author)
 Version     :
 Copyright   : $(copyright)
 Description : main definition
===============================================================================
*/

#if defined (__USE_LPCOPEN)
#if defined(NO_BOARD_LIB)
#include "chip.h"
#else
#include "board.h"
#endif
#endif

#include "spiHandler.h"
#include "w5500_init.h"
#include "common.h"
#include "loopback.h"
#include "mmcHandler.h"
#include "dataflashHandler.h"
#include "wizchip_conf.h"
#include "ffconf.h"
#include "eepromHandler.h"
#include "ftpc.h"
#include 

// TODO: insert other include files here


// TODO: insert other definitions and declarations here

typedef struct __Cfg_Info {
	uint8_t spiflash_flag[2];
} __attribute__((packed)) Cfg_Info;

/*****************************************************************************
 * Private types/enumerations/variables
 ****************************************************************************/
#define TICKRATE_HZ1 (1000)		/* 1000 ticks per second, for SysTick */
#define TICKRATE_HZ2 (1)		/* 1 ticks per second, for Timer0 */
volatile uint32_t msTicks; 		/* counts 1ms timeTicks */

/*****************************************************************************
 * Public types/enumerations/variables
 ****************************************************************************/
///////////////////////////////////////
// Debugging Message Printout enable //
///////////////////////////////////////
#define _MAIN_DEBUG_

///////////////////////////
// Demo Firmware Version //
///////////////////////////
#define VER_H		1
#define VER_L		00

#define POTENTIOMETER_CH				ADC_CH0		// ADC channel for on-board Potentiometer (ADC channel / Pin shared with Temperature sensor)
#define POTENTIOMETER_ADC_PORT_NUM		0
#define POTENTIOMETER_ADC_PIN_NUM		11

#define ADC_READ_PERIOD_MS				10			// ms

static ADC_CLOCK_SETUP_T 				ADCSetup;
static bool ADC_read_enable = false;

//////////////////////////////////////////////////
// Socket & Port number definition for Examples //
//////////////////////////////////////////////////
#define SOCK_TCPS       0
#define SOCK_UDPS       1
#define SOCK_WEBSERVER	1
#define PORT_TCPS		5000
#define PORT_UDPS       3000
#define PORT_WEBSERVER	80
/* for Web Server test debug message printout enable */
#define	_WEBSERVER_DEBUG_
#define	WEBSERVER
#define WEBSERVER_EX	1 // mode setting
/************************/
/* Select Web Server_MODE */
/************************/

#define LED_BLUE 					Chip_GPIO_ReadPortBit(LPC_GPIO, 1, 24)
////////////////////////////////////////////////
// Shared Buffer Definition for LOOPBACK TEST //
////////////////////////////////////////////////

uint8_t gDATABUF[DATA_BUF_SIZE];
uint8_t gFTPBUF[_MAX_SS];
//uint8_t LED_G = Chip_GPIO_ReadPortBit(LPC_GPIO, 1, 23);
///////////////////////////
// Network Configuration //
///////////////////////////
wiz_NetInfo gWIZNETINFO = { .mac = {0x00, 0x08, 0xdc, 0xab, 0xcd, 0xef},
                            .ip = {192, 168, 10, 111},
                            .sn = {255, 255, 255, 0},
                            .gw = {192, 168, 10, 1},
                            .dns = {8, 8, 8, 8},
                            .dhcp = NETINFO_STATIC };

// For TCP client loopback examples; destination network info
uint8_t destip[4] = {192, 168, 10, 230};
uint16_t destport = 5000;

int g_mkfs_done = 0;
int g_sdcard_done = 0;

////////////////////
// Button Control //
////////////////////
#define BUTTONS_PRESSED_TICKS		10		// ms
bool button1_enable = false;
bool button1_pressed_flag = false;

uint8_t send_dat[1024]={0,};
uint16_t dataADC;
static void Init_ADC_PinMux(void)
{
 	Chip_IOCON_PinMuxSet(LPC_IOCON, POTENTIOMETER_ADC_PORT_NUM, POTENTIOMETER_ADC_PIN_NUM, FUNC2);
}


static void display_SDcard_Info(uint8_t mount_ret);
static uint8_t Check_Buttons_Pressed(void);
void SysTick_Handler(void);
/* Web Server test example */
int32_t WebServer(uint8_t sn, uint8_t* buf, uint16_t port);
char proc_http(uint8_t sn, char * buf);
void sendHeader(uint8_t sn);
void sendData(uint8_t sn);
uint16_t calcu_len(void);
int main(void) {
	uint8_t ret = 0;
    //int32_t loopback_ret;
	//uint16_t dataADC;
	uint16_t previous_dataADC = 0;

#if defined (__USE_LPCOPEN)
#if !defined(NO_BOARD_LIB)
    // Read clock settings and update SystemCoreClock variable
    SystemCoreClockUpdate();
    // Set up and initialize all required blocks and
    // functions related to the board hardware
    Board_Init();
    // Set the LED to the state of "On"
    //Board_LED_Set(0, true); //GREEN
    //Board_LED_Set(1, true);		//BLUE
    Board_LED_Set(2, false);			//RED
#endif
#endif

	SPI_Init();
	W5500_Init();
	Net_Conf(gWIZNETINFO);

#ifdef _MAIN_DEBUG_
	uint8_t tmpstr[6] = {0,};

	ctlwizchip(CW_GET_ID,(void*)tmpstr);
/*
    printf("\r\n=======================================\r\n");
	printf(" WIZnet %s EVB Demos v%d.%.2d\r\n", tmpstr, VER_H, VER_L);
	printf("=======================================\r\n");
	printf(">> W5500 based FTP Client Example\r\n");
	printf("=======================================\r\n");
*/
	Display_Net_Conf(); // Print out the network information to serial terminal
#endif

	/* Enable and setup SysTick Timer at a periodic rate */
	SysTick_Config(SystemCoreClock / TICKRATE_HZ1);

	/* Initialize buttons on the W5500 EVB board */
	Board_Buttons_Init();
	/* ADC Init */
	Init_ADC_PinMux();
	Chip_ADC_Init(LPC_ADC, &ADCSetup);
	Chip_ADC_EnableChannel(LPC_ADC, POTENTIOMETER_CH, ENABLE);

	g_sdcard_done = 0;

#if defined(F_APP_FTPC)
	ftpc_init(gWIZNETINFO.ip);
#endif

	ret = flash_mount();
	if(ret > 0)
	{
		display_SDcard_Info(ret);
	}

	while(1) {
		if(ADC_read_enable){
			ADC_read_enable = false;
			/* Start A/D conversion */
			Chip_ADC_SetStartMode(LPC_ADC, ADC_START_NOW, ADC_TRIGGERMODE_RISING);
			/* Waiting for A/D conversion complete */
			while (Chip_ADC_ReadStatus(LPC_ADC, POTENTIOMETER_CH, ADC_DR_DONE_STAT) != SET) {}
			/* Read ADC value */
			Chip_ADC_ReadValue(LPC_ADC, POTENTIOMETER_CH, &dataADC);
		}
		//if(previous_dataADC != dataADC)
		if((previous_dataADC/10) != (dataADC/10)){
			previous_dataADC = dataADC;
			/* Print ADC value */
			printf("Read Potentiometer value is %d\r\n", dataADC);
		}

	   	/* Button: SW1 */
		if(Check_Buttons_Pressed() == BUTTONS_BUTTON1)
		{
			printf("\r\n########## SW1 was pressed.\r\n");
			printf("########## Data flash flag was cleared.\r\n");
			printf("########## Please reset a target.\r\n");
			release_factory_flag();
		}

#if defined(F_APP_FTPC)
		ftpc_run(gFTPBUF);
#endif
#if	defined(WEBSERVER)
		WebServer(SOCK_WEBSERVER, gDATABUF, PORT_WEBSERVER);
#endif
		/* Loopback Test: TCP Server and UDP */
		// Test for Ethernet data transfer validation
		{
			//loopback_tcps(SOCK_TCPS, gDATABUF, PORT_TCPS);
			//loopback_udps(SOCK_UDPS, gDATABUF, PORT_UDPS);
			//loopback_ret = loopback_tcpc(SOCK_TCPS, gDATABUF, destip, destport);

			//if(loopback_ret < 0) printf("loopback ret: %ld\r\n", loopback_ret); // TCP Socket Error code
		}
	}

    return 0 ;
}

static uint8_t Check_Buttons_Pressed(void)
{
	static uint8_t buttons_status;
	static uint8_t ret;

	buttons_status = Buttons_GetStatus();

	if((buttons_status & BUTTONS_BUTTON1) == BUTTONS_BUTTON1) button1_enable = true; // button pressed check timer enable
	else button1_enable = false;

	if(button1_pressed_flag)	// button1 pressed (Specified time elapsed, enabled by sysTick_Handler function)
	{
		button1_pressed_flag = false; // pressed button clear
		ret = BUTTONS_BUTTON1; // return pressed button status
	}
	else
	{
		ret = 0;
	}

	return ret;
}

void SysTick_Handler(void)
{
	static uint16_t button1_pressed_check_cnt = 0;
	static bool button1_press_detected = false;

	msTicks++; // increment counter

	if (msTicks % ADC_READ_PERIOD_MS == 0) ADC_read_enable = true;
	// Button1 control
	if(button1_enable == true)
	{
		if(!button1_press_detected)
		{
			button1_pressed_check_cnt++;
			if(button1_pressed_check_cnt >= BUTTONS_PRESSED_TICKS)
			{
				button1_pressed_flag = true;
				button1_pressed_check_cnt = 0;
				button1_enable = false;

				button1_press_detected = true;
			}
		}
	}
	else
	{
		button1_pressed_check_cnt = 0;
		button1_press_detected = false;
	}
}

int32_t WebServer(uint8_t sn, uint8_t* buf, uint16_t port)
{
   int32_t ret;
   uint16_t size = 0, sentsize=0, i=10000;
#ifdef _WEBSERVER_DEBUG_
   uint8_t destip[4];
   uint16_t destport;
#endif

   switch(getSn_SR(sn))
   {
      case SOCK_ESTABLISHED :
         if(getSn_IR(sn) & Sn_IR_CON)
         {
#ifdef _WEBSERVER_DEBUG_
			getSn_DIPR(sn, destip);
			destport = getSn_DPORT(sn);

			printf("%d:Connected - %d.%d.%d.%d : %d\r\n",sn, destip[0], destip[1], destip[2], destip[3], destport);
#endif
			setSn_IR(sn,Sn_IR_CON);
         }
         while(i){
        	 i--;
         }
		 if((size = getSn_RX_RSR(sn)) > 0) // Don't need to check SOCKERR_BUSY because it doesn't not occur.
         {
			if(size > DATA_BUF_SIZE) size = DATA_BUF_SIZE;
			ret = recv(sn, buf, size);

			if(ret <= 0) return ret;      // check SOCKERR_BUSY & SOCKERR_XXX. For showing the occurrence of SOCKERR_BUSY.
			sentsize = 0;

			printf("HTTP Request received: \r\n%s\r\n", buf);
			proc_http(sn, (char *)buf);

         }
         break;
      case SOCK_CLOSE_WAIT :
#ifdef _WEBSERVER_DEBUG_
         printf("%d:CloseWait\r\n",sn);
#endif
         if((ret = disconnect(sn)) != SOCK_OK) return ret;
#ifdef _WEBSERVER_DEBUG_
         printf("%d:Socket Closed\r\n", sn);
#endif
         break;
      case SOCK_INIT :
#ifdef _WEBSERVER_DEBUG_
    	 printf("%d:Listen, Web Server, port [%d]\r\n", sn, port);
#endif
         if( (ret = listen(sn)) != SOCK_OK) return ret;
         break;
      case SOCK_CLOSED:
#ifdef _WEBSERVER_DEBUG_
         printf("%d:Web Server start\r\n",sn);
#endif
         if((ret = socket(sn, Sn_MR_TCP, port, 0x00)) != sn) return ret;
#ifdef _WEBSERVER_DEBUG_
         printf("%d:Socket opened\r\n",sn);
#endif
         break;
      default:
         break;
   }
   return 1;
}
void sendHeader(uint8_t sn)
{
	uint8_t dat[128]={0,};
	uint8_t dat_t[128]={0,};
	uint8_t *dat_temp;
	dat_temp = "HTTP/1.1 200 OK\r\n";						strcat(dat, dat_temp);
	dat_temp = "Content-Type: text/html\r\n";					strcat(dat, dat_temp);
	dat_temp = "Connection: close\r\n";						strcat(dat, dat_temp);
	sprintf(dat, "%sContent-Length: %d", dat, calcu_len());
	dat_temp = "\r\n\r\n";								strcat(dat, dat_temp);
	send(sn, (uint8_t *)dat, strlen(dat));
	printf("%s", dat);
}
uint16_t calcu_len(void)
{
	uint8_t *dat_temp;
	char * forbidden = NULL;
	unsigned char str[4]={0,};
#if	WEBSERVER_EX==1
	dat_temp = "\r\n";												strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "\r\n";														strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "\r\n";					strcat(send_dat, dat_temp);
	dat_temp = "W5500-EVB WebServer Test!!\r\n";									strcat(send_dat, dat_temp);
	dat_temp = "\r\n";								strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "\r\n";														strcat(send_dat, dat_temp);
	dat_temp = "\r\n";				strcat(send_dat, dat_temp);
	dat_temp = "
W5500-EVB WebServer Test!!
\r\n";										strcat(send_dat, dat_temp);
	dat_temp = "

\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "</pre>
<form name="\&quot;form1\&quot;">\r\n";												strcat(send_dat, dat_temp);
	if(!LED_BLUE){
		dat_temp = "
<input name="\&quot;do\&quot;" type="\&quot;hidden\&quot;" value="\&quot;ON\&quot;" />
\r\n";						strcat(send_dat, dat_temp);
		dat_temp = "
LED BLUE <input name="\&quot;formbutton1\&quot;" type="\&quot;button\&quot;" value="\&quot;ON\&quot;" />
\r\n";	strcat(send_dat, dat_temp);
	}
	else{
		dat_temp = "
<input name="\&quot;do\&quot;" type="\&quot;hidden\&quot;" value="\&quot;OFF\&quot;" />
\r\n";						strcat(send_dat, dat_temp);
		dat_temp = "
LED BLUE <input name="\&quot;formbutton1\&quot;" type="\&quot;button\&quot;" value="\&quot;OFF\&quot;" />
\r\n";	strcat(send_dat, dat_temp);
	}
	dat_temp = "</form>
<pre>\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "

\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "\r\n";														strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
#endif
#if	WEBSERVER_EX==2
	dat_temp = "\r\n\r\n";														strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "\r\n\r\n";														strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "\r\n";				        strcat(send_dat, dat_temp);
	dat_temp = "W5500-EVB WebServer Test!!\r\n";									strcat(send_dat, dat_temp);
	dat_temp = "\r\n";								strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "\r\n";														strcat(send_dat, dat_temp);
	dat_temp = "\r\n";	                        strcat(send_dat, dat_temp);
	dat_temp = "
W5500-EVB WebServer Test!!
\r\n";										strcat(send_dat, dat_temp);
	dat_temp = "

\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "
Read Potentiometer = $$$$
\r\n";										strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
	dat_temp = "\r\n";														strcat(send_dat, dat_temp);
	dat_temp = "\r\n";													strcat(send_dat, dat_temp);
	forbidden = strstr(send_dat, "$$$$");
	memset(forbidden,0,4);
	sprintf((char*)str, "%d", dataADC);
	memcpy(forbidden,str,4);
#endif
	return strlen(send_dat);
}
void sendData(uint8_t sn)
{
	uint16_t i;
	send(sn, (uint8_t *)send_dat, strlen(send_dat));
	printf("%s", send_dat);
	for(i=0;i<1024;i++){
		send_dat[i]=0;
	}
}
char proc_http(uint8_t sn, char * buf)
{
	if((buf[0]=='G')&&(buf[1]=='E')&&(buf[2]=='T')&&(buf[3]==' ')){ // GET_Request
		if((buf[5]=='?')){
			if((buf[9]=='O')&&(buf[10]=='N')){
				Board_LED_Set(2, false);
				sendHeader(sn);
				sendData(sn);
			}
			else{
				Board_LED_Set(2, true);
				sendHeader(sn);
				sendData(sn);
			}
		}
		else{
			sendHeader(sn);
			sendData(sn);
		}
	}

	return 1;
}



[/code]


'잡지 > WIZnet 전자회로' 카테고리의 다른 글

W5500-EVB DXF file  (0) 2017.11.06
[WIZnet] W7500 IAP와 ISP의 차이점  (0) 2017.04.12
WIZnet WIZ105SR Schematic  (0) 2016.12.01
WIZnet Chip Altium Library  (0) 2016.04.27
WIZ550web schematic 설명  (0) 2015.11.27