前言
这学期开了一门《嵌入式应用系统开发》的课程,本身学软件,对硬件方面也不是特别精通,整理一下我自己学习过程中的想法和笔记分享出来仅供参考。
0 实验目的
通过单片机运行一个服务器(webserve),自己生成网页(HTML),通过浏览器访问单片机服务器里的页面,用按钮、超链接或者输入框提交(可以传递参数值)等方式给单片机发送指令。
1 实验前的准备
在阅读本教程之前,假设你已经具备一定的基础:
1、能够连接单片机,实现基本的串口读取数据等。(最后我会讲到我在学校机房可以成功连接单片机,但是在自己笔记本上找不到单片机连接端口的解决办法)
2、会用html语言编写网页。
所要用的的工具如下:
- VS2010
- Arduino(附某宝卖家提供的资料下载:http://pan.baidu.com/s/1i56mPMl
链接:http://pan.baidu.com/s/1eSwuDtK 密码:h6jq) - 51单片机(我们用的是WIFI开发板 D1 WiFi UNO R3开发板基于ESP8266 ESP-12F 直用IDE)如下图:
另附开发板引脚说明表:
2 连接上单片机
桌面电脑图标,属性找到设备管理器,找到端口
查看是否成功连接上单片机,以及是否安装上ch340驱动(端口会有CH340显示)
打开arduino后注意首先修改工具里这两处:
接下来就可以写代码了
3 arduino代码部分
本身开发板arduino上有示例程序,可根据我们的需要进行选择,再做部分修改,相当方便:
示例程序中ESP8266WiFi->WiFiWebServer,运行后可通过访问两个不同网址得到不同的显示(high/low),示例程序如下:
/*
* This sketch demonstrates how to set up a simple HTTP-like server.
* The server will set a GPIO pin depending on the request
* http://server_ip/gpio/0 will set the GPIO2 low,
* http://server_ip/gpio/1 will set the GPIO2 high
* server_ip is the IP address of the ESP8266 module, will be
* printed to Serial when the module is connected.
*/
#include <ESP8266WiFi.h>
const char* ssid = "your-ssid";
const char* password = "your-password";
// Create an instance of the server
// specify the port to listen on as an argument
WiFiServer server(80);
void setup() {
Serial.begin(115200);
delay(10);
// prepare GPIO2
pinMode(2, OUTPUT);
digitalWrite(2, 0);
// Connect to WiFi netwo