嵌入式Web服务很常见,比如电脑通过WIFI接入网络,在浏览器地址栏输入 "192.168.1.1",或者其他地址,跟自己电脑的IP在同一个网段内,一般IP最后一段是1即可,可以打开路由器的管理页面。就像这样:

这个页面就是有嵌入式小型web服务提供的网页。

STM32 W5500几乎也可以实现上述的功能,但是由于STM32的RAM和FLASH储存大小是有限的,实现一个简单的web服务是没有问题的。

STM32 W5500实现一个简单的web服务需要具备的几个条件:

1、STM32 W5500的基础入网配置,可以PING通,可以参考《STM32F103RC驱动W5500入网,并可ping通》

2、STM32 W5500的TCP Server回环测试没有问题,可以参考 《STM32 W5500 TCP Server 回环测试》

3、对HTTP协议有一定的认识。

确定一下要实现的web服务的功能,STM32 W5500配置入网后,通过DHCP动态获取IP地址,在电脑浏览器地址栏输入这个IP地址,可以获取到一个简单form表单的页面,在表单中输入数据,提交到W5500web服务,返回一个结果。如图:

功能比较简单,比较费事儿的是字符串的处理接收到的字符串的解析,以及返回信息的组装。

直接贴出测试的代码:

#ifndef __STM32F10X_H
#define __STM32F10X_H
#include "stm32f10x.h"
#endif#ifndef __Z_UTIL_TIME_H
#define __Z_UTIL_TIME_H
#include "z_util_time.h"
#endif#ifndef __Z_HARDWARE_LED_H
#define __Z_HARDWARE_LED_H
#include "z_hardware_led.h"
#endif#ifndef __Z_HARDWARE_SPI_H
#define __Z_HARDWARE_SPI_H
#include "z_hardware_spi.h"
#endif#ifndef __W5500_H
#define __W5500_H
#include "w5500.h"
#endif#ifndef __SOCKET_H
#define __SOCKET_H
#include "socket.h"
#endif#ifndef __W5500_CONF_H
#define __W5500_CONF_H
#include "w5500_conf.h"
#endif#ifndef __DHCP_H
#define __DHCP_H
#include "dhcp.h"
#endif#ifndef __Z_HARDWARE_USART2_H
#define __Z_HARDWARE_USART2_H
#include "z_hardware_usart2.h"
#endif#include "MQTTPacket.h"#ifndef __IMPL_MQTT_H
#define __IMPL_MQTT_H
#include "impl_mqtt.h"
#endif#define SOCK_TCPS 0
#define BUFFER_SIZE 1536
#define TEXT_TEMPLATE_OK "HTTP/1.1 200 OK\r\n"\
"Content-Type: text/html\r\n"\
"Content-Length: %d\r\n"\
"Connection: keep_alive\r\n\r\n%s"#define TEXT_TEMPLATE_ERR "HTTP/1.1 404 Not Found\r\n"\
"Content-Length: 0\r\n\r\n"\
"Connection: keep_alive\r\n\r\n"#define HTML_CONTENT "<!DOCTYPE html>"\
"<html>"\
"<head>"\
"<meta charset=\"utf-8\">"\
"<title>SN Config</title>"\
"<style>"\
".d-c{ position:absolute;left:40%;top:49%}"\
".b-c{ position:absolute;left:40%;top:90%}"\
"</style>"\
"</head>"\
"<body>"\
"<div class=\"d-c\">"\
"<form name=\"input\" action=\"sn_config.action\" method=\"post\">"\
"SN:<input type=\"text\" name=\"sn\">"\
"<input type=\"submit\" value=\"submit\">"\
"</form>"\
"</div>  "\
"<div class=\"b-c\">"\
"<p>System SN Config</p>"\
"</div>"\
"</body>"\
"</html>\r\n"#define HTML_RESULT_OK "<!DOCTYPE html>"\
"<html>"\
"<head>"\
"<meta charset=\"utf-8\">"\
"<title>SN Config</title>"\
"<style>"\
".c-c{ position:absolute;left:40%;top:49%}"\
"</style>"\
"</head>"\
"<body>"\
"<div class=\"c-c\">"\
"<p>System SN Config OK</p>"\
"</div>"\
"</body>"\
"</html>\r\n"#define HTML_RESULT_ERR "<!DOCTYPE html>"\
"<html>"\
"<head>"\
"<meta charset=\"utf-8\">"\
"<title>SN Config</title>"\
"<style>"\
".c-c{ position:absolute;left:40%;top:49%}"\
"</style>"\
"</head>"\
"<body>"\
"<div class=\"c-c\">"\
"<p>System SN Config FAIL</p>"\
"</div>"\
"</body>"\
"</html>\r\n"u8 func_analysis_http_request(u8* buffer, u16 len_recv, char* method, char* uri, char* data_body);
u8 func_package_http_response(u8* buffer, u16 *len_ret, u16 len_buf, char* cont, u16 len_cont);int main(void)
{u32 dhcp_timestamp;u8 mac[6]={0, };DHCP_Get dhcp_get;u16 len;u8 buffer[BUFFER_SIZE];char http_method[16];char http_uri[64];char http_body[256];u8 res_code;systick_configuration();init_led();init_system_spi();func_w5500_reset();init_hardware_usart2_dma(9600);getMacByLockCode(mac);setSHAR(mac);sysinit(txsize, rxsize);setRTR(2000);setRCR(3);//DHCPfor(;func_dhcp_get_ip_sub_gw(1, mac, &dhcp_get, 500) != 0;);   if(func_dhcp_get_ip_sub_gw(1, mac, &dhcp_get, 500) == 0){setSUBR(dhcp_get.sub);setGAR(dhcp_get.gw);setSIPR(dhcp_get.lip);close(1);}dhcp_timestamp = get_systick_timestamp();for(;;){if(get_systick_timestamp() - dhcp_timestamp > 59*1000)// 1 min dhcp{dhcp_timestamp = get_systick_timestamp();if(func_dhcp_get_ip_sub_gw(1, mac, &dhcp_get, 500) == 0){setSUBR(dhcp_get.sub);setGAR(dhcp_get.gw);setSIPR(dhcp_get.lip);close(1);}}switch(getSn_SR(SOCK_TCPS)){case SOCK_CLOSED:socket(SOCK_TCPS, Sn_MR_TCP, 80, Sn_MR_ND);break;case SOCK_INIT:listen(SOCK_TCPS);break;     case SOCK_ESTABLISHED:      if(getSn_IR(SOCK_TCPS) & Sn_IR_CON){setSn_IR(SOCK_TCPS, Sn_IR_CON);}len = getSn_RX_RSR(SOCK_TCPS);if(len>0){memset(buffer, 0, BUFFER_SIZE);len = recv(SOCK_TCPS, buffer, len);//analysis tcp msg, and package the feedback msgif(len > 0){res_code = func_analysis_http_request(buffer, len, http_method, http_uri, http_body);memset(buffer, 0, sizeof(buffer));if(res_code == 0){if(strcmp("GET", http_method) == 0 && strcmp("/", http_uri) == 0){func_package_http_response(buffer, &len, sizeof(buffer), HTML_CONTENT, strlen(HTML_CONTENT));send(SOCK_TCPS, buffer, len);                  }else if(strcmp("POST", http_method) == 0 && strcmp("/sn_config.action", http_uri) == 0){func_package_http_response(buffer, &len, BUFFER_SIZE, HTML_RESULT_OK, strlen(HTML_RESULT_OK));send(SOCK_TCPS, buffer, len);}else{memcpy(buffer, TEXT_TEMPLATE_ERR, strlen(TEXT_TEMPLATE_ERR));send(SOCK_TCPS, buffer, strlen(TEXT_TEMPLATE_ERR));}disconnect(SOCK_TCPS);}else{memcpy(buffer, TEXT_TEMPLATE_ERR, strlen(TEXT_TEMPLATE_ERR));send(SOCK_TCPS, buffer, strlen(TEXT_TEMPLATE_ERR));disconnect(SOCK_TCPS);}}}break;case SOCK_CLOSE_WAIT:close(SOCK_TCPS);break;}func_led1_on();delay_ms(500);func_led1_off();delay_ms(500);}
}u8 func_analysis_http_request(u8* buffer, u16 len_recv, char* method, char* uri, char* data_body)
{char chs[BUFFER_SIZE] = {0, };char *res, *end;if(len_recv > 0){memcpy(chs, buffer, 3);res = strstr(chs, "GET");if(strcmp("GET", res) == 0){memcpy(method, "GET", strlen("GET"));}else{memset(chs, 0, BUFFER_SIZE);memcpy(chs, buffer, 4);res = strstr(chs, "POST");if(strcmp("POST", res) == 0){memcpy(method, "POST", strlen("POST"));}else{return 1;}}memset(chs, 0, BUFFER_SIZE);memcpy(chs, buffer, len_recv + 1);res = strchr(chs, '/');if(res != NULL){end = strchr(res, ' ');if(end != NULL){memcpy(uri, res, end - res);}           }memset(chs, 0, BUFFER_SIZE);memcpy(chs, buffer, len_recv + 1);res = strstr(chs, "\r\n\r\n");if(res != NULL){if(strlen(res) > 4){memcpy(data_body, res + 4, strlen(res) - 4);}         }}return 0;
}u8 func_package_http_response(u8* buffer, u16 *len_ret, u16 len_buf, char* cont, u16 len_cont)
{memset(buffer, 0, BUFFER_SIZE);*len_ret = sprintf((char*)buffer, TEXT_TEMPLATE_OK, len_cont, cont);return 0;
}

