查看“Microduino ENC Network (5)”的源代码
←
Microduino ENC Network (5)
跳转至:
导航
、
搜索
因为以下原因,您没有权限编辑本页:
您所请求的操作仅限于该用户组的用户使用:
用户
您可以查看与复制此页面的源代码。
{{Language | Microduino ENC网络(五)}} {| style="width: 800px;" |- | ==Objective== This tutorial will show you how to make Microduino act like a web server, so you can access it in your home network. ==Equipment== *'''[[Microduino-Core]]''' *'''[[Microduino-FT232R]]''' *'''[[Microduino-ENC28J60]]''' *'''[[Microduino-RJ45]]''' *Other equipment **USB cable ==World Wide Web== Amongs the network services, the most used one is surely the web service: everytime you “surf” on a website, the program you use (named browser) connects to the web server hosting that site using HTTP protocol and gets the content (pages, images, videos…) to show. Web pages are composed using a markup language named HTML; this language uses tags to describe page content. The main advantages in using HTTP to talk with Arduino on a network connection are: *You don’t need to develop a dedicated client, you can use a common internet browser (Internet Explorer, Firefox…); *HTTP protocol is textual, you can easily manage it with Arduino’s string functions; *HTML language is also textual, you can easily create pages -also dynamic ones- with Arduino. ==Schematic== *Microduino-ENC28J60 *Microduino-RJ45 *Microduino-Core *Microduino-FT232R Stack all modules and then connect the ethernet cable, as follows: [[File:MicroduinoENCShow.jpg|600px|center|thumb]] ==Program== Refer to ENCnetworkfive ==Debug== Step 1:Download the EtherCard library and copy to your libraries fold of IDE, then restart IDE. https://github.com/jcw/ethercard Step 2:Explain the program: I’m going to show you a simple program that prints on serial connection browser‘s requests and answers with a simple HTML page. This program is useful to understand how to use HTTP functions and also to find out which informations a browser sends within the request (you’ll learn how to use those informations in a coming post). The complete sketch is available on GitHub’s repository, here is the most important code: word len = ether.packetReceive(); word pos = ether.packetLoop(len); In the loop I save in a variable the return value of packetLoop() method: this value is the position, within the rx buffer, where browser’s request starts. if(pos) { Serial.println("---------- NEW PACKET ----------"); Serial.println((char *)Ethernet::buffer + pos); Serial.println("--------------------------------"); Serial.println(); If pos is greater than zero, I really received a correct request and I can print it on serial port (buffer + pos is the position to start). BufferFiller bfill = ether.tcpOffset(); To store the response I’m going to send to client’s browser, I use a BufferFiller object. This object inherits from Print class and its constructor gets as parameter the ethernet’s buffer address (tcpOffset) where the response has to be saved. bfill.emit_p(PSTR("HTTP/1.0 200 OK\r\n" "Content-Type: text/html\r\nPragma: no-cache\r\n\r\n" "<html><body><h1>BasicServer Demo</h1></body></html>")); With emit_p() method I can save data in the buffer: for this example I create a simple HTML page that shows BasicServer Demo text. To save RAM memory space, I use PSTR() macro that stores strings in the PROGRAM. ether.httpServerReply(bfill.position()); At last, with httpServerReply() method, I send the response to the browser. This method needs, as a parameter, the number of characters to be sent. I can get the number of characters BufferFiller contains with its method position(). Step 3:Compile the code and download it. Step 4: Check you ip address by the serial port. Input the Microduino's IP in browser... ==Result== the result should be like this: [[File:basicServerDemo.jpg|600px|center|thumb]] And on your serial monitor: [[File:basicServerSerialCutImage.jpg|600px|center|thumb]] You may ask why the browser sent two requests, one for the page “/” (that is the site’s homepage) and one for the image favicon.ico. That is the icon some sites show next to their address; here’s for example the icon from DangerousPrototypes: [[File:yeelinkfavicon.jpg|600px|center|thumb]] ==Video== |}
该页面使用的模板:
模板:Language
(
查看源代码
)
模板:Nmbox
(
查看源代码
)
返回至
Microduino ENC Network (5)
。
导航菜单
个人工具
创建账户
登录
名字空间
页面
讨论
变种
视图
阅读
查看源代码
查看历史
更多
搜索
Welcome
首页
创客大赛
大赛详情
3D打印
安装月球车
图形化编程
操控月球车
升级月球车
编程工具下载
软件下载
Arduino
Processing
Mixly
Scratch
模块套件
Microduino 102
mCookie 102
mCookie 202
mCookie 302
IBC
其他
应用套件
四轴飞行器
平衡车
小车CUBE
音乐播放器
刷卡音乐播放器
wifi气象站
彩虹音乐触摸灯
分贝检测仪
迎门汇报
LED点阵时钟
LED点阵屏幕
硬件
mCookie
Sensor
Microduino
MicroWrt
MicroNux
MicroRobot-Core
MicroRobot-CoreESP
ideaBoard
ideaBox
MicroMV
MicroAI
帮助
常见问题
帮助
工具
链入页面
相关更改
特殊页面
页面信息