arduino esp8266_Arduino 高级编程 6:利用 ESP8266 连接 TCP 服务器

本文介绍了如何使用ESP8266-01模块与Arduino配合,建立TCP客户端连接到服务器。通过示例代码展示了如何接收服务器发送的数据,并提供了硬件配置和程序准备的详细步骤。
摘要由CSDN通过智能技术生成

5c507a7e42fd3c5ec3fd96eb56477aec.png

译者按:这篇是使用 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 服务器发过来的信息,你需要以下的零件:

  1. 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

2ff0d33e393949f0dec2f77539736298.png

Figure 6.1

6.2 Hardware Setup

6.2 硬件安装

Setup hardware as shown below:

按下图安装硬件:

a80d0d6b9cd16c21a57b81249f80e1dc.png

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:

运行该程序:

095e3861d9b38a97b2a3cb982c924a41.png

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 = "";

	/
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值