相关的基础函数库可以参考我的其他文章。

测试步骤,我的W5500的IP是动态获取到的,所以先登录到路由器管理页面查看他的IP地址。

我的 STM32 W5500 IP地址是"192.168.1.100"。在浏览器地址栏输入"192.168.1.100",可以看到页面

在SN后的input标签内输入任意内容,点击submit后,可以看到

数据已提交到服务。

通过Debug单步调试可以看到浏览器提交到的数据内容,或者通过串口打印出来。一般的操作都是收到这个数据后存储到EEPROM中。

STM32 W5500 HTTP Server 微型web服务实现相关推荐

  1. Windows server 2012 Web服务

    前言 作者简介:不知名白帽,网络安全学习者. 博客主页:https://blog.csdn.net/m0_63127854?type=blog Windows server 专栏:https://bl ...

  2. Windows server——部署web服务

    作者简介:一名云计算网络运维人员.每天分享网络与运维的技术与干货.   座右铭:低头赶路,敬事如仪 个人主页:网络豆的主页​​​​​​ 目录 前言 本章重点 一.web讲解 1.WWW概述 (1)WW ...

  3. stm32+W5500+阿里物联网平台

    前提: 非物联网专业出身,网络协议一知半解(就是没学过),最近调试一块stm32+w5500开发板,为了学习知识,实现以个依靠阿里云物联网平台控制开发板上LED开关功能.(2020年4月24日) 1: ...

  4. web server and web service

    问题:Web Service和Web Server的区别? 1. 概念上有什么不同? 2.   开发上有什么不同? 3. 哪些服务器支持web server ? 哪些支持web service? 4. ...

  5. Windows Server 2008 R2之三十二:证书注册WEB服务(一)

    由于证书注册WEB服务的部署方法,和CA与证书注册WEB服务是否安装在同一台计算机,以及安装过程中身份验证方式的选择有关, 以下CA与证书注册WEB服务安装在同一台计算机的设置过程. 实验环境: 所有 ...

  6. 【script】python3使用http.server搭建简易web服务

    ''' 更详细的web服务搭建可参考django: https://docs.djangoproject.com/zh-hans/2.1/intro/tutorial01/ '''from http. ...

  7. C# .net基于Http实现web server(web服务)

    C# .net基于Http实现web server(web服务) 原文:C# .net基于Http实现web server(web服务) 什么是 web server?  百度百科是这么解释的: We ...

  8. Windows Server 2008终端服务详解系列3:结合MOSS 2007部署TS Web Access

    Windows Server 2008终端服务详解系列3:结合MOSS 2007部署TS Web Access 前言: 本系列将全面的介绍Windows Server 2008终端服务,从概念到功能, ...

  9. 腾讯云服务器/Windows Server 2012 R2 上搭载web服务 动态图图解(http协议)# 谭子

    一.基于Windows Server 2012 r2搭网站/挂网页 在写好网页后,经常想着用链接的形式进行访问(拿手机或者浏览器能直接打开),网上搭建网站的方法很多,这里介绍一种在Windows Se ...

