译者按:这篇是使用 ESP8266 的简化板,希望我尽快能翻译到第 9 课,大家就可以用电脑控制 Arudino 小车了!
Arduino: Lesson 6 - Control ESP8266-01 to connect a TCP Server
第六课 - 利用 ESP8266-01 连接 TCP 服务器
6.1 Client Server Socket connection using ESP8266-01 board
6.1 利用 ESP8266-01 板创建 Socket 连接
This lesson provide an example on how to receive message sent from a TCP Server, you'll need the following parts:
这一课是一个例子,让我们接受 TCP 服务器发过来的信息,你需要以下的零件:
- An Arduino UNO/MEGA board.
1. 一只 Arduino UNO 或 MEGA 板。
2. ONE ESP8266-01 board, the price of this board is around US$1.50, sold in the following web site:
2. 一只 ESP8266-01 板,价格大概是 12 元人民币,某宝有售:
http://www.taobao.com
Figure 6.1
6.2 Hardware Setup
6.2 硬件安装
Setup hardware as shown below:
按下图安装硬件:
http://www.zephan.top/arduinolessons/lesson6/esp8266_setup.fzz
6.3 Programs
6.3 程序
6.3.1 Prepare the TCP Server Test Program
6.3.1 准备 TCP 服务器测试程序
You may write your own TCP Server program or download the TCP Server Test EXE from the following URL:
你可以自己写一个 TCP 服务器程序,或者在以下网址下载一个测试程序:
http://sockettest.sourceforge.net/
Run the program as shown:
运行该程序:
Figure 6.2
The IP Address is the IP Address of your PC, or the IP Address of another PC not connected . Select any port you want, however, if you are using Windows Server as the Operating System, remember to add an "In policy" in your Windows Firewall, otherwise, the ESP8266 is not allowed to connect to that Port.
那个 【IP Address】是电脑的 IP 地址。随便选择一个端口号,但是,如果你是使用 Windows,你需要在 Windows 防火墙的 "In policy" 里加一条允许其他计算机访问该端口,否则 ESP8266 将不能连接该电脑。
In our lessons, we intended to build a ROBOT class that has many functions for many purposes, so that in the future, no matter what our project is, we can use the ROBOT class as the base class of our projects. The ROBOTH header files may become larger and larger, and somtimes it contains some modules that are not required by the current project, but we still need to include them otherwise the compilation of the programs will fail.
在我们的教程里,我们尝试建立一个【ROBOT 类】,它包含了很多的方法,可以用于不同的目的。将来,不管你的项目是什么,我们都可以使用这个类作为项目的基类。那个 ROBOTH 头文件会变得越来越大,有时候会包括一些方法是你的项目不需要的,但是我们也必须把这些没用的源代码留下,否则编译程序会失败。
If you did not start our lessons from lesson 1, you may need to go back to the previous lessons to learn how we can "INCLUDE" some libraries in the ROBOTH header files that are required by other lessons.
如果你没有从我们的第一课开始看,你可能需要回看之前的教程,学习一下我们怎么 "INLCUDE" 一些教程有用的库。
Copy and Paste the following codes to corresponding files:
拷贝以下程序到相关档案里:
// ROBOTH.h
// We declare all variables(properties) and functions(methods) of the ROBOT object inside this header file
#ifndef _ROBOTH_h
#define _ROBOTH_h
#include "Arduino.h"
#include
// Class for MOTOR
class MOTOR
{
public:
// Motor Type:
// "D" - DC Motor
// "E" - EV3 Motor
String gstrMotorType = "";
/