最新文章

  1. C#程序调用外部程序
  2. 经典控制~系统的极点
  3. 2021年春季学期-信号与系统-第二次作业参考答案-第五小题
  4. Qt opencv开发环境
  5. linux上设置了log4j没有产生日志文件_关于 log4j 升级到 log4j2 的小结
  6. [Mysql]查看版本号的五种方式
  7. 集合中重写equals方法删除new的对象
  8. 用python连接数据库_用Python连接MySQL
  9. java 栈 大小_java – JVM堆栈大小规范
  10. 如何让程序异常退出后重启
  11. T-SQL Optimization Tips (3): SELECT COUNT(*)
  12. HDU2500 做一个正气的杭电人【水题】
  13. cmake的一些小经验
  14. REACT打印页面组件
  15. c4d细分曲面的使用和导出的一些快捷键笔记
  16. 福建省12.5米DEM数字高程数据
  17. 阿里天池大数据竞赛(杂)
  18. 【CentOS】CentOS修改IP地址
  19. word文档去掉复制过来的背景颜色
  20. leetcode之GaryCode

热门文章

  1. 【并发】shell调用shell nohup command 后台执行
  2. 自定义WhatsUp监控 1
  3. linux下将qt打包为可执行文件
  4. 2018年4月高等教育国际金融全国统一命题考试
  5. np.cov np.var
  6. 养老产业迎来消费寒冬?亲和源业绩大变脸,究竟是什么原因
  7. Anderson《空气动力学基础》5th读书笔记 第5记——推导二维机翼的空气动力学系数
  8. 关于android系统的移植
  9. U8 制作多合一Linux启动U盘-孙宇彤-专题视频课程
  10. 浏览器HTTP缓存策略